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.annotation.NotThreadSafe;
37  import org.apache.http.cookie.ClientCookie;
38  import org.apache.http.cookie.SetCookie;
39  import org.apache.http.util.Args;
40  
41  /**
42   * Default implementation of {@link SetCookie}.
43   *
44   * @since 4.0
45   */
46  @NotThreadSafe
47  public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable {
48  
49      private static final long serialVersionUID = -3869795591041535538L;
50  
51      /**
52       * Default Constructor taking a name and a value. The value may be null.
53       *
54       * @param name The name.
55       * @param value The value.
56       */
57      public BasicClientCookie(final String name, final String value) {
58          super();
59          Args.notNull(name, "Name");
60          this.name = name;
61          this.attribs = new HashMap<String, String>();
62          this.value = value;
63      }
64  
65      /**
66       * Returns the name.
67       *
68       * @return String name The name
69       */
70      @Override
71      public String getName() {
72          return this.name;
73      }
74  
75      /**
76       * Returns the value.
77       *
78       * @return String value The current value.
79       */
80      @Override
81      public String getValue() {
82          return this.value;
83      }
84  
85      /**
86       * Sets the value
87       *
88       * @param value
89       */
90      @Override
91      public void setValue(final String value) {
92          this.value = value;
93      }
94  
95      /**
96       * Returns the comment describing the purpose of this cookie, or
97       * {@code null} if no such comment has been defined.
98       *
99       * @return comment
100      *
101      * @see #setComment(String)
102      */
103     @Override
104     public String getComment() {
105         return cookieComment;
106     }
107 
108     /**
109      * If a user agent (web browser) presents this cookie to a user, the
110      * cookie's purpose will be described using this comment.
111      *
112      * @param comment
113      *
114      * @see #getComment()
115      */
116     @Override
117     public void setComment(final String comment) {
118         cookieComment = comment;
119     }
120 
121 
122     /**
123      * Returns null. Cookies prior to RFC2965 do not set this attribute
124      */
125     @Override
126     public String getCommentURL() {
127         return null;
128     }
129 
130 
131     /**
132      * Returns the expiration {@link Date} of the cookie, or {@code null}
133      * if none exists.
134      * <p><strong>Note:</strong> the object returned by this method is
135      * considered immutable. Changing it (e.g. using setTime()) could result
136      * in undefined behaviour. Do so at your peril. </p>
137      * @return Expiration {@link Date}, or {@code null}.
138      *
139      * @see #setExpiryDate(java.util.Date)
140      *
141      */
142     @Override
143     public Date getExpiryDate() {
144         return cookieExpiryDate;
145     }
146 
147     /**
148      * Sets expiration date.
149      * <p><strong>Note:</strong> the object returned by this method is considered
150      * immutable. Changing it (e.g. using setTime()) could result in undefined
151      * behaviour. Do so at your peril.</p>
152      *
153      * @param expiryDate the {@link Date} after which this cookie is no longer valid.
154      *
155      * @see #getExpiryDate
156      *
157      */
158     @Override
159     public void setExpiryDate (final Date expiryDate) {
160         cookieExpiryDate = expiryDate;
161     }
162 
163 
164     /**
165      * Returns {@code false} if the cookie should be discarded at the end
166      * of the "session"; {@code true} otherwise.
167      *
168      * @return {@code false} if the cookie should be discarded at the end
169      *         of the "session"; {@code true} otherwise
170      */
171     @Override
172     public boolean isPersistent() {
173         return (null != cookieExpiryDate);
174     }
175 
176 
177     /**
178      * Returns domain attribute of the cookie.
179      *
180      * @return the value of the domain attribute
181      *
182      * @see #setDomain(java.lang.String)
183      */
184     @Override
185     public String getDomain() {
186         return cookieDomain;
187     }
188 
189     /**
190      * Sets the domain attribute.
191      *
192      * @param domain The value of the domain attribute
193      *
194      * @see #getDomain
195      */
196     @Override
197     public void setDomain(final String domain) {
198         if (domain != null) {
199             cookieDomain = domain.toLowerCase(Locale.ROOT);
200         } else {
201             cookieDomain = null;
202         }
203     }
204 
205 
206     /**
207      * Returns the path attribute of the cookie
208      *
209      * @return The value of the path attribute.
210      *
211      * @see #setPath(java.lang.String)
212      */
213     @Override
214     public String getPath() {
215         return cookiePath;
216     }
217 
218     /**
219      * Sets the path attribute.
220      *
221      * @param path The value of the path attribute
222      *
223      * @see #getPath
224      *
225      */
226     @Override
227     public void setPath(final String path) {
228         cookiePath = path;
229     }
230 
231     /**
232      * @return {@code true} if this cookie should only be sent over secure connections.
233      * @see #setSecure(boolean)
234      */
235     @Override
236     public boolean isSecure() {
237         return isSecure;
238     }
239 
240     /**
241      * Sets the secure attribute of the cookie.
242      * <p>
243      * When {@code true} the cookie should only be sent
244      * using a secure protocol (https).  This should only be set when
245      * the cookie's originating server used a secure protocol to set the
246      * cookie's value.
247      *
248      * @param secure The value of the secure attribute
249      *
250      * @see #isSecure()
251      */
252     @Override
253     public void setSecure (final boolean secure) {
254         isSecure = secure;
255     }
256 
257 
258     /**
259      * Returns null. Cookies prior to RFC2965 do not set this attribute
260      */
261     @Override
262     public int[] getPorts() {
263         return null;
264     }
265 
266 
267     /**
268      * Returns the version of the cookie specification to which this
269      * cookie conforms.
270      *
271      * @return the version of the cookie.
272      *
273      * @see #setVersion(int)
274      *
275      */
276     @Override
277     public int getVersion() {
278         return cookieVersion;
279     }
280 
281     /**
282      * Sets the version of the cookie specification to which this
283      * cookie conforms.
284      *
285      * @param version the version of the cookie.
286      *
287      * @see #getVersion
288      */
289     @Override
290     public void setVersion(final int version) {
291         cookieVersion = version;
292     }
293 
294     /**
295      * Returns true if this cookie has expired.
296      * @param date Current time
297      *
298      * @return {@code true} if the cookie has expired.
299      */
300     @Override
301     public boolean isExpired(final Date date) {
302         Args.notNull(date, "Date");
303         return (cookieExpiryDate != null
304             && cookieExpiryDate.getTime() <= date.getTime());
305     }
306 
307     public void setAttribute(final String name, final String value) {
308         this.attribs.put(name, value);
309     }
310 
311     @Override
312     public String getAttribute(final String name) {
313         return this.attribs.get(name);
314     }
315 
316     @Override
317     public boolean containsAttribute(final String name) {
318         return this.attribs.get(name) != null;
319     }
320 
321     @Override
322     public Object clone() throws CloneNotSupportedException {
323         final BasicClientCookie clone = (BasicClientCookie) super.clone();
324         clone.attribs = new HashMap<String, String>(this.attribs);
325         return clone;
326     }
327 
328     @Override
329     public String toString() {
330         final StringBuilder buffer = new StringBuilder();
331         buffer.append("[version: ");
332         buffer.append(Integer.toString(this.cookieVersion));
333         buffer.append("]");
334         buffer.append("[name: ");
335         buffer.append(this.name);
336         buffer.append("]");
337         buffer.append("[value: ");
338         buffer.append(this.value);
339         buffer.append("]");
340         buffer.append("[domain: ");
341         buffer.append(this.cookieDomain);
342         buffer.append("]");
343         buffer.append("[path: ");
344         buffer.append(this.cookiePath);
345         buffer.append("]");
346         buffer.append("[expiry: ");
347         buffer.append(this.cookieExpiryDate);
348         buffer.append("]");
349         return buffer.toString();
350     }
351 
352    // ----------------------------------------------------- Instance Variables
353 
354     /** Cookie name */
355     private final String name;
356 
357     /** Cookie attributes as specified by the origin server */
358     private Map<String, String> attribs;
359 
360     /** Cookie value */
361     private String value;
362 
363     /** Comment attribute. */
364     private String  cookieComment;
365 
366     /** Domain attribute. */
367     private String  cookieDomain;
368 
369     /** Expiration {@link Date}. */
370     private Date cookieExpiryDate;
371 
372     /** Path attribute. */
373     private String cookiePath;
374 
375     /** My secure flag. */
376     private boolean isSecure;
377 
378     /** The version of the cookie specification I was created from. */
379     private int cookieVersion;
380 
381 }
382