1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/BasicClientCookie.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 659191 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-22 11:26:53 -0700 (Thu, 22 May 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements.  See the NOTICE file
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership.  The ASF licenses this file
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *   http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing,
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied.  See the License for the
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License.
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.cookie;
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Date;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.HashMap;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Locale;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Map;
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.cookie.ClientCookie;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.cookie.SetCookie;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * HTTP "magic-cookie" represents a piece of state information
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * that the HTTP agent and the target server can exchange to maintain
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * a session.
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author B.C. Holmes
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Rod Waldhoff
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author dIon Gillard
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Sean C. Sullivan
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Marc A. Saegesser
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Revision: 659191 $
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class BasicClientCookie implements SetCookie, ClientCookie, Cloneable {
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Default Constructor taking a name and a value. The value may be null.
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param name The name.
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value The value.
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public BasicClientCookie(final String name, final String value) {
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (name == null) {
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("Name may not be null");
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.name = name;
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.attribs = new HashMap<String, String>();
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.value = value;
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the name.
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return String name The name
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getName() {
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.name;
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the value.
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return String value The current value.
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getValue() {
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.value;
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets the value
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setValue(final String value) {
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.value = value;
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the comment describing the purpose of this cookie, or
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <tt>null</tt> if no such comment has been defined.
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return comment
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #setComment(String)
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getComment() {
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return cookieComment;
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If a user agent (web browser) presents this cookie to a user, the
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * cookie's purpose will be described using this comment.
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param comment
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #getComment()
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setComment(String comment) {
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        cookieComment = comment;
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns null. Cookies prior to RFC2965 do not set this attribute
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getCommentURL() {
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return null;
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the expiration {@link Date} of the cookie, or <tt>null</tt>
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * if none exists.
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <p><strong>Note:</strong> the object returned by this method is
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * considered immutable. Changing it (e.g. using setTime()) could result
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * in undefined behaviour. Do so at your peril. </p>
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return Expiration {@link Date}, or <tt>null</tt>.
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #setExpiryDate(java.util.Date)
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Date getExpiryDate() {
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return cookieExpiryDate;
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets expiration date.
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <p><strong>Note:</strong> the object returned by this method is considered
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * immutable. Changing it (e.g. using setTime()) could result in undefined
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * behaviour. Do so at your peril.</p>
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param expiryDate the {@link Date} after which this cookie is no longer valid.
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #getExpiryDate
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setExpiryDate (Date expiryDate) {
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        cookieExpiryDate = expiryDate;
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns <tt>false</tt> if the cookie should be discarded at the end
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * of the "session"; <tt>true</tt> otherwise.
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return <tt>false</tt> if the cookie should be discarded at the end
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *         of the "session"; <tt>true</tt> otherwise
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean isPersistent() {
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (null != cookieExpiryDate);
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns domain attribute of the cookie.
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the value of the domain attribute
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #setDomain(java.lang.String)
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getDomain() {
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return cookieDomain;
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets the domain attribute.
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param domain The value of the domain attribute
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #getDomain
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setDomain(String domain) {
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (domain != null) {
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            cookieDomain = domain.toLowerCase(Locale.ENGLISH);
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            cookieDomain = null;
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the path attribute of the cookie
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return The value of the path attribute.
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #setPath(java.lang.String)
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getPath() {
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return cookiePath;
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets the path attribute.
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param path The value of the path attribute
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #getPath
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setPath(String path) {
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        cookiePath = path;
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return <code>true</code> if this cookie should only be sent over secure connections.
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #setSecure(boolean)
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean isSecure() {
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return isSecure;
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets the secure attribute of the cookie.
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <p>
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * When <tt>true</tt> the cookie should only be sent
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * using a secure protocol (https).  This should only be set when
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * the cookie's originating server used a secure protocol to set the
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * cookie's value.
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param secure The value of the secure attribute
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #isSecure()
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setSecure (boolean secure) {
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        isSecure = secure;
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns null. Cookies prior to RFC2965 do not set this attribute
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int[] getPorts() {
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return null;
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the version of the cookie specification to which this
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * cookie conforms.
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the version of the cookie.
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #setVersion(int)
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int getVersion() {
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return cookieVersion;
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets the version of the cookie specification to which this
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * cookie conforms.
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param version the version of the cookie.
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #getVersion
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setVersion(int version) {
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        cookieVersion = version;
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns true if this cookie has expired.
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param date Current time
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
293069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return <tt>true</tt> if the cookie has expired.
294069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
295069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean isExpired(final Date date) {
296069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (date == null) {
297069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("Date may not be null");
298069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
299069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (cookieExpiryDate != null
300069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            && cookieExpiryDate.getTime() <= date.getTime());
301069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
302069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
303069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setAttribute(final String name, final String value) {
304069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.attribs.put(name, value);
305069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
306069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
307069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getAttribute(final String name) {
308069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.attribs.get(name);
309069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
310069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
311069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean containsAttribute(final String name) {
312069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.attribs.get(name) != null;
313069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
314069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
315069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
316069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object clone() throws CloneNotSupportedException {
317069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        BasicClientCookie clone = (BasicClientCookie) super.clone();
318069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        clone.attribs = new HashMap<String, String>(this.attribs);
319069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return clone;
320069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
321069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
322069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
323069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String toString() {
324069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        StringBuilder buffer = new StringBuilder();
325069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("[version: ");
326069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(Integer.toString(this.cookieVersion));
327069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("]");
328069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("[name: ");
329069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.name);
330069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("]");
331069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("[value: ");
332069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.value);
333069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("]");
334069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("[domain: ");
335069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.cookieDomain);
336069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("]");
337069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("[path: ");
338069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.cookiePath);
339069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("]");
340069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("[expiry: ");
341069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.cookieExpiryDate);
342069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("]");
343069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return buffer.toString();
344069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
345069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
346069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project   // ----------------------------------------------------- Instance Variables
347069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
348069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Cookie name */
349069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final String name;
350069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
351069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Cookie attributes as specified by the origin server */
352069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private Map<String, String> attribs;
353069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
354069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Cookie value */
355069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private String value;
356069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
357069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Comment attribute. */
358069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private String  cookieComment;
359069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
360069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Domain attribute. */
361069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private String  cookieDomain;
362069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
363069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Expiration {@link Date}. */
364069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private Date cookieExpiryDate;
365069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
366069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Path attribute. */
367069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private String cookiePath;
368069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
369069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** My secure flag. */
370069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private boolean isSecure;
371069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
372069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The version of the cookie specification I was created from. */
373069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private int cookieVersion;
374069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
375069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
376069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
377