View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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      @Override
59      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
60      {
61          if (PathMap.match(_pattern, target))
62          {
63              return apply(target,request, response);
64          }
65          return null;
66      }
67  
68      /* ------------------------------------------------------------ */
69      /** Apply the rule to the request
70       * @param target field to attempt match
71       * @param request request object
72       * @param response response object
73       * @return The target (possible updated)
74       * @throws IOException exceptions dealing with operating on request or response objects  
75       */
76      protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException;
77  
78      /**
79       * Returns the rule pattern.
80       */
81      @Override
82      public String toString()
83      {
84          return super.toString()+"["+_pattern+"]";                
85      }
86  }