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