mobile/android/thirdparty/ch/boye/httpclientandroidlib/auth/AuthSchemeRegistry.java

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

mercurial