mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/BasicCookieStore.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * ====================================================================
     3  *
     4  *  Licensed to the Apache Software Foundation (ASF) under one or more
     5  *  contributor license agreements.  See the NOTICE file distributed with
     6  *  this work for additional information regarding copyright ownership.
     7  *  The ASF licenses this file to You under the Apache License, Version 2.0
     8  *  (the "License"); you may not use this file except in compliance with
     9  *  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, software
    14  *  distributed under the License is distributed on an "AS IS" BASIS,
    15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  *  See the License for the specific language governing permissions and
    17  *  limitations under the License.
    18  * ====================================================================
    19  *
    20  * This software consists of voluntary contributions made by many
    21  * individuals on behalf of the Apache Software Foundation.  For more
    22  * information on the Apache Software Foundation, please see
    23  * <http://www.apache.org/>.
    24  *
    25  */
    27 package ch.boye.httpclientandroidlib.impl.client;
    29 import java.io.Serializable;
    30 import java.util.*;
    32 import ch.boye.httpclientandroidlib.annotation.GuardedBy;
    33 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
    35 import ch.boye.httpclientandroidlib.client.CookieStore;
    36 import ch.boye.httpclientandroidlib.cookie.Cookie;
    37 import ch.boye.httpclientandroidlib.cookie.CookieIdentityComparator;
    39 /**
    40  * Default implementation of {@link CookieStore}
    41  *
    42  *
    43  * @since 4.0
    44  */
    45 @ThreadSafe
    46 public class BasicCookieStore implements CookieStore, Serializable {
    48     private static final long serialVersionUID = -7581093305228232025L;
    50     @GuardedBy("this")
    51     private final TreeSet<Cookie> cookies;
    53     public BasicCookieStore() {
    54         super();
    55         this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
    56     }
    58     /**
    59      * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies.
    60      * If the given cookie has already expired it will not be added, but existing
    61      * values will still be removed.
    62      *
    63      * @param cookie the {@link Cookie cookie} to be added
    64      *
    65      * @see #addCookies(Cookie[])
    66      *
    67      */
    68     public synchronized void addCookie(Cookie cookie) {
    69         if (cookie != null) {
    70             // first remove any old cookie that is equivalent
    71             cookies.remove(cookie);
    72             if (!cookie.isExpired(new Date())) {
    73                 cookies.add(cookie);
    74             }
    75         }
    76     }
    78     /**
    79      * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and
    80      * in the given array order. If any of the given cookies has already expired it will
    81      * not be added, but existing values will still be removed.
    82      *
    83      * @param cookies the {@link Cookie cookies} to be added
    84      *
    85      * @see #addCookie(Cookie)
    86      *
    87      */
    88     public synchronized void addCookies(Cookie[] cookies) {
    89         if (cookies != null) {
    90             for (Cookie cooky : cookies) {
    91                 this.addCookie(cooky);
    92             }
    93         }
    94     }
    96     /**
    97      * Returns an immutable array of {@link Cookie cookies} that this HTTP
    98      * state currently contains.
    99      *
   100      * @return an array of {@link Cookie cookies}.
   101      */
   102     public synchronized List<Cookie> getCookies() {
   103         //create defensive copy so it won't be concurrently modified
   104         return new ArrayList<Cookie>(cookies);
   105     }
   107     /**
   108      * Removes all of {@link Cookie cookies} in this HTTP state
   109      * that have expired by the specified {@link java.util.Date date}.
   110      *
   111      * @return true if any cookies were purged.
   112      *
   113      * @see Cookie#isExpired(Date)
   114      */
   115     public synchronized boolean clearExpired(final Date date) {
   116         if (date == null) {
   117             return false;
   118         }
   119         boolean removed = false;
   120         for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
   121             if (it.next().isExpired(date)) {
   122                 it.remove();
   123                 removed = true;
   124             }
   125         }
   126         return removed;
   127     }
   129     /**
   130      * Clears all cookies.
   131      */
   132     public synchronized void clear() {
   133         cookies.clear();
   134     }
   136     @Override
   137     public synchronized String toString() {
   138         return cookies.toString();
   139     }
   141 }

mercurial