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  import org.eclipse.jetty.util.annotation.Name;
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          this(null,null);
48      }
49      
50      /* ------------------------------------------------------------ */
51      public RewriteRegexRule(@Name("regex") String regex, @Name("replacement") String replacement)
52      {
53          super(regex);
54          setHandling(false);
55          setTerminating(false);
56          setReplacement(replacement);
57      }
58  
59      /* ------------------------------------------------------------ */
60      /**
61       * Whenever a match is found, it replaces with this value.
62       * 
63       * @param replacement the replacement string.
64       */
65      public void setReplacement(String replacement)
66      {
67          if (replacement==null)
68          {
69              _replacement=null;
70              _query=null;
71              _queryGroup=false;
72          }
73          else
74          {
75              String[] split=replacement.split("\\?",2);
76              _replacement = split[0];
77              _query=split.length==2?split[1]:null;
78              _queryGroup=_query!=null && _query.contains("$Q");
79          }
80      }
81  
82  
83      /* ------------------------------------------------------------ */
84      /* (non-Javadoc)
85       * @see org.eclipse.jetty.server.handler.rules.RegexRule#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.regex.Matcher)
86       */
87      @Override
88      public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
89      {
90          target=_replacement;
91          String query=_query;
92          for (int g=1;g<=matcher.groupCount();g++)
93          {
94              String group=matcher.group(g);
95              if (group==null)
96                  group="";
97              else
98                  group = Matcher.quoteReplacement(group);
99              target=target.replaceAll("\\$"+g,group);
100             if (query!=null)
101                 query=query.replaceAll("\\$"+g,group);
102         }
103 
104         if (query!=null)
105         {
106             if (_queryGroup)
107                 query=query.replace("$Q",request.getQueryString()==null?"":request.getQueryString());
108             request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q",query);
109         }
110         
111         return target;
112     }
113 
114     /* ------------------------------------------------------------ */
115     @Override
116     public void applyURI(Request request, String oldURI, String newURI) throws IOException
117     {
118         if (_query==null)
119         {
120             request.setURIPathQuery(newURI);
121         }
122         else
123         {
124             String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
125             
126             if (!_queryGroup && request.getQueryString()!=null)
127                 query=request.getQueryString()+"&"+query;
128             request.setURIPathQuery(newURI);
129             request.setQueryString(query);
130         }
131     }
132 
133     /* ------------------------------------------------------------ */
134     /**
135      * Returns the replacement string.
136      */
137     @Override
138     public String toString()
139     {
140         return super.toString()+"["+_replacement+"]";
141     }
142 }