View Javadoc

    1   //========================================================================
    2   //$Id: ResponsePatternRule.java 966 2008-04-17 13:53:44Z gregw $
    3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15  package org.mortbay.jetty.handler.rewrite;
    16
    17  import javax.servlet.http.HttpServletRequest;
    18  import javax.servlet.http.HttpServletResponse;
    19  import java.io.IOException;
    20
    21  /**
    22   * Sends the response code whenever the rule finds a match.
    23   */
    24  public class ResponsePatternRule extends PatternRule
    25  {
    26      private String _code;
    27      private String _reason = "";
    28
    29      /* ------------------------------------------------------------ */
    30      public ResponsePatternRule()
    31      {
    32          _handling = true;
    33          _terminating = true;
    34      }
    35
    36      /* ------------------------------------------------------------ */
    37      /**
    38       * Sets the response status code.
    39       * @param code response code
    40       */
    41      public void setCode(String code)
    42      {
    43          _code = code;
    44      }
    45
    46      /* ------------------------------------------------------------ */
    47      /**
    48       * Sets the reason for the response status code. Reasons will only reflect
    49       * if the code value is greater or equal to 400.
    50       *
    51       * @param reason
    52       */
    53      public void setReason(String reason)
    54      {
    55          _reason = reason;
    56      }
    57
    58      /* ------------------------------------------------------------ */
    59      /*
    60       * (non-Javadoc)
    61       * @see org.mortbay.jetty.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    62       */
    63      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    64      {
    65          int code = Integer.parseInt(_code);
    66
    67          // status code 400 and up are error codes
    68          if (code >= 400)
    69          {
    70              response.sendError(code, _reason);
    71          }
    72          else
    73          {
    74              response.setStatus(code);
    75          }
    76          return target;
    77      }
    78
    79      /* ------------------------------------------------------------ */
    80      /**
    81       * Returns the code and reason string.
    82       */
    83      public String toString()
    84      {
    85          return super.toString()+"["+_code+","+_reason+"]";
    86      }
    87  }