/*******************************************************************************
*
* Copyright (c) 2004-2009 Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* Kohsuke Kawaguchi, id:digerata
*
*
*******************************************************************************/
package hudson;
import java.util.*;
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.HttpServletResponse;
import java.io.IOException;
/**
* This filter allows you to modify headers set by the container or other
* servlets that are out of your control. The particular headers you wish to
* change are configured in web.xml. <p> One particular header you you may wish
* to deal with is "Cache-Control: no-cache" This is a problem with Tomcat when
* security is used. Continue reading for further details. <p> If a web app has
* a <security-constraint> in its web.xml, Tomcat will add a Cache-Control
* header to every file it serves from that location. This header will prevent
* browsers from caching the file locally and this drastically slows down Hudson
* page load times. <p> To enable this filter, edit the web.xml file to include:
*
* <pre>
* <filter>
* <filter-name>change-headers-filter</filter-name>
* <filter-class>hudson.ResponseHeaderFilter</filter-class>
* <init-param>
* <param-name>Pragma</param-name>
* <param-value>public</param-value>
* </init-param>
* <init-param>
* <param-name>Cache-Control</param-name>
* <param-value>max-age=86400, public</param-value>
* </init-param>
* </filter>
*
* And down below that:
*
* <filter-mapping>
* <filter-name>Headers</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
* </pre>
*
* <p> In the case of the tomcat cache problem, it is important that the
* url-pattern for the filter matches the url-pattern set for the
* security-constraint.
*
* @author Mike Wille
*/
public class ResponseHeaderFilter implements Filter {
private FilterConfig config;
public void init(FilterConfig filterConfig) throws ServletException {
config = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
ServletException {
HttpServletResponse httpResp = (HttpServletResponse) resp;
Enumeration e = config.getInitParameterNames();
// for each configuration element...
while (e.hasMoreElements()) {
String headerName = (String) e.nextElement();
String headerValue = config.getInitParameter(headerName);
// set the header with the given name and value
httpResp.setHeader(headerName, headerValue);
}
chain.doFilter(req, resp);
}
public void destroy() {
}
}