Created
March 26, 2015 12:45
-
-
Save bradparks/88511eba31e3124448e0 to your computer and use it in GitHub Desktop.
Psuedo code for filter that adds content caching header to all responses - may not work - untested
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mport java.io.IOException; | |
import javax.servlet.*; | |
import javax.servlet.http.*; | |
import javax.ws.rs.core.MediaType; | |
public class AddCacheHeader implements Filter | |
{ | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException | |
{ | |
HttpServletResponse r = (HttpServletResponse) response; | |
HttpServletResponse wrappedResponse = new HttpServletResponseWrapper(r) | |
{ | |
@Override | |
public ServletOutputStream getOutputStream() throws java.io.IOException | |
{ | |
ServletResponse response = this.getResponse(); | |
String ct = (response != null) ? response.getContentType() : null; | |
if (ct != null && ct.toLowerCase().startsWith(MediaType.TEXT_HTML)) | |
{ | |
HttpServletResponse httpResponse = (HttpServletResponse) response; | |
httpResponse.setHeader("Cache-control", "no-cache, no-store, must-revlidate, max-age=0"); | |
} | |
return super.getOutputStream(); | |
} | |
}; | |
chain.doFilter(request, wrappedResponse); | |
} | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException | |
{ | |
// This method intentionally left blank | |
} | |
@Override | |
public void destroy() | |
{ | |
// This method intentionally left blank | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment