1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.gwtwidgets.client.util;
17
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 /***
24 * Class that is responsible for reading and writing cookies.
25 *
26 * @author Brian Glick
27 * @author rhanson
28 */
29 public class CookieUtils {
30
31 /***
32 * Creates, sets, and returns the Cookie object
33 * @param name
34 * @param value
35 * @param expires
36 * @param path
37 * @return the Cookie created or reset
38 */
39 public static Cookie write (String name, String value, Date expires, String path)
40 {
41 boolean useExp = false;
42 int year = -1, month = -1, day = -1;
43 if (expires != null) {
44 useExp = true;
45 year = expires.getYear() + 1900;
46 month = expires.getMonth();
47 day = expires.getDate();
48 }
49 writeNative(name, value, useExp, year, month, day, path);
50 return new Cookie(name, value);
51 }
52
53 public static Cookie write (String name, String value, Date expires)
54 {
55 return write(name, value, expires, "/");
56 }
57
58
59 private static void writeNative(String name, String value, boolean useExpires, int year, int month, int day)
60 {
61 writeNative(name, value, useExpires, year, month, day, "/");
62 }
63
64 private static native void writeNative(String name, String value, boolean useExpires, int year, int month, int day, String path)
65
66
67 ;
68
69 /***
70 * Erases cookie with given name.
71 * @param name
72 */
73 public static void erase (String name)
74 {
75 writeNative(name, "", true, 1970, 1, 1);
76 }
77
78 /***
79 * Gets the value of the cookie with the given name.
80 * @param name Name of cookie whose value should be retrieved
81 * @return Cookie value or null if cookie is not found
82 */
83 public static String readValue (String name)
84 {
85 Cookie c = read(name);
86 return c == null ? null : c.getValue();
87 }
88
89 /***
90 * Gets the cookie with the given name.
91 * @param name Name of cookie to get.
92 * @return Cookie or null if the cookie is not found.
93 */
94 public static Cookie read (String name)
95 {
96 String val = getValue(name);
97 return (val == null) ? null : new Cookie(name, val);
98 }
99
100 /***
101 * Tries to load the cookie, but only sets fields if they are found
102 * @param name
103 * @param c
104 */
105 private static native String getValue (String name)
106
107
108
109
110
111
112
113
114
115
116
117
118
119 ;
120
121 /***
122 * Erase the given cookie
123 * @param c
124 */
125 public static void erase (Cookie c)
126 {
127 erase(c.getName());
128 }
129
130 /***
131 * Write the given cookie
132 * @param c
133 */
134 public static void write (Cookie c, Date expires)
135 {
136 write(c.getName(), c.getValue(), expires);
137 }
138
139 public static Cookie[] getAll ()
140 {
141 Map cookies = getCookieMap();
142 Cookie[] rVal = new Cookie[cookies.size()];
143 int counter = 0;
144 for (Iterator i = cookies.keySet().iterator(); i.hasNext(); counter++) {
145 String name = (String) i.next();
146 rVal[counter] = new Cookie(name, (String) cookies.get(name));
147 }
148 return rVal;
149 }
150
151 private static Map getCookieMap ()
152 {
153 Map jar = new HashMap();
154 fillCookieJar(jar);
155 return jar;
156 }
157
158 private static native void fillCookieJar (Map jar)
159
160
161
162
163
164
165
166
167 ;
168
169 }