Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure embedded Jetty (v9) to set specific headers for specific resource files?

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.

like image 570
RTF Avatar asked Sep 19 '25 09:09

RTF


1 Answers

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>
like image 167
RTF Avatar answered Sep 21 '25 01:09

RTF