View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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      protected RegexRule()
38      {
39      }
40      
41      /* ------------------------------------------------------------ */
42      protected RegexRule(String pattern)
43      {
44          setRegex(pattern);
45      }
46      
47      /* ------------------------------------------------------------ */
48      /**
49       * Sets the regular expression string used to match with string URI.
50       * 
51       * @param regex the regular expression.
52       */
53      public void setRegex(String regex)
54      {
55          _regex=regex==null?null:Pattern.compile(regex);
56      }
57  
58      /* ------------------------------------------------------------ */
59      /**
60       * @return get the regular expression
61       */
62      public String getRegex()
63      {
64          return _regex==null?null:_regex.pattern();
65      }
66      
67  
68      /* ------------------------------------------------------------ */
69      @Override
70      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
71      {
72          Matcher matcher=_regex.matcher(target);
73          boolean matches = matcher.matches();
74          if (matches)
75              return apply(target,request,response, matcher);
76          return null;
77      }
78  
79      /* ------------------------------------------------------------ */
80      /** 
81       * Apply this rule to the request/response pair.
82       * Called by {@link #matchAndApply(String, HttpServletRequest, HttpServletResponse)} if the regex matches.
83       * @param target field to attempt match
84       * @param request request object
85       * @param response response object
86       * @param matcher The Regex matcher that matched the request (with capture groups available for replacement).
87       * @return The target (possible updated).
88       * @throws IOException exceptions dealing with operating on request or response objects
89       */
90      protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException;
91      
92  
93      /* ------------------------------------------------------------ */
94      /**
95       * Returns the regular expression string.
96       */
97      @Override
98      public String toString()
99      {
100         return super.toString()+"["+_regex+"]";
101     }
102 }