001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus;
021:
022: import java.io.Serializable;
023:
024: import java.util.Date;
025:
026: import org.apache.cactus.internal.util.CookieUtil;
027:
028: /**
029: * Client cookie. Used for manipulating client cookies either in
030: * <code>beginXXX()</code> (to send cookies) or in
031: * <code>endXXX()</code> methods (to assert returned cookies).
032: *
033: * @version $Id: Cookie.java 238991 2004-05-22 11:34:50Z vmassol $
034: */
035: public class Cookie implements Serializable {
036: /**
037: * The cookie name
038: */
039: private String name;
040:
041: /**
042: * The cookie value
043: */
044: private String value;
045:
046: /**
047: * The cookie description.
048: * @see #setComment(String)
049: */
050: private String comment;
051:
052: /**
053: * The cookie domain.
054: * @see #setDomain(String)
055: */
056: private String domain;
057:
058: /**
059: * The cookie expiry date.
060: * @see #setExpiryDate(Date)
061: */
062: private Date expiryDate;
063:
064: /**
065: * The cookie path.
066: * @see #setPath(String)
067: */
068: private String path;
069:
070: /**
071: * True if the cookie should only be sent over secure connections.
072: * @see #setSecure(boolean)
073: */
074: private boolean isSecure = false;
075:
076: /**
077: * Create a cookie.
078: *
079: * @param theDomain the cookie domain
080: * @param theName the cookie name
081: * @param theValue the cookie value
082: */
083: public Cookie(String theDomain, String theName, String theValue) {
084: if (theDomain == null) {
085: throw new NullPointerException("missing cookie domain");
086: }
087:
088: if (theName == null) {
089: throw new NullPointerException("missing cookie name");
090: }
091:
092: if (theValue == null) {
093: throw new NullPointerException("missing cookie value");
094: }
095:
096: setDomain(theDomain);
097: setName(theName);
098: setValue(theValue);
099: }
100:
101: /**
102: * Sets the cookie name
103: *
104: * @param theName the cookie name
105: */
106: public void setName(String theName) {
107: this .name = theName;
108: }
109:
110: /**
111: * @return the cookie name
112: */
113: public String getName() {
114: return this .name;
115: }
116:
117: /**
118: * Sets the cookie value
119: *
120: * @param theValue the cookie value
121: */
122: public void setValue(String theValue) {
123: this .value = theValue;
124: }
125:
126: /**
127: * @return the cookie value
128: */
129: public String getValue() {
130: return this .value;
131: }
132:
133: /**
134: * Returns the comment describing the purpose of this cookie, or
135: * null if no such comment has been defined.
136: *
137: * @return the cookie comment
138: */
139: public String getComment() {
140: return this .comment;
141: }
142:
143: /**
144: * If a user agent (web browser) presents this cookie to a user, the
145: * cookie's purpose will be described using this comment.
146: *
147: * @param theComment the cookie's text comment
148: */
149: public void setComment(String theComment) {
150: this .comment = theComment;
151: }
152:
153: /**
154: * Return the expiry date.
155: *
156: * @return the expiry date of this cookie, or null if none set.
157: */
158: public Date getExpiryDate() {
159: return this .expiryDate;
160: }
161:
162: /**
163: * Set the cookie expires date.
164: *
165: * <p>Netscape's original proposal defined an Expires header that took
166: * a date value in a fixed-length variant format in place of Max-Age:
167: *
168: * Wdy, DD-Mon-YY HH:MM:SS GMT
169: *
170: * Note that the Expires date format contains embedded spaces, and that
171: * "old" cookies did not have quotes around values. Clients that
172: * implement to this specification should be aware of "old" cookies and
173: * Expires.
174: *
175: * @param theExpiryDate the expires date.
176: */
177: public void setExpiryDate(Date theExpiryDate) {
178: this .expiryDate = theExpiryDate;
179: }
180:
181: /**
182: * @return true if the cookie should be discarded at the end of the
183: * session; false otherwise
184: */
185: public boolean isToBeDiscarded() {
186: return (this .getExpiryDate() != null);
187: }
188:
189: /**
190: * Returns the domain of this cookie.
191: *
192: * @return the cookie domain
193: */
194: public String getDomain() {
195: return this .domain;
196: }
197:
198: /**
199: * Sets the cookie domain. This cookie should be presented only to hosts
200: * satisfying this domain name pattern. Read RFC 2109 for specific
201: * details of the syntax.
202: *
203: * Briefly, a domain name name begins with a dot (".foo.com") and means
204: * that hosts in that DNS zone ("www.foo.com", but not "a.b.foo.com")
205: * should see the cookie. By default, cookies are only returned to
206: * the host which saved them.
207: *
208: * @param theDomain the cookie domain
209: */
210: public void setDomain(String theDomain) {
211: int ndx = theDomain.indexOf(":");
212:
213: if (ndx != -1) {
214: theDomain = theDomain.substring(0, ndx);
215: }
216:
217: this .domain = theDomain.toLowerCase();
218: }
219:
220: /**
221: * Return the path this cookie is associated with.
222: *
223: * @return the cookie path
224: */
225: public String getPath() {
226: return this .path;
227: }
228:
229: /**
230: * Sets the cookie path. This cookie should be presented only with
231: * requests beginning with this URL. Read RFC 2109 for a specification
232: * of the default behaviour. Basically, URLs in the same "directory" as
233: * the one which set the cookie, and in subdirectories, can all see the
234: * cookie unless a different path is set.
235: *
236: * @param thePath the cookie path
237: */
238: public void setPath(String thePath) {
239: this .path = thePath;
240: }
241:
242: /**
243: * @return true if the cookie should only be sent over secure connections.
244: */
245: public boolean isSecure() {
246: return this .isSecure;
247: }
248:
249: /**
250: * Indicates to the user agent that the cookie should only be sent
251: * using a secure protocol (https). This should only be set when
252: * the cookie's originating server used a secure protocol to set the
253: * cookie's value.
254: *
255: * @param isSecure true if the cookie should be sent over secure
256: * connections only
257: */
258: public void setSecure(boolean isSecure) {
259: this .isSecure = isSecure;
260: }
261:
262: /**
263: * @return true if this cookie has expired
264: */
265: public boolean isExpired() {
266: return (this .getExpiryDate() != null && this .getExpiryDate()
267: .getTime() <= System.currentTimeMillis());
268: }
269:
270: /**
271: * Hash up name, value and domain into new hash.
272: *
273: * @return the hashcode of this class
274: */
275: public int hashCode() {
276: return (this .getName().hashCode() + this .getValue().hashCode() + this
277: .getDomain().hashCode());
278: }
279:
280: /**
281: * Two cookies match if the name, path and domain match.
282: *
283: * @param theObject the cookie object to match
284: * @return true of the object passed as paramater is equal to this coookie
285: * instance
286: */
287: public boolean equals(Object theObject) {
288: if ((theObject != null) && (theObject instanceof Cookie)) {
289: Cookie other = (Cookie) theObject;
290:
291: return (this .getName().equals(other.getName())
292: && this .getPath().equals(other.getPath()) && this
293: .getDomain().equals(other.getDomain()));
294: }
295:
296: return false;
297: }
298:
299: /**
300: * @return a string representation of the cookie
301: */
302: public String toString() {
303: StringBuffer buffer = new StringBuffer();
304:
305: buffer.append("name = [" + getName() + "], ");
306: buffer.append("value = [" + getValue() + "], ");
307: buffer.append("domain = [" + getDomain() + "], ");
308: buffer.append("path = [" + getPath() + "], ");
309: buffer.append("isSecure = [" + isSecure() + "], ");
310: buffer.append("comment = [" + getComment() + "], ");
311: buffer.append("expiryDate = [" + getExpiryDate() + "]");
312:
313: return buffer.toString();
314: }
315:
316: /**
317: * @see CookieUtil#getCookieDomain(WebRequest, String)
318: * @deprecated use {@link CookieUtil#getCookieDomain(WebRequest, String)}
319: */
320: public static String getCookieDomain(WebRequest theRequest,
321: String theRealHost) {
322: return CookieUtil.getCookieDomain(theRequest, theRealHost);
323: }
324:
325: /**
326: * @see CookieUtil#getCookiePort(WebRequest, int)
327: * @deprecated use {@link CookieUtil#getCookiePort(WebRequest, int)}
328: */
329: public static int getCookiePort(WebRequest theRequest,
330: int theRealPort) {
331: return CookieUtil.getCookiePort(theRequest, theRealPort);
332: }
333:
334: /**
335: * @see CookieUtil#getCookiePath(WebRequest, String)
336: * @deprecated use {@link CookieUtil#getCookiePath(WebRequest, String)}
337: */
338: public static String getCookiePath(WebRequest theRequest,
339: String theRealPath) {
340: return CookieUtil.getCookiePath(theRequest, theRealPath);
341: }
342: }
|