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