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.cookie;
michael@0:
michael@0: import java.util.ArrayList;
michael@0: import java.util.List;
michael@0: import java.util.Locale;
michael@0: import java.util.Map;
michael@0: import java.util.concurrent.ConcurrentHashMap;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0:
michael@0: /**
michael@0: * Cookie specification registry that can be used to obtain the corresponding
michael@0: * cookie specification implementation for a given type of type or version of
michael@0: * cookie.
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @ThreadSafe
michael@0: public final class CookieSpecRegistry {
michael@0:
michael@0: private final ConcurrentHashMap registeredSpecs;
michael@0:
michael@0: public CookieSpecRegistry() {
michael@0: super();
michael@0: this.registeredSpecs = new ConcurrentHashMap();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Registers a {@link CookieSpecFactory} with the given identifier.
michael@0: * If a specification with the given name already exists it will be overridden.
michael@0: * This nameis the same one used to retrieve the {@link CookieSpecFactory}
michael@0: * from {@link #getCookieSpec(String)}.
michael@0: *
michael@0: * @param name the identifier for this specification
michael@0: * @param factory the {@link CookieSpecFactory} class to register
michael@0: *
michael@0: * @see #getCookieSpec(String)
michael@0: */
michael@0: public void register(final String name, final CookieSpecFactory factory) {
michael@0: if (name == null) {
michael@0: throw new IllegalArgumentException("Name may not be null");
michael@0: }
michael@0: if (factory == null) {
michael@0: throw new IllegalArgumentException("Cookie spec factory may not be null");
michael@0: }
michael@0: registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Unregisters the {@link CookieSpecFactory} with the given ID.
michael@0: *
michael@0: * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
michael@0: */
michael@0: public void unregister(final String id) {
michael@0: if (id == null) {
michael@0: throw new IllegalArgumentException("Id may not be null");
michael@0: }
michael@0: registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
michael@0: }
michael@0:
michael@0: /**
michael@0: * Gets the {@link CookieSpec cookie specification} with the given ID.
michael@0: *
michael@0: * @param name the {@link CookieSpec cookie specification} identifier
michael@0: * @param params the {@link HttpParams HTTP parameters} for the cookie
michael@0: * specification.
michael@0: *
michael@0: * @return {@link CookieSpec cookie specification}
michael@0: *
michael@0: * @throws IllegalStateException if a policy with the given name cannot be found
michael@0: */
michael@0: public CookieSpec getCookieSpec(final String name, final HttpParams params)
michael@0: throws IllegalStateException {
michael@0:
michael@0: if (name == null) {
michael@0: throw new IllegalArgumentException("Name may not be null");
michael@0: }
michael@0: CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
michael@0: if (factory != null) {
michael@0: return factory.newInstance(params);
michael@0: } else {
michael@0: throw new IllegalStateException("Unsupported cookie spec: " + name);
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Gets the {@link CookieSpec cookie specification} with the given name.
michael@0: *
michael@0: * @param name the {@link CookieSpec cookie specification} identifier
michael@0: *
michael@0: * @return {@link CookieSpec cookie specification}
michael@0: *
michael@0: * @throws IllegalStateException if a policy with the given name cannot be found
michael@0: */
michael@0: public CookieSpec getCookieSpec(final String name)
michael@0: throws IllegalStateException {
michael@0: return getCookieSpec(name, null);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Obtains a list containing the names of all registered {@link CookieSpec cookie
michael@0: * specs}.
michael@0: *
michael@0: * Note that the DEFAULT policy (if present) is likely to be the same
michael@0: * as one of the other policies, but does not have to be.
michael@0: *
michael@0: * @return list of registered cookie spec names
michael@0: */
michael@0: public List getSpecNames(){
michael@0: return new ArrayList(registeredSpecs.keySet());
michael@0: }
michael@0:
michael@0: /**
michael@0: * Populates the internal collection of registered {@link CookieSpec cookie
michael@0: * specs} with the content of the map passed as a parameter.
michael@0: *
michael@0: * @param map cookie specs
michael@0: */
michael@0: public void setItems(final Map map) {
michael@0: if (map == null) {
michael@0: return;
michael@0: }
michael@0: registeredSpecs.clear();
michael@0: registeredSpecs.putAll(map);
michael@0: }
michael@0:
michael@0: }