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