View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.rewrite.handler;
14  
15  import java.io.IOException;
16  
17  import javax.servlet.http.HttpServletRequest;
18  import javax.servlet.http.HttpServletResponse;
19  
20  import org.eclipse.jetty.http.PathMap;
21  import org.eclipse.jetty.server.Request;
22  import org.eclipse.jetty.util.URIUtil;
23  
24  /**
25   * Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string. 
26   */
27  public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
28  {
29      private String _replacement;
30  
31      /* ------------------------------------------------------------ */
32      public RewritePatternRule()
33      {
34          _handling = false;
35          _terminating = false;
36      }
37  
38      /* ------------------------------------------------------------ */
39      /**
40       * Whenever a match is found, it replaces with this value.
41       * 
42       * @param value the replacement string.
43       */
44      public void setReplacement(String value)
45      {
46          _replacement = value;
47      }
48  
49      /* ------------------------------------------------------------ */
50      /*
51       * (non-Javadoc)
52       * @see org.eclipse.jetty.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
53       */
54      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
55      {
56          target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,target));   
57          return target;
58      }
59  
60      /* ------------------------------------------------------------ */
61      public void applyURI(Request request, String oldTarget, String newTarget) throws IOException 
62      {
63          String uri = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,request.getRequestURI()));
64          request.setRequestURI(uri);
65      }
66  
67      /* ------------------------------------------------------------ */
68      /**
69       * Returns the replacement string.
70       */
71      public String toString()
72      {
73          return super.toString()+"["+_replacement+"]";
74      }
75  }