Commits

Anonymous committed ba97628

Issue number: CACHE-204 Allow disabling initial setting of last modified header
Obtained from: Lars Torunski

Comments (0)

Files changed (2)

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

     public static final int NOCACHE_OFF = 0;
     public static final int NOCACHE_SESSION_ID_IN_URL = 1;
     
+    // Last Modified parameter
+    public static final long LAST_MODIFIED_OFF = 0;
+    public static final long LAST_MODIFIED_ON = 1;
+    public static final long LAST_MODIFIED_INITIAL = -1;
+    
     // Expires parameter
     public static final long EXPIRES_OFF = 0;
     public static final long EXPIRES_ON = 1;
     private int fragment = FRAGMENT_AUTODETECT; // defines if this filter handles fragments of a page - default is auto detect
     private int time = 60 * 60; // time before cache should be refreshed - default one hour (in seconds)
     private int nocache = NOCACHE_OFF; // defines special no cache option for the requests - default is off
+    private long lastModified = LAST_MODIFIED_INITIAL; // defines if the last-modified-header will be sent - default is intial setting
     private long expires = EXPIRES_ON; // defines if the expires-header will be sent - default is on
     private ICacheKeyProvider cacheKeyProvider = this; // the provider of the cache key - default is the CacheFilter itselfs
     private ICacheGroupsProvider cacheGroupsProvider = this; // the provider of the cache groups - default is the CacheFilter itselfs
             }
 
             boolean acceptsGZip = false;
-            if (!fragmentRequest) {
+            if ((!fragmentRequest) && (lastModified != LAST_MODIFIED_OFF)) {
                 long clientLastModified = httpRequest.getDateHeader(HEADER_IF_MODIFIED_SINCE); // will return -1 if no header...
 
                 // only reply with SC_NOT_MODIFIED
                     log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
                 }
 
-                CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response, fragmentRequest, time * 1000, expires);
+                CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response, fragmentRequest, time * 1000, lastModified, expires);
                 chain.doFilter(request, cacheResponse);
                 cacheResponse.flushBuffer();
 
      * are <code>off</code> (default) and <code>sessionIdInURL</code> if the session id is
      * contained in the URL.</li>
      * 
+     * <li><b>lastModified</b> - defines if the last modified header will be sent in the response. Acceptable values are
+     * <code>off</code> for don't sending the header, even it is set in the filter chain, 
+     * <code>on</code> for sending it if it is set in the filter chain and 
+     * <code>inital</code> (default) the last modified information will be set based on the current time and changes are allowed.</li>
+     * 
      * <li><b>expires</b> - defines if the expires header will be sent in the response. Acceptable values are
      * <code>off</code> for don't sending the header, even it is set in the filter chain, 
      * <code>on</code> (default) for sending it if it is set in the filter chain and 
                 fragment = FRAGMENT_YES;
             } else if (fragmentString.equalsIgnoreCase("auto")) {
                 fragment = FRAGMENT_AUTODETECT;
-            } else {
-                // FIXME in 2.2 RC the values were -1, 0. In 2.2.1 or >= 2.3 delete this code
-                try {
-                    fragment = Integer.parseInt(fragmentString);
-
-                    if ((fragment < FRAGMENT_AUTODETECT) || (fragment > FRAGMENT_YES)) {
-                        log.info("Wrong init parameter 'fragment', setting to 'auto detect': " + fragment);
-                        fragment = FRAGMENT_AUTODETECT;
-                    }
-                    
-                    log.warn("The used value '" + fragmentString + "' for the fragment parameter is deprecated.");
-                } catch (Exception e2) {
-                    log.info("Could not get init parameter 'fragment', defaulting to 'auto detect'.");
-                }
-                // end of deletion
             }
         } catch (Exception e) {
             log.info("Could not get init parameter 'fragment', defaulting to 'auto detect'.");
             log.info("Could not get init parameter 'nocache', defaulting to 'off'.");
         }
 
+        // filter parameter last modified
+        try {
+            String lastModifiedString = config.getInitParameter("lastModified");
+            
+            if (lastModifiedString.equals("off")) {
+                lastModified = LAST_MODIFIED_OFF;
+            } else if (lastModifiedString.equals("on")) {
+                lastModified = LAST_MODIFIED_ON;
+            } else if (lastModifiedString.equalsIgnoreCase("initial")) {
+                lastModified = LAST_MODIFIED_INITIAL;
+            } 
+        } catch (Exception e) {
+            log.info("Could not get init parameter 'lastModified', defaulting to 'initial'.");
+        }
+        
         // filter parameter expires
         try {
             String expiresString = config.getInitParameter("expires");

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

     private boolean fragment = false;
     private int status = SC_OK;
     private long expires = CacheFilter.EXPIRES_ON;
+    private long lastModified = CacheFilter.LAST_MODIFIED_INITIAL;
 
     /**
      * Constructor
      * @param response The servlet response
      */
     public CacheHttpServletResponseWrapper(HttpServletResponse response) {
-        this(response, false, Long.MAX_VALUE, CacheFilter.EXPIRES_ON);
+        this(response, false, Long.MAX_VALUE, CacheFilter.EXPIRES_ON, CacheFilter.LAST_MODIFIED_INITIAL);
     }
 
     /**
      * @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
+     * @param lastModified defines if last modified header will be send, @see CacheFilter
      * @param expires defines if expires header will be send, @see CacheFilter
      */
-    public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment, long time, long expires) {
+    public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment, long time, long lastModified, long expires) {
         super(response);
         result = new ResponseContent();
         this.fragment = fragment;
         this.expires = expires;
+        this.lastModified = lastModified;
         
         // only set inital values for last modified and expires, when a complete page is cached
         if (!fragment) {
             // setting a default last modified value based on object creation and remove the millis
-            long current = System.currentTimeMillis() / 1000;
-            result.setLastModified(current * 1000);
-            super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified());
+            if (lastModified == CacheFilter.LAST_MODIFIED_INITIAL) {
+                long current = System.currentTimeMillis() / 1000;
+                result.setLastModified(current * 1000);
+                super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified());
+            }
             // setting the expires value
             if (expires == CacheFilter.EXPIRES_TIME) {
                 result.setExpires(result.getLastModified() + time);
         }
 
         // only set the last modified value, if a complete page is cached
-        if (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name)) {
+        if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
             if (!fragment) {
                 result.setLastModified(value);
             } // TODO should we return now by fragments to avoid putting the header to the response?
         }
 
         // only set the last modified value, if a complete page is cached
-        if (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name)) {
+        if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
             if (!fragment) {
                 result.setLastModified(value);
             } // TODO should we return now by fragments to avoid putting the header to the response?
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.