From: Kai Moritz Date: Thu, 2 Aug 2012 07:00:01 +0000 (+0200) Subject: Fehler im Zusammenhang mit dem Komprimieren gepufferter Antworten korrigiert X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=2ae9a9d1011d3cb6cb0a172b629cb390c4fdf0ea;p=percentcodec Fehler im Zusammenhang mit dem Komprimieren gepufferter Antworten korrigiert --- diff --git a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java index f76c7132..992d9dc9 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java @@ -1,6 +1,5 @@ package de.halbekunst.juplo.cachecontrol; -import de.halbekunst.juplo.cachecontrol.CacheControl.CacheMethodHandle; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; @@ -38,18 +37,11 @@ import org.springframework.beans.factory.annotation.Configurable; public class AcceleratorFilter implements Filter { private final static Logger log = LoggerFactory.getLogger(AcceleratorFilter.class); - private final static Map ADDITIONAL_HEADERS_NONE = Collections.unmodifiableMap(new HashMap()); - private final static Map ADDITIONAL_HEADERS_GZIP; + private final static Map EMPTY = Collections.unmodifiableMap(new HashMap()); public final static String REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri"; public final static String RESPONSE_WRAPPER = AcceleratorFilter.class.getName() + ".RESPONSE_WRAPPER"; - static { - Map map = new HashMap(1); - map.put(Headers.HEADER_CONTENT_ENCODING, "gzip"); - ADDITIONAL_HEADERS_GZIP = Collections.unmodifiableMap(map); - } - @Autowired CacheControl cacheControl; @Autowired(required=true) Integer buffer; @@ -99,7 +91,8 @@ public class AcceleratorFilter implements Filter { private final HttpServletRequest request; private final HttpServletResponse response; - private boolean zipped; + boolean zipped; // CacheControll greift direkt auf dieses Flag zu! + private int buffer; private GZIPServletOutputStream out; private ServletOutputStream stream; @@ -358,6 +351,11 @@ public class AcceleratorFilter implements Filter { + @Override + public boolean isZipped() { + return zipped && out != null && !out.empty; + } + @Override public long getTimestamp() { return now; @@ -395,13 +393,10 @@ public class AcceleratorFilter implements Filter { @Override public Map getAdditionalHeaders(HttpServletRequest request) { - if (zipped && !out.empty) - return ADDITIONAL_HEADERS_GZIP; - else - return ADDITIONAL_HEADERS_NONE; + return EMPTY; } - public void guessingFinished() { + public void supressGuessing() { guessing = false; } 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 2c909e81..0512de4a 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,8 +39,8 @@ 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( @@ -102,6 +103,9 @@ public class CacheControl { return false; } + if (handle.isZipped()) + response.addHeader(Headers.HEADER_CONTENT_ENCODING, "gzip"); + Map headers = handle.getAdditionalHeaders(request); for (String name : headers.keySet()) response.addHeader(name, headers.get(name)); @@ -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,11 +318,13 @@ 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; @@ -436,6 +430,11 @@ public class CacheControl { } + @Override + public boolean isZipped() { + return zipped; + } + @Override public long getTimestamp() { return now; diff --git a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControlInterceptor.java b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControlInterceptor.java index 04c6dbd9..254187f7 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControlInterceptor.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControlInterceptor.java @@ -34,23 +34,12 @@ public class CacheControlInterceptor implements HandlerInterceptor { return true; } - /** CacheControll initialisieren (Handler nach annotierte Methoden scannen etc.) */ - cacheControl.init(handler); - AccelerationWrapper wrapper = (AccelerationWrapper)request.getAttribute(AcceleratorFilter.RESPONSE_WRAPPER); - if (wrapper != null) { - wrapper.guessingFinished(); - if (log.isInfoEnabled()) { - StringBuilder builder = new StringBuilder(); - builder.append("request should not be filtered, because it is intercepted: "); - builder.append(request.getRequestURI()); - if (request.getQueryString() == null) { - builder.append ("?"); - builder.append(request.getQueryString()); - } - log.info(builder.toString()); - } - } + if (wrapper != null) + wrapper.supressGuessing(); + + /** CacheControll initialisieren (Handler nach annotierte Methoden scannen etc.) */ + cacheControl.init(handler, wrapper); if (cacheable.eager()) { return cacheControl.decorate(request, response, handler); diff --git a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheMethodHandle.java b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheMethodHandle.java new file mode 100644 index 00000000..a36196e6 --- /dev/null +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheMethodHandle.java @@ -0,0 +1,20 @@ +package de.halbekunst.juplo.cachecontrol; + +import java.util.Map; +import javax.servlet.http.HttpServletRequest; + +/** + * + * @author kai + */ +public interface CacheMethodHandle { + boolean isZipped(); + 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); +}