1 package org.apache.turbine.util.parser; 2 3 18 19 import javax.servlet.http.Cookie ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.apache.turbine.util.RunData; 27 import org.apache.turbine.util.pool.Recyclable; 28 import org.apache.turbine.util.uri.DataURI; 29 import org.apache.turbine.util.uri.URI; 30 31 59 public class DefaultCookieParser 60 extends BaseValueParser 61 implements CookieParser, Recyclable 62 { 63 64 private static Log log = LogFactory.getLog(DefaultCookieParser.class); 65 66 67 private RunData data = null; 68 69 70 private HttpServletRequest request; 71 72 73 private HttpServletResponse response; 74 75 76 private URI cookiePath = null; 77 78 81 public DefaultCookieParser() 82 { 83 super(); 84 } 85 86 89 public void dispose() 90 { 91 this.data = null; 92 this.cookiePath = null; 93 this.request = null; 94 this.response = null; 95 super.dispose(); 96 } 97 98 104 public RunData getRunData() 105 { 106 return data; 107 } 108 109 114 public HttpServletRequest getRequest() 115 { 116 return request; 117 } 118 119 132 public void setRunData(RunData data) 133 { 134 this.data = data; 135 setData(data.getRequest(), data.getResponse()); 136 } 137 138 146 public void setData (HttpServletRequest request, 147 HttpServletResponse response) 148 { 149 clear(); 150 151 String enc = request.getCharacterEncoding(); 152 setCharacterEncoding(enc != null ? enc : "US-ASCII"); 153 154 cookiePath = new DataURI(data); 155 156 Cookie [] cookies = request.getCookies(); 157 158 int cookiesCount = (cookies != null) ? cookies.length : 0; 159 160 log.debug ("Number of Cookies: " + cookiesCount); 161 162 for (int i = 0; i < cookiesCount; i++) 163 { 164 String name = convert (cookies[i].getName()); 165 String value = cookies[i].getValue(); 166 log.debug("Adding " + name + "=" + value); 167 add(name, value); 168 } 169 170 this.request = request; 171 this.response = response; 172 } 173 174 179 public URI getCookiePath() 180 { 181 return cookiePath; 182 } 183 184 189 public void setCookiePath(URI cookiePath) 190 { 191 this.cookiePath = cookiePath; 192 } 193 194 201 public void set(String name, String value) 202 { 203 set(name, value, AGE_SESSION); 204 } 205 206 214 public void set(String name, String value, int seconds_age) 215 { 216 if (response == null) 217 { 218 throw new IllegalStateException ("Servlet response not available"); 219 } 220 221 Cookie cookie = new Cookie (name, value); 222 cookie.setMaxAge(seconds_age); 223 cookie.setPath(cookiePath.getContextPath()+cookiePath.getScriptName()); 224 response.addCookie (cookie); 225 } 226 227 232 public void unset(String name) 233 { 234 set(name, " ", AGE_DELETE); 235 } 236 } 237 | Popular Tags |