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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/BasicCookieStore.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.impl.client;
    1.31 +
    1.32 +import java.io.Serializable;
    1.33 +import java.util.*;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.annotation.GuardedBy;
    1.36 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.client.CookieStore;
    1.39 +import ch.boye.httpclientandroidlib.cookie.Cookie;
    1.40 +import ch.boye.httpclientandroidlib.cookie.CookieIdentityComparator;
    1.41 +
    1.42 +/**
    1.43 + * Default implementation of {@link CookieStore}
    1.44 + *
    1.45 + *
    1.46 + * @since 4.0
    1.47 + */
    1.48 +@ThreadSafe
    1.49 +public class BasicCookieStore implements CookieStore, Serializable {
    1.50 +
    1.51 +    private static final long serialVersionUID = -7581093305228232025L;
    1.52 +
    1.53 +    @GuardedBy("this")
    1.54 +    private final TreeSet<Cookie> cookies;
    1.55 +
    1.56 +    public BasicCookieStore() {
    1.57 +        super();
    1.58 +        this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies.
    1.63 +     * If the given cookie has already expired it will not be added, but existing
    1.64 +     * values will still be removed.
    1.65 +     *
    1.66 +     * @param cookie the {@link Cookie cookie} to be added
    1.67 +     *
    1.68 +     * @see #addCookies(Cookie[])
    1.69 +     *
    1.70 +     */
    1.71 +    public synchronized void addCookie(Cookie cookie) {
    1.72 +        if (cookie != null) {
    1.73 +            // first remove any old cookie that is equivalent
    1.74 +            cookies.remove(cookie);
    1.75 +            if (!cookie.isExpired(new Date())) {
    1.76 +                cookies.add(cookie);
    1.77 +            }
    1.78 +        }
    1.79 +    }
    1.80 +
    1.81 +    /**
    1.82 +     * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and
    1.83 +     * in the given array order. If any of the given cookies has already expired it will
    1.84 +     * not be added, but existing values will still be removed.
    1.85 +     *
    1.86 +     * @param cookies the {@link Cookie cookies} to be added
    1.87 +     *
    1.88 +     * @see #addCookie(Cookie)
    1.89 +     *
    1.90 +     */
    1.91 +    public synchronized void addCookies(Cookie[] cookies) {
    1.92 +        if (cookies != null) {
    1.93 +            for (Cookie cooky : cookies) {
    1.94 +                this.addCookie(cooky);
    1.95 +            }
    1.96 +        }
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Returns an immutable array of {@link Cookie cookies} that this HTTP
   1.101 +     * state currently contains.
   1.102 +     *
   1.103 +     * @return an array of {@link Cookie cookies}.
   1.104 +     */
   1.105 +    public synchronized List<Cookie> getCookies() {
   1.106 +        //create defensive copy so it won't be concurrently modified
   1.107 +        return new ArrayList<Cookie>(cookies);
   1.108 +    }
   1.109 +
   1.110 +    /**
   1.111 +     * Removes all of {@link Cookie cookies} in this HTTP state
   1.112 +     * that have expired by the specified {@link java.util.Date date}.
   1.113 +     *
   1.114 +     * @return true if any cookies were purged.
   1.115 +     *
   1.116 +     * @see Cookie#isExpired(Date)
   1.117 +     */
   1.118 +    public synchronized boolean clearExpired(final Date date) {
   1.119 +        if (date == null) {
   1.120 +            return false;
   1.121 +        }
   1.122 +        boolean removed = false;
   1.123 +        for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
   1.124 +            if (it.next().isExpired(date)) {
   1.125 +                it.remove();
   1.126 +                removed = true;
   1.127 +            }
   1.128 +        }
   1.129 +        return removed;
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Clears all cookies.
   1.134 +     */
   1.135 +    public synchronized void clear() {
   1.136 +        cookies.clear();
   1.137 +    }
   1.138 +
   1.139 +    @Override
   1.140 +    public synchronized String toString() {
   1.141 +        return cookies.toString();
   1.142 +    }
   1.143 +
   1.144 +}

mercurial