View Javadoc

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