From b4a820623057c6dbd7963975340bca6542efa48d Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 10 May 2006 15:13:32 +0000 Subject: [PATCH] Added an abstract cookie spec class git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@405771 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/cookie/CookieAttributeHandler.java | 6 +- .../org/apache/http/cookie/CookieOrigin.java | 8 ++ .../http/cookie/impl/AbstractCookieSpec.java | 120 +++++++++++++++++ .../cookie/impl/TestAbstractCookieSpec.java | 121 ++++++++++++++++++ 4 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java create mode 100644 src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java diff --git a/src/java/org/apache/http/cookie/CookieAttributeHandler.java b/src/java/org/apache/http/cookie/CookieAttributeHandler.java index 1c60edc38..1fb63eedd 100644 --- a/src/java/org/apache/http/cookie/CookieAttributeHandler.java +++ b/src/java/org/apache/http/cookie/CookieAttributeHandler.java @@ -30,14 +30,16 @@ package org.apache.http.cookie; /** * Ths interface represents a cookie attribute handler responsible - * for parsing, validating, matching and formatting a specific - * cookie attribute, such as path, domain, port, etc. + * for parsing, validating, and matching a specific cookie attribute, + * such as path, domain, port, etc. * * Different cookie specifications can provide a specific * implementation for this class based on their cookie handling * rules. * * @author jain.samit@gmail.com (Samit Jain) + * + * @since 3.1 */ public interface CookieAttributeHandler { diff --git a/src/java/org/apache/http/cookie/CookieOrigin.java b/src/java/org/apache/http/cookie/CookieOrigin.java index 9de6809c8..5280583a1 100644 --- a/src/java/org/apache/http/cookie/CookieOrigin.java +++ b/src/java/org/apache/http/cookie/CookieOrigin.java @@ -28,6 +28,14 @@ */ package org.apache.http.cookie; +/** + * CookieOrigin class incapsulates details of an origin server that + * are relevant when parsing, validating or matching HTTP cookies. + * + * @author Oleg Kalnichevski + * + * @since 3.1 + */ public final class CookieOrigin { private final String host; diff --git a/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java b/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java new file mode 100644 index 000000000..ffa5f760f --- /dev/null +++ b/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java @@ -0,0 +1,120 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.cookie.impl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.http.cookie.CookieAttributeHandler; +import org.apache.http.cookie.CookieSpec; + + +/** + * Abstract cookie specification which can delegate the job of parsing, + * validation or matching cookie attributes to a number of arbitrary + * {@link CookieAttributeHandler}s. + * + * @author Oleg Kalnichevski + * + * @since 4.0 + */ +public abstract class AbstractCookieSpec implements CookieSpec { + + /** + * Stores the list of attribute handlers + */ + private final List attribHandlerList; + + /** + * Stores attribute name -> attribute handler mappings + */ + private final Map attribHandlerMap; + + /** + * Default constructor + * */ + public AbstractCookieSpec() { + super(); + this.attribHandlerMap = new HashMap(10); + this.attribHandlerList = new ArrayList(10); + } + + public void registerAttribHandler( + final String name, final CookieAttributeHandler handler) { + if (name == null) { + throw new IllegalArgumentException("Attribute name may not be null"); + } + if (handler == null) { + throw new IllegalArgumentException("Attribute handler may not be null"); + } + if (!this.attribHandlerList.contains(handler)) { + this.attribHandlerList.add(handler); + } + this.attribHandlerMap.put(name, handler); + } + + /** + * Finds an attribute handler {@link CookieAttributeHandler} for the + * given attribute. Returns null if no attribute handler is + * found for the specified attribute. + * + * @param name attribute name. e.g. Domain, Path, etc. + * @return an attribute handler or null + */ + protected CookieAttributeHandler findAttribHandler(final String name) { + return (CookieAttributeHandler) this.attribHandlerMap.get(name); + } + + /** + * Gets attribute handler {@link CookieAttributeHandler} for the + * given attribute. + * + * @param name attribute name. e.g. Domain, Path, etc. + * @throws IllegalStateException if handler not found for the + * specified attribute. + */ + protected CookieAttributeHandler getAttribHandler(final String name) { + CookieAttributeHandler handler = findAttribHandler(name); + if (handler == null) { + throw new IllegalStateException("Handler not registered for " + + name + " attribute."); + } else { + return handler; + } + } + + protected Iterator getAttribHandlerIterator() { + return this.attribHandlerList.iterator(); + } + +} diff --git a/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java b/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java new file mode 100644 index 000000000..ae15af156 --- /dev/null +++ b/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java @@ -0,0 +1,121 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * ==================================================================== + * + * Copyright 2002-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.cookie.impl; + +import java.io.IOException; + +import org.apache.http.Header; +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieAttributeHandler; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.MalformedCookieException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestAbstractCookieSpec extends TestCase { + + public TestAbstractCookieSpec(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(TestAbstractCookieSpec.class); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestAbstractCookieSpec.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + private static class DummyCookieSpec extends AbstractCookieSpec { + + public Header[] formatCookies(Cookie[] cookies) { + return null; + } + + public boolean match(Cookie cookie, CookieOrigin origin) { + return true; + } + + public Cookie[] parse(Header header, CookieOrigin origin) throws MalformedCookieException { + return null; + } + + public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { + } + + } + + private static class DummyCookieAttribHandler implements CookieAttributeHandler { + + public boolean match(Cookie cookie, CookieOrigin origin) { + return true; + } + + public void parse(Cookie cookie, String value) throws MalformedCookieException { + } + + public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { + } + + } + + public void testSimpleRegisterAndGet() throws IOException { + CookieAttributeHandler h1 = new DummyCookieAttribHandler(); + CookieAttributeHandler h2 = new DummyCookieAttribHandler(); + + AbstractCookieSpec cookiespec = new DummyCookieSpec(); + cookiespec.registerAttribHandler("this", h1); + cookiespec.registerAttribHandler("that", h2); + + assertTrue(h1 == cookiespec.getAttribHandler("this")); + assertTrue(h2 == cookiespec.getAttribHandler("that")); + } + + public void testInvalidHandler() throws IOException { + CookieAttributeHandler h1 = new DummyCookieAttribHandler(); + CookieAttributeHandler h2 = new DummyCookieAttribHandler(); + + AbstractCookieSpec cookiespec = new DummyCookieSpec(); + cookiespec.registerAttribHandler("this", h1); + cookiespec.registerAttribHandler("that", h2); + + assertNull(cookiespec.findAttribHandler("whatever")); + try { + cookiespec.getAttribHandler("whatever"); + fail("IllegalStateException should have been thrown"); + } catch (IllegalStateException ex) { + // expected + } + } + +} \ No newline at end of file