View Javadoc

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