CookieStore converted to generics

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@604434 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-12-15 14:45:48 +00:00
parent d417db6bba
commit 2370dc5fd3
6 changed files with 39 additions and 30 deletions

View File

@ -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<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
// Consume response content

View File

@ -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<Cookie> 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());
}
}
}

View File

@ -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<Cookie> getCookies();
/**
* Removes all of {@link Cookie cookies} in this store that have expired by

View File

@ -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<Cookie> cookies = cookieStore.getCookies();
// Find cookies matching the given origin
List<Cookie> matchedCookies = new ArrayList<Cookie>(cookies.length);
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
List<Cookie> matchedCookies = new ArrayList<Cookie>();
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;
}
}

View File

@ -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<Cookie> getCookies() {
return Collections.unmodifiableList(this.cookies);
}
/**

View File

@ -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<Cookie> 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<Cookie> 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<Cookie> 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<Cookie> 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);
}
}