12

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?

4 Answers 4

13

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>
Sign up to request clarification or add additional context in comments.

1 Comment

The links are dead
10

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?

1 Comment

Accepted until somebody finds out how to do it declaratively.
0

There is a way to adding header in jetty. using java.lang.instrument and javassist. refer following link for details:

http://hunmr.blogspot.com/2012/12/mock-jetty-behavior-adding-customized.html

Comments

0

Alternate programmatic method:

HeaderPatternRule rule = new HeaderPatternRule("*.html", "myheader", "the value of myheader");
rule.setAdd(true);
rewriteHandler.addRule(rule);

I happened to have a rewriteHandler in my code already, and I recognized it in Jérôme's jetty.xml answer.

If you don't have rewriteHandler, here's how my code uses it (not everything may apply to everyone):

server = new Server(port);

ServletContextHandler webAppContext  = makeHttpHandler();
Handler               rewriteHandler = wrapInRewriteHandler(webAppContext);
server.setHandler(rewriteHandler);

private static ServletContextHandler makeHttpHandler() {
    ServletContextHandler webAppContext = new WebAppContext();
    webAppContext.setContextPath("/");
    webAppContext.setWelcomeFiles(new String[]{"index.html"});
    webAppContext.setResourceBase(PROGRAM_DIRECTORY + "/web/");

    return webAppContext;
}

private static Handler wrapInRewriteHandler(ServletContextHandler webAppContext) {
    RewriteHandler rewriteHandler = new ToOriginRewriteHandler(webAppContext);
    rewriteHandler.setRewriteRequestURI(true);
    rewriteHandler.setRewritePathInfo(false);
    rewriteHandler.setOriginalPathAttribute("requestedPath");

    rewriteHandler.addRule(new RewriteRegexRule("/.*", "/index.html"));
    HeaderPatternRule rule = new HeaderPatternRule("*.html", "myheader", "the value of myheader");
    rule.setAdd(true);
    rewriteHandler.addRule(rule);

    rewriteHandler.setHandler(webAppContext);
    return rewriteHandler;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.