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 /** 21 * Redirects the response whenever the rule finds a match. 22 */ 23 public class RedirectPatternRule extends PatternRule 24 { 25 private String _location; 26 27 /* ------------------------------------------------------------ */ 28 public RedirectPatternRule() 29 { 30 _handling = true; 31 _terminating = true; 32 } 33 34 /* ------------------------------------------------------------ */ 35 /** 36 * Sets the redirect location. 37 * 38 * @param value the location to redirect. 39 */ 40 public void setLocation(String value) 41 { 42 _location = value; 43 } 44 45 /* ------------------------------------------------------------ */ 46 /* 47 * (non-Javadoc) 48 * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 49 */ 50 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException 51 { 52 response.sendRedirect(response.encodeRedirectURL(_location)); 53 return target; 54 } 55 56 /* ------------------------------------------------------------ */ 57 /** 58 * Returns the redirect location. 59 */ 60 public String toString() 61 { 62 return super.toString()+"["+_location+"]"; 63 } 64 }