[Dive4elements-commits] [PATCH 3 of 5] Add new Filter class to change caching

Wald Commits scm-commit at wald.intevation.org
Tue Nov 6 13:40:37 CET 2012


# HG changeset patch
# User Björn Ricks <bjoern.ricks at intevation.de>
# Date 1352205126 -3600
# Node ID 0b0f415203f0ac3415f32f49f5ccbc808f122b99
# Parent  45d310b41c2352ddd834457142923d577b0e2539
Add new Filter class to change caching

Add new Filter class to avoid caching of GWT nocache files in browsers and http
proxies. The new Filter class NoCacheFilter sets http headers to stop the
proxies and browesers from storing the nocache files.

diff -r 45d310b41c23 -r 0b0f415203f0 flys-client/src/main/java/de/intevation/flys/client/server/NoCacheFilter.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/NoCacheFilter.java	Tue Nov 06 13:32:06 2012 +0100
@@ -0,0 +1,74 @@
+package de.intevation.flys.client.server;
+
+import java.io.IOException;
+import java.util.Enumeration;
+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;
+
+import org.apache.log4j.Logger;
+
+/** ServletFilter to avoid caching for GWTs *.nocache.* files. */
+public class NoCacheFilter implements Filter {
+
+    private static final long DAY = 86400000L;
+
+    private static final String NO_CACHE = ".nocache.";
+
+    private static Logger log = Logger.getLogger(NoCacheFilter.class);
+
+    /**
+     * Initialize.
+     */
+    @Override
+    public void init(FilterConfig config)
+    throws ServletException
+    {
+    }
+
+
+    /**
+     * Called when filter in chain invoked.
+     * @param req request to servlet
+     * @param resp response of servlet
+     * @param chain the filter chain
+     */
+    @Override
+    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
+    throws IOException, ServletException
+    {
+        HttpServletRequest httpreq = (HttpServletRequest)req;
+        String uri = httpreq.getRequestURI();
+
+        if (uri.contains(NO_CACHE)) {
+            log.debug("Set no-cache for " + uri);
+
+            Date now = new Date();
+            HttpServletResponse httpresp = (HttpServletResponse)resp;
+            httpresp.setDateHeader("Date", now.getTime());
+            httpresp.setDateHeader("Expires", now.getTime() - DAY);
+            httpresp.setHeader("Pragma", "no-cache");
+            httpresp.setHeader("Cache-control",
+                    "no-cache, no-store, must-revalidate");
+        }
+
+        chain.doFilter(req, resp);
+    }
+
+
+    /**
+     * Do nothing at destruction.
+     */
+    @Override
+    public void destroy() {
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list