View Javadoc

1   //========================================================================
2   //$Id: RewritePatternRule.java 966 2008-04-17 13:53:44Z gregw $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  package org.mortbay.jetty.handler.rewrite;
16  
17  import java.io.IOException;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.mortbay.jetty.servlet.PathMap;
23  import org.mortbay.util.URIUtil;
24  
25  /**
26   * Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string. 
27   */
28  public class RewritePatternRule extends PatternRule
29  {
30      private String _replacement;
31  
32      /* ------------------------------------------------------------ */
33      public RewritePatternRule()
34      {
35          _handling = false;
36          _terminating = false;
37      }
38  
39      /* ------------------------------------------------------------ */
40      /**
41       * Whenever a match is found, it replaces with this value.
42       * 
43       * @param value the replacement string.
44       */
45      public void setReplacement(String value)
46      {
47          _replacement = value;
48      }
49  
50      /* ------------------------------------------------------------ */
51      /*
52       * (non-Javadoc)
53       * @see org.mortbay.jetty.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
54       */
55      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
56      {
57          target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,target));   
58          return target;
59      }
60  
61      /* ------------------------------------------------------------ */
62      /**
63       * Returns the replacement string.
64       */
65      public String toString()
66      {
67          return super.toString()+"["+_replacement+"]";
68      }
69  }