From b9b16529d58719edd2040da7615121de36981832 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 11 Dec 2012 21:11:27 +0100 Subject: [PATCH] Removed jetty-client's CookieStore to use java.net.CookieStore instead. This unifies the usage of CookieStores between jetty-client and jetty-websocket, and hopefully other modules as well. --- .../org/eclipse/jetty/client/HttpClient.java | 32 ++- .../eclipse/jetty/client/HttpConnection.java | 5 +- .../eclipse/jetty/client/HttpCookieStore.java | 151 --------------- .../eclipse/jetty/client/HttpReceiver.java | 48 +++-- .../eclipse/jetty/client/api/CookieStore.java | 97 ---------- .../eclipse/jetty/client/HttpClientTest.java | 7 +- .../jetty/client/HttpCookieStoreTest.java | 183 ------------------ .../eclipse/jetty/client/HttpCookieTest.java | 15 +- .../org/eclipse/jetty/client/api/Usage.java | 5 +- .../eclipse/jetty/util/HttpCookieStore.java | 75 +++++++ 10 files changed, 149 insertions(+), 469 deletions(-) delete mode 100644 jetty-client/src/main/java/org/eclipse/jetty/client/HttpCookieStore.java delete mode 100644 jetty-client/src/main/java/org/eclipse/jetty/client/api/CookieStore.java delete mode 100644 jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieStoreTest.java create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/HttpCookieStore.java diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index eabcc8e9957..309bbc42610 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -20,6 +20,9 @@ package org.eclipse.jetty.client; import java.io.IOException; import java.net.ConnectException; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.CookieStore; import java.net.SocketAddress; import java.net.SocketException; import java.net.URI; @@ -41,13 +44,11 @@ import javax.net.ssl.SSLEngine; import org.eclipse.jetty.client.api.AuthenticationStore; import org.eclipse.jetty.client.api.Connection; import org.eclipse.jetty.client.api.ContentResponse; -import org.eclipse.jetty.client.api.CookieStore; import org.eclipse.jetty.client.api.Destination; import org.eclipse.jetty.client.api.ProxyConfiguration; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.http.HttpField; -import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.io.ByteBufferPool; @@ -112,7 +113,8 @@ public class HttpClient extends ContainerLifeCycle private final AuthenticationStore authenticationStore = new HttpAuthenticationStore(); private final Set decoderFactories = Collections.newSetFromMap(new ConcurrentHashMap()); private final SslContextFactory sslContextFactory; - private volatile CookieStore cookieStore = new HttpCookieStore(); + private volatile CookieManager cookieManager; + private volatile CookieStore cookieStore; private volatile Executor executor; private volatile ByteBufferPool byteBufferPool; private volatile Scheduler scheduler; @@ -201,6 +203,9 @@ public class HttpClient extends ContainerLifeCycle decoderFactories.add(new GZIPContentDecoder.Factory()); + cookieManager = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL); + cookieStore = cookieManager.getCookieStore(); + super.doStart(); LOG.info("Started {}", this); @@ -216,17 +221,19 @@ public class HttpClient extends ContainerLifeCycle { LOG.debug("Stopping {}", this); + cookieStore.removeAll(); + cookieStore = null; + decoderFactories.clear(); + handlers.clear(); + for (HttpDestination destination : destinations.values()) destination.close(); - destinations.clear(); + conversations.clear(); - handlers.clear(); requestListeners.clear(); - cookieStore.clear(); authenticationStore.clearAuthentications(); authenticationStore.clearAuthenticationResults(); - decoderFactories.clear(); super.doStop(); @@ -257,6 +264,17 @@ public class HttpClient extends ContainerLifeCycle this.cookieStore = cookieStore; } + /** + * Keep this method package-private because its interface is so ugly + * that we really don't want to expose it more than strictly needed. + * + * @return the cookie manager + */ + CookieManager getCookieManager() + { + return cookieManager; + } + /** * @return the authentication store associated with this instance */ diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java index 650f5d427a2..818f932bbd8 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java @@ -19,6 +19,8 @@ package org.eclipse.jetty.client; import java.io.UnsupportedEncodingException; +import java.net.HttpCookie; +import java.net.URI; import java.net.URLEncoder; import java.nio.charset.UnsupportedCharsetException; import java.util.Collections; @@ -34,7 +36,6 @@ import org.eclipse.jetty.client.api.ContentProvider; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.util.StringContentProvider; -import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; @@ -232,7 +233,7 @@ public class HttpConnection extends AbstractConnection implements Connection } // Cookies - List cookies = client.getCookieStore().findCookies(getDestination(), request.getPath()); + List cookies = client.getCookieStore().get(URI.create(request.getURI())); StringBuilder cookieString = null; for (int i = 0; i < cookies.size(); ++i) { diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpCookieStore.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpCookieStore.java deleted file mode 100644 index 2f0b4256df1..00000000000 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpCookieStore.java +++ /dev/null @@ -1,151 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.client; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ConcurrentMap; - -import org.eclipse.jetty.client.api.CookieStore; -import org.eclipse.jetty.client.api.Destination; -import org.eclipse.jetty.http.HttpCookie; - -public class HttpCookieStore implements CookieStore -{ - private final ConcurrentMap> allCookies = new ConcurrentHashMap<>(); - - @Override - public List findCookies(Destination destination, String path) - { - List result = new ArrayList<>(); - - String host = destination.getHost(); - int port = destination.getPort(); - String key = host + ":" + port; - - // Root path lookup - Queue cookies = allCookies.get(key + "/"); - if (cookies != null) - accumulateCookies(destination, cookies, result); - - // Path lookup - if (path == null) - path = "/"; - String[] split = path.split("/"); - for (int i = 1; i < split.length; i++) - { - String segment = split[i]; - key += "/" + segment; - cookies = allCookies.get(key); - if (cookies != null) - accumulateCookies(destination, cookies, result); - if (segment.length() > 0) - key += "/"; - } - - // Domain lookup - int domains = host.split("\\.").length - 1; - for (int i = 2; i <= domains; ++i) - { - String[] hostParts = host.split("\\.", i); - host = hostParts[hostParts.length - 1]; - key = host + ":" + port + "/"; - cookies = allCookies.get(key); - if (cookies != null) - accumulateCookies(destination, cookies, result); - } - - return result; - } - - private void accumulateCookies(Destination destination, Queue cookies, List result) - { - for (Iterator iterator = cookies.iterator(); iterator.hasNext(); ) - { - HttpCookie cookie = iterator.next(); - if (cookie.isExpired(System.nanoTime())) - { - iterator.remove(); - } - else - { - if (!"https".equalsIgnoreCase(destination.getScheme()) && cookie.isSecure()) - continue; - result.add(cookie); - } - } - } - - @Override - public boolean addCookie(Destination destination, HttpCookie cookie) - { - String destinationDomain = destination.getHost() + ":" + destination.getPort(); - - // Check whether it is the same domain - String domain = cookie.getDomain(); - if (domain == null) - domain = destinationDomain; - - if (domain.indexOf(':') < 0) - domain += ":" + ("https".equalsIgnoreCase(destination.getScheme()) ? 443 : 80); - - // Cookie domains may start with a ".", like ".domain.com" - // This also avoids that a request to sub.domain.com sets a cookie for domain.com - if (!domain.endsWith(destinationDomain)) - return false; - - // Normalize the path - String path = cookie.getPath(); - if (path == null || path.length() == 0) - path = "/"; - - String key = destination.getHost() + ":" + destination.getPort() + path; - Queue cookies = allCookies.get(key); - if (cookies == null) - { - cookies = new ConcurrentLinkedQueue<>(); - Queue existing = allCookies.putIfAbsent(key, cookies); - if (existing != null) - cookies = existing; - } - cookies.add(cookie); - return true; - } - - @Override - public boolean removeCookie(Destination destination, HttpCookie cookie) - { - for (Queue cookies : allCookies.values()) - { - if (cookies.remove(cookie)) - return true; - } - return false; - } - - @Override - public void clear() - { - allCookies.clear(); - } -} diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpReceiver.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpReceiver.java index 2fd12f11609..1f5b9f1abb1 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpReceiver.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpReceiver.java @@ -19,20 +19,21 @@ package org.eclipse.jetty.client; import java.io.EOFException; +import java.io.IOException; +import java.net.URI; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; -import java.util.Locale; +import java.util.Map; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicMarkableReference; import java.util.concurrent.atomic.AtomicReference; -import org.eclipse.jetty.client.api.CookieStore; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; -import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpParser; @@ -193,22 +194,21 @@ public class HttpReceiver implements HttpParser.ResponseHandler if (exchange != null) { exchange.getResponse().getHeaders().add(field); - if (field.getHeader()!=null) - switch (field.getHeader()) + HttpHeader fieldHeader = field.getHeader(); + if (fieldHeader != null) { - case SET_COOKIE: - case SET_COOKIE2: + switch (fieldHeader) { - CookieStore cookieStore = connection.getHttpClient().getCookieStore(); - HttpDestination destination = connection.getDestination(); - List cookies = HttpCookieParser.parseCookies(field.getValue()); - for (HttpCookie cookie : cookies) - cookieStore.addCookie(destination, cookie); - break; - } - default: - { - break; + case SET_COOKIE: + case SET_COOKIE2: + { + storeCookie(exchange.getRequest().getURI(), field); + break; + } + default: + { + break; + } } } } @@ -216,6 +216,20 @@ public class HttpReceiver implements HttpParser.ResponseHandler return false; } + private void storeCookie(String uri, HttpField field) + { + try + { + Map> header = new HashMap<>(1); + header.put(field.getHeader().asString(), Collections.singletonList(field.getValue())); + connection.getHttpClient().getCookieManager().put(URI.create(uri), header); + } + catch (IOException x) + { + LOG.debug(x); + } + } + @Override public boolean headerComplete() { diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/api/CookieStore.java b/jetty-client/src/main/java/org/eclipse/jetty/client/api/CookieStore.java deleted file mode 100644 index e3b222d88b0..00000000000 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/api/CookieStore.java +++ /dev/null @@ -1,97 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.client.api; - -import java.util.Collections; -import java.util.List; - -import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.http.HttpCookie; - -/** - * A store for HTTP cookies that offers methods to match cookies for a given destination and path. - * - * @see HttpClient#getCookieStore() - */ -public interface CookieStore -{ - /** - * Returns the non-expired cookies that match the given destination and path, - * recursively matching parent paths (for the same domain) and parent domains - * (for the root path). - * - * @param destination the destination representing the domain - * @param path the request path - * @return the list of matching cookies - */ - List findCookies(Destination destination, String path); - - /** - * Adds the given cookie to this store for the given destination. - * If the cookie's domain and the destination host do not match, the cookie is not added. - * - * @param destination the destination the cookie should belong to - * @param cookie the cookie to add - * @return whether the cookie has been added or not - */ - boolean addCookie(Destination destination, HttpCookie cookie); - - /** - * Removes the given cookie from this store for the given destination. - * - * @param destination the destination the cookie belongs to - * @param cookie the cookie to remove - * @return whether the cookie has been removed or not - */ - boolean removeCookie(Destination destination, HttpCookie cookie); - - /** - * Removes all the cookies from this store. - */ - void clear(); - - /** - * A {@link CookieStore} that is always empty - */ - public static class Empty implements CookieStore - { - @Override - public List findCookies(Destination destination, String path) - { - return Collections.emptyList(); - } - - @Override - public boolean addCookie(Destination destination, HttpCookie cookie) - { - return false; - } - - @Override - public boolean removeCookie(Destination destination, HttpCookie cookie) - { - return false; - } - - @Override - public void clear() - { - } - } -} diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java index d9deb6afe21..50a0bb24dc7 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java @@ -20,6 +20,8 @@ package org.eclipse.jetty.client; import java.io.IOException; import java.io.OutputStream; +import java.net.HttpCookie; +import java.net.URI; import java.net.URLEncoder; import java.nio.ByteBuffer; import java.nio.file.Files; @@ -46,7 +48,6 @@ import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.client.util.BytesContentProvider; -import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.annotation.Slow; @@ -86,14 +87,14 @@ public class HttpClientTest extends AbstractHttpClientServerTest } Assert.assertNotNull(connection); - client.getCookieStore().addCookie(destination, new HttpCookie("foo", "bar", null, path)); + String uri = destination.getScheme() + "://" + destination.getHost() + ":" + destination.getPort(); + client.getCookieStore().add(URI.create(uri), new HttpCookie("foo", "bar")); client.stop(); Assert.assertEquals(0, client.getDestinations().size()); Assert.assertEquals(0, destination.getIdleConnections().size()); Assert.assertEquals(0, destination.getActiveConnections().size()); - Assert.assertEquals(0, client.getCookieStore().findCookies(destination, path).size()); Assert.assertFalse(connection.getEndPoint().isOpen()); } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieStoreTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieStoreTest.java deleted file mode 100644 index 846d2c2a3d9..00000000000 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieStoreTest.java +++ /dev/null @@ -1,183 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.client; - -import java.util.List; - -import org.eclipse.jetty.client.api.CookieStore; -import org.eclipse.jetty.client.api.Destination; -import org.eclipse.jetty.http.HttpCookie; -import org.eclipse.jetty.toolchain.test.TestTracker; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; - -public class HttpCookieStoreTest -{ - @Rule - public final TestTracker tracker = new TestTracker(); - - private HttpClient client = new HttpClient(); - - @Test - public void testCookieStoredIsRetrieved() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1"))); - - List result = cookies.findCookies(destination, "/"); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - HttpCookie cookie = result.get(0); - Assert.assertEquals("a", cookie.getName()); - Assert.assertEquals("1", cookie.getValue()); - } - - @Test - public void testCookieWithChildDomainIsStored() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", "child.localhost", "/"))); - - List result = cookies.findCookies(destination, "/"); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - HttpCookie cookie = result.get(0); - Assert.assertEquals("a", cookie.getName()); - Assert.assertEquals("1", cookie.getValue()); - } - - @Test - public void testCookieWithParentDomainIsNotStored() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "child.localhost", 80); - Assert.assertFalse(cookies.addCookie(destination, new HttpCookie("a", "1", "localhost", "/"))); - } - - @Test - public void testCookieStoredWithPathIsNotRetrievedWithRootPath() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/path"))); - - List result = cookies.findCookies(destination, "/"); - Assert.assertNotNull(result); - Assert.assertEquals(0, result.size()); - } - - @Test - public void testCookieStoredWithRootPathIsRetrievedWithPath() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/"))); - - List result = cookies.findCookies(destination, "/path"); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - HttpCookie cookie = result.get(0); - Assert.assertEquals("a", cookie.getName()); - Assert.assertEquals("1", cookie.getValue()); - } - - @Test - public void testCookieStoredWithPathIsRetrievedWithChildPath() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/path"))); - - List result = cookies.findCookies(destination, "/path/child"); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - HttpCookie cookie = result.get(0); - Assert.assertEquals("a", cookie.getName()); - Assert.assertEquals("1", cookie.getValue()); - } - - @Test - public void testCookieStoredWithParentDomainIsRetrievedWithChildDomain() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination parentDestination = new HttpDestination(client, "http", "localhost.org", 80); - Assert.assertTrue(cookies.addCookie(parentDestination, new HttpCookie("a", "1", null, "/"))); - - Destination childDestination = new HttpDestination(client, "http", "child.localhost.org", 80); - Assert.assertTrue(cookies.addCookie(childDestination, new HttpCookie("b", "2", null, "/"))); - - Destination grandChildDestination = new HttpDestination(client, "http", "grand.child.localhost.org", 80); - Assert.assertTrue(cookies.addCookie(grandChildDestination, new HttpCookie("b", "2", null, "/"))); - - List result = cookies.findCookies(grandChildDestination, "/path"); - Assert.assertNotNull(result); - Assert.assertEquals(2, result.size()); - } - - @Test - public void testCookieStoredWithChildDomainIsNotRetrievedWithParentDomain() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination childDestination = new HttpDestination(client, "http", "child.localhost.org", 80); - Assert.assertTrue(cookies.addCookie(childDestination, new HttpCookie("b", "2", null, "/"))); - - Destination parentDestination = new HttpDestination(client, "http", "localhost.org", 80); - List result = cookies.findCookies(parentDestination, "/path"); - Assert.assertNotNull(result); - Assert.assertEquals(0, result.size()); - } - - @Test - public void testExpiredCookieIsNotRetrieved() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost.org", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/", 0, false, false))); - - List result = cookies.findCookies(destination, "/"); - Assert.assertNotNull(result); - Assert.assertEquals(0, result.size()); - } - - @Test - public void testSecureCookieIsNotRetrieved() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost.org", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/", -1, false, true))); - - List result = cookies.findCookies(destination, "/"); - Assert.assertNotNull(result); - Assert.assertEquals(0, result.size()); - } - - @Test - public void testCookiesAreCleared() throws Exception - { - CookieStore cookies = new HttpCookieStore(); - Destination destination = new HttpDestination(client, "http", "localhost.org", 80); - Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/", -1, false, true))); - - cookies.clear(); - Assert.assertEquals(0, cookies.findCookies(destination, "/").size()); - } -} diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieTest.java index 568197c85a8..838f2a848b2 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpCookieTest.java @@ -19,6 +19,8 @@ package org.eclipse.jetty.client; import java.io.IOException; +import java.net.HttpCookie; +import java.net.URI; import java.util.List; import java.util.concurrent.TimeUnit; import javax.servlet.ServletException; @@ -26,9 +28,7 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.eclipse.jetty.client.api.Destination; import org.eclipse.jetty.client.api.Response; -import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.util.ssl.SslContextFactory; @@ -60,11 +60,11 @@ public class HttpCookieTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); String path = "/path"; - Response response = client.GET(scheme + "://" + host + ":" + port + path).get(5, TimeUnit.SECONDS); + String uri = scheme + "://" + host + ":" + port + path; + Response response = client.GET(uri).get(5, TimeUnit.SECONDS); Assert.assertEquals(200, response.getStatus()); - Destination destination = client.getDestination(scheme, host, port); - List cookies = client.getCookieStore().findCookies(destination, path); + List cookies = client.getCookieStore().get(URI.create(uri)); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); HttpCookie cookie = cookies.get(0); @@ -94,8 +94,9 @@ public class HttpCookieTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); String path = "/path"; - Destination destination = client.getDestination(scheme, host, port); - client.getCookieStore().addCookie(destination, new HttpCookie(name, value, null, path)); + String uri = scheme + "://" + host + ":" + port; + HttpCookie cookie = new HttpCookie(name, value); + client.getCookieStore().add(URI.create(uri), cookie); Response response = client.GET(scheme + "://" + host + ":" + port + path).get(5, TimeUnit.SECONDS); Assert.assertEquals(200, response.getStatus()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java b/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java index e7a0e4535ea..97c552a0f8e 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java @@ -20,6 +20,8 @@ package org.eclipse.jetty.client.api; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.net.HttpCookie; +import java.net.URI; import java.nio.file.Paths; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; @@ -31,7 +33,6 @@ import org.eclipse.jetty.client.util.BasicAuthentication; import org.eclipse.jetty.client.util.BlockingResponseListener; import org.eclipse.jetty.client.util.InputStreamContentProvider; import org.eclipse.jetty.client.util.InputStreamResponseListener; -import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpVersion; import org.junit.Assert; @@ -181,7 +182,7 @@ public class Usage client.start(); // Set a cookie to be sent in requests that match the cookie's domain - client.getCookieStore().addCookie(client.getDestination("http", "host", 8080), new HttpCookie("name", "value")); + client.getCookieStore().add(URI.create("http://host:8080/path"), new HttpCookie("name", "value")); // Send a request for the cookie's domain Response response = client.newRequest("host", 8080).send().get(); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/HttpCookieStore.java b/jetty-util/src/main/java/org/eclipse/jetty/util/HttpCookieStore.java new file mode 100644 index 00000000000..30bc0dcbe8c --- /dev/null +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/HttpCookieStore.java @@ -0,0 +1,75 @@ +// +// ======================================================================== +// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util; + +import java.net.CookieManager; +import java.net.CookieStore; +import java.net.HttpCookie; +import java.net.URI; +import java.util.List; + +/** + * Implementation of {@link CookieStore} that delegates to an instance created by {@link CookieManager} + * via {@link CookieManager#getCookieStore()}. + */ +public class HttpCookieStore implements CookieStore +{ + private final CookieStore delegate; + + public HttpCookieStore() + { + delegate = new CookieManager().getCookieStore(); + } + + @Override + public void add(URI uri, HttpCookie cookie) + { + delegate.add(uri, cookie); + } + + @Override + public List get(URI uri) + { + return delegate.get(uri); + } + + @Override + public List getCookies() + { + return delegate.getCookies(); + } + + @Override + public List getURIs() + { + return delegate.getURIs(); + } + + @Override + public boolean remove(URI uri, HttpCookie cookie) + { + return delegate.remove(uri, cookie); + } + + @Override + public boolean removeAll() + { + return delegate.removeAll(); + } +}