View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.rewrite.handler;
14  
15  import java.io.IOException;
16  import java.util.regex.Matcher;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jetty.server.Request;
22  
23  /**
24   * Rewrite the URI by matching with a regular expression. 
25   * The replacement string may use $n" to replace the nth capture group.
26   */
27  public class RewriteRegexRule extends RegexRule  implements Rule.ApplyURI
28  {
29      private String _replacement;
30  
31      /* ------------------------------------------------------------ */
32      public RewriteRegexRule()
33      {
34          _handling = false;
35          _terminating = false;
36      }
37  
38      /* ------------------------------------------------------------ */
39      /**
40       * Whenever a match is found, it replaces with this value.
41       * 
42       * @param replacement the replacement string.
43       */
44      public void setReplacement(String replacement)
45      {
46          _replacement = replacement;
47      }
48  
49  
50      /* ------------------------------------------------------------ */
51      /* (non-Javadoc)
52       * @see org.eclipse.jetty.server.handler.rules.RegexRule#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.regex.Matcher)
53       */
54      public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
55      {
56          target=_replacement;
57          for (int g=1;g<=matcher.groupCount();g++)
58          {
59              String group = Matcher.quoteReplacement(matcher.group(g));
60              target=target.replaceAll("\\$"+g,group);
61          }
62  
63          return target;
64      }
65  
66      /* ------------------------------------------------------------ */
67      public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
68      {
69          Matcher matcher=_regex.matcher(request.getRequestURI());
70          boolean matches = matcher.matches();
71          if (matches)
72          {
73              String uri=_replacement;
74              for (int g=1;g<=matcher.groupCount();g++)
75              {
76                  String group = Matcher.quoteReplacement(matcher.group(g));
77                  uri=uri.replaceAll("\\$"+g,group);
78              }
79              request.setRequestURI(uri);
80          }
81          else
82              request.setRequestURI(newTarget);
83      }
84  
85      /* ------------------------------------------------------------ */
86      /**
87       * Returns the replacement string.
88       */
89      public String toString()
90      {
91          return super.toString()+"["+_replacement+"]";
92      }
93  }