|
1
|
|
/* Generated by AspectJ version 1.0.5 */
|
|
2
|
|
package org.apache.cactus;
|
|
3
|
|
import org.apache.commons.logging.Log;
|
|
4
|
|
import org.apache.commons.logging.LogFactory;
|
|
5
|
|
import java.io.Serializable;
|
|
6
|
|
import java.util.Date;
|
|
7
|
|
|
|
8
|
|
/**
|
|
9
|
|
* Client cookie. Used for manipulating client cookies either in
|
|
10
|
|
* <code>beginXXX()</code> (to send cookies) or in
|
|
11
|
|
* <code>endXXX()</code> methods (to assert returned cookies).
|
|
12
|
|
*
|
|
13
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
14
|
|
*
|
|
15
|
|
* @version $Id: Cookie.java,v 1.3 2002/07/23 22:35:36 vmassol Exp $
|
|
16
|
|
*/
|
|
17
|
|
public class Cookie implements Serializable {
|
|
18
|
|
/**
|
|
19
|
|
* The logger
|
|
20
|
|
*/
|
|
21
|
|
private static Log LOGGER;
|
|
22
|
|
/**
|
|
23
|
|
* The cookie name
|
|
24
|
|
*/
|
|
25
|
|
private String name;
|
|
26
|
|
/**
|
|
27
|
|
* The cookie value
|
|
28
|
|
*/
|
|
29
|
|
private String value;
|
|
30
|
|
/**
|
|
31
|
|
* The cookie description.
|
|
32
|
|
* @see #setComment(String)
|
|
33
|
|
*/
|
|
34
|
|
private String comment;
|
|
35
|
|
/**
|
|
36
|
|
* The cookie domain.
|
|
37
|
|
* @see #setDomain(String)
|
|
38
|
|
*/
|
|
39
|
|
private String domain;
|
|
40
|
|
/**
|
|
41
|
|
* The cookie expiry date.
|
|
42
|
|
* @see #setExpiryDate(Date)
|
|
43
|
|
*/
|
|
44
|
|
private Date expiryDate;
|
|
45
|
|
/**
|
|
46
|
|
* The cookie path.
|
|
47
|
|
* @see #setPath(String)
|
|
48
|
|
*/
|
|
49
|
|
private String path;
|
|
50
|
|
/**
|
|
51
|
|
* True if the cookie should only be sent over secure connections.
|
|
52
|
|
* @see #setSecure(boolean)
|
|
53
|
|
*/
|
|
54
|
|
private boolean isSecure;
|
|
55
|
|
/**
|
|
56
|
|
* Create a cookie.
|
|
57
|
|
*
|
|
58
|
|
* @param theDomain the cookie domain
|
|
59
|
|
* @param theName the cookie name
|
|
60
|
|
* @param theValue the cookie value
|
|
61
|
|
*/
|
|
62
|
43
|
public Cookie(String theDomain, String theName, String theValue) {
|
|
63
|
43
|
super();
|
|
64
|
|
{
|
|
65
|
|
/**
|
|
66
|
|
* True if the cookie should only be sent over secure connections.
|
|
67
|
|
* @see #setSecure(boolean)
|
|
68
|
|
*/
|
|
69
|
43
|
this.isSecure = false;
|
|
70
|
|
}
|
|
71
|
43
|
if (theDomain == null) {
|
|
72
|
0
|
throw new NullPointerException("missing cookie domain");
|
|
73
|
|
}
|
|
74
|
43
|
if (theName == null) {
|
|
75
|
0
|
throw new NullPointerException("missing cookie name");
|
|
76
|
|
}
|
|
77
|
43
|
if (theValue == null) {
|
|
78
|
0
|
throw new NullPointerException("missing cookie value");
|
|
79
|
|
}
|
|
80
|
43
|
this.setDomain(theDomain);
|
|
81
|
43
|
this.setName(theName);
|
|
82
|
43
|
this.setValue(theValue);
|
|
83
|
|
}
|
|
84
|
|
/**
|
|
85
|
|
* Sets the cookie name
|
|
86
|
|
*
|
|
87
|
|
* @param theName the cookie name
|
|
88
|
|
*/
|
|
89
|
43
|
public void setName(String theName) {
|
|
90
|
43
|
this.name = theName;
|
|
91
|
|
}
|
|
92
|
|
|
|
93
|
|
/**
|
|
94
|
|
* @return the cookie name
|
|
95
|
|
*/
|
|
96
|
44
|
public String getName() {
|
|
97
|
44
|
return this.name;
|
|
98
|
|
}
|
|
99
|
|
|
|
100
|
|
/**
|
|
101
|
|
* Sets the cookie value
|
|
102
|
|
*
|
|
103
|
|
* @param theValue the cookie value
|
|
104
|
|
*/
|
|
105
|
43
|
public void setValue(String theValue) {
|
|
106
|
43
|
this.value = theValue;
|
|
107
|
|
}
|
|
108
|
|
|
|
109
|
|
/**
|
|
110
|
|
* @return the cookie value
|
|
111
|
|
*/
|
|
112
|
36
|
public String getValue() {
|
|
113
|
36
|
return this.value;
|
|
114
|
|
}
|
|
115
|
|
|
|
116
|
|
/**
|
|
117
|
|
* Returns the comment describing the purpose of this cookie, or
|
|
118
|
|
* null if no such comment has been defined.
|
|
119
|
|
*
|
|
120
|
|
* @return the cookie comment
|
|
121
|
|
*/
|
|
122
|
29
|
public String getComment() {
|
|
123
|
29
|
return this.comment;
|
|
124
|
|
}
|
|
125
|
|
|
|
126
|
|
/**
|
|
127
|
|
* If a user agent (web browser) presents this cookie to a user, the
|
|
128
|
|
* cookie's purpose will be described using this comment.
|
|
129
|
|
*
|
|
130
|
|
* @param theComment the cookie's text comment
|
|
131
|
|
*/
|
|
132
|
14
|
public void setComment(String theComment) {
|
|
133
|
14
|
this.comment = theComment;
|
|
134
|
|
}
|
|
135
|
|
|
|
136
|
|
/**
|
|
137
|
|
* Return the expiry date.
|
|
138
|
|
*
|
|
139
|
|
* @return the expiry date of this cookie, or null if none set.
|
|
140
|
|
*/
|
|
141
|
29
|
public Date getExpiryDate() {
|
|
142
|
29
|
return this.expiryDate;
|
|
143
|
|
}
|
|
144
|
|
|
|
145
|
|
/**
|
|
146
|
|
* Set the cookie expires date.
|
|
147
|
|
*
|
|
148
|
|
* <p>Netscape's original proposal defined an Expires header that took
|
|
149
|
|
* a date value in a fixed-length variant format in place of Max-Age:
|
|
150
|
|
*
|
|
151
|
|
* Wdy, DD-Mon-YY HH:MM:SS GMT
|
|
152
|
|
*
|
|
153
|
|
* Note that the Expires date format contains embedded spaces, and that
|
|
154
|
|
* "old" cookies did not have quotes around values. Clients that
|
|
155
|
|
* implement to this specification should be aware of "old" cookies and
|
|
156
|
|
* Expires.
|
|
157
|
|
*
|
|
158
|
|
* @param theExpiryDate the expires date.
|
|
159
|
|
*/
|
|
160
|
14
|
public void setExpiryDate(Date theExpiryDate) {
|
|
161
|
14
|
this.expiryDate = theExpiryDate;
|
|
162
|
|
}
|
|
163
|
|
|
|
164
|
|
/**
|
|
165
|
|
* @return true if the cookie should be discarded at the end of the
|
|
166
|
|
* session; false otherwise
|
|
167
|
|
*/
|
|
168
|
0
|
public boolean isToBeDiscarded() {
|
|
169
|
0
|
return (this.getExpiryDate() != null);
|
|
170
|
|
}
|
|
171
|
|
|
|
172
|
|
/**
|
|
173
|
|
* Returns the domain of this cookie.
|
|
174
|
|
*
|
|
175
|
|
* @return the cookie domain
|
|
176
|
|
*/
|
|
177
|
64
|
public String getDomain() {
|
|
178
|
64
|
return this.domain;
|
|
179
|
|
}
|
|
180
|
|
|
|
181
|
|
/**
|
|
182
|
|
* Sets the cookie domain. This cookie should be presented only to hosts
|
|
183
|
|
* satisfying this domain name pattern. Read RFC 2109 for specific
|
|
184
|
|
* details of the syntax.
|
|
185
|
|
*
|
|
186
|
|
* Briefly, a domain name name begins with a dot (".foo.com") and means
|
|
187
|
|
* that hosts in that DNS zone ("www.foo.com", but not "a.b.foo.com")
|
|
188
|
|
* should see the cookie. By default, cookies are only returned to
|
|
189
|
|
* the host which saved them.
|
|
190
|
|
*
|
|
191
|
|
* @param theDomain the cookie domain
|
|
192
|
|
*/
|
|
193
|
43
|
public void setDomain(String theDomain) {
|
|
194
|
43
|
int ndx = theDomain.indexOf(":");
|
|
195
|
43
|
if (ndx != -1) {
|
|
196
|
0
|
theDomain = theDomain.substring(0, ndx);
|
|
197
|
|
}
|
|
198
|
43
|
this.domain = theDomain.toLowerCase();
|
|
199
|
|
}
|
|
200
|
|
|
|
201
|
|
/**
|
|
202
|
|
* Return the path this cookie is associated with.
|
|
203
|
|
*
|
|
204
|
|
* @return the cookie path
|
|
205
|
|
*/
|
|
206
|
29
|
public String getPath() {
|
|
207
|
29
|
return this.path;
|
|
208
|
|
}
|
|
209
|
|
|
|
210
|
|
/**
|
|
211
|
|
* Sets the cookie path. This cookie should be presented only with
|
|
212
|
|
* requests beginning with this URL. Read RFC 2109 for a specification
|
|
213
|
|
* of the default behaviour. Basically, URLs in the same "directory" as
|
|
214
|
|
* the one which set the cookie, and in subdirectories, can all see the
|
|
215
|
|
* cookie unless a different path is set.
|
|
216
|
|
*
|
|
217
|
|
* @param thePath the cookie path
|
|
218
|
|
*/
|
|
219
|
14
|
public void setPath(String thePath) {
|
|
220
|
14
|
this.path = thePath;
|
|
221
|
|
}
|
|
222
|
|
|
|
223
|
|
/**
|
|
224
|
|
* @return true if the cookie should only be sent over secure connections.
|
|
225
|
|
*/
|
|
226
|
29
|
public boolean isSecure() {
|
|
227
|
29
|
return this.isSecure;
|
|
228
|
|
}
|
|
229
|
|
|
|
230
|
|
/**
|
|
231
|
|
* Indicates to the user agent that the cookie should only be sent
|
|
232
|
|
* using a secure protocol (https). This should only be set when
|
|
233
|
|
* the cookie's originating server used a secure protocol to set the
|
|
234
|
|
* cookie's value.
|
|
235
|
|
*
|
|
236
|
|
* @param isSecure true if the cookie should be sent over secure
|
|
237
|
|
* connections only
|
|
238
|
|
*/
|
|
239
|
14
|
public void setSecure(boolean isSecure) {
|
|
240
|
14
|
this.isSecure = isSecure;
|
|
241
|
|
}
|
|
242
|
|
|
|
243
|
|
/**
|
|
244
|
|
* @return true if this cookie has expired
|
|
245
|
|
*/
|
|
246
|
0
|
public boolean isExpired() {
|
|
247
|
0
|
return (this.getExpiryDate() != null && this.getExpiryDate().getTime() <=
|
|
248
|
|
System.currentTimeMillis());
|
|
249
|
|
}
|
|
250
|
|
|
|
251
|
|
/**
|
|
252
|
|
* Hash up name, value and domain into new hash.
|
|
253
|
|
*
|
|
254
|
|
* @return the hashcode of this class
|
|
255
|
|
*/
|
|
256
|
0
|
public int hashCode() {
|
|
257
|
0
|
return (this.getName().hashCode() + this.getValue().hashCode() + this.getDomain().hashCode());
|
|
258
|
|
}
|
|
259
|
|
|
|
260
|
|
/**
|
|
261
|
|
* Two cookies match if the name, path and domain match.
|
|
262
|
|
*
|
|
263
|
|
* @param theObject the cookie object to match
|
|
264
|
|
* @return true of the object passed as paramater is equal to this coookie
|
|
265
|
|
* instance
|
|
266
|
|
*/
|
|
267
|
0
|
public boolean equals(Object theObject) {
|
|
268
|
0
|
if ((theObject != null) && (theObject instanceof Cookie)) {
|
|
269
|
0
|
Cookie other = (Cookie)theObject;
|
|
270
|
0
|
return (this.getName().equals(other.getName()) && this.getPath().equals(other.getPath()) &&
|
|
271
|
|
this.getDomain().equals(other.getDomain()));
|
|
272
|
|
}
|
|
273
|
0
|
return false;
|
|
274
|
|
}
|
|
275
|
|
|
|
276
|
|
/**
|
|
277
|
|
* @return a string representation of the cookie
|
|
278
|
|
*/
|
|
279
|
1
|
public String toString() {
|
|
280
|
1
|
StringBuffer buffer = new StringBuffer();
|
|
281
|
1
|
buffer.append("name = [" + this.getName() + "], ");
|
|
282
|
1
|
buffer.append("value = [" + this.getValue() + "], ");
|
|
283
|
1
|
buffer.append("domain = [" + this.getDomain() + "], ");
|
|
284
|
1
|
buffer.append("path = [" + this.getPath() + "], ");
|
|
285
|
1
|
buffer.append("isSecure = [" + this.isSecure() + "], ");
|
|
286
|
1
|
buffer.append("comment = [" + this.getComment() + "], ");
|
|
287
|
1
|
buffer.append("expiryDate = [" + this.getExpiryDate() + "]");
|
|
288
|
1
|
return buffer.toString();
|
|
289
|
|
}
|
|
290
|
|
|
|
291
|
|
/**
|
|
292
|
|
* Returns the domain that will be used to send the cookies. If a host
|
|
293
|
|
* was specified using <code>setURL()</code> then the domain will be
|
|
294
|
|
* this host. Otherwise it will be the real redirector host.
|
|
295
|
|
*
|
|
296
|
|
* @param theRequest the request containing all data to pass to the server
|
|
297
|
|
* redirector.
|
|
298
|
|
* @param theRealHost the real host to which we are connecting to. We will
|
|
299
|
|
* use it if no simulation host has been specified.
|
|
300
|
|
* @return the cookie domain to use
|
|
301
|
|
*/
|
|
302
|
28
|
public static String getCookieDomain(WebRequest theRequest, String theRealHost) {
|
|
303
|
28
|
String domain;
|
|
304
|
28
|
ServletURL url = theRequest.getURL();
|
|
305
|
28
|
if ((url != null) && (url.getHost() != null)) {
|
|
306
|
7
|
domain = url.getHost();
|
|
307
|
|
} else {
|
|
308
|
21
|
domain = theRealHost;
|
|
309
|
|
}
|
|
310
|
28
|
Cookie.LOGGER.debug("Cookie validation domain = [" + domain + "]");
|
|
311
|
28
|
return domain;
|
|
312
|
|
}
|
|
313
|
|
|
|
314
|
|
/**
|
|
315
|
|
* Returns the port that will be used to send the cookies. If a port
|
|
316
|
|
* was specified using <code>setURL()</code> then the port sent will be
|
|
317
|
|
* this port. Otherwise it will be the real redirector port.
|
|
318
|
|
*
|
|
319
|
|
* @param theRequest the request containing all data to pass to the server
|
|
320
|
|
* redirector.
|
|
321
|
|
* @param theRealPort the real port to which we are connecting to. We will
|
|
322
|
|
* use it if no simulation port has been specified.
|
|
323
|
|
* @return the cookie domain to use
|
|
324
|
|
*/
|
|
325
|
7
|
public static int getCookiePort(WebRequest theRequest, int theRealPort) {
|
|
326
|
7
|
int port;
|
|
327
|
7
|
ServletURL url = theRequest.getURL();
|
|
328
|
7
|
if ((url != null) && (url.getHost() != null)) {
|
|
329
|
7
|
port = url.getPort();
|
|
330
|
|
} else {
|
|
331
|
0
|
port = theRealPort;
|
|
332
|
|
}
|
|
333
|
7
|
Cookie.LOGGER.debug("Cookie validation port = [" + port + "]");
|
|
334
|
7
|
return port;
|
|
335
|
|
}
|
|
336
|
|
|
|
337
|
|
/**
|
|
338
|
|
* Returns the path that will be used to validate if a cookie will be
|
|
339
|
|
* sent or not. The algorithm is as follows : if the cookie path is not
|
|
340
|
|
* set (i.e. null) then the cookie is always sent (provided the domain
|
|
341
|
|
* is right). If the cookie path is set, the cookie is sent only if
|
|
342
|
|
* the request path starts with the same string as the cookie path. If
|
|
343
|
|
* <code>setURL()</code> has been called, return the path it has been
|
|
344
|
|
* set to (context + servletPath + pathInfo). Otherwise return the
|
|
345
|
|
* real redirector path.
|
|
346
|
|
*
|
|
347
|
|
* @param theRequest the request containing all data to pass to the server
|
|
348
|
|
* redirector.
|
|
349
|
|
* @param theRealPath the real path to which we are connecting to. We will
|
|
350
|
|
* use it if no simulation path has been specified.
|
|
351
|
|
* @return the path to use to decide if a cookie will get sent
|
|
352
|
|
*/
|
|
353
|
56
|
public static String getCookiePath(WebRequest theRequest, String theRealPath) {
|
|
354
|
56
|
String path;
|
|
355
|
56
|
ServletURL url = theRequest.getURL();
|
|
356
|
56
|
if ((url != null) && (url.getPath() != null)) {
|
|
357
|
0
|
path = url.getPath();
|
|
358
|
|
} else {
|
|
359
|
56
|
String file = theRealPath;
|
|
360
|
56
|
if (file != null) {
|
|
361
|
56
|
int q = file.lastIndexOf('?');
|
|
362
|
56
|
if (q != -1) {
|
|
363
|
56
|
path = file.substring(0, q);
|
|
364
|
|
} else {
|
|
365
|
0
|
path = file;
|
|
366
|
|
}
|
|
367
|
|
} else {
|
|
368
|
0
|
path = null;
|
|
369
|
|
}
|
|
370
|
|
}
|
|
371
|
56
|
Cookie.LOGGER.debug("Cookie validation pah = [" + path + "]");
|
|
372
|
56
|
return path;
|
|
373
|
|
}
|
|
374
|
|
|
|
375
|
|
/**
|
|
376
|
|
* Client cookie. Used for manipulating client cookies either in
|
|
377
|
|
* <code>beginXXX()</code> (to send cookies) or in
|
|
378
|
|
* <code>endXXX()</code> methods (to assert returned cookies).
|
|
379
|
|
*
|
|
380
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
381
|
|
*
|
|
382
|
|
* @version $Id: Cookie.java,v 1.3 2002/07/23 22:35:36 vmassol Exp $
|
|
383
|
|
*/
|
|
384
|
|
static {
|
|
385
|
|
/**
|
|
386
|
|
* The logger
|
|
387
|
|
*/
|
|
388
|
15
|
Cookie.LOGGER = LogFactory.getLog(Cookie.class);
|
|
389
|
|
}
|
|
390
|
|
|
|
391
|
|
}
|