mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/BasicClientCookie.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial