1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/cookie/CookieSpecRegistry.java $
3 * $Revision: 652950 $
4 * $Date: 2008-05-02 16:49:48 -0700 (Fri, 02 May 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.cookie;
33
34import java.util.ArrayList;
35import java.util.LinkedHashMap;
36import java.util.List;
37import java.util.Locale;
38import java.util.Map;
39
40import org.apache.http.params.HttpParams;
41
42/**
43 * Cookie specification registry that can be used to obtain the corresponding
44 * cookie specification implementation for a given type of type or version of
45 * cookie.
46 *
47 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49 *
50 * @since 4.0
51 *
52 * @deprecated Please use {@link java.net.URL#openConnection} instead.
53 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
54 *     for further details.
55 */
56@Deprecated
57public final class CookieSpecRegistry {
58
59    private final Map<String,CookieSpecFactory> registeredSpecs;
60
61    public CookieSpecRegistry() {
62        super();
63        this.registeredSpecs = new LinkedHashMap<String,CookieSpecFactory>();
64    }
65
66    /**
67     * Registers a {@link CookieSpecFactory} with the given identifier.
68     * If a specification with the given name already exists it will be overridden.
69     * This nameis the same one used to retrieve the {@link CookieSpecFactory}
70     * from {@link #getCookieSpec(String)}.
71     *
72     * @param name the identifier for this specification
73     * @param factory the {@link CookieSpecFactory} class to register
74     *
75     * @see #getCookieSpec(String)
76     */
77    public synchronized void register(final String name, final CookieSpecFactory factory) {
78         if (name == null) {
79             throw new IllegalArgumentException("Name may not be null");
80         }
81        if (factory == null) {
82            throw new IllegalArgumentException("Cookie spec factory may not be null");
83        }
84        registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
85    }
86
87    /**
88     * Unregisters the {@link CookieSpecFactory} with the given ID.
89     *
90     * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
91     */
92    public synchronized void unregister(final String id) {
93         if (id == null) {
94             throw new IllegalArgumentException("Id may not be null");
95         }
96         registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
97    }
98
99    /**
100     * Gets the {@link CookieSpec cookie specification} with the given ID.
101     *
102     * @param name the {@link CookieSpec cookie specification} identifier
103     * @param params the {@link HttpParams HTTP parameters} for the cookie
104     *  specification.
105     *
106     * @return {@link CookieSpec cookie specification}
107     *
108     * @throws IllegalStateException if a policy with the given name cannot be found
109     */
110    public synchronized CookieSpec getCookieSpec(final String name, final HttpParams params)
111        throws IllegalStateException {
112
113        if (name == null) {
114            throw new IllegalArgumentException("Name may not be null");
115        }
116        CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
117        if (factory != null) {
118            return factory.newInstance(params);
119        } else {
120            throw new IllegalStateException("Unsupported cookie spec: " + name);
121        }
122    }
123
124    /**
125     * Gets the {@link CookieSpec cookie specification} with the given name.
126     *
127     * @param name the {@link CookieSpec cookie specification} identifier
128     *
129     * @return {@link CookieSpec cookie specification}
130     *
131     * @throws IllegalStateException if a policy with the given name cannot be found
132     */
133    public synchronized CookieSpec getCookieSpec(final String name)
134        throws IllegalStateException {
135        return getCookieSpec(name, null);
136    }
137
138    /**
139     * Obtains a list containing names of all registered {@link CookieSpec cookie
140     * specs} in their default order.
141     *
142     * Note that the DEFAULT policy (if present) is likely to be the same
143     * as one of the other policies, but does not have to be.
144     *
145     * @return list of registered cookie spec names
146     */
147    public synchronized List<String> getSpecNames(){
148        return new ArrayList<String>(registeredSpecs.keySet());
149    }
150
151    /**
152     * Populates the internal collection of registered {@link CookieSpec cookie
153     * specs} with the content of the map passed as a parameter.
154     *
155     * @param map cookie specs
156     */
157    public synchronized void setItems(final Map<String, CookieSpecFactory> map) {
158        if (map == null) {
159            return;
160        }
161        registeredSpecs.clear();
162        registeredSpecs.putAll(map);
163    }
164
165}
166