michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.cookie;
michael@0:
michael@0: import java.util.Collection;
michael@0: import java.util.HashMap;
michael@0: import java.util.Map;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler;
michael@0: import ch.boye.httpclientandroidlib.cookie.CookieSpec;
michael@0:
michael@0: /**
michael@0: * Abstract cookie specification which can delegate the job of parsing,
michael@0: * validation or matching cookie attributes to a number of arbitrary
michael@0: * {@link CookieAttributeHandler}s.
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @NotThreadSafe // HashMap is not thread-safe
michael@0: public abstract class AbstractCookieSpec implements CookieSpec {
michael@0:
michael@0: /**
michael@0: * Stores attribute name -> attribute handler mappings
michael@0: */
michael@0: private final Map attribHandlerMap;
michael@0:
michael@0: /**
michael@0: * Default constructor
michael@0: * */
michael@0: public AbstractCookieSpec() {
michael@0: super();
michael@0: this.attribHandlerMap = new HashMap(10);
michael@0: }
michael@0:
michael@0: public void registerAttribHandler(
michael@0: final String name, final CookieAttributeHandler handler) {
michael@0: if (name == null) {
michael@0: throw new IllegalArgumentException("Attribute name may not be null");
michael@0: }
michael@0: if (handler == null) {
michael@0: throw new IllegalArgumentException("Attribute handler may not be null");
michael@0: }
michael@0: this.attribHandlerMap.put(name, handler);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Finds an attribute handler {@link CookieAttributeHandler} for the
michael@0: * given attribute. Returns null if no attribute handler is
michael@0: * found for the specified attribute.
michael@0: *
michael@0: * @param name attribute name. e.g. Domain, Path, etc.
michael@0: * @return an attribute handler or null
michael@0: */
michael@0: protected CookieAttributeHandler findAttribHandler(final String name) {
michael@0: return this.attribHandlerMap.get(name);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Gets attribute handler {@link CookieAttributeHandler} for the
michael@0: * given attribute.
michael@0: *
michael@0: * @param name attribute name. e.g. Domain, Path, etc.
michael@0: * @throws IllegalStateException if handler not found for the
michael@0: * specified attribute.
michael@0: */
michael@0: protected CookieAttributeHandler getAttribHandler(final String name) {
michael@0: CookieAttributeHandler handler = findAttribHandler(name);
michael@0: if (handler == null) {
michael@0: throw new IllegalStateException("Handler not registered for " +
michael@0: name + " attribute.");
michael@0: } else {
michael@0: return handler;
michael@0: }
michael@0: }
michael@0:
michael@0: protected Collection getAttribHandlers() {
michael@0: return this.attribHandlerMap.values();
michael@0: }
michael@0:
michael@0: }