From 2370dc5fd3dcebf23ff82e5619ab54a6b1d039fc Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 15 Dec 2007 14:45:48 +0000 Subject: [PATCH] CookieStore converted to generics git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@604434 13f79535-47bb-0310-9956-ffa450edef68 --- .../examples/client/ClientCustomContext.java | 8 ++++--- .../http/examples/client/ClientFormLogin.java | 16 +++++++------- .../org/apache/http/client/CookieStore.java | 3 ++- .../client/protocol/RequestAddCookies.java | 13 ++++++------ .../http/impl/client/BasicCookieStore.java | 8 ++++--- .../client/protocol/TestCookie2Support.java | 21 ++++++++++--------- 6 files changed, 39 insertions(+), 30 deletions(-) diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java b/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java index 8482018cc..0cb0a4b01 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java @@ -31,6 +31,8 @@ package org.apache.http.examples.client; +import java.util.List; + import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; @@ -78,9 +80,9 @@ public class ClientCustomContext { System.out.println("Response content length: " + entity.getContentLength()); System.out.println("Chunked?: " + entity.isChunked()); } - Cookie[] cookies = cookieStore.getCookies(); - for (int i = 0; i < cookies.length; i++) { - System.out.println("Local cookie: " + cookies[i]); + List cookies = cookieStore.getCookies(); + for (int i = 0; i < cookies.size(); i++) { + System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java b/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java index 46adf518c..4317b82cf 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java @@ -29,6 +29,8 @@ package org.apache.http.examples.client; +import java.util.List; + import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -64,12 +66,12 @@ public class ClientFormLogin { entity.consumeContent(); } System.out.println("Initial set of cookies:"); - Cookie[] cookies = httpclient.getCookieStore().getCookies(); - if (cookies.length == 0) { + List cookies = httpclient.getCookieStore().getCookies(); + if (cookies.isEmpty()) { System.out.println("None"); } else { - for (int i = 0; i < cookies.length; i++) { - System.out.println("- " + cookies[i].toString()); + for (int i = 0; i < cookies.size(); i++) { + System.out.println("- " + cookies.get(i).toString()); } } @@ -95,11 +97,11 @@ public class ClientFormLogin { System.out.println("Post logon cookies:"); cookies = httpclient.getCookieStore().getCookies(); - if (cookies.length == 0) { + if (cookies.isEmpty()) { System.out.println("None"); } else { - for (int i = 0; i < cookies.length; i++) { - System.out.println("- " + cookies[i].toString()); + for (int i = 0; i < cookies.size(); i++) { + System.out.println("- " + cookies.get(i).toString()); } } } diff --git a/module-client/src/main/java/org/apache/http/client/CookieStore.java b/module-client/src/main/java/org/apache/http/client/CookieStore.java index e294d617e..6d682d8f4 100644 --- a/module-client/src/main/java/org/apache/http/client/CookieStore.java +++ b/module-client/src/main/java/org/apache/http/client/CookieStore.java @@ -31,6 +31,7 @@ package org.apache.http.client; import java.util.Date; +import java.util.List; import org.apache.http.cookie.Cookie; @@ -57,7 +58,7 @@ public interface CookieStore { * * @return all cookies */ - Cookie[] getCookies(); + List getCookies(); /** * Removes all of {@link Cookie cookies} in this store that have expired by diff --git a/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java b/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java index 957889d1a..d4996f6cf 100644 --- a/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java +++ b/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java @@ -146,11 +146,11 @@ public class RequestAddCookies implements HttpRequestInterceptor { // Get an instance of the selected cookie policy CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams()); // Get all cookies available in the HTTP state - Cookie[] cookies = cookieStore.getCookies(); + List cookies = cookieStore.getCookies(); // Find cookies matching the given origin - List matchedCookies = new ArrayList(cookies.length); - for (int i = 0; i < cookies.length; i++) { - Cookie cookie = cookies[i]; + List matchedCookies = new ArrayList(); + for (int i = 0; i < cookies.size(); i++) { + Cookie cookie = cookies.get(i); if (cookieSpec.match(cookie, cookieOrigin)) { if (LOG.isDebugEnabled()) { LOG.debug("Cookie " + cookie + " match " + cookieOrigin); @@ -169,8 +169,9 @@ public class RequestAddCookies implements HttpRequestInterceptor { int ver = cookieSpec.getVersion(); if (ver > 0) { boolean needVersionHeader = false; - for (int i = 0; i < cookies.length; i++) { - if (ver != cookies[i].getVersion()) { + for (int i = 0; i < matchedCookies.size(); i++) { + Cookie cookie = matchedCookies.get(i); + if (ver != cookie.getVersion()) { needVersionHeader = true; } } diff --git a/module-client/src/main/java/org/apache/http/impl/client/BasicCookieStore.java b/module-client/src/main/java/org/apache/http/impl/client/BasicCookieStore.java index 535333b9a..f9a348f0c 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/BasicCookieStore.java +++ b/module-client/src/main/java/org/apache/http/impl/client/BasicCookieStore.java @@ -31,9 +31,11 @@ package org.apache.http.impl.client; 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 org.apache.http.client.CookieStore; import org.apache.http.cookie.Cookie; @@ -114,13 +116,13 @@ public class BasicCookieStore implements CookieStore { } /** - * Returns an array of {@link Cookie cookies} that this HTTP + * Returns an immutable array of {@link Cookie cookies} that this HTTP * state currently contains. * * @return an array of {@link Cookie cookies}. */ - public synchronized Cookie[] getCookies() { - return (Cookie[]) (cookies.toArray(new Cookie[cookies.size()])); + public synchronized List getCookies() { + return Collections.unmodifiableList(this.cookies); } /** diff --git a/module-client/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java b/module-client/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java index c33775836..3eea5d803 100644 --- a/module-client/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java +++ b/module-client/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java @@ -29,6 +29,7 @@ package org.apache.http.client.protocol; import java.io.IOException; +import java.util.List; import junit.framework.Test; import junit.framework.TestSuite; @@ -115,9 +116,9 @@ public class TestCookie2Support extends ServerTestBase { e1.consumeContent(); } - Cookie[] cookies = cookieStore.getCookies(); + List cookies = cookieStore.getCookies(); assertNotNull(cookies); - assertEquals(1, cookies.length); + assertEquals(1, cookies.size()); HttpResponse response2 = client.execute(getServerHttp(), httpget, context); HttpEntity e2 = response2.getEntity(); @@ -166,9 +167,9 @@ public class TestCookie2Support extends ServerTestBase { e1.consumeContent(); } - Cookie[] cookies = cookieStore.getCookies(); + List cookies = cookieStore.getCookies(); assertNotNull(cookies); - assertEquals(2, cookies.length); + assertEquals(2, cookies.size()); HttpResponse response2 = client.execute(getServerHttp(), httpget, context); HttpEntity e2 = response2.getEntity(); @@ -215,9 +216,9 @@ public class TestCookie2Support extends ServerTestBase { e1.consumeContent(); } - Cookie[] cookies = cookieStore.getCookies(); + List cookies = cookieStore.getCookies(); assertNotNull(cookies); - assertEquals(1, cookies.length); + assertEquals(1, cookies.size()); HttpResponse response2 = client.execute(getServerHttp(), httpget, context); HttpEntity e2 = response2.getEntity(); @@ -266,11 +267,11 @@ public class TestCookie2Support extends ServerTestBase { e1.consumeContent(); } - Cookie[] cookies = cookieStore.getCookies(); + List cookies = cookieStore.getCookies(); assertNotNull(cookies); - assertEquals(1, cookies.length); - assertEquals("right", cookies[0].getValue()); - assertTrue(cookies[0] instanceof SetCookie2); + assertEquals(1, cookies.size()); + assertEquals("right", cookies.get(0).getValue()); + assertTrue(cookies.get(0) instanceof SetCookie2); } }