001 package org.apache.fulcrum.parser;
002
003
004 /*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements. See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership. The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License. You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied. See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024 import javax.servlet.http.Cookie;
025 import javax.servlet.http.HttpServletRequest;
026 import javax.servlet.http.HttpServletResponse;
027
028 /**
029 * CookieParser is used to get and set values of Cookies on the Client
030 * Browser. You can use CookieParser to convert Cookie values to
031 * various types or to set Bean values with setParameters(). See the
032 * Servlet Spec for more information on Cookies.
033 * <p>
034 * Use set() or unset() to Create or Destroy Cookies.
035 * <p>
036 * NOTE: The name= portion of a name=value pair may be converted
037 * to lowercase or uppercase when the object is initialized and when
038 * new data is added. This behaviour is determined by the url.case.folding
039 * property in TurbineResources.properties. Adding a name/value pair may
040 * overwrite existing name=value pairs if the names match:
041 *
042 * <pre>
043 * CookieParser cp = data.getCookies();
044 * cp.add("ERROR",1);
045 * cp.add("eRrOr",2);
046 * int result = cp.getInt("ERROR");
047 * </pre>
048 *
049 * In the above example, result is 2.
050 *
051 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
052 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
053 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
054 * @version $Id: DefaultCookieParser.java 812786 2009-09-09 07:01:49Z tv $
055 */
056 public class DefaultCookieParser
057 extends BaseValueParser
058 implements CookieParser
059
060 {
061 /**
062 * The servlet request objects to parse.
063 */
064 private HttpServletRequest request;
065 private HttpServletResponse response;
066
067 /**
068 * Constructs a new CookieParser.
069 */
070 public DefaultCookieParser()
071 {
072 super();
073 }
074
075 /**
076 * Disposes the parser.
077 */
078 public void dispose()
079 {
080 this.request = null;
081 super.dispose();
082 }
083
084 /**
085 * Gets the servlet request.
086 *
087 * @return the servlet request object or null.
088 */
089 public HttpServletRequest getRequest()
090 {
091 return this.request;
092 }
093
094 /**
095 * Sets the servlet request and response to be parsed.
096 * All previous cookies will be cleared.
097 *
098 * @param request the servlet request object.
099 * @param response the servlet response object
100 */
101 public void setData (HttpServletRequest request,
102 HttpServletResponse response)
103 {
104 clear();
105
106 String enc = request.getCharacterEncoding();
107 setCharacterEncoding(enc != null ? enc : "US-ASCII");
108
109 Cookie[] cookies = request.getCookies();
110
111 getLogger().debug ("Number of Cookies "+cookies.length);
112
113 for (int i=0; i<cookies.length; i++)
114 {
115 String name = convert (cookies[i].getName());
116 String value = cookies[i].getValue();
117 getLogger().debug ("Adding "+name+"="+value);
118 add (name,value);
119 }
120
121 this.request = request;
122 this.response = response;
123 }
124
125 /**
126 * Set a cookie that will be stored on the client for
127 * the duration of the session.
128 */
129 public void set (String name, String value)
130 {
131 set (name,value,AGE_SESSION);
132 }
133
134 /**
135 * Set a persisten cookie on the client that will expire
136 * after a maximum age (given in seconds).
137 */
138 public void set (String name, String value, int seconds_age)
139 {
140 if (response == null)
141 {
142 throw new IllegalStateException("Servlet response not available");
143 }
144
145 Cookie cookie = new Cookie (name,value);
146 cookie.setMaxAge (seconds_age);
147 cookie.setPath (request.getServletPath());
148 response.addCookie (cookie);
149 }
150
151 /**
152 * Remove a previously set cookie from the client machine.
153 */
154 public void unset (String name)
155 {
156 set (name," ",AGE_DELETE);
157 }
158
159 }