Commits

Anonymous committed ea884fb

A fix for the "Last-Modified" header problem (see CACHE-58 & CACHE-70), as supplied by Kurt Williams.

  • Participants
  • Parent commits b5f7807

Comments (0)

Files changed (3)

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

      * @throws ServletException IOException
      */
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
-        log.info("<cache>: filter in scope " + cacheScope);
+        if (log.isInfoEnabled()) {
+            log.info("<cache>: filter in scope " + cacheScope);
+        }
 
         HttpServletRequest httpRequest = (HttpServletRequest) request;
         String key = admin.generateEntryKey(null, httpRequest, cacheScope);
 
         try {
             ResponseContent respContent = (ResponseContent) cache.getFromCache(key, time);
-            log.info("<cache>: Using cached entry for " + key);
+
+            if (log.isInfoEnabled()) {
+                log.info("<cache>: Using cached entry for " + key);
+            }
+
+            long clientLastModified = httpRequest.getDateHeader("If-Modified-Since"); // will return -1 if no header...
+
+            if (clientLastModified >= respContent.getLastModified()) {
+                ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+                return;
+            }
+
             respContent.writeTo(response);
         } catch (NeedsRefreshException nre) {
             boolean updateSucceeded = false;
 
             try {
-                log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
+                if (log.isInfoEnabled()) {
+                    log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
+                }
 
                 CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response);
                 chain.doFilter(request, cacheResponse);

File src/core/java/com/opensymphony/oscache/web/filter/CacheHttpServletResponseWrapper.java

             log.debug("dateheader: " + name + ": " + value);
         }
 
+        if ("last-modified".equalsIgnoreCase(name)) {
+            result.setLastModified(value);
+        }
+
         super.setDateHeader(name, value);
     }
 

File src/core/java/com/opensymphony/oscache/web/filter/ResponseContent.java

 import java.util.Locale;
 
 import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * Holds the servlet response in a byte array so that it can be held
     private Locale locale = null;
     private String contentType = null;
     private byte[] content = null;
+    private long lastModified = -1;
 
     /**
      * Set the content type. We capture this so that when we serve this
         contentType = value;
     }
 
+    public long getLastModified() {
+        return lastModified;
+    }
+
+    public void setLastModified(long value) {
+        lastModified = value;
+    }
+
     /**
      * Set the Locale. We capture this so that when we serve this data from
      * cache, we can set the correct locale on the response.
             response.setContentType(contentType);
         }
 
+        if (response instanceof HttpServletResponse) {
+            ((HttpServletResponse) response).setDateHeader("Last-Modified", lastModified);
+        }
+
         response.setContentLength(content.length);
 
         if (locale != null) {