← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17159: Impl and configured HttpMaxCacheFilter. Builds in caching of static content. This means we no lon...

 

------------------------------------------------------------
revno: 17159
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-10-17 18:11:39 +0200
message:
  Impl and configured HttpMaxCacheFilter. Builds in caching of static content. This means we no longer need akward nginx configs.
added:
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/servlet/filter/HttpMaxCacheFilter.java
modified:
  dhis-2/dhis-web/dhis-web-portal/src/main/webapp/WEB-INF/web.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/servlet/filter/HttpMaxCacheFilter.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/servlet/filter/HttpMaxCacheFilter.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/servlet/filter/HttpMaxCacheFilter.java	2014-10-17 16:11:39 +0000
@@ -0,0 +1,102 @@
+package org.hisp.dhis.servlet.filter;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.hisp.dhis.system.util.DateUtils;
+import org.joda.time.DateTime;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class HttpMaxCacheFilter
+    implements Filter
+{
+    private static final String PARAM_REGEX = "regex";
+    private static final String HEADER_EXPIRES = "Expires";
+    private static final String HEADER_CACHE_CONTROL = "Cache-Control";
+    private static final String HEADER_MAX_CACHE = "public, max-age=31536000";
+    
+    private Pattern PATTERN = null;
+    
+    @Override
+    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
+        throws IOException, ServletException
+    {
+        if ( PATTERN != null && request != null && request instanceof HttpServletRequest &&
+            response != null && response instanceof HttpServletResponse )
+        {
+            HttpServletRequest httpRequest = (HttpServletRequest) request;
+            HttpServletResponse httpResponse = (HttpServletResponse) response;
+            
+            String uri = httpRequest.getRequestURI();
+            
+            if ( uri != null && PATTERN.matcher( uri ).find() )
+            {
+                DateTime expiry = new DateTime().plusYears( 1 );
+                
+                httpResponse.setHeader( HEADER_EXPIRES, DateUtils.getHttpDateString( expiry.toDate() ) );
+                httpResponse.setHeader( HEADER_CACHE_CONTROL, HEADER_MAX_CACHE );                
+            }
+        }
+        
+        chain.doFilter( request, response );
+    }
+
+    @Override
+    public void init( FilterConfig config )
+        throws ServletException
+    {
+        String regex = config.getInitParameter( PARAM_REGEX );
+        
+        if ( regex == null )
+        {
+            throw new IllegalStateException( "Init parameter 'regex' must be defined for HttpMaxCacheFilter" );
+        }
+        
+        PATTERN = Pattern.compile( regex );
+    }
+
+    @Override
+    public void destroy()
+    {
+    }
+}
+

=== modified file 'dhis-2/dhis-web/dhis-web-portal/src/main/webapp/WEB-INF/web.xml'
--- dhis-2/dhis-web/dhis-web-portal/src/main/webapp/WEB-INF/web.xml	2014-01-15 14:09:56 +0000
+++ dhis-2/dhis-web/dhis-web-portal/src/main/webapp/WEB-INF/web.xml	2014-10-17 16:11:39 +0000
@@ -13,6 +13,8 @@
     <param-value>databaseAdmin</param-value>
   </context-param>
 
+  <!-- Filters -->
+
   <filter>
     <filter-name>RedirectFilter</filter-name>
     <filter-class>org.hisp.dhis.servlet.filter.HttpRedirectFilter</filter-class>
@@ -22,6 +24,14 @@
     </init-param>
   </filter>
   <filter>
+    <filter-name>HttpMaxCacheFilter</filter-name>
+    <filter-class>org.hisp.dhis.servlet.filter.HttpMaxCacheFilter</filter-class>
+    <init-param>
+      <param-name>regex</param-name>
+      <param-value>(\.js|\.css|\.gif|\.woff|\.ttf|\.eot|\.ico|(/dhis-web-commons/|/images/|/icons/).*\.png)$</param-value>
+    </init-param>
+  </filter>
+  <filter>
     <filter-name>shallowEtagHeaderFilter</filter-name>
     <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
   </filter>
@@ -41,7 +51,6 @@
     <filter-name>Struts</filter-name>
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
-
   <filter>
     <filter-name>encoding-filter</filter-name>
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
@@ -64,6 +73,10 @@
     <url-pattern>/</url-pattern>
   </filter-mapping>
   <filter-mapping>
+    <filter-name>HttpMaxCacheFilter</filter-name>
+    <url-pattern>/*</url-pattern>
+  </filter-mapping>
+  <filter-mapping>
     <filter-name>OpenSessionInViewFilter</filter-name>
     <url-pattern>*.action</url-pattern>
   </filter-mapping>
@@ -96,6 +109,8 @@
     <url-pattern>*.action</url-pattern>
   </filter-mapping>
 
+  <!-- Listeners -->
+
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
@@ -103,6 +118,8 @@
     <listener-class>org.hisp.dhis.system.startup.StartupListener</listener-class>
   </listener>
 
+  <!-- Servlets -->
+
   <servlet>
     <servlet-name>webapi</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
@@ -112,7 +129,6 @@
     </init-param>
     <load-on-startup>1</load-on-startup>
   </servlet>
-
   <servlet>
     <servlet-name>mobile</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
@@ -122,7 +138,6 @@
     </init-param>
     <load-on-startup>1</load-on-startup>
   </servlet>
-
   <servlet>
     <servlet-name>ohie</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
@@ -133,37 +148,26 @@
     <load-on-startup>1</load-on-startup>
   </servlet>
 
-  <!-- Mobile -->
-
   <servlet-mapping>
     <servlet-name>mobile</servlet-name>
     <url-pattern>/mobile</url-pattern>
   </servlet-mapping>
-
   <servlet-mapping>
     <servlet-name>mobile</servlet-name>
     <url-pattern>/mobile/*</url-pattern>
   </servlet-mapping>
-
-  <!-- Web-API -->
-
   <servlet-mapping>
     <servlet-name>webapi</servlet-name>
     <url-pattern>/api</url-pattern>
   </servlet-mapping>
-
   <servlet-mapping>
     <servlet-name>webapi</servlet-name>
     <url-pattern>/api/*</url-pattern>
   </servlet-mapping>
-
-  <!-- OHIE Web-API -->
-
   <servlet-mapping>
     <servlet-name>ohie</servlet-name>
     <url-pattern>/ohie</url-pattern>
   </servlet-mapping>
-
   <servlet-mapping>
     <servlet-name>ohie</servlet-name>
     <url-pattern>/ohie/*</url-pattern>