Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you assign per web app jetty-rewrite.xml files in Jetty?

Is there a way per web app / context to specify a rewrite file just for that particular webapp? The only way I see this currently working is via the command line when you start it. I was thinking perhaps a setting in the override.xml file or even in the context xml file.

like image 455
AdiFatLady Avatar asked Nov 14 '22 15:11

AdiFatLady


1 Answers

If you use context.xml deployables you can integrate the RewriteHandler rules into the app specific context xml.

Example: this is a replacement context.xml of the ${jetty.home}/contexts/test.xml found in the jetty-distribution. It adds a rule that simply adds a cookie (visited=yes) on the response for all requests.

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="handler">
    <New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
      <Set name="handler">
        <New class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="contextPath">/</Set>
          <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>
          <Set name="extractWAR">true</Set>
          <Set name="copyWebDir">false</Set>
          <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
          <Set name="overrideDescriptor"><SystemProperty name="jetty.home" default="."/>/contexts/test.d/override-web.xml</Set>
        </New>
      </Set>
      <Set name="rewriteRequestURI">true</Set>
      <Set name="rewritePathInfo">false</Set>
      <Set name="originalPathAttribute">requestedPath</Set>

      <!-- add a cookie to each path visited -->
      <Call name="addRule">
        <Arg>
          <New class="org.eclipse.jetty.rewrite.handler.CookiePatternRule">
            <Set name="pattern">/*</Set>
            <Set name="name">visited</Set>
            <Set name="value">yes</Set>
          </New>
        </Arg>
      </Call>
     </New>
  </Set>
</Configure>

To verify that this rule works, start jetty, goto http://localhost:8080/ and then use the "Sessions" test component, you'll see that visited=true will be visible as a cookie at the top.

like image 175
Joakim Erdfelt Avatar answered Dec 06 '22 03:12

Joakim Erdfelt