View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/cookie/Cookie2.java $
3    * $Revision:400312 $
4    * $Date:2006-05-06 14:49:41 +0200 (Sat, 06 May 2006) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient.cookie;
32  
33  import java.util.Date;
34  
35  import org.apache.commons.httpclient.Cookie;
36  
37  /***
38   * <p>
39   * Cookie class for {@link org.apache.commons.httpclient.cookie.RFC2965Spec}
40   * cookie specification. It extends {@link Cookie} class and adds newer cookie
41   * attributes and functions required for this specification.
42   * </p>
43   *
44   * @author Samit Jain (jain.samit@gmail.com)
45   * 
46   * @since 3.1
47   */
48  public class Cookie2 extends Cookie {
49  
50      // string constants for cookie attributes
51      public static final String DOMAIN = "domain";
52      public static final String PATH = "path";
53      public static final String PORT = "port";
54      public static final String VERSION = "version";
55      public static final String SECURE = "secure";
56      public static final String MAXAGE = "max-age";
57      public static final String COMMENT = "comment";
58      public static final String COMMENTURL = "commenturl";
59      public static final String DISCARD = "discard";
60  
61      /***
62       * Default constructor. Creates a blank cookie
63       */
64      public Cookie2() {
65          super(null, "noname", null, null, null, false);
66      }
67  
68      /***
69       * Creates a cookie with the given name, value and domain attribute.
70       *
71       * @param name    the cookie name
72       * @param value   the cookie value
73       * @param domain  the domain this cookie can be sent to
74       */
75      public Cookie2(String domain, String name, String value) {
76          super(domain, name, value);
77      }
78  
79      /***
80       * Creates a cookie with the given name, value, domain attribute,
81       * path attribute, expiration attribute, and secure attribute
82       *
83       * @param name    the cookie name
84       * @param value   the cookie value
85       * @param domain  the domain this cookie can be sent to
86       * @param path    the path prefix for which this cookie can be sent
87       * @param expires the {@link Date} at which this cookie expires,
88       *                or <tt>null</tt> if the cookie expires at the end
89       *                of the session
90       * @param secure if true this cookie can only be sent over secure
91       * connections
92       * @throws IllegalArgumentException If cookie name is null or blank,
93       *   cookie name contains a blank, or cookie name starts with character $
94       *
95       */
96      public Cookie2(String domain, String name, String value,
97                     String path, Date expires, boolean secure) {
98          super(domain, name, value, path, expires, secure);
99      }
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 =
272                 CookiePolicy.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 
309 }
310