1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/SchemeRegistry.java $
3 * $Revision: 648356 $
4 * $Date: 2008-04-15 10:57:53 -0700 (Tue, 15 Apr 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 */
31package org.apache.http.conn.scheme;
32
33import java.util.ArrayList;
34import java.util.LinkedHashMap;
35import java.util.List;
36import java.util.Map;
37
38import org.apache.http.HttpHost;
39
40/**
41 * A set of supported protocol {@link Scheme schemes}.
42 * Schemes are identified by lowercase names.
43 *
44 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
45 *
46 *
47 * <!-- empty lines to avoid svn diff problems -->
48 * @version   $Revision: 648356 $ $Date: 2008-04-15 10:57:53 -0700 (Tue, 15 Apr 2008) $
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 SchemeRegistry {
58
59    /** The available schemes in this registry. */
60    private final Map<String,Scheme> registeredSchemes;
61
62
63    /**
64     * Creates a new, empty scheme registry.
65     */
66    public SchemeRegistry() {
67        super();
68        registeredSchemes = new LinkedHashMap<String,Scheme>();
69    }
70
71
72    /**
73     * Obtains a scheme by name.
74     *
75     * @param name      the name of the scheme to look up (in lowercase)
76     *
77     * @return  the scheme, never <code>null</code>
78     *
79     * @throws IllegalStateException
80     *          if the scheme with the given name is not registered
81     */
82    public synchronized final Scheme getScheme(String name) {
83        Scheme found = get(name);
84        if (found == null) {
85            throw new IllegalStateException
86                ("Scheme '"+name+"' not registered.");
87        }
88        return found;
89    }
90
91
92    /**
93     * Obtains the scheme for a host.
94     * Convenience method for <code>getScheme(host.getSchemeName())</pre>
95     *
96     * @param host      the host for which to obtain the scheme
97     *
98     * @return  the scheme for the given host, never <code>null</code>
99     *
100     * @throws IllegalStateException
101     *          if a scheme with the respective name is not registered
102     */
103    public synchronized final Scheme getScheme(HttpHost host) {
104        if (host == null) {
105            throw new IllegalArgumentException("Host must not be null.");
106        }
107        return getScheme(host.getSchemeName());
108    }
109
110
111    /**
112     * Obtains a scheme by name, if registered.
113     *
114     * @param name      the name of the scheme to look up (in lowercase)
115     *
116     * @return  the scheme, or
117     *          <code>null</code> if there is none by this name
118     */
119    public synchronized final Scheme get(String name) {
120        if (name == null)
121            throw new IllegalArgumentException("Name must not be null.");
122
123        // leave it to the caller to use the correct name - all lowercase
124        //name = name.toLowerCase();
125        Scheme found = registeredSchemes.get(name);
126        return found;
127    }
128
129
130    /**
131     * Registers a scheme.
132     * The scheme can later be retrieved by its name
133     * using {@link #getScheme(String) getScheme} or {@link #get get}.
134     *
135     * @param sch       the scheme to register
136     *
137     * @return  the scheme previously registered with that name, or
138     *          <code>null</code> if none was registered
139     */
140    public synchronized final Scheme register(Scheme sch) {
141        if (sch == null)
142            throw new IllegalArgumentException("Scheme must not be null.");
143
144        Scheme old = registeredSchemes.put(sch.getName(), sch);
145        return old;
146    }
147
148
149    /**
150     * Unregisters a scheme.
151     *
152     * @param name      the name of the scheme to unregister (in lowercase)
153     *
154     * @return  the unregistered scheme, or
155     *          <code>null</code> if there was none
156     */
157    public synchronized final Scheme unregister(String name) {
158        if (name == null)
159            throw new IllegalArgumentException("Name must not be null.");
160
161        // leave it to the caller to use the correct name - all lowercase
162        //name = name.toLowerCase();
163        Scheme gone = registeredSchemes.remove(name);
164        return gone;
165    }
166
167
168    /**
169     * Obtains the names of the registered schemes in their default order.
170     *
171     * @return  List containing registered scheme names.
172     */
173    public synchronized final List<String> getSchemeNames() {
174        return new ArrayList<String>(registeredSchemes.keySet());
175    }
176
177    /**
178     * Populates the internal collection of registered {@link Scheme protocol schemes}
179     * with the content of the map passed as a parameter.
180     *
181     * @param map protocol schemes
182     */
183    public synchronized void setItems(final Map<String, Scheme> map) {
184        if (map == null) {
185            return;
186        }
187        registeredSchemes.clear();
188        registeredSchemes.putAll(map);
189    }
190
191} // class SchemeRegistry
192
193