HTTPCLIENT-969: BasicCookieStore#getCookies() to return a copy of Cookie list

Contributed by David Smiley <dsmiley at mitre.org>


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@967227 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-07-23 19:45:47 +00:00
parent 5059eddd97
commit c2622c8ca2
2 changed files with 15 additions and 30 deletions

View File

@ -1,6 +1,9 @@
Changes since 4.1 ALPHA2 Changes since 4.1 ALPHA2
------------------- -------------------
* [HTTPCLIENT-969] BasicCookieStore#getCookies() to return a copy of Cookie list
Contributed by David Smiley <dsmiley at mitre.org>
* [HTTPCLIENT-965] Fixed problem with cache not honoring must-revalidate or * [HTTPCLIENT-965] Fixed problem with cache not honoring must-revalidate or
proxy-revalidate Cache-Control directives. proxy-revalidate Cache-Control directives.
Contributed by Jonathan Moore <jonathan_moore at comcast.com> Contributed by Jonathan Moore <jonathan_moore at comcast.com>

View File

@ -27,12 +27,7 @@
package org.apache.http.impl.client; package org.apache.http.impl.client;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.http.annotation.GuardedBy; import org.apache.http.annotation.GuardedBy;
import org.apache.http.annotation.ThreadSafe; import org.apache.http.annotation.ThreadSafe;
@ -50,23 +45,14 @@ import org.apache.http.cookie.CookieIdentityComparator;
@ThreadSafe @ThreadSafe
public class BasicCookieStore implements CookieStore, Serializable { public class BasicCookieStore implements CookieStore, Serializable {
private static final long serialVersionUID = -1113466491038527240L; private static final long serialVersionUID = -7581093305228232025L;
@GuardedBy("this") @GuardedBy("this")
private final ArrayList<Cookie> cookies; private final TreeSet<Cookie> cookies;
@GuardedBy("this")
private final Comparator<Cookie> cookieComparator;
// -------------------------------------------------------- Class Variables
/**
* Default constructor.
*/
public BasicCookieStore() { public BasicCookieStore() {
super(); super();
this.cookies = new ArrayList<Cookie>(); this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
this.cookieComparator = new CookieIdentityComparator();
} }
/** /**
@ -82,12 +68,7 @@ public class BasicCookieStore implements CookieStore, Serializable {
public synchronized void addCookie(Cookie cookie) { public synchronized void addCookie(Cookie cookie) {
if (cookie != null) { if (cookie != null) {
// first remove any old cookie that is equivalent // first remove any old cookie that is equivalent
for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) { cookies.remove(cookie);
if (cookieComparator.compare(cookie, it.next()) == 0) {
it.remove();
break;
}
}
if (!cookie.isExpired(new Date())) { if (!cookie.isExpired(new Date())) {
cookies.add(cookie); cookies.add(cookie);
} }
@ -119,7 +100,8 @@ public class BasicCookieStore implements CookieStore, Serializable {
* @return an array of {@link Cookie cookies}. * @return an array of {@link Cookie cookies}.
*/ */
public synchronized List<Cookie> getCookies() { public synchronized List<Cookie> getCookies() {
return Collections.unmodifiableList(this.cookies); //create defensive copy so it won't be concurrently modified
return new ArrayList<Cookie>(cookies);
} }
/** /**
@ -144,11 +126,6 @@ public class BasicCookieStore implements CookieStore, Serializable {
return removed; return removed;
} }
@Override
public String toString() {
return cookies.toString();
}
/** /**
* Clears all cookies. * Clears all cookies.
*/ */
@ -156,4 +133,9 @@ public class BasicCookieStore implements CookieStore, Serializable {
cookies.clear(); cookies.clear();
} }
@Override
public synchronized String toString() {
return cookies.toString();
}
} }