001: /*
002: * $Header: /cvsroot/httpc-cookie2/httpc-cookie2/httpcookie2SVN-patch.082805-2100.diff,v 1.1 2005/08/29 05:01:58 sjain700 Exp $
003: * $Revision:400312 $
004: * $Date:2006-05-06 14:49:41 +0200 (Sat, 06 May 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.cookie;
032:
033: import java.util.Date;
034:
035: import org.apache.commons.httpclient.Cookie;
036:
037: /**
038: * <p>
039: * Cookie class for {@link org.apache.commons.httpclient.cookie.RFC2965Spec}
040: * cookie specification. It extends {@link Cookie} class and adds newer cookie
041: * attributes and functions required for this specification.
042: * </p>
043: *
044: * @author Samit Jain (jain.samit@gmail.com)
045: *
046: * @since 3.1
047: */
048: public class Cookie2 extends Cookie {
049:
050: // string constants for cookie attributes
051: public static final String DOMAIN = "domain";
052: public static final String PATH = "path";
053: public static final String PORT = "port";
054: public static final String VERSION = "version";
055: public static final String SECURE = "secure";
056: public static final String MAXAGE = "max-age";
057: public static final String COMMENT = "comment";
058: public static final String COMMENTURL = "commenturl";
059: public static final String DISCARD = "discard";
060:
061: /**
062: * Default constructor. Creates a blank cookie
063: */
064: public Cookie2() {
065: super (null, "noname", null, null, null, false);
066: }
067:
068: /**
069: * Creates a cookie with the given name, value and domain attribute.
070: *
071: * @param name the cookie name
072: * @param value the cookie value
073: * @param domain the domain this cookie can be sent to
074: */
075: public Cookie2(String domain, String name, String value) {
076: super (domain, name, value);
077: }
078:
079: /**
080: * Creates a cookie with the given name, value, domain attribute,
081: * path attribute, expiration attribute, and secure attribute
082: *
083: * @param name the cookie name
084: * @param value the cookie value
085: * @param domain the domain this cookie can be sent to
086: * @param path the path prefix for which this cookie can be sent
087: * @param expires the {@link Date} at which this cookie expires,
088: * or <tt>null</tt> if the cookie expires at the end
089: * of the session
090: * @param secure if true this cookie can only be sent over secure
091: * connections
092: * @throws IllegalArgumentException If cookie name is null or blank,
093: * cookie name contains a blank, or cookie name starts with character $
094: *
095: */
096: public Cookie2(String domain, String name, String value,
097: String path, Date expires, boolean secure) {
098: super (domain, name, value, path, expires, secure);
099: }
100:
101: /**
102: * Creates a cookie with the given name, value, domain attribute,
103: * path attribute, expiration attribute, secure attribute, and ports
104: * attribute.
105: *
106: * @param name the cookie name
107: * @param value the cookie value
108: * @param domain the domain this cookie can be sent to
109: * @param path the path prefix for which this cookie can be sent
110: * @param expires the {@link Date} at which this cookie expires,
111: * or <tt>null</tt> if the cookie expires at the end
112: * of the session
113: * @param secure if true this cookie can only be sent over secure
114: * connections
115: * @param ports the ports for which this cookie can be sent
116: * @throws IllegalArgumentException If cookie name is null or blank,
117: * cookie name contains a blank, or cookie name starts with character $
118: *
119: */
120: public Cookie2(String domain, String name, String value,
121: String path, Date expires, boolean secure, int[] ports) {
122: super (domain, name, value, path, expires, secure);
123: setPorts(ports);
124: }
125:
126: /**
127: * If a user agent (web browser) presents this cookie to a user, the
128: * cookie's purpose will be described by the information at this URL.
129: *
130: * @see #setCommentURL(String)
131: */
132: public String getCommentURL() {
133: return cookieCommentURL;
134: }
135:
136: /**
137: * If a user agent (web browser) presents this cookie to a user, the
138: * cookie's purpose will be described by the information at this URL.
139: *
140: * @param commentURL
141: *
142: * @see #getCommentURL()
143: */
144: public void setCommentURL(String commentURL) {
145: this .cookieCommentURL = commentURL;
146: }
147:
148: /**
149: * Get the Port attribute. It restricts the ports to which a cookie
150: * may be returned in a Cookie request header.
151: *
152: * @see #setPorts(int[])
153: */
154: public int[] getPorts() {
155: return cookiePorts;
156: }
157:
158: /**
159: * Set the Port attribute. It restricts the ports to which a cookie
160: * may be returned in a Cookie request header.
161: *
162: * @param ports
163: *
164: * @see #getPorts()
165: */
166: public void setPorts(int[] ports) {
167: this .cookiePorts = ports;
168: }
169:
170: /**
171: * Set the Discard attribute.
172: *
173: * Note: <tt>Discard</tt> attribute overrides <tt>Max-age</tt>.
174: *
175: * @see #isPersistent()
176: */
177: public void setDiscard(boolean toDiscard) {
178: discard = toDiscard;
179: }
180:
181: /**
182: * Returns <tt>false</tt> if the cookie should be discarded at the end
183: * of the "session"; <tt>true</tt> otherwise.
184: *
185: * @return <tt>false</tt> if the cookie should be discarded at the end
186: * of the "session"; <tt>true</tt> otherwise
187: */
188: public boolean isPersistent() {
189: return (null != getExpiryDate()) && !discard;
190: }
191:
192: /**
193: * Indicates whether the cookie had a port attribute specified in the
194: * <tt>Set-Cookie2</tt> response header.
195: *
196: * @param value <tt>true</tt> if port attribute is specified in response
197: * header.
198: *
199: * @see #isPortAttributeSpecified
200: */
201: public void setPortAttributeSpecified(boolean value) {
202: hasPortAttribute = value;
203: }
204:
205: /**
206: * @return <tt>true</tt> if cookie port attribute was specified in the
207: * <tt>Set-Cookie2</tt> header.
208: *
209: * @see #setPortAttributeSpecified
210: */
211: public boolean isPortAttributeSpecified() {
212: return hasPortAttribute;
213: }
214:
215: /**
216: * Indicates whether the Port attribute in <tt>Set-Cookie2</tt> header
217: * contains no value (is of the form Port="").
218: * <p>
219: * This value is required for generating
220: * the <tt>Cookie</tt> request header because the specification requires that if
221: * <tt>Set-Cookie2</tt> header contains a blank value for port attribute,
222: * the <tt>Cookie</tt> header should also contain a port attribute with no value.
223: *
224: * @param value <tt>true</tt> if port attribute is specified as blank in response
225: * header.
226: *
227: * @see #isPortAttributeBlank
228: */
229: public void setPortAttributeBlank(boolean value) {
230: isPortAttributeBlank = value;
231: }
232:
233: /**
234: * @return <tt>true</tt> if the port attribute in <tt>Set-Cookie2</tt> header
235: * had no value (was of the form Port="").
236: *
237: * @see #setPortAttributeBlank
238: */
239: public boolean isPortAttributeBlank() {
240: return isPortAttributeBlank;
241: }
242:
243: /**
244: * Indicates whether the cookie had a version attribute specified in the
245: * <tt>Set-Cookie2</tt> response header.
246: *
247: * @param value <tt>true</tt> if version attribute is specified in response
248: * header.
249: * @see #isVersionAttributeSpecified()
250: */
251: public void setVersionAttributeSpecified(boolean value) {
252: hasVersionAttribute = value;
253: }
254:
255: /**
256: * @return <tt>true</tt> if cookie version attribute was specified in the
257: * <tt>Set-Cookie2</tt> header.
258: *
259: * @see #setVersionAttributeSpecified
260: */
261: public boolean isVersionAttributeSpecified() {
262: return hasVersionAttribute;
263: }
264:
265: /**
266: * Return a textual representation of the cookie.
267: *
268: * @return string.
269: */
270: public String toExternalForm() {
271: CookieSpec spec = CookiePolicy
272: .getCookieSpec(CookiePolicy.RFC_2965);
273: return spec.formatCookie(this );
274: }
275:
276: /**
277: * Comment URL attribute
278: */
279: private String cookieCommentURL;
280:
281: /**
282: * Port attribute.
283: */
284: private int[] cookiePorts;
285:
286: /**
287: * Discard attribute.
288: */
289: private boolean discard = false;
290:
291: /**
292: * Indicates if the set-cookie2 header included a Port attribute for this
293: * cookie
294: */
295: private boolean hasPortAttribute = false;
296:
297: /**
298: * Indicates if the set-cookie2 header's Port attribute did not have
299: * any value.
300: */
301: private boolean isPortAttributeBlank = false;
302:
303: /**
304: * Indicates if the set-cookie2 header included a Version attribute
305: */
306: private boolean hasVersionAttribute = false;
307:
308: }
|