Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CookieJar |
|
| 3.0;3 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | ||
23 | ||
24 | package org.webmacro.servlet; | |
25 | ||
26 | import org.webmacro.UnsettableException; | |
27 | ||
28 | import javax.servlet.http.Cookie; | |
29 | import javax.servlet.http.HttpServletRequest; | |
30 | import javax.servlet.http.HttpServletResponse; | |
31 | ||
32 | ||
33 | /** | |
34 | * Provide access to form variables | |
35 | */ | |
36 | final public class CookieJar | |
37 | { | |
38 | ||
39 | private Cookie[] jar; | |
40 | private HttpServletResponse res; | |
41 | ||
42 | /** | |
43 | * Read the form data from the supplied Request object | |
44 | */ | |
45 | CookieJar (final HttpServletRequest rq, final HttpServletResponse rs) | |
46 | 0 | { |
47 | 0 | jar = rq.getCookies(); |
48 | 0 | res = rs; |
49 | 0 | } |
50 | ||
51 | /** | |
52 | * Get a form value | |
53 | */ | |
54 | final public Object get (String cookieName) | |
55 | { | |
56 | 0 | if (jar == null) |
57 | { | |
58 | 0 | return null; |
59 | } | |
60 | 0 | for (int i = 0; i < jar.length; i++) |
61 | { | |
62 | 0 | if ((jar[i] != null) && (jar[i].getName().equals(cookieName))) |
63 | { | |
64 | 0 | return jar[i]; |
65 | } | |
66 | } | |
67 | 0 | return null; |
68 | } | |
69 | ||
70 | /** | |
71 | * Create a new cookie with the supplied name and value and set it | |
72 | * in the response header. | |
73 | */ | |
74 | public final void put (final String name, final String value) | |
75 | { | |
76 | 0 | Cookie c = new Cookie(name, value); |
77 | 0 | res.addCookie(c); |
78 | 0 | } |
79 | ||
80 | /** | |
81 | * Calls put | |
82 | */ | |
83 | public final void set (final String name, final Object value) | |
84 | throws UnsettableException | |
85 | { | |
86 | try | |
87 | { | |
88 | 0 | put(name, (String) value); |
89 | } | |
90 | 0 | catch (ClassCastException ce) |
91 | { | |
92 | 0 | throw new UnsettableException("Value must be a String"); |
93 | 0 | } |
94 | 0 | } |
95 | ||
96 | } | |
97 |