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 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 20 import org.eclipse.jetty.http.PathMap; 21 22 /** 23 * Abstract rule that use a {@link PathMap} for pattern matching. It uses the 24 * servlet pattern syntax. 25 */ 26 public abstract class PatternRule extends Rule 27 { 28 protected String _pattern; 29 30 31 /* ------------------------------------------------------------ */ 32 public String getPattern() 33 { 34 return _pattern; 35 } 36 37 /* ------------------------------------------------------------ */ 38 /** 39 * Sets the rule pattern. 40 * 41 * @param pattern the pattern 42 */ 43 public void setPattern(String pattern) 44 { 45 _pattern = pattern; 46 } 47 48 /* ------------------------------------------------------------ */ 49 /* (non-Javadoc) 50 * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#matchAndApply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 51 */ 52 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException 53 { 54 if (PathMap.match(_pattern, target)) 55 { 56 return apply(target,request, response); 57 } 58 return null; 59 } 60 61 /* ------------------------------------------------------------ */ 62 /** Apply the rule to the request 63 * @param target field to attempt match 64 * @param request request object 65 * @param response response object 66 * @return The target (possible updated) 67 * @throws IOException exceptions dealing with operating on request or response objects 68 */ 69 protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException; 70 71 /** 72 * Returns the rule pattern. 73 */ 74 public String toString() 75 { 76 return super.toString()+"["+_pattern+"]"; 77 } 78 }