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
This commit is contained in:
Oleg Kalnichevski 2006-05-10 15:13:32 +00:00
parent bdbd032f1c
commit b4a8206230
4 changed files with 253 additions and 2 deletions

View File

@ -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 {

View File

@ -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;

View File

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

View File

@ -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
}
}
}