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.http.HttpURI;
22  import org.eclipse.jetty.server.Request;
23  
24  /**
25   * Rewrite the URI by matching with a regular expression. 
26   * The replacement string may use $n" to replace the nth capture group.
27   * If the replacement string contains ? character, then it is split into a path
28   * and query string component.  The replacement query string may also contain $Q, which 
29   * is replaced with the original query string. 
30   * The returned target contains only the path.
31   */
32  public class RewriteRegexRule extends RegexRule  implements Rule.ApplyURI
33  {
34      private String _replacement;
35      private String _query;
36      private boolean _queryGroup;
37  
38      /* ------------------------------------------------------------ */
39      public RewriteRegexRule()
40      {
41          _handling = false;
42          _terminating = false;
43      }
44  
45      /* ------------------------------------------------------------ */
46      /**
47       * Whenever a match is found, it replaces with this value.
48       * 
49       * @param replacement the replacement string.
50       */
51      public void setReplacement(String replacement)
52      {
53          String[] split=replacement.split("\\?",2);
54          _replacement = split[0];
55          _query=split.length==2?split[1]:null;
56          _queryGroup=_query!=null && _query.contains("$Q");
57      }
58  
59  
60      /* ------------------------------------------------------------ */
61      /* (non-Javadoc)
62       * @see org.eclipse.jetty.server.handler.rules.RegexRule#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.regex.Matcher)
63       */
64      public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
65      {
66          target=_replacement;
67          String query=_query;
68          for (int g=1;g<=matcher.groupCount();g++)
69          {
70              String group=matcher.group(g);
71              if (group==null)
72                  group="";
73              else
74                  group = Matcher.quoteReplacement(group);
75              target=target.replaceAll("\\$"+g,group);
76              if (query!=null)
77                  query=query.replaceAll("\\$"+g,group);
78          }
79  
80          if (query!=null)
81          {
82              if (_queryGroup)
83                  query=query.replace("$Q",request.getQueryString()==null?"":request.getQueryString());
84              request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q",query);
85          }
86          
87          return target;
88      }
89  
90      /* ------------------------------------------------------------ */
91      public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
92      {
93          if (_query==null)
94          {
95              request.setRequestURI(newTarget);
96          }
97          else
98          {
99              String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
100             
101             if (!_queryGroup && request.getQueryString()!=null)
102                 query=request.getQueryString()+"&"+query;
103             HttpURI uri=new HttpURI(newTarget+"?"+query);
104             request.setUri(uri);
105             request.setRequestURI(newTarget);
106             request.setQueryString(query);
107         }
108     }
109 
110     /* ------------------------------------------------------------ */
111     /**
112      * Returns the replacement string.
113      */
114     public String toString()
115     {
116         return super.toString()+"["+_replacement+"]";
117     }
118 }