package de.halbekunst.juplo.cachecontrol;
-import de.halbekunst.juplo.cachecontrol.CacheControl.CacheMethodHandle;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
public class AcceleratorFilter implements Filter {
private final static Logger log = LoggerFactory.getLogger(AcceleratorFilter.class);
- private final static Map<String,String> ADDITIONAL_HEADERS_NONE = Collections.unmodifiableMap(new HashMap<String,String>());
- private final static Map<String,String> ADDITIONAL_HEADERS_GZIP;
+ private final static Map<String,String> EMPTY = Collections.unmodifiableMap(new HashMap<String,String>());
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<String,String> map = new HashMap<String,String>(1);
- map.put(Headers.HEADER_CONTENT_ENCODING, "gzip");
- ADDITIONAL_HEADERS_GZIP = Collections.unmodifiableMap(map);
- }
-
@Autowired CacheControl cacheControl;
@Autowired(required=true) Integer buffer;
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;
+ @Override
+ public boolean isZipped() {
+ return zipped && out != null && !out.empty;
+ }
+
@Override
public long getTimestamp() {
return now;
@Override
public Map<String,String> 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;
}
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;
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(
return false;
}
+ if (handle.isZipped())
+ response.addHeader(Headers.HEADER_CONTENT_ENCODING, "gzip");
+
Map<String,String> headers = handle.getAdditionalHeaders(request);
for (String name : headers.keySet())
response.addHeader(name, headers.get(name));
}
- 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<String, String> cacheControlMap);
- Map<String,String> getAdditionalHeaders(HttpServletRequest request);
- }
-
-
class ReflectionCacheMethodHandle implements CacheMethodHandle {
private Object handler;
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;
}
+ @Override
+ public boolean isZipped() {
+ return zipped;
+ }
+
@Override
public long getTimestamp() {
return now;
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);
--- /dev/null
+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<String, String> cacheControlMap);
+ Map<String,String> getAdditionalHeaders(HttpServletRequest request);
+}