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 @Override 59 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException 60 { 61 Matcher matcher=_regex.matcher(target); 62 boolean matches = matcher.matches(); 63 if (matches) 64 return apply(target,request,response, matcher); 65 return null; 66 } 67 68 /* ------------------------------------------------------------ */ 69 /** 70 * Apply this rule to the request/response pair. 71 * Called by {@link #matchAndApply(String, HttpServletRequest, HttpServletResponse)} if the regex matches. 72 * @param target field to attempt match 73 * @param request request object 74 * @param response response object 75 * @param matcher The Regex matcher that matched the request (with capture groups available for replacement). 76 * @return The target (possible updated). 77 * @throws IOException exceptions dealing with operating on request or response objects 78 */ 79 protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException; 80 81 82 /* ------------------------------------------------------------ */ 83 /** 84 * Returns the regular expression string. 85 */ 86 @Override 87 public String toString() 88 { 89 return super.toString()+"["+_regex+"]"; 90 } 91 }