1 package org.apache.fulcrum.parser;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23
24 import javax.servlet.http.Cookie;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 /**
29 * CookieParser is used to get and set values of Cookies on the Client
30 * Browser. You can use CookieParser to convert Cookie values to
31 * various types or to set Bean values with setParameters(). See the
32 * Servlet Spec for more information on Cookies.
33 * <p>
34 * Use set() or unset() to Create or Destroy Cookies.
35 * <p>
36 * NOTE: The name= portion of a name=value pair may be converted
37 * to lowercase or uppercase when the object is initialized and when
38 * new data is added. This behaviour is determined by the url.case.folding
39 * property in TurbineResources.properties. Adding a name/value pair may
40 * overwrite existing name=value pairs if the names match:
41 *
42 * <pre>
43 * CookieParser cp = data.getCookies();
44 * cp.add("ERROR",1);
45 * cp.add("eRrOr",2);
46 * int result = cp.getInt("ERROR");
47 * </pre>
48 *
49 * In the above example, result is 2.
50 *
51 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
52 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
53 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
54 * @version $Id$
55 */
56 public class DefaultCookieParser
57 extends BaseValueParser
58 implements CookieParser
59
60 {
61 /**
62 * The servlet request objects to parse.
63 */
64 private HttpServletRequest request;
65 private HttpServletResponse response;
66
67 /**
68 * Constructs a new CookieParser.
69 */
70 public DefaultCookieParser()
71 {
72 super();
73 }
74
75 /**
76 * Disposes the parser.
77 */
78 public void dispose()
79 {
80 this.request = null;
81 this.response = null;
82 super.dispose();
83 }
84
85 /**
86 * Gets the servlet request.
87 *
88 * @return the servlet request object or null.
89 */
90 public HttpServletRequest getRequest()
91 {
92 return this.request;
93 }
94
95 /**
96 * Sets the servlet request and response to be parsed.
97 * All previous cookies will be cleared.
98 *
99 * @param request the servlet request object.
100 * @param response the servlet response object
101 */
102 public void setData (HttpServletRequest request,
103 HttpServletResponse response)
104 {
105 clear();
106
107 String enc = request.getCharacterEncoding();
108 setCharacterEncoding(enc != null ? enc : "US-ASCII");
109
110 Cookie[] cookies = request.getCookies();
111 if ( cookies != null )
112 {
113 getLogger().debug ("Number of Cookies "+cookies.length);
114
115 for (Cookie cookie : cookies)
116 {
117 String name = convert(cookie.getName());
118 String value = cookie.getValue();
119 getLogger().debug ("Adding " + name + "=" + value);
120 add(name, value);
121 }
122 }
123
124 this.request = request;
125 this.response = response;
126 }
127
128 /**
129 * Set a cookie that will be stored on the client for
130 * the duration of the session.
131 */
132 public void set (String name, String value)
133 {
134 set(name, value, AGE_SESSION);
135 }
136
137 /* (non-Javadoc)
138 * @see org.apache.fulcrum.parser.CookieParser#set(java.lang.String, java.lang.String, int)
139 *
140 * Set a persistent cookie on the client that will expire
141 * after a maximum age (given in seconds).
142 */
143 public void set(String name, String value, int secondsAge)
144 {
145 if (response == null)
146 {
147 throw new IllegalStateException("Servlet response not available");
148 }
149
150 Cookie cookie = new Cookie(name, value);
151 cookie.setMaxAge(secondsAge);
152 cookie.setPath(request.getServletPath());
153 response.addCookie(cookie);
154 }
155
156
157 /* (non-Javadoc)
158 * @see org.apache.fulcrum.parser.CookieParser#unset(java.lang.String)
159 *
160 * Remove a previously set cookie from the client machine.
161 *
162 */
163 public void unset(String name)
164 {
165 set(name, " ", AGE_DELETE);
166 }
167
168 /* (non-Javadoc)
169 * @see org.apache.fulcrum.parser.BaseValueParser#isValid()
170 */
171 public boolean isValid()
172 {
173 if ( this.parameters.size() == 0 )
174 {
175 return true;
176 }
177 return false;
178 }
179
180 }