Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting HTTP headers with Jetty

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?

like image 844
Chris Lercher Avatar asked Jun 09 '10 20:06

Chris Lercher


2 Answers

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>
like image 157
Jérôme Radix Avatar answered Nov 13 '22 14:11

Jérôme Radix


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?

like image 9
Chris Lercher Avatar answered Nov 13 '22 13:11

Chris Lercher