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