From c2622c8ca28efc661d2f1f447205d1f84eb6fed2 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 23 Jul 2010 19:45:47 +0000 Subject: [PATCH] HTTPCLIENT-969: BasicCookieStore#getCookies() to return a copy of Cookie list Contributed by David Smiley git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@967227 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 3 ++ .../http/impl/client/BasicCookieStore.java | 42 ++++++------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 1a65f0fb9..3862ac25b 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,9 @@ Changes since 4.1 ALPHA2 ------------------- +* [HTTPCLIENT-969] BasicCookieStore#getCookies() to return a copy of Cookie list + Contributed by David Smiley + * [HTTPCLIENT-965] Fixed problem with cache not honoring must-revalidate or proxy-revalidate Cache-Control directives. Contributed by Jonathan Moore diff --git a/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java b/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java index 43b6a26b0..09fc02080 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java @@ -27,12 +27,7 @@ package org.apache.http.impl.client; import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.Iterator; -import java.util.List; +import java.util.*; import org.apache.http.annotation.GuardedBy; import org.apache.http.annotation.ThreadSafe; @@ -50,23 +45,14 @@ import org.apache.http.cookie.CookieIdentityComparator; @ThreadSafe public class BasicCookieStore implements CookieStore, Serializable { - private static final long serialVersionUID = -1113466491038527240L; + private static final long serialVersionUID = -7581093305228232025L; @GuardedBy("this") - private final ArrayList cookies; + private final TreeSet cookies; - @GuardedBy("this") - private final Comparator cookieComparator; - - // -------------------------------------------------------- Class Variables - - /** - * Default constructor. - */ public BasicCookieStore() { super(); - this.cookies = new ArrayList(); - this.cookieComparator = new CookieIdentityComparator(); + this.cookies = new TreeSet(new CookieIdentityComparator()); } /** @@ -82,12 +68,7 @@ public class BasicCookieStore implements CookieStore, Serializable { public synchronized void addCookie(Cookie cookie) { if (cookie != null) { // first remove any old cookie that is equivalent - for (Iterator it = cookies.iterator(); it.hasNext();) { - if (cookieComparator.compare(cookie, it.next()) == 0) { - it.remove(); - break; - } - } + cookies.remove(cookie); if (!cookie.isExpired(new Date())) { cookies.add(cookie); } @@ -119,7 +100,8 @@ public class BasicCookieStore implements CookieStore, Serializable { * @return an array of {@link Cookie cookies}. */ public synchronized List getCookies() { - return Collections.unmodifiableList(this.cookies); + //create defensive copy so it won't be concurrently modified + return new ArrayList(cookies); } /** @@ -144,11 +126,6 @@ public class BasicCookieStore implements CookieStore, Serializable { return removed; } - @Override - public String toString() { - return cookies.toString(); - } - /** * Clears all cookies. */ @@ -156,4 +133,9 @@ public class BasicCookieStore implements CookieStore, Serializable { cookies.clear(); } + @Override + public synchronized String toString() { + return cookies.toString(); + } + }