001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.cookie;
029
030import java.io.Serializable;
031import java.util.Date;
032import java.util.HashMap;
033import java.util.Locale;
034import java.util.Map;
035
036import org.apache.http.cookie.ClientCookie;
037import org.apache.http.cookie.SetCookie;
038import org.apache.http.util.Args;
039
040/**
041 * Default implementation of {@link SetCookie}.
042 *
043 * @since 4.0
044 */
045public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable {
046
047    private static final long serialVersionUID = -3869795591041535538L;
048
049    /**
050     * Default Constructor taking a name and a value. The value may be null.
051     *
052     * @param name The name.
053     * @param value The value.
054     */
055    public BasicClientCookie(final String name, final String value) {
056        super();
057        Args.notNull(name, "Name");
058        this.name = name;
059        this.attribs = new HashMap<String, String>();
060        this.value = value;
061    }
062
063    /**
064     * Returns the name.
065     *
066     * @return String name The name
067     */
068    @Override
069    public String getName() {
070        return this.name;
071    }
072
073    /**
074     * Returns the value.
075     *
076     * @return String value The current value.
077     */
078    @Override
079    public String getValue() {
080        return this.value;
081    }
082
083    /**
084     * Sets the value
085     *
086     * @param value
087     */
088    @Override
089    public void setValue(final String value) {
090        this.value = value;
091    }
092
093    /**
094     * Returns the comment describing the purpose of this cookie, or
095     * {@code null} if no such comment has been defined.
096     *
097     * @return comment
098     *
099     * @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 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