views:

56

answers:

1

What's the best way to set HTTP headers (based on filename patterns) in Jetty 6.1? Is it possible via jetty.xml (or jetty-web.xml)? Or do I have to modify web.xml?

A: 

The generic answer to my question is of course this:

<web-app>

  <filter>
    <filter-name>headersFilter</filter-name>
    <filter-class>com.example.MyHeadersFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>headersFilter</filter-name>
    <url-pattern>*</url-pattern>
  </filter-mapping>

  ...

</web-app>

public class MyHeadersFilter implements Filter {

    @Override
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
            throws IOException,
            ServletException {

        final HttpServletRequest httpRequest = (HttpServletRequest) request;
        final HttpServletResponse httpResponse = (HttpServletResponse) response;

        final String requestUri = httpRequest.getRequestURI();

        if (requestUri.matches(...)) {
            httpResponse.addHeader(...);
        }

        chain.doFilter(request, response);
    }
}

This should work in any JavaEE web container (and can be made more configurable with <init-param>s).

But isn't there a way to do this purely declaratively in Jetty?

Chris Lercher
Accepted until somebody finds out how to do it declaratively.
Chris Lercher