Commits

Anonymous committed e330666

Two new methods isCacheable. Needed for the issues jsessionid
cache option and further support of the cache directive headers.
And easier sub-classing of CacheFilter.

Issue number: CACHE-120, CACHE-137, CACHE-141
Submitted by: Lars Torunski

Comments (0)

Files changed (1)

src/core/java/com/opensymphony/oscache/web/filter/CacheFilter.java

     public static final String HEADER_CONTENT_ENCODING = "Content-Encoding";
     public static final String HEADER_EXPIRES = "Expires";
     public static final String HEADER_IF_MODIFIED_SINCE = "If-Modified-Since";
+    public static final String HEADER_CACHE_CONTROL = "Cache-control";
+    public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
 
     // Fragment parameter
     public static final int FRAGMENT_AUTODETECT = -1;
             log.info("<cache>: filter in scope " + cacheScope);
         }
 
-        // avoid reentrance (CACHE-128)
-        if (isFilteredBefore(request)) {
+        // avoid reentrance (CACHE-128) and check if request is cacheable
+        if (isFilteredBefore(request) || !isCacheable(request)) {
             chain.doFilter(request, response);
             return;
         }
-
         request.setAttribute(REQUEST_FILTERED, Boolean.TRUE);
 
         HttpServletRequest httpRequest = (HttpServletRequest) request;
 
-        // checks if the response is a fragment of a apge
+        // checks if the response well be a fragment of a page
         boolean fragmentRequest = isFragment(httpRequest);
 
         // generate the cache entry key
 
         // avoid useless session creation for application scope pages (CACHE-129)
         Cache cache;
-
         if (cacheScope == PageContext.SESSION_SCOPE) {
             cache = admin.getSessionScopeCache(httpRequest.getSession(true));
         } else {
                 chain.doFilter(request, cacheResponse);
                 cacheResponse.flushBuffer();
 
-                // Only cache if the response was 200
-                if (cacheResponse.getStatus() == HttpServletResponse.SC_OK) {
+                // Only cache if the response is cacheable
+                if (isCacheable(cacheResponse)) {
                     //Store as the cache content the result of the response
                     cache.putInCache(key, cacheResponse.getContent(), expiresRefreshPolicy);
                     updateSucceeded = true;
     }
 
     /**
+     * isCacheable is a method allowing subclass to decide if a request is
+     * cachable or not.
+     * 
+     * @param request The servlet request
+     * @return Returns a boolean indicating if the request can be cached or not.
+     */
+    protected boolean isCacheable(ServletRequest request) {
+        // TODO implement CACHE-120, CACHE-137 and CACHE-141 here
+        boolean cachable = request instanceof HttpServletRequest;
+
+        if (log.isDebugEnabled()) {
+            log.debug("<cache>: the request " + ((cachable) ? "is" : "is not") + " cachable.");
+        }
+        
+        return cachable;
+    }
+    
+    /**
+     * isCacheable is a method allowing subclass to decide if a response is
+     * cachable or not.
+     * 
+     * @param cacheResponse The HTTP servlet response
+     * @return Returns a boolean indicating if the response can be cached or not.
+     */
+    protected boolean isCacheable(CacheHttpServletResponseWrapper cacheResponse) {
+        // TODO implement CACHE-137 and CACHE-141 here
+        // Only cache if the response was 200
+        boolean cachable = cacheResponse.getStatus() == HttpServletResponse.SC_OK;
+
+        if (log.isDebugEnabled()) {
+            log.debug("<cache>: the response " + ((cachable) ? "is" : "is not") + " cachable.");
+        }
+        
+        return cachable;
+    }
+
+    /**
      * Check if the client browser support gzip compression.
+     * 
      * @param request the http request
      * @return true if client browser supports GZIP
      */
     protected boolean acceptsGZipEncoding(HttpServletRequest request) {
-        String acceptEncoding = request.getHeader("Accept-Encoding");
+        String acceptEncoding = request.getHeader(HEADER_ACCEPT_ENCODING);
         return  (acceptEncoding != null) && (acceptEncoding.indexOf("gzip") != -1);
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.