|
| 1 | +/* |
| 2 | + * $Id$ |
| 3 | + * |
| 4 | + * Copyright 2008 samaxes.com |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.samaxes.filter; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import javax.servlet.Filter; |
| 23 | +import javax.servlet.FilterChain; |
| 24 | +import javax.servlet.FilterConfig; |
| 25 | +import javax.servlet.ServletException; |
| 26 | +import javax.servlet.ServletRequest; |
| 27 | +import javax.servlet.ServletResponse; |
| 28 | +import javax.servlet.http.HttpServletResponse; |
| 29 | + |
| 30 | +/** |
| 31 | + * Filter responsible for browser caching. |
| 32 | + * |
| 33 | + * @author : Samuel Santos |
| 34 | + * @version : $Revision: 25 $ |
| 35 | + */ |
| 36 | +public class CacheFilter implements Filter { |
| 37 | + |
| 38 | + private FilterConfig filterConfig; |
| 39 | + |
| 40 | + /** |
| 41 | + * Place this filter into service. |
| 42 | + * |
| 43 | + * @param filterConfig {@link FilterConfig} |
| 44 | + */ |
| 45 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 46 | + this.filterConfig = filterConfig; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Take this filter out of service. |
| 51 | + */ |
| 52 | + public void destroy() { |
| 53 | + this.filterConfig = null; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets cache headers directives. |
| 58 | + * |
| 59 | + * @param servletRequest {@link ServletRequest} |
| 60 | + * @param servletResponse {@link ServletResponse} |
| 61 | + * @param filterChain {@link FilterChain} |
| 62 | + * @throws IOException {@link FilterChain} |
| 63 | + * @throws ServletException {@link ServletException} |
| 64 | + */ |
| 65 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) |
| 66 | + throws IOException, ServletException { |
| 67 | + HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; |
| 68 | + String privacy = filterConfig.getInitParameter("privacy"); |
| 69 | + String expirationTime = filterConfig.getInitParameter("expirationTime"); |
| 70 | + |
| 71 | + if (httpServletResponse != null && privacy != null && !"".equals(privacy) && expirationTime != null |
| 72 | + && !"".equals(expirationTime)) { |
| 73 | + long seconds = Long.valueOf(expirationTime); |
| 74 | + |
| 75 | + // set the provided HTTP response parameters |
| 76 | + httpServletResponse.setHeader("Cache-Control", privacy + ", max-age=" + seconds + ", must-revalidate"); |
| 77 | + httpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + seconds * 1000L); |
| 78 | + } |
| 79 | + |
| 80 | + // pass the request/response on |
| 81 | + filterChain.doFilter(servletRequest, servletResponse); |
| 82 | + } |
| 83 | +} |
0 commit comments