View Javadoc

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