1 package org.apache.velocity.tools.view;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.servlet.http.Cookie;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.apache.velocity.runtime.log.Log;
28 import org.apache.velocity.tools.config.DefaultKey;
29 import org.apache.velocity.tools.config.ValidScope;
30 import org.apache.velocity.tools.ConversionUtils;
31 import org.apache.velocity.tools.Scope;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 @DefaultKey("cookies")
56 @ValidScope(Scope.REQUEST)
57 public class CookieTool
58 {
59 protected HttpServletRequest request;
60 protected HttpServletResponse response;
61 protected Log log;
62 private List<Cookie> jar;
63
64
65
66
67
68
69
70
71 public void setRequest(HttpServletRequest request)
72 {
73 if (request == null)
74 {
75 throw new NullPointerException("request should not be null");
76 }
77 this.request = request;
78 }
79
80
81
82
83
84
85 public void setResponse(HttpServletResponse response)
86 {
87 if (response == null)
88 {
89 throw new NullPointerException("response should not be null");
90 }
91 this.response = response;
92 }
93
94
95
96
97
98 public void setLog(Log log)
99 {
100 this.log = log;
101 }
102
103
104
105
106
107
108
109
110
111
112 public List<Cookie> getAll()
113 {
114 if (jar == null) {
115 Cookie[] array = request.getCookies();
116 if (array == null)
117 {
118 return null;
119 }
120
121 jar = new ArrayList<Cookie>(array.length);
122 for (Cookie c : array)
123 {
124 Cookie sc = new SugarCookie(c);
125 jar.add(sc);
126 }
127 }
128 return jar;
129 }
130
131
132
133
134
135
136
137
138 public Cookie get(String name)
139 {
140 List<Cookie> all = getAll();
141 if (all != null)
142 {
143 for (Cookie c : all)
144 {
145 if (c.getName().equals(name))
146 {
147 return c;
148 }
149 }
150 }
151 return null;
152 }
153
154
155
156
157
158
159
160
161
162
163 public String add(Cookie c)
164 {
165 if (c == null)
166 {
167 return null;
168 }
169 response.addCookie(c);
170 return "";
171 }
172
173
174
175
176
177
178
179
180
181
182 public String add(String name, String value)
183 {
184 return add(create(name, value));
185 }
186
187
188
189
190
191
192
193
194
195
196 public String add(String name, String value, Object maxAge)
197 {
198 return add(create(name, value, maxAge));
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212 public Cookie create(String name, String value)
213 {
214 try
215 {
216 return new SugarCookie(name, value);
217 }
218 catch (IllegalArgumentException iae)
219 {
220 if (log != null && log.isDebugEnabled())
221 {
222 log.debug("CookieTool: Could not create cookie with name \""+name+"\"", iae);
223 }
224 return null;
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238 public Cookie create(String name, String value, Object maxAge)
239 {
240 SugarCookie sc = (SugarCookie)create(name, value);
241 if (sc == null)
242 {
243 return null;
244 }
245 return sc.maxAge(maxAge);
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260 public String delete(String name)
261 {
262 Cookie c = get(name);
263 if (c == null)
264 {
265 return null;
266 }
267 c.setMaxAge(0);
268 return add(c);
269 }
270
271 @Override
272 public String toString()
273 {
274 List<Cookie> all = getAll();
275 if (all == null)
276 {
277 return super.toString();
278 }
279 StringBuilder out = new StringBuilder();
280 out.append('[');
281 for (int i=0; i < all.size(); i++)
282 {
283 if (i != 0)
284 {
285 out.append(", ");
286 }
287 Cookie c = all.get(i);
288 out.append(c.getName());
289 out.append('=');
290 out.append(c.getValue());
291 }
292 out.append(']');
293 return out.toString();
294 }
295
296
297
298
299
300
301
302 public static class SugarCookie extends Cookie
303 {
304 private Cookie plain;
305
306
307 public SugarCookie(Cookie c)
308 {
309 this(c.getName(), c.getValue());
310 setMaxAge(c.getMaxAge());
311 setComment(c.getComment());
312 setPath(c.getPath());
313 setVersion(c.getVersion());
314 setSecure(c.getSecure());
315
316 if (c.getDomain() != null)
317 {
318 setDomain(c.getDomain());
319 }
320 this.plain = c;
321 }
322
323 public SugarCookie(String name, String value)
324 {
325 super(name, value);
326 }
327
328 public SugarCookie value(Object obj)
329 {
330 String value = ConversionUtils.toString(obj);
331 setValue(value);
332 if (plain != null)
333 {
334 plain.setValue(value);
335 }
336 return this;
337 }
338
339 public SugarCookie maxAge(Object obj)
340 {
341 Number maxAge = ConversionUtils.toNumber(obj);
342 if (maxAge == null)
343 {
344 return null;
345 }
346 setMaxAge(maxAge.intValue());
347 if (plain != null)
348 {
349 plain.setMaxAge(maxAge.intValue());
350 }
351 return this;
352 }
353
354 public SugarCookie comment(Object obj)
355 {
356 String comment = ConversionUtils.toString(obj);
357 setComment(comment);
358 if (plain != null)
359 {
360 plain.setComment(comment);
361 }
362 return this;
363 }
364
365 public SugarCookie domain(Object obj)
366 {
367 String domain = ConversionUtils.toString(obj);
368 if (domain == null)
369 {
370 return null;
371 }
372 setDomain(domain);
373 if (plain != null)
374 {
375 plain.setDomain(domain);
376 }
377 return this;
378 }
379
380 public SugarCookie path(Object obj)
381 {
382 String path = ConversionUtils.toString(obj);
383 setPath(path);
384 if (plain != null)
385 {
386 plain.setPath(path);
387 }
388 return this;
389 }
390
391 public SugarCookie version(Object obj)
392 {
393 Number version = ConversionUtils.toNumber(obj);
394 if (version == null)
395 {
396 return null;
397 }
398 setVersion(version.intValue());
399 if (plain != null)
400 {
401 plain.setVersion(version.intValue());
402 }
403 return this;
404 }
405
406 public SugarCookie secure(Object obj)
407 {
408 Boolean secure = ConversionUtils.toBoolean(obj);
409 if (secure == null)
410 {
411 return null;
412 }
413 setSecure(secure.booleanValue());
414 if (plain != null)
415 {
416 plain.setSecure(secure.booleanValue());
417 }
418 return this;
419 }
420
421 public Cookie getPlain()
422 {
423 return plain;
424 }
425
426 @Override
427 public String toString()
428 {
429 return getValue();
430 }
431 }
432
433 }