[1240] | 1 | package gov.va.med.edp.web.servlet.filter;
|
---|
| 2 |
|
---|
| 3 | import javax.servlet.*;
|
---|
| 4 | import javax.servlet.http.HttpServletResponse;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.util.Enumeration;
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Servlet filter that sets HTTP Response Headers based on initialization parameters.
|
---|
| 10 | * <p/>
|
---|
| 11 | * Example usage in web.xml of servlet application:
|
---|
| 12 | * <code>
|
---|
| 13 | * <filter>
|
---|
| 14 | * <filter-name>responseHeaderFilter</filter-name>
|
---|
| 15 | * <filter-class>gov.va.med.edp.web.servlet.filter.ResponseHeaderFilter</filter-class>
|
---|
| 16 | * <init-param>
|
---|
| 17 | * <param-name>Cache-Control</param-name>
|
---|
| 18 | * <param-value>max-age=3600</param-value>
|
---|
| 19 | * </init-param>
|
---|
| 20 | * </filter>
|
---|
| 21 | * ...
|
---|
| 22 | * <filter-mapping>
|
---|
| 23 | * <filter-name>responseHeaderFilter</filter-name>
|
---|
| 24 | * <url-pattern>/logo.png</url-pattern>
|
---|
| 25 | * </filter-mapping>
|
---|
| 26 | * </code>
|
---|
| 27 | */
|
---|
| 28 | public class ResponseHeaderFilter implements Filter {
|
---|
| 29 |
|
---|
| 30 | FilterConfig filterConfig;
|
---|
| 31 |
|
---|
| 32 | public void init(FilterConfig filterConfig) throws ServletException {
|
---|
| 33 | this.filterConfig = filterConfig;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
|
---|
| 37 | HttpServletResponse response = (HttpServletResponse) res;
|
---|
| 38 | // set the provided HTTP response parameters
|
---|
| 39 | for (Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements();) {
|
---|
| 40 | String headerName = (String) e.nextElement();
|
---|
| 41 | response.addHeader(headerName, filterConfig.getInitParameter(headerName));
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | filterChain.doFilter(req, response);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public void destroy() {
|
---|
| 48 | this.filterConfig = null;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|