Setting HTTP headers with Jetty

25,509

Solution 1

You can add headers by configuration. In jetty.xml, put the following example :

<New id="RewriteHandler" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
  <Set name="rules">
    <Array type="org.eclipse.jetty.rewrite.handler.Rule">

      <Item>
        <New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
          <Set name="pattern">*.jsp</Set>
          <Set name="name">myheader</Set>
          <Set name="value">the value of myheader</Set>
        </New>
      </Item>

    </Array>
  </Set>
</New>

<Set name="handler">
  <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
    <Set name="handlers">
      <Array type="org.eclipse.jetty.server.Handler">
        <Item>
          <Ref id="RewriteHandler"/>
        </Item>
      </Array>
    </Set>
  </New>
</Set>

See RewriteHandler api for more examples of what is possible (it was already available in Jetty 6 RewriteHandler)

For information, here is my maven configuration of jetty plugin :

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>8.1.5.v20120716</version>
  <configuration>
    <jettyXml>${basedir}/src/main/etc/jetty.xml</jettyXml>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-http</artifactId>
      <version>8.1.5.v20120716</version>
      <type>jar</type>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-rewrite</artifactId>
      <version>8.1.5.v20120716</version>
      <type>jar</type>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</plugin>

Solution 2

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?

Share:
25,509
Chris Lercher
Author by

Chris Lercher

Real name: Christian Lercher Started programming when I was 7 years old (Basic on C64, then Assembler, later C/C++, and now mostly Java). First Windows experience: ca 1993 (Win 3.11) First Unix experience: 1998 (Solaris, then SuSE Linux, now mostly Debian/Ubuntu and macOS. I'm just as much interested in technical things, as in social topics, and economics. It really depends.

Updated on December 27, 2020

Comments

  • Chris Lercher
    Chris Lercher over 3 years

    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?

  • Chris Lercher
    Chris Lercher about 14 years
    Accepted until somebody finds out how to do it declaratively.
  • Rushil Paul
    Rushil Paul over 6 years
    The links are dead