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  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  
29  /**
30   * Abstract rule to use as a base class for rules that match with a regular expression.
31   */
32  public abstract class RegexRule extends Rule
33  {
34      protected Pattern _regex; 
35  
36      /* ------------------------------------------------------------ */
37      /**
38       * Sets the regular expression string used to match with string URI.
39       * 
40       * @param regex the regular expression.
41       */
42      public void setRegex(String regex)
43      {
44          _regex=Pattern.compile(regex);
45      }
46  
47      /* ------------------------------------------------------------ */
48      /**
49       * @return get the regular expression
50       */
51      public String getRegex()
52      {
53          return _regex==null?null:_regex.pattern();
54      }
55      
56  
57      /* ------------------------------------------------------------ */
58      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
59      {
60          Matcher matcher=_regex.matcher(target);
61          boolean matches = matcher.matches();
62          if (matches)
63              return apply(target,request,response, matcher);
64          return null;
65      }
66  
67      /* ------------------------------------------------------------ */
68      /** 
69       * Apply this rule to the request/response pair.
70       * Called by {@link #matchAndApply(String, HttpServletRequest, HttpServletResponse)} if the regex matches.
71       * @param target field to attempt match
72       * @param request request object
73       * @param response response object
74       * @param matcher The Regex matcher that matched the request (with capture groups available for replacement).
75       * @return The target (possible updated).
76       * @throws IOException exceptions dealing with operating on request or response objects
77       */
78      protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException;
79      
80  
81      /* ------------------------------------------------------------ */
82      /**
83       * Returns the regular expression string.
84       */
85      public String toString()
86      {
87          return super.toString()+"["+_regex+"]";
88      }
89  }