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  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.eclipse.jetty.http.PathMap;
27  import org.eclipse.jetty.server.Request;
28  import org.eclipse.jetty.util.URIUtil;
29  import org.eclipse.jetty.util.annotation.Name;
30  
31  /**
32   * Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string.
33   */
34  public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
35  {
36      private String _replacement;
37      private String _query;
38  
39      /* ------------------------------------------------------------ */
40      public RewritePatternRule()
41      {
42          this(null,null);
43      }
44      
45      /* ------------------------------------------------------------ */
46      public RewritePatternRule(@Name("pattern") String pattern, @Name("replacement") String replacement)
47      {
48          super(pattern);
49          _handling = false;
50          _terminating = false;
51          setReplacement(replacement);
52      }
53      
54      
55      /* ------------------------------------------------------------ */
56      /**
57       * Whenever a match is found, it replaces with this value.
58       *
59       * @param replacement the replacement string.
60       */
61      public void setReplacement(String replacement)
62      {
63          if (replacement==null)
64          {
65              _replacement=null;
66              _query=null;
67          }
68          else
69          {
70              String[] split = replacement.split("\\?", 2);
71              _replacement = split[0];
72              _query = split.length == 2 ? split[1] : null;
73          }
74      }
75  
76      /* ------------------------------------------------------------ */
77      @Override
78      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
79      {
80          target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern, target));
81          return target;
82      }
83  
84      /* ------------------------------------------------------------ */
85      /**
86       * This method will add _query to the requests's queryString and also combine it with existing queryStrings in
87       * the request. However it won't take care for duplicate. E.g. if request.getQueryString contains a parameter
88       * <code>param1 = true</code> and _query will contain <code>param1=false</code> the result will be <code>param1=true&amp;param1=false</code>.
89       * To cover this use case some more complex pattern matching is necessary. We can implement this if there's use
90       * cases.
91       *
92       * @param request the request
93       * @param oldURI the old URI
94       * @param newURI the new URI
95       * @throws IOException if unable to apply the URI
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 queryString = request.getQueryString();
107             if (queryString != null)
108                 queryString = queryString + "&" + _query;
109             else
110                 queryString = _query;
111             request.setURIPathQuery(newURI);
112             request.setQueryString(queryString);
113         }
114     }
115 
116     /* ------------------------------------------------------------ */
117     /**
118      * Returns the replacement string.
119      */
120     @Override
121     public String toString()
122     {
123         return super.toString()+"["+_replacement+"]";
124     }
125 }