1 package info.magnolia.cms.cache; 2 3 import java.io.IOException ; 4 5 import javax.servlet.Filter ; 6 import javax.servlet.FilterChain ; 7 import javax.servlet.FilterConfig ; 8 import javax.servlet.ServletException ; 9 import javax.servlet.ServletRequest ; 10 import javax.servlet.ServletResponse ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import org.apache.commons.lang.StringUtils; 15 16 17 22 public class CacheFilter implements Filter { 23 24 public static String ALREADY_FILTERED = CacheFilter.class.getName(); 25 26 29 private CacheManager cacheManager; 30 31 34 public void init(FilterConfig filterConfig) throws ServletException { 35 this.cacheManager = CacheManagerFactory.getCacheManager(); 36 } 37 38 41 public void destroy() { 42 this.cacheManager = null; 43 } 44 45 48 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException , 49 ServletException { 50 51 if (cacheManager.isEnabled() && cacheManager.isRunning()) { 52 HttpServletRequest request = (HttpServletRequest ) req; 53 boolean cacheable = cacheManager.isCacheable(request); 54 55 if (cacheable) { 60 HttpServletResponse response = (HttpServletResponse ) res; 61 62 CacheKey key = cacheManager.getCacheKey(request); 63 if (!this.ifModifiedSince((HttpServletRequest )req, cacheManager.getCreationTime(key))) { 64 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 65 return; 66 } 67 boolean canCompress = cacheManager.canCompress(request); 68 boolean usedCache = cacheManager.streamFromCache(key, response, canCompress 69 && clientAcceptsGzip(request)); 70 if (!usedCache && !isAlreadyFiltered(request) && cacheManager.isCacheable(request)) { 71 72 request.setAttribute(ALREADY_FILTERED, Boolean.TRUE); 74 75 CacheResponseWrapper responseWrapper = new CacheResponseWrapper(response); 76 77 chain.doFilter(request, responseWrapper); 78 79 if (responseWrapper.getStatus() == HttpServletResponse.SC_OK) { 81 CacheableEntry cacheableEntry = responseWrapper.getCacheableEntry(); 82 if (cacheableEntry != null) { 83 this.cacheManager.cacheRequest(key, cacheableEntry, canCompress); 84 } 85 } 86 return; 87 } 88 else if (usedCache) { 89 return; 91 } 92 } 93 } 94 95 chain.doFilter(req, res); 97 98 } 99 100 106 private boolean ifModifiedSince(HttpServletRequest request, long lastModified) { 107 try { 108 long headerValue = request.getDateHeader("If-Modified-Since"); 109 if (headerValue != -1) { 110 if ((request.getHeader("If-None-Match") == null) 113 && (lastModified <= headerValue + 1000)) { 114 return false; 115 } 116 } 117 } catch(IllegalArgumentException illegalArgument) { 118 return true; 119 } 120 return true; 121 } 122 123 private boolean clientAcceptsGzip(HttpServletRequest request) { 124 return StringUtils.contains(request.getHeader("Accept-Encoding"), "gzip"); 125 } 126 127 protected boolean isAlreadyFiltered(HttpServletRequest request) { 128 return request.getAttribute(ALREADY_FILTERED) != null; 129 } 130 131 } 132 | Popular Tags |