View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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      @Override
71      public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
72      {
73          target=_replacement;
74          String query=_query;
75          for (int g=1;g<=matcher.groupCount();g++)
76          {
77              String group=matcher.group(g);
78              if (group==null)
79                  group="";
80              else
81                  group = Matcher.quoteReplacement(group);
82              target=target.replaceAll("\\$"+g,group);
83              if (query!=null)
84                  query=query.replaceAll("\\$"+g,group);
85          }
86  
87          if (query!=null)
88          {
89              if (_queryGroup)
90                  query=query.replace("$Q",request.getQueryString()==null?"":request.getQueryString());
91              request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q",query);
92          }
93          
94          return target;
95      }
96  
97      /* ------------------------------------------------------------ */
98      @Override
99      public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
100     {
101         if (_query==null)
102         {
103             request.setRequestURI(newTarget);
104         }
105         else
106         {
107             String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
108             
109             if (!_queryGroup && request.getQueryString()!=null)
110                 query=request.getQueryString()+"&"+query;
111             HttpURI uri=new HttpURI(newTarget+"?"+query);
112             request.setUri(uri);
113             request.setRequestURI(newTarget);
114             request.setQueryString(query);
115         }
116     }
117 
118     /* ------------------------------------------------------------ */
119     /**
120      * Returns the replacement string.
121      */
122     @Override
123     public String toString()
124     {
125         return super.toString()+"["+_replacement+"]";
126     }
127 }