|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Cookie.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus;
|
|
| 58 |
|
|
| 59 |
import java.io.Serializable;
|
|
| 60 |
|
|
| 61 |
import java.util.Date;
|
|
| 62 |
|
|
| 63 |
import org.apache.cactus.util.CookieUtil;
|
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* Client cookie. Used for manipulating client cookies either in
|
|
| 67 |
* <code>beginXXX()</code> (to send cookies) or in
|
|
| 68 |
* <code>endXXX()</code> methods (to assert returned cookies).
|
|
| 69 |
*
|
|
| 70 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 71 |
*
|
|
| 72 |
* @version $Id: Cookie.java,v 1.9 2003/06/22 15:03:54 vmassol Exp $
|
|
| 73 |
*/
|
|
| 74 |
public class Cookie implements Serializable |
|
| 75 |
{
|
|
| 76 |
/**
|
|
| 77 |
* The cookie name
|
|
| 78 |
*/
|
|
| 79 |
private String name;
|
|
| 80 |
|
|
| 81 |
/**
|
|
| 82 |
* The cookie value
|
|
| 83 |
*/
|
|
| 84 |
private String value;
|
|
| 85 |
|
|
| 86 |
/**
|
|
| 87 |
* The cookie description.
|
|
| 88 |
* @see #setComment(String)
|
|
| 89 |
*/
|
|
| 90 |
private String comment;
|
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* The cookie domain.
|
|
| 94 |
* @see #setDomain(String)
|
|
| 95 |
*/
|
|
| 96 |
private String domain;
|
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* The cookie expiry date.
|
|
| 100 |
* @see #setExpiryDate(Date)
|
|
| 101 |
*/
|
|
| 102 |
private Date expiryDate;
|
|
| 103 |
|
|
| 104 |
/**
|
|
| 105 |
* The cookie path.
|
|
| 106 |
* @see #setPath(String)
|
|
| 107 |
*/
|
|
| 108 |
private String path;
|
|
| 109 |
|
|
| 110 |
/**
|
|
| 111 |
* True if the cookie should only be sent over secure connections.
|
|
| 112 |
* @see #setSecure(boolean)
|
|
| 113 |
*/
|
|
| 114 |
private boolean isSecure = false; |
|
| 115 |
|
|
| 116 |
/**
|
|
| 117 |
* Create a cookie.
|
|
| 118 |
*
|
|
| 119 |
* @param theDomain the cookie domain
|
|
| 120 |
* @param theName the cookie name
|
|
| 121 |
* @param theValue the cookie value
|
|
| 122 |
*/
|
|
| 123 | 0 |
public Cookie(String theDomain, String theName, String theValue)
|
| 124 |
{
|
|
| 125 | 0 |
if (theDomain == null) |
| 126 |
{
|
|
| 127 | 0 |
throw new NullPointerException("missing cookie domain"); |
| 128 |
} |
|
| 129 |
|
|
| 130 | 0 |
if (theName == null) |
| 131 |
{
|
|
| 132 | 0 |
throw new NullPointerException("missing cookie name"); |
| 133 |
} |
|
| 134 |
|
|
| 135 | 0 |
if (theValue == null) |
| 136 |
{
|
|
| 137 | 0 |
throw new NullPointerException("missing cookie value"); |
| 138 |
} |
|
| 139 |
|
|
| 140 | 0 |
setDomain(theDomain); |
| 141 | 0 |
setName(theName); |
| 142 | 0 |
setValue(theValue); |
| 143 |
} |
|
| 144 |
|
|
| 145 |
/**
|
|
| 146 |
* Sets the cookie name
|
|
| 147 |
*
|
|
| 148 |
* @param theName the cookie name
|
|
| 149 |
*/
|
|
| 150 | 0 |
public void setName(String theName) |
| 151 |
{
|
|
| 152 | 0 |
this.name = theName;
|
| 153 |
} |
|
| 154 |
|
|
| 155 |
/**
|
|
| 156 |
* @return the cookie name
|
|
| 157 |
*/
|
|
| 158 | 0 |
public String getName()
|
| 159 |
{
|
|
| 160 | 0 |
return this.name; |
| 161 |
} |
|
| 162 |
|
|
| 163 |
/**
|
|
| 164 |
* Sets the cookie value
|
|
| 165 |
*
|
|
| 166 |
* @param theValue the cookie value
|
|
| 167 |
*/
|
|
| 168 | 0 |
public void setValue(String theValue) |
| 169 |
{
|
|
| 170 | 0 |
this.value = theValue;
|
| 171 |
} |
|
| 172 |
|
|
| 173 |
/**
|
|
| 174 |
* @return the cookie value
|
|
| 175 |
*/
|
|
| 176 | 0 |
public String getValue()
|
| 177 |
{
|
|
| 178 | 0 |
return this.value; |
| 179 |
} |
|
| 180 |
|
|
| 181 |
/**
|
|
| 182 |
* Returns the comment describing the purpose of this cookie, or
|
|
| 183 |
* null if no such comment has been defined.
|
|
| 184 |
*
|
|
| 185 |
* @return the cookie comment
|
|
| 186 |
*/
|
|
| 187 | 0 |
public String getComment()
|
| 188 |
{
|
|
| 189 | 0 |
return this.comment; |
| 190 |
} |
|
| 191 |
|
|
| 192 |
/**
|
|
| 193 |
* If a user agent (web browser) presents this cookie to a user, the
|
|
| 194 |
* cookie's purpose will be described using this comment.
|
|
| 195 |
*
|
|
| 196 |
* @param theComment the cookie's text comment
|
|
| 197 |
*/
|
|
| 198 | 0 |
public void setComment(String theComment) |
| 199 |
{
|
|
| 200 | 0 |
this.comment = theComment;
|
| 201 |
} |
|
| 202 |
|
|
| 203 |
/**
|
|
| 204 |
* Return the expiry date.
|
|
| 205 |
*
|
|
| 206 |
* @return the expiry date of this cookie, or null if none set.
|
|
| 207 |
*/
|
|
| 208 | 0 |
public Date getExpiryDate()
|
| 209 |
{
|
|
| 210 | 0 |
return this.expiryDate; |
| 211 |
} |
|
| 212 |
|
|
| 213 |
/**
|
|
| 214 |
* Set the cookie expires date.
|
|
| 215 |
*
|
|
| 216 |
* <p>Netscape's original proposal defined an Expires header that took
|
|
| 217 |
* a date value in a fixed-length variant format in place of Max-Age:
|
|
| 218 |
*
|
|
| 219 |
* Wdy, DD-Mon-YY HH:MM:SS GMT
|
|
| 220 |
*
|
|
| 221 |
* Note that the Expires date format contains embedded spaces, and that
|
|
| 222 |
* "old" cookies did not have quotes around values. Clients that
|
|
| 223 |
* implement to this specification should be aware of "old" cookies and
|
|
| 224 |
* Expires.
|
|
| 225 |
*
|
|
| 226 |
* @param theExpiryDate the expires date.
|
|
| 227 |
*/
|
|
| 228 | 0 |
public void setExpiryDate(Date theExpiryDate) |
| 229 |
{
|
|
| 230 | 0 |
this.expiryDate = theExpiryDate;
|
| 231 |
} |
|
| 232 |
|
|
| 233 |
/**
|
|
| 234 |
* @return true if the cookie should be discarded at the end of the
|
|
| 235 |
* session; false otherwise
|
|
| 236 |
*/
|
|
| 237 | 0 |
public boolean isToBeDiscarded() |
| 238 |
{
|
|
| 239 | 0 |
return (this.getExpiryDate() != null); |
| 240 |
} |
|
| 241 |
|
|
| 242 |
/**
|
|
| 243 |
* Returns the domain of this cookie.
|
|
| 244 |
*
|
|
| 245 |
* @return the cookie domain
|
|
| 246 |
*/
|
|
| 247 | 0 |
public String getDomain()
|
| 248 |
{
|
|
| 249 | 0 |
return this.domain; |
| 250 |
} |
|
| 251 |
|
|
| 252 |
/**
|
|
| 253 |
* Sets the cookie domain. This cookie should be presented only to hosts
|
|
| 254 |
* satisfying this domain name pattern. Read RFC 2109 for specific
|
|
| 255 |
* details of the syntax.
|
|
| 256 |
*
|
|
| 257 |
* Briefly, a domain name name begins with a dot (".foo.com") and means
|
|
| 258 |
* that hosts in that DNS zone ("www.foo.com", but not "a.b.foo.com")
|
|
| 259 |
* should see the cookie. By default, cookies are only returned to
|
|
| 260 |
* the host which saved them.
|
|
| 261 |
*
|
|
| 262 |
* @param theDomain the cookie domain
|
|
| 263 |
*/
|
|
| 264 | 0 |
public void setDomain(String theDomain) |
| 265 |
{
|
|
| 266 | 0 |
int ndx = theDomain.indexOf(":"); |
| 267 |
|
|
| 268 | 0 |
if (ndx != -1)
|
| 269 |
{
|
|
| 270 | 0 |
theDomain = theDomain.substring(0, ndx); |
| 271 |
} |
|
| 272 |
|
|
| 273 | 0 |
this.domain = theDomain.toLowerCase();
|
| 274 |
} |
|
| 275 |
|
|
| 276 |
/**
|
|
| 277 |
* Return the path this cookie is associated with.
|
|
| 278 |
*
|
|
| 279 |
* @return the cookie path
|
|
| 280 |
*/
|
|
| 281 | 0 |
public String getPath()
|
| 282 |
{
|
|
| 283 | 0 |
return this.path; |
| 284 |
} |
|
| 285 |
|
|
| 286 |
/**
|
|
| 287 |
* Sets the cookie path. This cookie should be presented only with
|
|
| 288 |
* requests beginning with this URL. Read RFC 2109 for a specification
|
|
| 289 |
* of the default behaviour. Basically, URLs in the same "directory" as
|
|
| 290 |
* the one which set the cookie, and in subdirectories, can all see the
|
|
| 291 |
* cookie unless a different path is set.
|
|
| 292 |
*
|
|
| 293 |
* @param thePath the cookie path
|
|
| 294 |
*/
|
|
| 295 | 0 |
public void setPath(String thePath) |
| 296 |
{
|
|
| 297 | 0 |
this.path = thePath;
|
| 298 |
} |
|
| 299 |
|
|
| 300 |
/**
|
|
| 301 |
* @return true if the cookie should only be sent over secure connections.
|
|
| 302 |
*/
|
|
| 303 | 0 |
public boolean isSecure() |
| 304 |
{
|
|
| 305 | 0 |
return this.isSecure; |
| 306 |
} |
|
| 307 |
|
|
| 308 |
/**
|
|
| 309 |
* Indicates to the user agent that the cookie should only be sent
|
|
| 310 |
* using a secure protocol (https). This should only be set when
|
|
| 311 |
* the cookie's originating server used a secure protocol to set the
|
|
| 312 |
* cookie's value.
|
|
| 313 |
*
|
|
| 314 |
* @param isSecure true if the cookie should be sent over secure
|
|
| 315 |
* connections only
|
|
| 316 |
*/
|
|
| 317 | 0 |
public void setSecure(boolean isSecure) |
| 318 |
{
|
|
| 319 | 0 |
this.isSecure = isSecure;
|
| 320 |
} |
|
| 321 |
|
|
| 322 |
/**
|
|
| 323 |
* @return true if this cookie has expired
|
|
| 324 |
*/
|
|
| 325 | 0 |
public boolean isExpired() |
| 326 |
{
|
|
| 327 | 0 |
return (this.getExpiryDate() != null |
| 328 |
&& this.getExpiryDate().getTime() <= System.currentTimeMillis());
|
|
| 329 |
} |
|
| 330 |
|
|
| 331 |
/**
|
|
| 332 |
* Hash up name, value and domain into new hash.
|
|
| 333 |
*
|
|
| 334 |
* @return the hashcode of this class
|
|
| 335 |
*/
|
|
| 336 | 0 |
public int hashCode() |
| 337 |
{
|
|
| 338 | 0 |
return (this.getName().hashCode() + this.getValue().hashCode() |
| 339 |
+ this.getDomain().hashCode());
|
|
| 340 |
} |
|
| 341 |
|
|
| 342 |
/**
|
|
| 343 |
* Two cookies match if the name, path and domain match.
|
|
| 344 |
*
|
|
| 345 |
* @param theObject the cookie object to match
|
|
| 346 |
* @return true of the object passed as paramater is equal to this coookie
|
|
| 347 |
* instance
|
|
| 348 |
*/
|
|
| 349 | 0 |
public boolean equals(Object theObject) |
| 350 |
{
|
|
| 351 | 0 |
if ((theObject != null) && (theObject instanceof Cookie)) |
| 352 |
{
|
|
| 353 | 0 |
Cookie other = (Cookie) theObject; |
| 354 |
|
|
| 355 | 0 |
return (this.getName().equals(other.getName()) |
| 356 |
&& this.getPath().equals(other.getPath())
|
|
| 357 |
&& this.getDomain().equals(other.getDomain()));
|
|
| 358 |
} |
|
| 359 |
|
|
| 360 | 0 |
return false; |
| 361 |
} |
|
| 362 |
|
|
| 363 |
/**
|
|
| 364 |
* @return a string representation of the cookie
|
|
| 365 |
*/
|
|
| 366 | 0 |
public String toString()
|
| 367 |
{
|
|
| 368 | 0 |
StringBuffer buffer = new StringBuffer();
|
| 369 |
|
|
| 370 | 0 |
buffer.append("name = [" + getName() + "], "); |
| 371 | 0 |
buffer.append("value = [" + getValue() + "], "); |
| 372 | 0 |
buffer.append("domain = [" + getDomain() + "], "); |
| 373 | 0 |
buffer.append("path = [" + getPath() + "], "); |
| 374 | 0 |
buffer.append("isSecure = [" + isSecure() + "], "); |
| 375 | 0 |
buffer.append("comment = [" + getComment() + "], "); |
| 376 | 0 |
buffer.append("expiryDate = [" + getExpiryDate() + "]"); |
| 377 |
|
|
| 378 | 0 |
return buffer.toString();
|
| 379 |
} |
|
| 380 |
|
|
| 381 |
/**
|
|
| 382 |
* @see CookieUtil#getCookieDomain(WebRequest, String)
|
|
| 383 |
* @deprecated use {@link CookieUtil#getCookieDomain(WebRequest, String)}
|
|
| 384 |
*/
|
|
| 385 | 0 |
public static String getCookieDomain(WebRequest theRequest, |
| 386 |
String theRealHost) |
|
| 387 |
{
|
|
| 388 | 0 |
return CookieUtil.getCookieDomain(theRequest, theRealHost);
|
| 389 |
} |
|
| 390 |
|
|
| 391 |
/**
|
|
| 392 |
* @see CookieUtil#getCookiePort(WebRequest, int)
|
|
| 393 |
* @deprecated use {@link CookieUtil#getCookiePort(WebRequest, int)}
|
|
| 394 |
*/
|
|
| 395 | 0 |
public static int getCookiePort(WebRequest theRequest, int theRealPort) |
| 396 |
{
|
|
| 397 | 0 |
return CookieUtil.getCookiePort(theRequest, theRealPort);
|
| 398 |
} |
|
| 399 |
|
|
| 400 |
/**
|
|
| 401 |
* @see CookieUtil#getCookiePath(WebRequest, String)
|
|
| 402 |
* @deprecated use {@link CookieUtil#getCookiePath(WebRequest, String)}
|
|
| 403 |
*/
|
|
| 404 | 0 |
public static String getCookiePath(WebRequest theRequest, |
| 405 |
String theRealPath) |
|
| 406 |
{
|
|
| 407 | 0 |
return CookieUtil.getCookiePath(theRequest, theRealPath);
|
| 408 |
} |
|
| 409 |
} |
|
| 410 |
|
|
||||||||||