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.util.URIUtil;
22  
23  /**
24   * Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string. 
25   */
26  public class RewritePatternRule extends PatternRule
27  {
28      private String _replacement;
29  
30      /* ------------------------------------------------------------ */
31      public RewritePatternRule()
32      {
33          _handling = false;
34          _terminating = false;
35      }
36  
37      /* ------------------------------------------------------------ */
38      /**
39       * Whenever a match is found, it replaces with this value.
40       * 
41       * @param value the replacement string.
42       */
43      public void setReplacement(String value)
44      {
45          _replacement = value;
46      }
47  
48      /* ------------------------------------------------------------ */
49      /*
50       * (non-Javadoc)
51       * @see org.eclipse.jetty.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
52       */
53      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
54      {
55          target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,target));   
56          return target;
57      }
58  
59      /* ------------------------------------------------------------ */
60      /**
61       * Returns the replacement string.
62       */
63      public String toString()
64      {
65          return super.toString()+"["+_replacement+"]";
66      }
67  }