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.HttpURI;
27  import org.eclipse.jetty.http.PathMap;
28  import org.eclipse.jetty.server.Request;
29  import org.eclipse.jetty.util.URIUtil;
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          _handling = false;
43          _terminating = false;
44      }
45  
46      /* ------------------------------------------------------------ */
47      /**
48       * Whenever a match is found, it replaces with this value.
49       *
50       * @param replacement the replacement string.
51       */
52      public void setReplacement(String replacement)
53      {
54          String[] split = replacement.split("\\?", 2);
55          _replacement = split[0];
56          _query = split.length == 2 ? split[1] : null;
57      }
58  
59      /* ------------------------------------------------------------ */
60      /*
61       * (non-Javadoc)
62       *
63       * @see org.eclipse.jetty.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest,
64       * javax.servlet.http.HttpServletResponse)
65       */
66      @Override
67      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
68      {
69          target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern, target));
70          return target;
71      }
72  
73      /* ------------------------------------------------------------ */
74      /**
75       * This method will add _query to the requests's queryString and also combine it with existing queryStrings in
76       * the request. However it won't take care for duplicate. E.g. if request.getQueryString contains a parameter
77       * "param1 = true" and _query will contain "param1=false" the result will be param1=true&param1=false.
78       * To cover this use case some more complex pattern matching is necessary. We can implement this if there's use
79       * cases.
80       *
81       * @param request
82       * @param oldURI
83       * @param newURI
84       * @throws IOException
85       */
86      @Override
87      public void applyURI(Request request, String oldURI, String newURI) throws IOException
88      {
89          if (_query == null)
90          {
91              request.setRequestURI(newURI);
92          }
93          else
94          {
95              String queryString = request.getQueryString();
96              if (queryString != null)
97                  queryString = queryString + "&" + _query;
98              else
99                  queryString = _query;
100             HttpURI uri = new HttpURI(newURI + "?" + queryString);
101             request.setUri(uri);
102             request.setRequestURI(newURI);
103             request.setQueryString(queryString);
104         }
105     }
106 
107     /* ------------------------------------------------------------ */
108     /**
109      * Returns the replacement string.
110      */
111     @Override
112     public String toString()
113     {
114         return super.toString()+"["+_replacement+"]";
115     }
116 }