X-Git-Url: https://juplo.de/gitweb/?p=percentcodec;a=blobdiff_plain;f=cachecontrol%2Fsrc%2Fmain%2Fjava%2Fde%2Fhalbekunst%2Fjuplo%2Fcachecontrol%2FCacheControl.java;h=c7bee9e0a0ce9710c26c73af50809b878218855a;hp=b248227716f25f2a9583c7d7e59f649d64e16a4f;hb=61a5f26b9005470c242ca76d398dd07687074713;hpb=1bb1a0f0e1d347538ae93c23395bba172cd87342 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 b2482277..c7bee9e0 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java @@ -1,5 +1,6 @@ package de.halbekunst.juplo.cachecontrol; +import de.halbekunst.juplo.cachecontrol.AcceleratorFilter.AccelerationWrapper; import de.halbekunst.juplo.cachecontrol.annotations.CacheSeconds; import de.halbekunst.juplo.cachecontrol.annotations.Accepts; import de.halbekunst.juplo.cachecontrol.annotations.AdditionalHeaders; @@ -38,21 +39,20 @@ public class CacheControl { CacheControl.tl.set(handle); } - public void init(Object handler) throws NoSuchMethodException { - CacheControl.tl.set(new ReflectionCacheMethodHandle(handler)); + void init(Object handler, AccelerationWrapper wrapper) throws NoSuchMethodException { + CacheControl.tl.set(new ReflectionCacheMethodHandle(handler, wrapper == null ? false : wrapper.zipped)); } public boolean decorate( HttpServletRequest request, - HttpServletResponse response, - Object handler + HttpServletResponse response ) { try { - CacheMethodHandle controller = CacheControl.tl.get(); + CacheMethodHandle handle = CacheControl.tl.get(); /** Doppelte Ausführung verhindern... */ - if (controller == null) { + if (handle == null) { /** Dekoration wurde bereits durchgeführt! */ return true; } @@ -62,10 +62,10 @@ public class CacheControl { * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18 RFC 2616, * Abschnitt 14.18} einen Date-Header enthalten */ - response.setDateHeader(Headers.HEADER_DATE, controller.getTimestamp()); + response.setDateHeader(Headers.HEADER_DATE, handle.getTimestamp()); /** Besondere Maßnahmen für besondere HTTP-Status-Codes ?!? */ - int status = controller.accepts(request); + int status = handle.accepts(request); switch (status) { case HttpServletResponse.SC_OK: // 200 case HttpServletResponse.SC_NO_CONTENT: // 204 @@ -93,16 +93,15 @@ public class CacheControl { case HttpServletResponse.SC_NOT_IMPLEMENTED: // 501 case HttpServletResponse.SC_SERVICE_UNAVAILABLE: // 503 case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED: // 505 - return true; default: /** * Es ist nicht klar, was der Handler noch machen wird/muss: * Antwort nicht dekorieren und Kontroller an den Handler übergeben... */ - return false; + return true; } - Map headers = controller.getAdditionalHeaders(request); + Map headers = handle.getAdditionalHeaders(request); for (String name : headers.keySet()) response.addHeader(name, headers.get(name)); @@ -120,10 +119,10 @@ public class CacheControl { } } - int cacheSeconds = controller.getCacheSeconds(request); + int cacheSeconds = handle.getCacheSeconds(request); if (cacheSeconds < 1) { log.debug("{}: caching disabled!", url); - response.setDateHeader(Headers.HEADER_DATE, controller.getTimestamp()); + response.setDateHeader(Headers.HEADER_DATE, handle.getTimestamp()); response.setDateHeader(Headers.HEADER_EXPIRES, 0); response.addHeader(Headers.HEADER_PRAGMA, "no-cache"); response.addHeader(Headers.HEADER_CACHE_CONTROL, "private"); @@ -131,6 +130,8 @@ public class CacheControl { response.addHeader(Headers.HEADER_CACHE_CONTROL, "no-store"); response.addHeader(Headers.HEADER_CACHE_CONTROL, "max-age=0"); response.addHeader(Headers.HEADER_CACHE_CONTROL, "s-max-age=0"); + if (handle.isZipped()) + response.addHeader(Headers.HEADER_CONTENT_ENCODING, "gzip"); return true; } @@ -142,7 +143,7 @@ public class CacheControl { log.error("Exception while fetching If-Modified-Since: {}", e); } - long lastModified = controller.getLastModified(request); + long lastModified = handle.getLastModified(request); /** * Sicherstellen, dass der Wert keine Millisekunden enthält, da die @@ -152,7 +153,7 @@ public class CacheControl { lastModified = lastModified - (lastModified % 1000); String ifNoneMatch = request.getHeader(Headers.HEADER_IF_NONE_MATCH); - String eTag = controller.getETag(request); + String eTag = handle.getETag(request); /** * 304-Antworten sollen nach dem {@plainlink @@ -162,7 +163,7 @@ public class CacheControl { */ if (eTag != null) { StringBuilder builder = new StringBuilder(); - if (controller.isETagWeak()) + if (handle.isETagWeak()) builder.append("W/"); builder.append('"'); builder.append(eTag); @@ -207,7 +208,7 @@ public class CacheControl { * Antwort stark sind (starke Gleichheit!), oder wenn die Antwort nur * schwache Gleichheit fordert... */ - if (ifNoneMatch.equals(eTag) && (controller.isETagWeak() || !weak)) { + if (ifNoneMatch.equals(eTag) && (handle.isETagWeak() || !weak)) { log.debug("{}: ETag {} not changed -> 304 ", url, ifNoneMatch); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return false; @@ -221,6 +222,9 @@ public class CacheControl { log.debug("{}: first up!", url); + if (handle.isZipped()) + response.addHeader(Headers.HEADER_CONTENT_ENCODING, "gzip"); + /** HTTP/1.1-Caching-Header richtig setzen!! */ response.setDateHeader(Headers.HEADER_LAST_MODIFIED, lastModified); @@ -245,11 +249,11 @@ public class CacheControl { * Expires-Header für HTTP/1.0-Clients setzen. */ cacheControl.put("max-age", Integer.toString(cacheSeconds)); - response.setDateHeader(Headers.HEADER_EXPIRES, (controller.getTimestamp() + (long) cacheSeconds * 1000)); + response.setDateHeader(Headers.HEADER_EXPIRES, (handle.getTimestamp() + (long) cacheSeconds * 1000)); } /** Dem Handler die Gelegenheit geben, den Cache-Controll-Header anzupassen */ - controller.cacheControl(request, cacheControl); + handle.cacheControl(request, cacheControl); if (cacheControl.containsKey("private")) { @@ -293,18 +297,6 @@ public class CacheControl { } - public interface CacheMethodHandle { - long getTimestamp(); - int accepts(HttpServletRequest request); - int getCacheSeconds(HttpServletRequest request); - long getLastModified(HttpServletRequest request); - String getETag(HttpServletRequest request); - boolean isETagWeak(); - void cacheControl(HttpServletRequest request, Map cacheControlMap); - Map getAdditionalHeaders(HttpServletRequest request); - } - - class ReflectionCacheMethodHandle implements CacheMethodHandle { private Object handler; @@ -326,15 +318,16 @@ public class CacheControl { private boolean isCacheControlMethodDefined; private boolean isAdditionalHeadersMethodDefined; private boolean weak; + private boolean zipped; - ReflectionCacheMethodHandle(Object handler) throws NoSuchMethodException { + ReflectionCacheMethodHandle(Object handler, boolean zipped) throws NoSuchMethodException { this.handler = handler; + this.zipped = zipped; cacheSeconds = CacheControl.this.defaultCacheSeconds; lastModified = CacheControl.this.defaultLastModified; - eTag = ""; /** Class-Level-Annotations auslesen */ for (Annotation annotation : handler.getClass().getAnnotations()) { @@ -436,6 +429,11 @@ public class CacheControl { } + @Override + public boolean isZipped() { + return zipped; + } + @Override public long getTimestamp() { return now;