michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.client; michael@0: michael@0: import java.io.Serializable; michael@0: import java.util.*; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.GuardedBy; michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.client.CookieStore; michael@0: import ch.boye.httpclientandroidlib.cookie.Cookie; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieIdentityComparator; michael@0: michael@0: /** michael@0: * Default implementation of {@link CookieStore} michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @ThreadSafe michael@0: public class BasicCookieStore implements CookieStore, Serializable { michael@0: michael@0: private static final long serialVersionUID = -7581093305228232025L; michael@0: michael@0: @GuardedBy("this") michael@0: private final TreeSet cookies; michael@0: michael@0: public BasicCookieStore() { michael@0: super(); michael@0: this.cookies = new TreeSet(new CookieIdentityComparator()); michael@0: } michael@0: michael@0: /** michael@0: * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies. michael@0: * If the given cookie has already expired it will not be added, but existing michael@0: * values will still be removed. michael@0: * michael@0: * @param cookie the {@link Cookie cookie} to be added michael@0: * michael@0: * @see #addCookies(Cookie[]) michael@0: * michael@0: */ michael@0: public synchronized void addCookie(Cookie cookie) { michael@0: if (cookie != null) { michael@0: // first remove any old cookie that is equivalent michael@0: cookies.remove(cookie); michael@0: if (!cookie.isExpired(new Date())) { michael@0: cookies.add(cookie); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and michael@0: * in the given array order. If any of the given cookies has already expired it will michael@0: * not be added, but existing values will still be removed. michael@0: * michael@0: * @param cookies the {@link Cookie cookies} to be added michael@0: * michael@0: * @see #addCookie(Cookie) michael@0: * michael@0: */ michael@0: public synchronized void addCookies(Cookie[] cookies) { michael@0: if (cookies != null) { michael@0: for (Cookie cooky : cookies) { michael@0: this.addCookie(cooky); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns an immutable array of {@link Cookie cookies} that this HTTP michael@0: * state currently contains. michael@0: * michael@0: * @return an array of {@link Cookie cookies}. michael@0: */ michael@0: public synchronized List getCookies() { michael@0: //create defensive copy so it won't be concurrently modified michael@0: return new ArrayList(cookies); michael@0: } michael@0: michael@0: /** michael@0: * Removes all of {@link Cookie cookies} in this HTTP state michael@0: * that have expired by the specified {@link java.util.Date date}. michael@0: * michael@0: * @return true if any cookies were purged. michael@0: * michael@0: * @see Cookie#isExpired(Date) michael@0: */ michael@0: public synchronized boolean clearExpired(final Date date) { michael@0: if (date == null) { michael@0: return false; michael@0: } michael@0: boolean removed = false; michael@0: for (Iterator it = cookies.iterator(); it.hasNext();) { michael@0: if (it.next().isExpired(date)) { michael@0: it.remove(); michael@0: removed = true; michael@0: } michael@0: } michael@0: return removed; michael@0: } michael@0: michael@0: /** michael@0: * Clears all cookies. michael@0: */ michael@0: public synchronized void clear() { michael@0: cookies.clear(); michael@0: } michael@0: michael@0: @Override michael@0: public synchronized String toString() { michael@0: return cookies.toString(); michael@0: } michael@0: michael@0: }