1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13 package org.eclipse.jetty.rewrite.handler;
14
15 import java.io.IOException;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22
23 /**
24 * Abstract rule to use as a base class for rules that match with a regular expression.
25 */
26 public abstract class RegexRule extends Rule
27 {
28 protected Pattern _regex;
29
30 /* ------------------------------------------------------------ */
31 /**
32 * Sets the regular expression string used to match with string URI.
33 *
34 * @param regex the regular expression.
35 */
36 public void setRegex(String regex)
37 {
38 _regex=Pattern.compile(regex);
39 }
40
41 /* ------------------------------------------------------------ */
42 /**
43 * @return get the regular expression
44 */
45 public String getRegex()
46 {
47 return _regex==null?null:_regex.pattern();
48 }
49
50
51 /* ------------------------------------------------------------ */
52 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
53 {
54 Matcher matcher=_regex.matcher(target);
55 boolean matches = matcher.matches();
56 if (matches)
57 return apply(target,request,response, matcher);
58 return null;
59 }
60
61 /* ------------------------------------------------------------ */
62 /**
63 * Apply this rule to the request/response pair.
64 * Called by {@link #matchAndApply(String, HttpServletRequest, HttpServletResponse)} if the regex matches.
65 * @param target field to attempt match
66 * @param request request object
67 * @param response response object
68 * @param matcher The Regex matcher that matched the request (with capture groups available for replacement).
69 * @return The target (possible updated).
70 * @throws IOException exceptions dealing with operating on request or response objects
71 */
72 protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException;
73
74
75 /* ------------------------------------------------------------ */
76 /**
77 * Returns the regular expression string.
78 */
79 public String toString()
80 {
81 return super.toString()+"["+_regex+"]";
82 }
83 }