1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/BasicClientCookie.java $
3  * $Revision: 659191 $
4  * $Date: 2008-05-22 11:26:53 -0700 (Thu, 22 May 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with 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,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.impl.cookie;
33 
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.Locale;
37 import java.util.Map;
38 
39 import org.apache.http.cookie.ClientCookie;
40 import org.apache.http.cookie.SetCookie;
41 
42 /**
43  * HTTP "magic-cookie" represents a piece of state information
44  * that the HTTP agent and the target server can exchange to maintain
45  * a session.
46  *
47  * @author B.C. Holmes
48  * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
49  * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
50  * @author Rod Waldhoff
51  * @author dIon Gillard
52  * @author Sean C. Sullivan
53  * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
54  * @author Marc A. Saegesser
55  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
56  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
57  *
58  * @version $Revision: 659191 $
59  *
60  * @deprecated Please use {@link java.net.URL#openConnection} instead.
61  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
62  *     for further details.
63  */
64 @Deprecated
65 public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable {
66 
67     /**
68      * Default Constructor taking a name and a value. The value may be null.
69      *
70      * @param name The name.
71      * @param value The value.
72      */
BasicClientCookie(final String name, final String value)73     public BasicClientCookie(final String name, final String value) {
74         super();
75         if (name == null) {
76             throw new IllegalArgumentException("Name may not be null");
77         }
78         this.name = name;
79         this.attribs = new HashMap<String, String>();
80         this.value = value;
81     }
82 
83     /**
84      * Returns the name.
85      *
86      * @return String name The name
87      */
getName()88     public String getName() {
89         return this.name;
90     }
91 
92     /**
93      * Returns the value.
94      *
95      * @return String value The current value.
96      */
getValue()97     public String getValue() {
98         return this.value;
99     }
100 
101     /**
102      * Sets the value
103      *
104      * @param value
105      */
setValue(final String value)106     public void setValue(final String value) {
107         this.value = value;
108     }
109 
110     /**
111      * Returns the comment describing the purpose of this cookie, or
112      * <tt>null</tt> if no such comment has been defined.
113      *
114      * @return comment
115      *
116      * @see #setComment(String)
117      */
getComment()118     public String getComment() {
119         return cookieComment;
120     }
121 
122     /**
123      * If a user agent (web browser) presents this cookie to a user, the
124      * cookie's purpose will be described using this comment.
125      *
126      * @param comment
127      *
128      * @see #getComment()
129      */
setComment(String comment)130     public void setComment(String comment) {
131         cookieComment = comment;
132     }
133 
134 
135     /**
136      * Returns null. Cookies prior to RFC2965 do not set this attribute
137      */
getCommentURL()138     public String getCommentURL() {
139         return null;
140     }
141 
142 
143     /**
144      * Returns the expiration {@link Date} of the cookie, or <tt>null</tt>
145      * if none exists.
146      * <p><strong>Note:</strong> the object returned by this method is
147      * considered immutable. Changing it (e.g. using setTime()) could result
148      * in undefined behaviour. Do so at your peril. </p>
149      * @return Expiration {@link Date}, or <tt>null</tt>.
150      *
151      * @see #setExpiryDate(java.util.Date)
152      *
153      */
getExpiryDate()154     public Date getExpiryDate() {
155         return cookieExpiryDate;
156     }
157 
158     /**
159      * Sets expiration date.
160      * <p><strong>Note:</strong> the object returned by this method is considered
161      * immutable. Changing it (e.g. using setTime()) could result in undefined
162      * behaviour. Do so at your peril.</p>
163      *
164      * @param expiryDate the {@link Date} after which this cookie is no longer valid.
165      *
166      * @see #getExpiryDate
167      *
168      */
setExpiryDate(Date expiryDate)169     public void setExpiryDate (Date expiryDate) {
170         cookieExpiryDate = expiryDate;
171     }
172 
173 
174     /**
175      * Returns <tt>false</tt> if the cookie should be discarded at the end
176      * of the "session"; <tt>true</tt> otherwise.
177      *
178      * @return <tt>false</tt> if the cookie should be discarded at the end
179      *         of the "session"; <tt>true</tt> otherwise
180      */
isPersistent()181     public boolean isPersistent() {
182         return (null != cookieExpiryDate);
183     }
184 
185 
186     /**
187      * Returns domain attribute of the cookie.
188      *
189      * @return the value of the domain attribute
190      *
191      * @see #setDomain(java.lang.String)
192      */
getDomain()193     public String getDomain() {
194         return cookieDomain;
195     }
196 
197     /**
198      * Sets the domain attribute.
199      *
200      * @param domain The value of the domain attribute
201      *
202      * @see #getDomain
203      */
setDomain(String domain)204     public void setDomain(String domain) {
205         if (domain != null) {
206             cookieDomain = domain.toLowerCase(Locale.ENGLISH);
207         } else {
208             cookieDomain = null;
209         }
210     }
211 
212 
213     /**
214      * Returns the path attribute of the cookie
215      *
216      * @return The value of the path attribute.
217      *
218      * @see #setPath(java.lang.String)
219      */
getPath()220     public String getPath() {
221         return cookiePath;
222     }
223 
224     /**
225      * Sets the path attribute.
226      *
227      * @param path The value of the path attribute
228      *
229      * @see #getPath
230      *
231      */
setPath(String path)232     public void setPath(String path) {
233         cookiePath = path;
234     }
235 
236     /**
237      * @return <code>true</code> if this cookie should only be sent over secure connections.
238      * @see #setSecure(boolean)
239      */
isSecure()240     public boolean isSecure() {
241         return isSecure;
242     }
243 
244     /**
245      * Sets the secure attribute of the cookie.
246      * <p>
247      * When <tt>true</tt> the cookie should only be sent
248      * using a secure protocol (https).  This should only be set when
249      * the cookie's originating server used a secure protocol to set the
250      * cookie's value.
251      *
252      * @param secure The value of the secure attribute
253      *
254      * @see #isSecure()
255      */
setSecure(boolean secure)256     public void setSecure (boolean secure) {
257         isSecure = secure;
258     }
259 
260 
261     /**
262      * Returns null. Cookies prior to RFC2965 do not set this attribute
263      */
getPorts()264     public int[] getPorts() {
265         return null;
266     }
267 
268 
269     /**
270      * Returns the version of the cookie specification to which this
271      * cookie conforms.
272      *
273      * @return the version of the cookie.
274      *
275      * @see #setVersion(int)
276      *
277      */
getVersion()278     public int getVersion() {
279         return cookieVersion;
280     }
281 
282     /**
283      * Sets the version of the cookie specification to which this
284      * cookie conforms.
285      *
286      * @param version the version of the cookie.
287      *
288      * @see #getVersion
289      */
setVersion(int version)290     public void setVersion(int version) {
291         cookieVersion = version;
292     }
293 
294     /**
295      * Returns true if this cookie has expired.
296      * @param date Current time
297      *
298      * @return <tt>true</tt> if the cookie has expired.
299      */
isExpired(final Date date)300     public boolean isExpired(final Date date) {
301         if (date == null) {
302             throw new IllegalArgumentException("Date may not be null");
303         }
304         return (cookieExpiryDate != null
305             && cookieExpiryDate.getTime() <= date.getTime());
306     }
307 
setAttribute(final String name, final String value)308     public void setAttribute(final String name, final String value) {
309         this.attribs.put(name, value);
310     }
311 
getAttribute(final String name)312     public String getAttribute(final String name) {
313         return this.attribs.get(name);
314     }
315 
containsAttribute(final String name)316     public boolean containsAttribute(final String name) {
317         return this.attribs.get(name) != null;
318     }
319 
320     @Override
clone()321     public Object clone() throws CloneNotSupportedException {
322         BasicClientCookie clone = (BasicClientCookie) super.clone();
323         clone.attribs = new HashMap<String, String>(this.attribs);
324         return clone;
325     }
326 
327     @Override
toString()328     public String toString() {
329         StringBuilder buffer = new StringBuilder();
330         buffer.append("[version: ");
331         buffer.append(Integer.toString(this.cookieVersion));
332         buffer.append("]");
333         buffer.append("[name: ");
334         buffer.append(this.name);
335         buffer.append("]");
336         buffer.append("[value: ");
337         buffer.append(this.value);
338         buffer.append("]");
339         buffer.append("[domain: ");
340         buffer.append(this.cookieDomain);
341         buffer.append("]");
342         buffer.append("[path: ");
343         buffer.append(this.cookiePath);
344         buffer.append("]");
345         buffer.append("[expiry: ");
346         buffer.append(this.cookieExpiryDate);
347         buffer.append("]");
348         return buffer.toString();
349     }
350 
351    // ----------------------------------------------------- Instance Variables
352 
353     /** Cookie name */
354     private final String name;
355 
356     /** Cookie attributes as specified by the origin server */
357     private Map<String, String> attribs;
358 
359     /** Cookie value */
360     private String value;
361 
362     /** Comment attribute. */
363     private String  cookieComment;
364 
365     /** Domain attribute. */
366     private String  cookieDomain;
367 
368     /** Expiration {@link Date}. */
369     private Date cookieExpiryDate;
370 
371     /** Path attribute. */
372     private String cookiePath;
373 
374     /** My secure flag. */
375     private boolean isSecure;
376 
377     /** The version of the cookie specification I was created from. */
378     private int cookieVersion;
379 
380 }
381 
382