1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13 package org.eclipse.jetty.rewrite.handler;
14
15 import java.io.IOException;
16
17 import javax.servlet.http.Cookie;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21
22 /**
23 * Sets the cookie in the response whenever the rule finds a match.
24 *
25 * @see Cookie
26 */
27 public class CookiePatternRule extends PatternRule
28 {
29 private String _name;
30 private String _value;
31
32 /* ------------------------------------------------------------ */
33 public CookiePatternRule()
34 {
35 _handling = false;
36 _terminating = false;
37 }
38
39 /* ------------------------------------------------------------ */
40 /**
41 * Assigns the cookie name.
42 *
43 * @param name a <code>String</code> specifying the name of the cookie.
44 */
45 public void setName(String name)
46 {
47 _name = name;
48 }
49
50 /* ------------------------------------------------------------ */
51 /**
52 * Assigns the cookie value.
53 *
54 * @param value a <code>String</code> specifying the value of the cookie
55 * @see Cookie#setValue(String)
56 */
57 public void setValue(String value)
58 {
59 _value = value;
60 }
61
62 /* ------------------------------------------------------------ */
63 /*
64 * (non-Javadoc)
65 * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
66 */
67 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
68 {
69 response.addCookie(new Cookie(_name, _value));
70 return target;
71 }
72
73 /* ------------------------------------------------------------ */
74 /**
75 * Returns the cookie contents.
76 */
77 public String toString()
78 {
79 return super.toString()+"["+_name+","+_value + "]";
80 }
81 }