Commits

Anonymous committed 44268af

Reporting expected expiry to clients/browsers/proxy
Issue number: CACHE-143
Submitted by: Lars Torunski

Comments (0)

Files changed (3)

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

                     log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
                 }
 
-                CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response, fragmentRequest);
+                CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response, fragmentRequest, time * 1000);
                 chain.doFilter(request, cacheResponse);
                 cacheResponse.flushBuffer();
 
     }
 
     /**
-     * isCacheable is a method allowing subclass to decide if a request is
+     * isCacheable is a method allowing a subclass to decide if a request is
      * cachable or not.
      * 
      * @param request The servlet request
 
         if (cachable) {
             HttpServletRequest requestHttp = (HttpServletRequest) request;
-            if (nocache == NOCACHE_SESSION_ID_IN_URL) { // don't cache requests if session id is in URL
+            if (nocache == NOCACHE_SESSION_ID_IN_URL) { // don't cache requests if session id is in the URL
                 cachable = !requestHttp.isRequestedSessionIdFromURL();
             }
         }

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

      * @param response The servlet response
      */
     public CacheHttpServletResponseWrapper(HttpServletResponse response) {
-        this(response, false);
+        this(response, false, Long.MAX_VALUE);
     }
 
     /**
      * Constructor
      *
      * @param response The servlet response
+     * @param fragment true if the repsonse indicates that it is a fragement of a page
+     * @param time the refresh time in millis
      */
-    public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment) {
+    public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment, long time) {
         super(response);
         result = new ResponseContent();
         this.fragment = fragment;
             long current = System.currentTimeMillis() / 1000;
             result.setLastModified(current * 1000);
             super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified());
+            // setting the expires value
+            result.setExpires(result.getLastModified() + time);
+            super.setDateHeader(CacheFilter.HEADER_EXPIRES, result.getExpires());
         }
     }
 

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

             response.setContentType(contentType);
         }
         
-        // Don't support gzip compression if the content is a fragment of a page
         if (fragment) {
+            // Don't support gzip compression if the content is a fragment of a page
             acceptsGZip = false;
+        } else {
+            // add special headers for a complete page
+            if (response instanceof HttpServletResponse) {
+                HttpServletResponse httpResponse = (HttpServletResponse) response;
+                
+                // add the last modified header
+                if (lastModified != -1) {
+                    httpResponse.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified);
+                }
+                
+                // add the expires header
+                if (expires != Long.MAX_VALUE) {
+                    httpResponse.setDateHeader(CacheFilter.HEADER_EXPIRES, expires);
+                }
+            }
         }
 
-        // Don't add in the Last-Modified header in a fragment of a page
-        if ((!fragment) && (lastModified != -1) && (response instanceof HttpServletResponse)) {
-            ((HttpServletResponse) response).setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified);
-        } // FIXME maybe remove header if it exists
-
         if (locale != null) {
             response.setLocale(locale);
         }