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