You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/05/10 17:13:36 UTC

svn commit: r405771 - in /jakarta/httpcomponents/httpclient/trunk/src: java/org/apache/http/cookie/ java/org/apache/http/cookie/impl/ test/org/apache/http/ test/org/apache/http/cookie/ test/org/apache/http/cookie/impl/

Author: olegk
Date: Wed May 10 08:13:32 2006
New Revision: 405771

URL: http://svn.apache.org/viewcvs?rev=405771&view=rev
Log:
Added an abstract cookie spec class

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java   (with props)
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java   (with props)
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieAttributeHandler.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieOrigin.java

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieAttributeHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieAttributeHandler.java?rev=405771&r1=405770&r2=405771&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieAttributeHandler.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieAttributeHandler.java Wed May 10 08:13:32 2006
@@ -30,14 +30,16 @@
 
 /**
  * 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 {
 

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieOrigin.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieOrigin.java?rev=405771&r1=405770&r2=405771&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieOrigin.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/CookieOrigin.java Wed May 10 08:13:32 2006
@@ -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 <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ * 
+ * @since 3.1
+ */
 public final class CookieOrigin {
 
 	private final String host;

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java?rev=405771&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java Wed May 10 08:13:32 2006
@@ -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
+ * <http://www.apache.org/>.
+ *
+ */ 
+
+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 <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ * 
+ * @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 <tt>null</tt> if no attribute handler is
+     * found for the specified attribute.
+     *
+     * @param name attribute name. e.g. Domain, Path, etc.
+     * @return an attribute handler or <tt>null</tt>
+     */
+    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();
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieSpec.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java?rev=405771&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java Wed May 10 08:13:32 2006
@@ -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
+ * <http://www.apache.org/>.
+ *
+ */
+
+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

Propchange: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain