Make sure *nocache* files are never cached and *cache* files are cached for a year...
authorPanagiotis Astithas <pastith@gmail.com>
Fri, 30 Oct 2009 15:00:13 +0000 (17:00 +0200)
committerPanagiotis Astithas <pastith@gmail.com>
Fri, 30 Oct 2009 15:00:13 +0000 (17:00 +0200)
src/gr/ebs/gss/server/CacheFilter.java [new file with mode: 0644]
war/WEB-INF/web.xml

diff --git a/src/gr/ebs/gss/server/CacheFilter.java b/src/gr/ebs/gss/server/CacheFilter.java
new file mode 100644 (file)
index 0000000..734c166
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2009 Electronic Business Systems Ltd.
+ *
+ * This file is part of GSS.
+ *
+ * GSS is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GSS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package gr.ebs.gss.server;
+
+import java.io.IOException;
+import java.util.Date;
+
+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;
+
+/**
+ * A cache filter that properly caches GWT-generated files.
+ *
+ * @author past
+ *
+ */
+public class CacheFilter implements Filter {
+       public void doFilter( ServletRequest request, ServletResponse response,
+                       FilterChain filterChain) throws IOException, ServletException {
+               HttpServletRequest httpRequest = (HttpServletRequest)request;
+               String requestURI = httpRequest.getRequestURI();
+               if (requestURI.contains(".nocache.")) {
+                       HttpServletResponse httpResponse = (HttpServletResponse)response;
+                       httpResponse.setHeader("Expires", "Fri, 01 Jan 1990 00:00:00 GMT");
+                       httpResponse.setHeader("Pragma", "no-cache");
+                       httpResponse.setHeader("Cache-control", "no-cache, must-revalidate");
+               } else if (requestURI.contains(".cache.")) {
+                       long today = new Date().getTime();
+                       HttpServletResponse httpResponse = (HttpServletResponse)response;
+                       httpResponse.setDateHeader("Expires", today+31536000000L);
+               } else {
+                       long today = new Date().getTime();
+                       HttpServletResponse httpResponse = (HttpServletResponse)response;
+                       httpResponse.setDateHeader("Expires", today+3456000000L);
+               }
+               filterChain.doFilter(request, response);
+       }
+
+       @Override
+       public void destroy() {
+       }
+
+       @Override
+       public void init(FilterConfig filterConfig) {
+       }
+}
index 57b6ad8..e98246f 100644 (file)
                    /error403.html
                </location>
            </error-page>
+
+               <filter>
+                       <filter-name>CacheFilter</filter-name>
+                       <filter-class>gr.ebs.gss.server.CacheFilter</filter-class>
+               </filter>
+               <filter-mapping>
+                       <filter-name>CacheFilter</filter-name>
+                       <url-pattern>/*</url-pattern>
+               </filter-mapping>
+
         <servlet>
                 <servlet-name>Login</servlet-name>
                 <servlet-class>gr.ebs.gss.server.Login</servlet-class>