View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.server.Request;
28  
29  /**
30   * Rewrite the URI by matching with a regular expression. 
31   * The replacement string may use $n" to replace the nth capture group.
32   * If the replacement string contains ? character, then it is split into a path
33   * and query string component.  The replacement query string may also contain $Q, which 
34   * is replaced with the original query string. 
35   * The returned target contains only the path.
36   */
37  public class RewriteRegexRule extends RegexRule  implements Rule.ApplyURI
38  {
39      private String _replacement;
40      private String _query;
41      private boolean _queryGroup;
42  
43      /* ------------------------------------------------------------ */
44      public RewriteRegexRule()
45      {
46          _handling = false;
47          _terminating = false;
48      }
49  
50      /* ------------------------------------------------------------ */
51      /**
52       * Whenever a match is found, it replaces with this value.
53       * 
54       * @param replacement the replacement string.
55       */
56      public void setReplacement(String replacement)
57      {
58          String[] split=replacement.split("\\?",2);
59          _replacement = split[0];
60          _query=split.length==2?split[1]:null;
61          _queryGroup=_query!=null && _query.contains("$Q");
62      }
63  
64  
65      /* ------------------------------------------------------------ */
66      /* (non-Javadoc)
67       * @see org.eclipse.jetty.server.handler.rules.RegexRule#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.regex.Matcher)
68       */
69      @Override
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      @Override
98      public void applyURI(Request request, String oldURI, String newURI) throws IOException
99      {
100         if (_query==null)
101         {
102             request.setURIPathQuery(newURI);
103         }
104         else
105         {
106             String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
107             
108             if (!_queryGroup && request.getQueryString()!=null)
109                 query=request.getQueryString()+"&"+query;
110             request.setURIPathQuery(newURI);
111             request.setQueryString(query);
112         }
113     }
114 
115     /* ------------------------------------------------------------ */
116     /**
117      * Returns the replacement string.
118      */
119     @Override
120     public String toString()
121     {
122         return super.toString()+"["+_replacement+"]";
123     }
124 }