configurationjetty

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?


Solution

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