Mercurial > dive4elements > river
changeset 4421:0b0f415203f0
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.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Tue, 06 Nov 2012 13:32:06 +0100 |
parents | 45d310b41c23 |
children | 95ffae40c8f2 |
files | flys-client/src/main/java/de/intevation/flys/client/server/NoCacheFilter.java |
diffstat | 1 files changed, 74 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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 :