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 * Sends the response code whenever the rule finds a match. 22 */ 23 public class ResponsePatternRule extends PatternRule 24 { 25 private String _code; 26 private String _reason = ""; 27 28 /* ------------------------------------------------------------ */ 29 public ResponsePatternRule() 30 { 31 _handling = true; 32 _terminating = true; 33 } 34 35 /* ------------------------------------------------------------ */ 36 /** 37 * Sets the response status code. 38 * @param code response code 39 */ 40 public void setCode(String code) 41 { 42 _code = code; 43 } 44 45 /* ------------------------------------------------------------ */ 46 /** 47 * Sets the reason for the response status code. Reasons will only reflect 48 * if the code value is greater or equal to 400. 49 * 50 * @param reason 51 */ 52 public void setReason(String reason) 53 { 54 _reason = reason; 55 } 56 57 /* ------------------------------------------------------------ */ 58 /* 59 * (non-Javadoc) 60 * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 61 */ 62 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException 63 { 64 int code = Integer.parseInt(_code); 65 66 // status code 400 and up are error codes 67 if (code >= 400) 68 { 69 response.sendError(code, _reason); 70 } 71 else 72 { 73 response.setStatus(code); 74 } 75 return target; 76 } 77 78 /* ------------------------------------------------------------ */ 79 /** 80 * Returns the code and reason string. 81 */ 82 public String toString() 83 { 84 return super.toString()+"["+_code+","+_reason+"]"; 85 } 86 }