001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.cookie;
029
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Locale;
033import java.util.Map;
034import java.util.concurrent.ConcurrentHashMap;
035
036import org.apache.http.HttpRequest;
037import org.apache.http.annotation.Contract;
038import org.apache.http.annotation.ThreadingBehavior;
039import org.apache.http.config.Lookup;
040import org.apache.http.params.HttpParams;
041import org.apache.http.protocol.ExecutionContext;
042import org.apache.http.protocol.HttpContext;
043import org.apache.http.util.Args;
044
045/**
046 * Cookie specification registry that can be used to obtain the corresponding
047 * cookie specification implementation for a given type of type or version of
048 * cookie.
049 *
050 * @since 4.0
051 *
052 * @deprecated (4.3) use {@link org.apache.http.config.Registry}.
053 */
054@Contract(threading = ThreadingBehavior.SAFE)
055@Deprecated
056public final class CookieSpecRegistry implements Lookup<CookieSpecProvider> {
057
058    private final ConcurrentHashMap<String,CookieSpecFactory> registeredSpecs;
059
060    public CookieSpecRegistry() {
061        super();
062        this.registeredSpecs = new ConcurrentHashMap<String,CookieSpecFactory>();
063    }
064
065    /**
066     * Registers a {@link CookieSpecFactory} with the given identifier.
067     * If a specification with the given name already exists it will be overridden.
068     * This nameis the same one used to retrieve the {@link CookieSpecFactory}
069     * from {@link #getCookieSpec(String)}.
070     *
071     * @param name the identifier for this specification
072     * @param factory the {@link CookieSpecFactory} class to register
073     *
074     * @see #getCookieSpec(String)
075     */
076    public void register(final String name, final CookieSpecFactory factory) {
077         Args.notNull(name, "Name");
078        Args.notNull(factory, "Cookie spec factory");
079        registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
080    }
081
082    /**
083     * Unregisters the {@link CookieSpecFactory} with the given ID.
084     *
085     * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
086     */
087    public void unregister(final String id) {
088         Args.notNull(id, "Id");
089         registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
090    }
091
092    /**
093     * Gets the {@link CookieSpec cookie specification} with the given ID.
094     *
095     * @param name the {@link CookieSpec cookie specification} identifier
096     * @param params the {@link HttpParams HTTP parameters} for the cookie
097     *  specification.
098     *
099     * @return {@link CookieSpec cookie specification}
100     *
101     * @throws IllegalStateException if a policy with the given name cannot be found
102     */
103    public CookieSpec getCookieSpec(final String name, final HttpParams params)
104        throws IllegalStateException {
105
106        Args.notNull(name, "Name");
107        final CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
108        if (factory != null) {
109            return factory.newInstance(params);
110        } else {
111            throw new IllegalStateException("Unsupported cookie spec: " + name);
112        }
113    }
114
115    /**
116     * Gets the {@link CookieSpec cookie specification} with the given name.
117     *
118     * @param name the {@link CookieSpec cookie specification} identifier
119     *
120     * @return {@link CookieSpec cookie specification}
121     *
122     * @throws IllegalStateException if a policy with the given name cannot be found
123     */
124    public CookieSpec getCookieSpec(final String name)
125        throws IllegalStateException {
126        return getCookieSpec(name, null);
127    }
128
129    /**
130     * Obtains a list containing the names of all registered {@link CookieSpec cookie
131     * specs}.
132     *
133     * Note that the DEFAULT policy (if present) is likely to be the same
134     * as one of the other policies, but does not have to be.
135     *
136     * @return list of registered cookie spec names
137     */
138    public List<String> getSpecNames(){
139        return new ArrayList<String>(registeredSpecs.keySet());
140    }
141
142    /**
143     * Populates the internal collection of registered {@link CookieSpec cookie
144     * specs} with the content of the map passed as a parameter.
145     *
146     * @param map cookie specs
147     */
148    public void setItems(final Map<String, CookieSpecFactory> map) {
149        if (map == null) {
150            return;
151        }
152        registeredSpecs.clear();
153        registeredSpecs.putAll(map);
154    }
155
156    @Override
157    public CookieSpecProvider lookup(final String name) {
158        return new CookieSpecProvider() {
159
160            @Override
161            public CookieSpec create(final HttpContext context) {
162                final HttpRequest request = (HttpRequest) context.getAttribute(
163                        ExecutionContext.HTTP_REQUEST);
164                return getCookieSpec(name, request.getParams());
165            }
166
167        };
168    }
169
170}