You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/01/19 19:41:01 UTC

svn commit: r370568 - /incubator/roller/trunk/src/org/roller/presentation/filters/IfPlanetModifiedFilter.java

Author: agilliland
Date: Thu Jan 19 10:40:59 2006
New Revision: 370568

URL: http://svn.apache.org/viewcvs?rev=370568&view=rev
Log:
updated to cache the last modified date rather than fetch it from the db on every request.


Modified:
    incubator/roller/trunk/src/org/roller/presentation/filters/IfPlanetModifiedFilter.java

Modified: incubator/roller/trunk/src/org/roller/presentation/filters/IfPlanetModifiedFilter.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/filters/IfPlanetModifiedFilter.java?rev=370568&r1=370567&r2=370568&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/filters/IfPlanetModifiedFilter.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/filters/IfPlanetModifiedFilter.java Thu Jan 19 10:40:59 2006
@@ -14,19 +14,16 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.roller.RollerException;
-import org.roller.model.Roller;
+import org.roller.config.RollerConfig;
 import org.roller.model.RollerFactory;
-import org.roller.presentation.RollerRequest;
+import org.roller.presentation.PlanetRequest;
+import org.roller.presentation.cache.ExpiringCacheEntry;
 
 
 /**
- * Entry point filter for Newsfeed Servlets, this filter
- * Handles If-Modified-Since header using per-user and per-category
- * last weblog pub time. Returns 304 if requested weblog has not been
- * modified since. Also, sets Last-Modified on outgoing response.
+ * Handles if-modified-since checking for planet resources.
  *
  * @web.filter name="IfPlanetModifiedFilter"
- * web.filter-mapping url-pattern="/planetrss/*"
  *
  * @author David M Johnson
  */
@@ -34,19 +31,17 @@
     
     private static Log mLogger = LogFactory.getLog(IfPlanetModifiedFilter.class);
     
-    SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
-    
+    private long timeout = 15 * 60 * 1000;
+    private ExpiringCacheEntry lastUpdateTime = null;
     
-    public IfPlanetModifiedFilter() {
-        super();
-    }
+    SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
     
     
     /**
-     * @see javax.servlet.Filter#doFilter(
-     * javax.servlet.ServletRequest,
-     * javax.servlet.ServletResponse,
-     * javax.servlet.FilterChain)
+     * Filter processing.
+     *
+     * We check the incoming request for an "if-modified-since" header and
+     * repond with a 304 NOT MODIFIED when appropriate.
      */
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
             throws IOException, ServletException {
@@ -54,9 +49,33 @@
         HttpServletRequest request = (HttpServletRequest) req;
         HttpServletResponse response = (HttpServletResponse) res;
         
+        PlanetRequest planetRequest = null;
+        try {
+            planetRequest = new PlanetRequest(request);
+        } catch(Exception e) {
+            mLogger.error("error creating planet request", e);
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return;
+        }
+        
         Date updateTime = null;
         try {
-            updateTime = getLastPublishedDate(request);
+            // first try our cached version
+            if(this.lastUpdateTime != null) {
+                updateTime = (Date) this.lastUpdateTime.getValue();
+            }
+            
+            // we need to get a fresh value
+            if(updateTime == null) {
+                
+                updateTime = RollerFactory.getRoller().getPlanetManager().getLastUpdated();
+                if (updateTime == null) {
+                    updateTime = new Date();
+                    mLogger.warn("Can't get lastUpdate time, using current time instead");
+                }
+                
+                this.lastUpdateTime = new ExpiringCacheEntry(updateTime, this.timeout);
+            }
             
             // RSS context loader needs updateTime, so stash it
             request.setAttribute("updateTime", updateTime);
@@ -76,12 +95,13 @@
                     return;
                 }
             }
-            mLogger.debug("Not returning 304 for: "+request.getRequestURI());
-        } catch (RollerException e) {
-            // Thrown by getLastPublishedDate if there is a db-type error
-            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+            
+        } catch(RollerException re) {
+            // problem talking to db?
+            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+            request.setAttribute("DisplayException", re);
             return;
-        } catch (IllegalArgumentException e) {
+        } catch(IllegalArgumentException e) {
             // Thrown by getDateHeader if not in valid format. This can be
             // safely ignored, the only consequence is that the NOT MODIFIED
             // response is not set.
@@ -96,22 +116,22 @@
     }
     
     
-    public static Date getLastPublishedDate(HttpServletRequest request)
-            throws RollerException {
+    /**
+     * Init method for this filter
+     */
+    public void init(FilterConfig filterConfig) {
         
-        RollerRequest rreq = RollerRequest.getRollerRequest(request);
-        Roller roller = RollerFactory.getRoller();
-        Date lastUpdated = roller.getPlanetManager().getLastUpdated();
-        if (lastUpdated == null) {
-            lastUpdated = new Date();
-            mLogger.warn("Can't get lastUpdate time, using current time instead");
-        }
+        mLogger.info("Initializing if-modified planet filter");
         
-        return lastUpdated;
+        // lookup our timeout value
+        String timeoutString = RollerConfig.getProperty("cache.planet.timeout");
+        try {
+            long timeoutSecs = Long.parseLong(timeoutString);
+            this.timeout = timeoutSecs * 1000;
+        } catch(Exception e) {
+            // ignored ... illegal value
+        }
     }
-    
-
-    public void init(FilterConfig filterConfig) throws ServletException {}
     
     
     public void destroy() {}