View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.rewrite.handler;
20  
21  import java.io.IOException;
22  import java.util.regex.Matcher;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.http.HttpURI;
28  import org.eclipse.jetty.server.Request;
29  
30  /**
31   * Rewrite the URI by matching with a regular expression. 
32   * The replacement string may use $n" to replace the nth capture group.
33   * If the replacement string contains ? character, then it is split into a path
34   * and query string component.  The replacement query string may also contain $Q, which 
35   * is replaced with the original query string. 
36   * The returned target contains only the path.
37   */
38  public class RewriteRegexRule extends RegexRule  implements Rule.ApplyURI
39  {
40      private String _replacement;
41      private String _query;
42      private boolean _queryGroup;
43  
44      /* ------------------------------------------------------------ */
45      public RewriteRegexRule()
46      {
47          _handling = false;
48          _terminating = false;
49      }
50  
51      /* ------------------------------------------------------------ */
52      /**
53       * Whenever a match is found, it replaces with this value.
54       * 
55       * @param replacement the replacement string.
56       */
57      public void setReplacement(String replacement)
58      {
59          String[] split=replacement.split("\\?",2);
60          _replacement = split[0];
61          _query=split.length==2?split[1]:null;
62          _queryGroup=_query!=null && _query.contains("$Q");
63      }
64  
65  
66      /* ------------------------------------------------------------ */
67      /* (non-Javadoc)
68       * @see org.eclipse.jetty.server.handler.rules.RegexRule#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.regex.Matcher)
69       */
70      public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
71      {
72          target=_replacement;
73          String query=_query;
74          for (int g=1;g<=matcher.groupCount();g++)
75          {
76              String group=matcher.group(g);
77              if (group==null)
78                  group="";
79              else
80                  group = Matcher.quoteReplacement(group);
81              target=target.replaceAll("\\$"+g,group);
82              if (query!=null)
83                  query=query.replaceAll("\\$"+g,group);
84          }
85  
86          if (query!=null)
87          {
88              if (_queryGroup)
89                  query=query.replace("$Q",request.getQueryString()==null?"":request.getQueryString());
90              request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q",query);
91          }
92          
93          return target;
94      }
95  
96      /* ------------------------------------------------------------ */
97      public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
98      {
99          if (_query==null)
100         {
101             request.setRequestURI(newTarget);
102         }
103         else
104         {
105             String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
106             
107             if (!_queryGroup && request.getQueryString()!=null)
108                 query=request.getQueryString()+"&"+query;
109             HttpURI uri=new HttpURI(newTarget+"?"+query);
110             request.setUri(uri);
111             request.setRequestURI(newTarget);
112             request.setQueryString(query);
113         }
114     }
115 
116     /* ------------------------------------------------------------ */
117     /**
118      * Returns the replacement string.
119      */
120     public String toString()
121     {
122         return super.toString()+"["+_replacement+"]";
123     }
124 }