X-Git-Url: https://juplo.de/gitweb/?p=percentcodec;a=blobdiff_plain;f=cachecontrol%2Fsrc%2Fmain%2Fjava%2Fde%2Fhalbekunst%2Fjuplo%2Fcachecontrol%2FCacheControl.java;h=b248227716f25f2a9583c7d7e59f649d64e16a4f;hp=19a60b51f9dd6ac24eb5403d42ede80d6d2a2523;hb=1bb1a0f0e1d347538ae93c23395bba172cd87342;hpb=3324545626f8f93c43e3b54cf56004d19af17da2 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 19a60b51..b2482277 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java @@ -2,11 +2,13 @@ package de.halbekunst.juplo.cachecontrol; import de.halbekunst.juplo.cachecontrol.annotations.CacheSeconds; import de.halbekunst.juplo.cachecontrol.annotations.Accepts; +import de.halbekunst.juplo.cachecontrol.annotations.AdditionalHeaders; import de.halbekunst.juplo.cachecontrol.annotations.LastModified; import de.halbekunst.juplo.cachecontrol.annotations.ETag; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Date; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; @@ -14,18 +16,22 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; /** * * @author kai */ +@Component public class CacheControl { private final static Logger log = LoggerFactory.getLogger(CacheControl.class); private static final ThreadLocal tl = new ThreadLocal(); - private Integer defaultCacheSeconds; - private Long defaultLastModified; + @Autowired @Qualifier("cacheSeconds") private Integer defaultCacheSeconds; + @Autowired @Qualifier("lastModified") private Long defaultLastModified; public void init(CacheMethodHandle handle) { @@ -56,7 +62,7 @@ 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(HeaderNames.HEADER_DATE, controller.getTimestamp()); + response.setDateHeader(Headers.HEADER_DATE, controller.getTimestamp()); /** Besondere Maßnahmen für besondere HTTP-Status-Codes ?!? */ int status = controller.accepts(request); @@ -96,6 +102,10 @@ public class CacheControl { return false; } + Map headers = controller.getAdditionalHeaders(request); + for (String name : headers.keySet()) + response.addHeader(name, headers.get(name)); + String url = null; if (log.isDebugEnabled()) { if (request.getQueryString() == null) { @@ -113,20 +123,20 @@ public class CacheControl { int cacheSeconds = controller.getCacheSeconds(request); if (cacheSeconds < 1) { log.debug("{}: caching disabled!", url); - response.setDateHeader(HeaderNames.HEADER_DATE, controller.getTimestamp()); - response.setDateHeader(HeaderNames.HEADER_EXPIRES, 0); - response.addHeader(HeaderNames.HEADER_PRAGMA, "no-cache"); - response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, "private"); - response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, "no-cache"); - response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, "no-store"); - response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, "max-age=0"); - response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, "s-max-age=0"); + response.setDateHeader(Headers.HEADER_DATE, controller.getTimestamp()); + response.setDateHeader(Headers.HEADER_EXPIRES, 0); + response.addHeader(Headers.HEADER_PRAGMA, "no-cache"); + response.addHeader(Headers.HEADER_CACHE_CONTROL, "private"); + response.addHeader(Headers.HEADER_CACHE_CONTROL, "no-cache"); + 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"); return true; } long ifModifiedSince = -1; try { - ifModifiedSince = request.getDateHeader(HeaderNames.HEADER_IF_MODIFIED_SINCE); + ifModifiedSince = request.getDateHeader(Headers.HEADER_IF_MODIFIED_SINCE); } catch (Exception e) { log.error("Exception while fetching If-Modified-Since: {}", e); @@ -141,7 +151,7 @@ public class CacheControl { */ lastModified = lastModified - (lastModified % 1000); - String ifNoneMatch = request.getHeader(HeaderNames.HEADER_IF_NONE_MATCH); + String ifNoneMatch = request.getHeader(Headers.HEADER_IF_NONE_MATCH); String eTag = controller.getETag(request); /** @@ -157,7 +167,7 @@ public class CacheControl { builder.append('"'); builder.append(eTag); builder.append('"'); - response.setHeader(HeaderNames.HEADER_ETAG, builder.toString()); + response.setHeader(Headers.HEADER_ETAG, builder.toString()); } @@ -191,7 +201,7 @@ public class CacheControl { ifNoneMatch = ifNoneMatch.substring(1, ifNoneMatch.length() - 1); } - if (!weak || (request.getMethod().equals("GET") && request.getHeader(HeaderNames.HEADER_RANGE) == null)) { + if (!weak || (request.getMethod().equals("GET") && request.getHeader(Headers.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 @@ -212,7 +222,7 @@ public class CacheControl { log.debug("{}: first up!", url); /** HTTP/1.1-Caching-Header richtig setzen!! */ - response.setDateHeader(HeaderNames.HEADER_LAST_MODIFIED, lastModified); + response.setDateHeader(Headers.HEADER_LAST_MODIFIED, lastModified); /** Cache-Control für HTTP/1.1-Clients generieren */ Map cacheControl = new TreeMap(); @@ -235,7 +245,7 @@ public class CacheControl { * Expires-Header für HTTP/1.0-Clients setzen. */ cacheControl.put("max-age", Integer.toString(cacheSeconds)); - response.setDateHeader(HeaderNames.HEADER_EXPIRES, (controller.getTimestamp() + (long) cacheSeconds * 1000)); + response.setDateHeader(Headers.HEADER_EXPIRES, (controller.getTimestamp() + (long) cacheSeconds * 1000)); } /** Dem Handler die Gelegenheit geben, den Cache-Controll-Header anzupassen */ @@ -251,8 +261,8 @@ public class CacheControl { * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.32 * Abschnitt 14.32}) */ - response.setDateHeader(HeaderNames.HEADER_EXPIRES, 0l); - response.addHeader(HeaderNames.HEADER_PRAGMA, "no-cache"); + response.setDateHeader(Headers.HEADER_EXPIRES, 0l); + response.addHeader(Headers.HEADER_PRAGMA, "no-cache"); } StringBuilder builder = new StringBuilder(); @@ -263,7 +273,7 @@ public class CacheControl { builder.append('='); builder.append(entry.getValue()); } - response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, builder.toString()); + response.addHeader(Headers.HEADER_CACHE_CONTROL, builder.toString()); } return true; @@ -291,6 +301,7 @@ public class CacheControl { String getETag(HttpServletRequest request); boolean isETagWeak(); void cacheControl(HttpServletRequest request, Map cacheControlMap); + Map getAdditionalHeaders(HttpServletRequest request); } @@ -301,16 +312,19 @@ public class CacheControl { private Integer cacheSeconds; private Long lastModified; private String eTag; + private Map additionalHeaders; private Method acceptsMethod; private Method cacheSecondsMethod; private Method lastModifiedMethod; private Method eTagMethod; private Method cacheControlMethod; + private Method additionalHeadersMethod; private boolean isAcceptsMethodDefined; private boolean isCacheSecondsMethodDefined; private boolean isLastModifiedMethodDefined; private boolean isETagMethodDefined; private boolean isCacheControlMethodDefined; + private boolean isAdditionalHeadersMethodDefined; private boolean weak; @@ -349,6 +363,23 @@ public class CacheControl { isETagMethodDefined = true; continue; } + if (annotation.annotationType().equals(AdditionalHeaders.class)) { + AdditionalHeaders additionalHeadersAnnotation = (AdditionalHeaders)annotation; + additionalHeaders = new HashMap(); + for (String header : additionalHeadersAnnotation.value()) { + int i = header.indexOf(':'); + if (i < 0) { + log.error("invalid header: [{}]", header); + } + else { + String name = header.substring(0,i).trim(); + String value = header.substring(i+1,header.length()).trim(); + additionalHeaders.put(name, value); + } + } + isAdditionalHeadersMethodDefined = true; + continue; + } } /** Method-Level-Annotations auslesen */ @@ -390,8 +421,18 @@ public class CacheControl { isCacheControlMethodDefined = true; continue; } + if (annotation.annotationType().equals(AdditionalHeaders.class)) { + if (isAdditionalHeadersMethodDefined) + throw new IllegalArgumentException("Die Annotation @AdditionalHeaders wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); + additionalHeadersMethod = method; + isAdditionalHeadersMethodDefined = true; + continue; + } } } + + if (!isAdditionalHeadersMethodDefined) + additionalHeaders = new HashMap(); } @@ -481,14 +522,20 @@ public class CacheControl { } } } - } - - - public void setDefaultCacheSeconds(Integer defaultCacheSeconds) { - this.defaultCacheSeconds = defaultCacheSeconds; - } - public void setDefaultLastModified(Long defaultLastModified) { - this.defaultLastModified = defaultLastModified; + @Override + public Map getAdditionalHeaders(HttpServletRequest request) throws IllegalArgumentException { + if (additionalHeadersMethod == null) { + return additionalHeaders; + } + else { + try { + return (Map)additionalHeadersMethod.invoke(handler, request); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + } } }