开发者

Setting HTTP headers with Jetty

开发者 https://www.devze.com 2023-01-03 02:39 出处:网络
What\'s the best way to set HTTP headers (based on filename patterns) in Jetty 6.1? Is it possible v开发者_如何学Cia jetty.xml (or jetty-web.xml)? Or do I have to modify web.xml?You can add headers by

What's the best way to set HTTP headers (based on filename patterns) in Jetty 6.1? Is it possible v开发者_如何学Cia jetty.xml (or jetty-web.xml)? Or do I have to modify web.xml?


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>


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消