View Javadoc

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          _handling = true;
46          _terminating = true;
47          setCode(code);
48          setReason(reason);
49      }
50  
51      /* ------------------------------------------------------------ */
52      /**
53       * Sets the response status code. 
54       * @param code response code
55       */
56      public void setCode(String code)
57      {
58          _code = code;
59      }
60  
61      /* ------------------------------------------------------------ */
62      /**
63       * Sets the reason for the response status code. Reasons will only reflect
64       * if the code value is greater or equal to 400.
65       * 
66       * @param reason the reason
67       */
68      public void setReason(String reason)
69      {
70          _reason = reason;
71      }
72  
73      /* ------------------------------------------------------------ */
74      /*
75       * (non-Javadoc)
76       * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
77       */
78      @Override
79      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
80      {
81          int code = Integer.parseInt(_code);
82  
83          // status code 400 and up are error codes
84          if (code >= 400)
85          {
86              response.sendError(code, _reason);
87          }
88          else
89          {
90              response.setStatus(code);
91          }
92          return target;
93      }
94  
95      /* ------------------------------------------------------------ */
96      /**
97       * Returns the code and reason string.
98       */
99      @Override
100     public String toString()
101     {
102         return super.toString()+"["+_code+","+_reason+"]";
103     }
104 }