1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/AuthSchemeRegistry.java $
3 * $Revision: 652950 $
4 * $Date: 2008-05-02 16:49:48 -0700 (Fri, 02 May 2008) $
5 *
6 * ====================================================================
7 *
8 *  Licensed to the Apache Software Foundation (ASF) under one or more
9 *  contributor license agreements.  See the NOTICE file distributed with
10 *  this work for additional information regarding copyright ownership.
11 *  The ASF licenses this file to You under the Apache License, Version 2.0
12 *  (the "License"); you may not use this file except in compliance with
13 *  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, software
18 *  distributed under the License is distributed on an "AS IS" BASIS,
19 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 *  See the License for the specific language governing permissions and
21 *  limitations under the License.
22 * ====================================================================
23 *
24 * This software consists of voluntary contributions made by many
25 * individuals on behalf of the Apache Software Foundation.  For more
26 * information on the Apache Software Foundation, please see
27 * <http://www.apache.org/>.
28 *
29 */
30
31package org.apache.http.auth;
32
33import java.util.ArrayList;
34import java.util.LinkedHashMap;
35import java.util.List;
36import java.util.Locale;
37import java.util.Map;
38
39import org.apache.http.params.HttpParams;
40
41/**
42 * Authentication scheme registry that can be used to obtain the corresponding
43 * authentication scheme implementation for a given type of authorization challenge.
44 *
45 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46 *
47 *
48 * @version $Revision: 652950 $
49 * @since 4.0
50 */
51public final class AuthSchemeRegistry {
52
53    private final Map<String,AuthSchemeFactory> registeredSchemes;
54
55    public AuthSchemeRegistry() {
56        super();
57        this.registeredSchemes = new LinkedHashMap<String,AuthSchemeFactory>();
58    }
59
60    /**
61     * Registers a {@link AuthSchemeFactory} with  the given identifier. If a factory with the
62     * given name already exists it will be overridden. This name is the same one used to
63     * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}.
64     *
65     * <p>
66     * Please note that custom authentication preferences, if used, need to be updated accordingly
67     * for the new {@link AuthScheme authentication scheme} to take effect.
68     * </p>
69     *
70     * @param name the identifier for this scheme
71     * @param factory the {@link AuthSchemeFactory} class to register
72     *
73     * @see #getAuthScheme
74     */
75    public synchronized void register(
76            final String name,
77            final AuthSchemeFactory factory) {
78         if (name == null) {
79             throw new IllegalArgumentException("Name may not be null");
80         }
81        if (factory == null) {
82            throw new IllegalArgumentException("Authentication scheme factory may not be null");
83        }
84        registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory);
85    }
86
87    /**
88     * Unregisters the class implementing an {@link AuthScheme authentication scheme} with
89     * the given name.
90     *
91     * @param name the identifier of the class to unregister
92     */
93    public synchronized void unregister(final String name) {
94         if (name == null) {
95             throw new IllegalArgumentException("Name may not be null");
96         }
97        registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH));
98    }
99
100    /**
101     * Gets the {@link AuthScheme authentication scheme} with the given name.
102     *
103     * @param name the {@link AuthScheme authentication scheme} identifier
104     * @param params the {@link HttpParams HTTP parameters} for the authentication
105     *  scheme.
106     *
107     * @return {@link AuthScheme authentication scheme}
108     *
109     * @throws IllegalStateException if a scheme with the given name cannot be found
110     */
111    public synchronized AuthScheme getAuthScheme(final String name, final HttpParams params)
112        throws IllegalStateException {
113
114        if (name == null) {
115            throw new IllegalArgumentException("Name may not be null");
116        }
117        AuthSchemeFactory factory = registeredSchemes.get(name.toLowerCase(Locale.ENGLISH));
118        if (factory != null) {
119            return factory.newInstance(params);
120        } else {
121            throw new IllegalStateException("Unsupported authentication scheme: " + name);
122        }
123    }
124
125    /**
126     * Obtains a list containing names of all registered {@link AuthScheme authentication
127     * schemes} in their default order.
128     *
129     * @return list of registered scheme names
130     */
131    public synchronized List<String> getSchemeNames() {
132        return new ArrayList<String>(registeredSchemes.keySet());
133    }
134
135    /**
136     * Populates the internal collection of registered {@link AuthScheme authentication schemes}
137     * with the content of the map passed as a parameter.
138     *
139     * @param map authentication schemes
140     */
141    public synchronized void setItems(final Map<String, AuthSchemeFactory> map) {
142        if (map == null) {
143            return;
144        }
145        registeredSchemes.clear();
146        registeredSchemes.putAll(map);
147    }
148
149}
150