View Javadoc

1   //========================================================================
2   //$Id: PatternRule.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  
24  /**
25   * Abstract rule that use a {@link PathMap} for pattern matching. It uses the 
26   * servlet pattern syntax.
27   */
28  public abstract class PatternRule extends Rule
29  {
30      protected String _pattern;
31  
32  
33      /* ------------------------------------------------------------ */
34      public String getPattern()
35      {
36          return _pattern;
37      }
38  
39      /* ------------------------------------------------------------ */
40      /**
41       * Sets the rule pattern.
42       * 
43       * @param pattern the pattern
44       */
45      public void setPattern(String pattern)
46      {
47          _pattern = pattern;
48      }
49  
50      /* ------------------------------------------------------------ */
51      /* (non-Javadoc)
52       * @see org.mortbay.jetty.handler.rules.RuleBase#matchAndApply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
53       */
54      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
55      {
56          if (PathMap.match(_pattern, target))
57          {
58              return apply(target,request, response);
59          }
60          return null;
61      }
62  
63      /* ------------------------------------------------------------ */
64      /** Apply the rule to the request
65       * @param target field to attempt match
66       * @param request request object
67       * @param response response object
68       * @return The target (possible updated)
69       * @throws IOException exceptions dealing with operating on request or response objects  
70       */
71      protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException;
72  
73      /**
74       * Returns the rule pattern.
75       */
76      public String toString()
77      {
78          return super.toString()+"["+_pattern+"]";                
79      }
80  }