View Javadoc

    1   //========================================================================
    2   //$Id: CookiePatternRule.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 java.io.IOException;
    18
    19  import javax.servlet.http.Cookie;
    20  import javax.servlet.http.HttpServletRequest;
    21  import javax.servlet.http.HttpServletResponse;
    22
    23
    24  /**
    25   * Sets the cookie in the response whenever the rule finds a match.
    26   *
    27   * @see Cookie
    28   */
    29  public class CookiePatternRule extends PatternRule
    30  {
    31      private String _name;
    32      private String _value;
    33
    34      /* ------------------------------------------------------------ */
    35      public CookiePatternRule()
    36      {
    37          _handling = false;
    38          _terminating = false;
    39      }
    40
    41      /* ------------------------------------------------------------ */
    42      /**
    43       * Assigns the cookie name.
    44       *
    45       * @param name a <code>String</code> specifying the name of the cookie.
    46       */
    47      public void setName(String name)
    48      {
    49          _name = name;
    50      }
    51
    52      /* ------------------------------------------------------------ */
    53      /**
    54       * Assigns the cookie value.
    55       *
    56       * @param value a <code>String</code> specifying the value of the cookie
    57       * @see Cookie#setValue(String)
    58       */
    59      public void setValue(String value)
    60      {
    61          _value = value;
    62      }
    63
    64      /* ------------------------------------------------------------ */
    65      /*
    66       * (non-Javadoc)
    67       * @see org.mortbay.jetty.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    68       */
    69      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    70      {
    71          response.addCookie(new Cookie(_name, _value));
    72          return target;
    73      }
    74
    75      /* ------------------------------------------------------------ */
    76      /**
    77       * Returns the cookie contents.
    78       */
    79      public String toString()
    80      {
    81          return super.toString()+"["+_name+","+_value + "]";
    82      }
    83  }