3

Is it possible to configure embedded Jetty (v9) to set specific headers for specific resource file types only.

At the moment, I'm not doing anything special to handle static resources, so presumably Jetty has some default handler setup to do that. Is it possible to extend or overload that default handler with some custom setup so that I can set the Cache-Control header for html files only?

I'm trying to accomplish something analogous to the following bit of Apache config:

<Files "*.html">
  Header set Cache-Control "public, max-age=900"
</Files>

...in my Jetty setup:

public static void main(String[] args) throws Exception {
    Server server = new Server(443);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar("war");
    server.setHandler(webapp);
    ...
    ...
}

Actually, if this can be accomplished in jetty.xml or some other configuration file, that would be preferable.

RTF
  • 6,214
  • 12
  • 64
  • 132
  • I finally found the doc that I need, which seems to explain things well: http://www.eclipse.org/jetty/documentation/current/rewrite-handler.html ...I'll post an answer myself with a specific solution if I can get it working – RTF Mar 31 '16 at 17:25

2 Answers2

3

I was able to accomplish what I wanted using this configuration in jetty-env.xml for my webapp:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <Call name="insertHandler">
      <Arg>
        <New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
        <Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true"/></Set>
        <Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false"/></Set>
        <Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath"/></Set>

        <Call name="addRule">
          <Arg>
            <New class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
              <Set name="pattern">*.html</Set>
              <Set name="name">Cache-Control</Set>
              <Set name="value">Max-Age=900,public</Set>
              <Set name="terminating">true</Set>
            </New>
          </Arg>
        </Call>

      </New>
    </Arg>
  </Call>

</Configure>
RTF
  • 6,214
  • 12
  • 64
  • 132
3

Your solution helped me to get thatin a totaly embedded way which works fine.

I am using jetty 9.2.19 org.eclipse.jetty.rewrite.handler.RewriteHandler. I wrap my ResourceHandler in this RewriteHandler to set specific http headers (here Cache-Control):

RewriteHandler rewriteHandler = new RewriteHandler();
rewriteHandler.addRule(new Rule()
  {
  @Override
  public String matchAndApply(String target, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException
    {
    // you can check the target for its path or filetype or whatever
    if((target.endsWith(".woff2")) || ... || (target.endsWith(".jpg")))
      {
      httpServletResponse.setHeader("Cache-Control", "public, max-age=2592000");
      }
    return null;
    }
  });
// set the RewriteHandler around the ResourceHandler
rewriteHandler.setHandler(resource_handler);
// set the RewriteHandler to the Webservers Handlerlist...
Ruwen
  • 3,008
  • 1
  • 19
  • 16