X-Git-Url: https://juplo.de/gitweb/?p=percentcodec;a=blobdiff_plain;f=cachecontrol%2Fsrc%2Fmain%2Fjava%2Fde%2Fhalbekunst%2Fjuplo%2Fcachecontrol%2FCacheControl.java;h=77e2004ce30838fb5331b0aaeb40d031ea6705d9;hp=07f4c58e13875f745f3380adf7a51610fdcc70dc;hb=44e648e70606cc8d92f7db4268a468426b52da85;hpb=75b55d74f57705c6c5ebee2bcd66c87f3f09089d diff --git a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java index 07f4c58e..77e2004c 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java @@ -31,6 +31,7 @@ public class CacheControl { public static final String HEADER_PRAGMA = "Pragma"; public static final String HEADER_IF_MODIFIED_SINCE = "If-Modified-Since"; public static final String HEADER_IF_NONE_MATCH = "If-None-Match"; + public static final String HEADER_RANGE = "Range"; private static final ThreadLocal tl = new ThreadLocal(); @@ -162,8 +163,15 @@ public class CacheControl { * 2616, Abschnitt 10.3.5} einen ETag-Header enthalten, wenn auch die * 200-Antwort einen enthalten hätte. */ - if (eTag != null) - response.setHeader(HEADER_ETAG, eTag); + if (eTag != null) { + StringBuilder builder = new StringBuilder(); + if (controller.isETagWeak()) + builder.append("W/"); + builder.append('"'); + builder.append(eTag); + builder.append('"'); + response.setHeader(HEADER_ETAG, builder.toString()); + } if (ifModifiedSince >= lastModified && lastModified > 0) { @@ -186,10 +194,31 @@ public class CacheControl { } } - if (ifNoneMatch != null && ifNoneMatch.equals(eTag)) { - log.debug("{}: ETag {} not changed -> 304 ", url, ifNoneMatch); - response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - return false; + if (ifNoneMatch != null) { + boolean weak = false; + if (ifNoneMatch.startsWith("W/")) { + weak = true; + ifNoneMatch = ifNoneMatch.substring(3, ifNoneMatch.length() - 1); + } + else { + ifNoneMatch = ifNoneMatch.substring(1, ifNoneMatch.length() - 1); + } + + if (!weak || (request.getMethod().equals("GET") && request.getHeader(HEADER_RANGE) == null)) { + /** + * Die Gleichheit gilt nur, wenn die ETag's der Anfrage _und_ der + * Antwort stark sind (starke Gleichheit!), oder wenn die Antwort nur + * schwache Gleichheit fordert... + */ + if (ifNoneMatch.equals(eTag) && (controller.isETagWeak() || !weak)) { + log.debug("{}: ETag {} not changed -> 304 ", url, ifNoneMatch); + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + return false; + } + } + else { + log.warn("{}: ignoring weak ETag W/\"{}\", because the request was no GET-request or the Range-Header was present!", url, ifNoneMatch); + } } @@ -273,87 +302,64 @@ public class CacheControl { int getCacheSeconds(HttpServletRequest request) throws Exception; long getLastModified(HttpServletRequest request) throws Exception; String getETag(HttpServletRequest request) throws Exception; + boolean isETagWeak(); void cacheControl(HttpServletRequest request, Map cacheControlMap) throws Exception; } - class DefaultCacheMethodHandle implements CacheMethodHandle { - - long now = System.currentTimeMillis(); - Integer cacheSeconds; - Long lastModified; - String eTag; - - - DefaultCacheMethodHandle() { - this.cacheSeconds = CacheControl.this.defaultCacheSeconds; - this.lastModified = CacheControl.this.defaultLastModified; - this.eTag = null; - } - - - @Override - public long getTimestamp() { - return now; - } - - @Override - public int accepts(HttpServletRequest request) { - return HttpServletResponse.SC_OK; - } - - @Override - public int getCacheSeconds(HttpServletRequest request) { - return cacheSeconds; - } - - @Override - public long getLastModified(HttpServletRequest request) { - return lastModified; - } - - @Override - public String getETag(HttpServletRequest request) { - return eTag; - } - - @Override - public void cacheControl(HttpServletRequest request, Map cacheControlMap) { - } - } class ReflectionCacheMethodHandle implements CacheMethodHandle { private Object handler; - private DefaultCacheMethodHandle defaults = new DefaultCacheMethodHandle(); - private Method accepts, cacheSeconds, lastModified, eTag, cacheControl; - private boolean isAcceptsDefined, isCacheSecondsDefined, isLastModifiedDefined, isETagDefined, isCacheControlDefined; + private long now = System.currentTimeMillis(); + private Integer cacheSeconds; + private Long lastModified; + private String eTag; + private Method acceptsMethod; + private Method cacheSecondsMethod; + private Method lastModifiedMethod; + private Method eTagMethod; + private Method cacheControlMethod; + private boolean isAcceptsMethodDefined; + private boolean isCacheSecondsMethodDefined; + private boolean isLastModifiedMethodDefined; + private boolean isETagMethodDefined; + private boolean isCacheControlMethodDefined; + private boolean weak; ReflectionCacheMethodHandle(Object handler) throws NoSuchMethodException { + this.handler = handler; + + cacheSeconds = CacheControl.this.defaultCacheSeconds; + lastModified = CacheControl.this.defaultLastModified; + eTag = ""; + /** Class-Level-Annotations auslesen */ for (Annotation annotation : handler.getClass().getAnnotations()) { if (annotation.annotationType().equals(CacheSeconds.class)) { - defaults.cacheSeconds = ((CacheSeconds)annotation).value(); - isCacheSecondsDefined = true; + cacheSeconds = ((CacheSeconds)annotation).value(); + isCacheSecondsMethodDefined = true; continue; } if (annotation.annotationType().equals(LastModified.class)) { - defaults.lastModified = ((LastModified)annotation).value(); - if (defaults.lastModified < 1) { + lastModified = ((LastModified)annotation).value(); + if (lastModified < 1) { /** * Ein Last-Modified-Header wurde angefordert, aber es wurde kein * statischer Wert spezifiziert: * globalen statischen Default-Wert benutzen! */ - defaults.lastModified = defaultLastModified; + lastModified = defaultLastModified; } - isLastModifiedDefined = true; + isLastModifiedMethodDefined = true; continue; } if (annotation.annotationType().equals(ETag.class)) { - defaults.eTag = ((ETag)annotation).value(); - isETagDefined = true; + ETag eTagAnnotation = (ETag)annotation; + eTag = eTagAnnotation.value(); + weak = eTagAnnotation.weak(); + isETagMethodDefined = true; continue; } } @@ -362,38 +368,39 @@ public class CacheControl { for (Method method : handler.getClass().getMethods()) { for (Annotation annotation : method.getAnnotations()) { if (annotation.annotationType().equals(Accepts.class)) { - if (isAcceptsDefined) + if (isAcceptsMethodDefined) throw new IllegalArgumentException("Die Annotation @Accept wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - accepts = method; - isAcceptsDefined = true; + acceptsMethod = method; + isAcceptsMethodDefined = true; continue; } if (annotation.annotationType().equals(CacheSeconds.class)) { - if (isCacheSecondsDefined) + if (isCacheSecondsMethodDefined) throw new IllegalArgumentException("Die Annotation @CacheSeconds wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - cacheSeconds = method; - isCacheSecondsDefined = true; + cacheSecondsMethod = method; + isCacheSecondsMethodDefined = true; continue; } if (annotation.annotationType().equals(LastModified.class)) { - if (isLastModifiedDefined) + if (isLastModifiedMethodDefined) throw new IllegalArgumentException("Die Annotation @LastModified wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - lastModified = method; - isLastModifiedDefined = true; + lastModifiedMethod = method; + isLastModifiedMethodDefined = true; continue; } if (annotation.annotationType().equals(ETag.class)) { - if (isETagDefined) + if (isETagMethodDefined) throw new IllegalArgumentException("Die Annotation @ETag wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - eTag = method; - isETagDefined = true; + eTagMethod = method; + weak = ((ETag)annotation).weak(); + isETagMethodDefined = true; continue; } if (annotation.annotationType().equals(de.halbekunst.juplo.cachecontrol.annotations.CacheControl.class)) { - if (isCacheControlDefined) + if (isCacheControlMethodDefined) throw new IllegalArgumentException("Die Annotation @CacheControl wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - cacheControl = method; - isCacheControlDefined = true; + cacheControlMethod = method; + isCacheControlMethodDefined = true; continue; } } @@ -403,7 +410,7 @@ public class CacheControl { @Override public long getTimestamp() { - return defaults.now; + return now; } @Override @@ -412,10 +419,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (accepts == null) - return defaults.accepts(request); + if (acceptsMethod == null) + return HttpServletResponse.SC_OK; else - return (Integer)accepts.invoke(handler, request); + return (Integer)acceptsMethod.invoke(handler, request); } @Override @@ -424,10 +431,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (cacheSeconds == null) - return defaults.getCacheSeconds(request); + if (cacheSecondsMethod == null) + return cacheSeconds; else - return (Integer)cacheSeconds.invoke(handler, request); + return (Integer)cacheSecondsMethod.invoke(handler, request); } @Override @@ -436,10 +443,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (lastModified == null) - return defaults.getLastModified(request); + if (lastModifiedMethod == null) + return lastModified; else - return (Long)lastModified.invoke(handler, request); + return (Long)lastModifiedMethod.invoke(handler, request); } @Override @@ -448,10 +455,15 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (eTag == null) - return defaults.getETag(request); + if (eTagMethod == null) + return eTag; else - return (String)eTag.invoke(handler, request); + return (String)eTagMethod.invoke(handler, request); + } + + @Override + public boolean isETagWeak() { + return weak; } @Override @@ -463,10 +475,8 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (cacheControl == null) - defaults.cacheControl(request, cacheControlMap); - else - cacheControl.invoke(handler, request, cacheControlMap); + if (cacheControlMethod != null) + cacheControlMethod.invoke(handler, request, cacheControlMap); } }