X-Git-Url: https://juplo.de/gitweb/?p=percentcodec;a=blobdiff_plain;f=cachecontrol%2Fsrc%2Fmain%2Fjava%2Fde%2Fhalbekunst%2Fjuplo%2Fcachecontrol%2FCacheControl.java;h=19a60b51f9dd6ac24eb5403d42ede80d6d2a2523;hp=77e2004ce30838fb5331b0aaeb40d031ea6705d9;hb=3324545626f8f93c43e3b54cf56004d19af17da2;hpb=44e648e70606cc8d92f7db4268a468426b52da85 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 77e2004c..19a60b51 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java @@ -5,7 +5,6 @@ import de.halbekunst.juplo.cachecontrol.annotations.Accepts; import de.halbekunst.juplo.cachecontrol.annotations.LastModified; import de.halbekunst.juplo.cachecontrol.annotations.ETag; import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; import java.util.Map; @@ -23,32 +22,25 @@ import org.slf4j.LoggerFactory; public class CacheControl { private final static Logger log = LoggerFactory.getLogger(CacheControl.class); - public static final String HEADER_DATE = "Date"; - public static final String HEADER_CACHE_CONTROL = "Cache-Control"; - public static final String HEADER_LAST_MODIFIED = "Last-Modified"; - public static final String HEADER_ETAG = "ETag"; - public static final String HEADER_EXPIRES = "Expires"; - 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(); private Integer defaultCacheSeconds; private Long defaultLastModified; - public void init(Object handler) throws Exception { - if (CacheControl.tl.get() == null) - CacheControl.tl.set(new ReflectionCacheMethodHandle(handler)); + public void init(CacheMethodHandle handle) { + CacheControl.tl.set(handle); + } + + public void init(Object handler) throws NoSuchMethodException { + CacheControl.tl.set(new ReflectionCacheMethodHandle(handler)); } public boolean decorate( HttpServletRequest request, HttpServletResponse response, Object handler - ) throws Exception + ) { try { CacheMethodHandle controller = CacheControl.tl.get(); @@ -64,7 +56,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(HEADER_DATE, controller.getTimestamp()); + response.setDateHeader(HeaderNames.HEADER_DATE, controller.getTimestamp()); /** Besondere Maßnahmen für besondere HTTP-Status-Codes ?!? */ int status = controller.accepts(request); @@ -95,11 +87,6 @@ public class CacheControl { case HttpServletResponse.SC_NOT_IMPLEMENTED: // 501 case HttpServletResponse.SC_SERVICE_UNAVAILABLE: // 503 case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED: // 505 - /** - * Ein Fehlercode kann stellvertretend für den Handler gesendet werden, - * da im Fehlerfall eh keine weiteren Daten ausgegeben werden! - */ - response.sendError(status); return true; default: /** @@ -126,20 +113,20 @@ public class CacheControl { int cacheSeconds = controller.getCacheSeconds(request); if (cacheSeconds < 1) { log.debug("{}: caching disabled!", url); - response.setDateHeader(HEADER_DATE, controller.getTimestamp()); - response.setDateHeader(HEADER_EXPIRES, 0); - response.addHeader(HEADER_PRAGMA, "no-cache"); - response.addHeader(HEADER_CACHE_CONTROL, "private"); - response.addHeader(HEADER_CACHE_CONTROL, "no-cache"); - response.addHeader(HEADER_CACHE_CONTROL, "no-store"); - response.addHeader(HEADER_CACHE_CONTROL, "max-age=0"); - response.addHeader(HEADER_CACHE_CONTROL, "s-max-age=0"); + 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"); return true; } long ifModifiedSince = -1; try { - ifModifiedSince = request.getDateHeader(HEADER_IF_MODIFIED_SINCE); + ifModifiedSince = request.getDateHeader(HeaderNames.HEADER_IF_MODIFIED_SINCE); } catch (Exception e) { log.error("Exception while fetching If-Modified-Since: {}", e); @@ -154,7 +141,7 @@ public class CacheControl { */ lastModified = lastModified - (lastModified % 1000); - String ifNoneMatch = request.getHeader(HEADER_IF_NONE_MATCH); + String ifNoneMatch = request.getHeader(HeaderNames.HEADER_IF_NONE_MATCH); String eTag = controller.getETag(request); /** @@ -170,7 +157,7 @@ public class CacheControl { builder.append('"'); builder.append(eTag); builder.append('"'); - response.setHeader(HEADER_ETAG, builder.toString()); + response.setHeader(HeaderNames.HEADER_ETAG, builder.toString()); } @@ -204,7 +191,7 @@ public class CacheControl { ifNoneMatch = ifNoneMatch.substring(1, ifNoneMatch.length() - 1); } - if (!weak || (request.getMethod().equals("GET") && request.getHeader(HEADER_RANGE) == null)) { + if (!weak || (request.getMethod().equals("GET") && request.getHeader(HeaderNames.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 @@ -225,7 +212,7 @@ public class CacheControl { log.debug("{}: first up!", url); /** HTTP/1.1-Caching-Header richtig setzen!! */ - response.setDateHeader(HEADER_LAST_MODIFIED, lastModified); + response.setDateHeader(HeaderNames.HEADER_LAST_MODIFIED, lastModified); /** Cache-Control für HTTP/1.1-Clients generieren */ Map cacheControl = new TreeMap(); @@ -248,7 +235,7 @@ public class CacheControl { * Expires-Header für HTTP/1.0-Clients setzen. */ cacheControl.put("max-age", Integer.toString(cacheSeconds)); - response.setDateHeader(HEADER_EXPIRES, (controller.getTimestamp() + (long) cacheSeconds * 1000)); + response.setDateHeader(HeaderNames.HEADER_EXPIRES, (controller.getTimestamp() + (long) cacheSeconds * 1000)); } /** Dem Handler die Gelegenheit geben, den Cache-Controll-Header anzupassen */ @@ -264,8 +251,8 @@ public class CacheControl { * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.32 * Abschnitt 14.32}) */ - response.setDateHeader(HEADER_EXPIRES, 0l); - response.addHeader(HEADER_PRAGMA, "no-cache"); + response.setDateHeader(HeaderNames.HEADER_EXPIRES, 0l); + response.addHeader(HeaderNames.HEADER_PRAGMA, "no-cache"); } StringBuilder builder = new StringBuilder(); @@ -276,7 +263,7 @@ public class CacheControl { builder.append('='); builder.append(entry.getValue()); } - response.addHeader(HEADER_CACHE_CONTROL, builder.toString()); + response.addHeader(HeaderNames.HEADER_CACHE_CONTROL, builder.toString()); } return true; @@ -296,14 +283,14 @@ public class CacheControl { } - interface CacheMethodHandle { + public interface CacheMethodHandle { long getTimestamp(); - int accepts(HttpServletRequest request) throws Exception; - int getCacheSeconds(HttpServletRequest request) throws Exception; - long getLastModified(HttpServletRequest request) throws Exception; - String getETag(HttpServletRequest request) throws Exception; + int accepts(HttpServletRequest request); + int getCacheSeconds(HttpServletRequest request); + long getLastModified(HttpServletRequest request); + String getETag(HttpServletRequest request); boolean isETagWeak(); - void cacheControl(HttpServletRequest request, Map cacheControlMap) throws Exception; + void cacheControl(HttpServletRequest request, Map cacheControlMap); } @@ -414,51 +401,63 @@ public class CacheControl { } @Override - public int accepts(HttpServletRequest request) - throws IllegalAccessException, - IllegalArgumentException, - InvocationTargetException - { - if (acceptsMethod == null) + public int accepts(HttpServletRequest request) throws IllegalArgumentException { + if (acceptsMethod == null) { return HttpServletResponse.SC_OK; - else - return (Integer)acceptsMethod.invoke(handler, request); + } + else { + try { + return (Integer)acceptsMethod.invoke(handler, request); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } } @Override - public int getCacheSeconds(HttpServletRequest request) - throws IllegalAccessException, - IllegalArgumentException, - InvocationTargetException - { - if (cacheSecondsMethod == null) + public int getCacheSeconds(HttpServletRequest request) throws IllegalArgumentException { + if (cacheSecondsMethod == null) { return cacheSeconds; - else - return (Integer)cacheSecondsMethod.invoke(handler, request); + } + else { + try { + return (Integer)cacheSecondsMethod.invoke(handler, request); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } } @Override - public long getLastModified(HttpServletRequest request) - throws IllegalAccessException, - IllegalArgumentException, - InvocationTargetException - { - if (lastModifiedMethod == null) + public long getLastModified(HttpServletRequest request) throws IllegalArgumentException { + if (lastModifiedMethod == null) { return lastModified; - else - return (Long)lastModifiedMethod.invoke(handler, request); + } + else { + try { + return (Long)lastModifiedMethod.invoke(handler, request); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } } @Override - public String getETag(HttpServletRequest request) - throws IllegalAccessException, - IllegalArgumentException, - InvocationTargetException - { - if (eTagMethod == null) + public String getETag(HttpServletRequest request) throws IllegalArgumentException { + if (eTagMethod == null) { return eTag; - else - return (String)eTagMethod.invoke(handler, request); + } + else { + try { + return (String)eTagMethod.invoke(handler, request); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } } @Override @@ -471,12 +470,16 @@ public class CacheControl { HttpServletRequest request, Map cacheControlMap ) - throws IllegalAccessException, - IllegalArgumentException, - InvocationTargetException + throws IllegalArgumentException { - if (cacheControlMethod != null) - cacheControlMethod.invoke(handler, request, cacheControlMap); + if (cacheControlMethod != null) { + try { + cacheControlMethod.invoke(handler, request, cacheControlMap); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } } }