Package hudson

Class ResponseHeaderFilter

java.lang.Object
hudson.ResponseHeaderFilter
All Implemented Interfaces:
jakarta.servlet.Filter, org.kohsuke.stapler.CompatibleFilter

public class ResponseHeaderFilter extends Object implements org.kohsuke.stapler.CompatibleFilter
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.

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.

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.

To enable this filter, edit the web.xml file to include:

 <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>
 

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
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    doFilter(jakarta.servlet.ServletRequest req, jakarta.servlet.ServletResponse resp, jakarta.servlet.FilterChain chain)
     
    void
    init(jakarta.servlet.FilterConfig filterConfig)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.kohsuke.stapler.CompatibleFilter

    doFilter, init
  • Constructor Details

    • ResponseHeaderFilter

      public ResponseHeaderFilter()
  • Method Details

    • init

      public void init(jakarta.servlet.FilterConfig filterConfig) throws jakarta.servlet.ServletException
      Specified by:
      init in interface jakarta.servlet.Filter
      Throws:
      jakarta.servlet.ServletException
    • doFilter

      public void doFilter(jakarta.servlet.ServletRequest req, jakarta.servlet.ServletResponse resp, jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException
      Specified by:
      doFilter in interface jakarta.servlet.Filter
      Throws:
      IOException
      jakarta.servlet.ServletException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface jakarta.servlet.Filter