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