From c31c3d025f6cc6f59faec00d4dd64691f69e4bc9 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 13 Nov 2011 22:49:59 +0100 Subject: [PATCH] =?utf8?q?=C3=9Cberfl=C3=BCssige=20Hilfsklasse=20DefaultCa?= =?utf8?q?cheMethodHandle=20ausgebaut?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../juplo/cachecontrol/CacheControl.java | 145 +++++++----------- 1 file changed, 58 insertions(+), 87 deletions(-) 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 07f4c58e..0d79436e 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/CacheControl.java @@ -276,84 +276,57 @@ public class CacheControl { void cacheControl(HttpServletRequest request, Map cacheControlMap) throws Exception; } - class DefaultCacheMethodHandle implements CacheMethodHandle { - - long now = System.currentTimeMillis(); - Integer cacheSeconds; - Long lastModified; - String eTag; - - - DefaultCacheMethodHandle() { - this.cacheSeconds = CacheControl.this.defaultCacheSeconds; - this.lastModified = CacheControl.this.defaultLastModified; - this.eTag = null; - } - - - @Override - public long getTimestamp() { - return now; - } - - @Override - public int accepts(HttpServletRequest request) { - return HttpServletResponse.SC_OK; - } - - @Override - public int getCacheSeconds(HttpServletRequest request) { - return cacheSeconds; - } - - @Override - public long getLastModified(HttpServletRequest request) { - return lastModified; - } - - @Override - public String getETag(HttpServletRequest request) { - return eTag; - } - - @Override - public void cacheControl(HttpServletRequest request, Map cacheControlMap) { - } - } class ReflectionCacheMethodHandle implements CacheMethodHandle { private Object handler; - private DefaultCacheMethodHandle defaults = new DefaultCacheMethodHandle(); - private Method accepts, cacheSeconds, lastModified, eTag, cacheControl; - private boolean isAcceptsDefined, isCacheSecondsDefined, isLastModifiedDefined, isETagDefined, isCacheControlDefined; + private long now = System.currentTimeMillis(); + private Integer cacheSeconds; + private Long lastModified; + private String eTag; + private Method acceptsMethod; + private Method cacheSecondsMethod; + private Method lastModifiedMethod; + private Method eTagMethod; + private Method cacheControlMethod; + private boolean isAcceptsMethodDefined; + private boolean isCacheSecondsMethodDefined; + private boolean isLastModifiedMethodDefined; + private boolean isETagMethodDefined; + private boolean isCacheControlMethodDefined; ReflectionCacheMethodHandle(Object handler) throws NoSuchMethodException { + this.handler = handler; + + cacheSeconds = CacheControl.this.defaultCacheSeconds; + lastModified = CacheControl.this.defaultLastModified; + eTag = null; + /** Class-Level-Annotations auslesen */ for (Annotation annotation : handler.getClass().getAnnotations()) { if (annotation.annotationType().equals(CacheSeconds.class)) { - defaults.cacheSeconds = ((CacheSeconds)annotation).value(); - isCacheSecondsDefined = true; + cacheSeconds = ((CacheSeconds)annotation).value(); + isCacheSecondsMethodDefined = true; continue; } if (annotation.annotationType().equals(LastModified.class)) { - defaults.lastModified = ((LastModified)annotation).value(); - if (defaults.lastModified < 1) { + lastModified = ((LastModified)annotation).value(); + if (lastModified < 1) { /** * Ein Last-Modified-Header wurde angefordert, aber es wurde kein * statischer Wert spezifiziert: * globalen statischen Default-Wert benutzen! */ - defaults.lastModified = defaultLastModified; + lastModified = defaultLastModified; } - isLastModifiedDefined = true; + isLastModifiedMethodDefined = true; continue; } if (annotation.annotationType().equals(ETag.class)) { - defaults.eTag = ((ETag)annotation).value(); - isETagDefined = true; + eTag = ((ETag)annotation).value(); + isETagMethodDefined = true; continue; } } @@ -362,38 +335,38 @@ public class CacheControl { for (Method method : handler.getClass().getMethods()) { for (Annotation annotation : method.getAnnotations()) { if (annotation.annotationType().equals(Accepts.class)) { - if (isAcceptsDefined) + if (isAcceptsMethodDefined) throw new IllegalArgumentException("Die Annotation @Accept wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - accepts = method; - isAcceptsDefined = true; + acceptsMethod = method; + isAcceptsMethodDefined = true; continue; } if (annotation.annotationType().equals(CacheSeconds.class)) { - if (isCacheSecondsDefined) + if (isCacheSecondsMethodDefined) throw new IllegalArgumentException("Die Annotation @CacheSeconds wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - cacheSeconds = method; - isCacheSecondsDefined = true; + cacheSecondsMethod = method; + isCacheSecondsMethodDefined = true; continue; } if (annotation.annotationType().equals(LastModified.class)) { - if (isLastModifiedDefined) + if (isLastModifiedMethodDefined) throw new IllegalArgumentException("Die Annotation @LastModified wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - lastModified = method; - isLastModifiedDefined = true; + lastModifiedMethod = method; + isLastModifiedMethodDefined = true; continue; } if (annotation.annotationType().equals(ETag.class)) { - if (isETagDefined) + if (isETagMethodDefined) throw new IllegalArgumentException("Die Annotation @ETag wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - eTag = method; - isETagDefined = true; + eTagMethod = method; + isETagMethodDefined = true; continue; } if (annotation.annotationType().equals(de.halbekunst.juplo.cachecontrol.annotations.CacheControl.class)) { - if (isCacheControlDefined) + if (isCacheControlMethodDefined) throw new IllegalArgumentException("Die Annotation @CacheControl wurde in der Klasse " + handler.getClass().getSimpleName() + " mehrfach verwendet!"); - cacheControl = method; - isCacheControlDefined = true; + cacheControlMethod = method; + isCacheControlMethodDefined = true; continue; } } @@ -403,7 +376,7 @@ public class CacheControl { @Override public long getTimestamp() { - return defaults.now; + return now; } @Override @@ -412,10 +385,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (accepts == null) - return defaults.accepts(request); + if (acceptsMethod == null) + return HttpServletResponse.SC_OK; else - return (Integer)accepts.invoke(handler, request); + return (Integer)acceptsMethod.invoke(handler, request); } @Override @@ -424,10 +397,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (cacheSeconds == null) - return defaults.getCacheSeconds(request); + if (cacheSecondsMethod == null) + return cacheSeconds; else - return (Integer)cacheSeconds.invoke(handler, request); + return (Integer)cacheSecondsMethod.invoke(handler, request); } @Override @@ -436,10 +409,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (lastModified == null) - return defaults.getLastModified(request); + if (lastModifiedMethod == null) + return lastModified; else - return (Long)lastModified.invoke(handler, request); + return (Long)lastModifiedMethod.invoke(handler, request); } @Override @@ -448,10 +421,10 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (eTag == null) - return defaults.getETag(request); + if (eTagMethod == null) + return eTag; else - return (String)eTag.invoke(handler, request); + return (String)eTagMethod.invoke(handler, request); } @Override @@ -463,10 +436,8 @@ public class CacheControl { IllegalArgumentException, InvocationTargetException { - if (cacheControl == null) - defaults.cacheControl(request, cacheControlMap); - else - cacheControl.invoke(handler, request, cacheControlMap); + if (cacheControlMethod != null) + cacheControlMethod.invoke(handler, request, cacheControlMap); } } -- 2.20.1