Home | History | Annotate | Download | only in auth
      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 
     31 package org.apache.http.auth;
     32 
     33 import java.util.ArrayList;
     34 import java.util.LinkedHashMap;
     35 import java.util.List;
     36 import java.util.Locale;
     37 import java.util.Map;
     38 
     39 import 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  *
     51  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     52  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     53  *     for further details.
     54  */
     55 @Deprecated
     56 public final class AuthSchemeRegistry {
     57 
     58     private final Map<String,AuthSchemeFactory> registeredSchemes;
     59 
     60     public AuthSchemeRegistry() {
     61         super();
     62         this.registeredSchemes = new LinkedHashMap<String,AuthSchemeFactory>();
     63     }
     64 
     65     /**
     66      * Registers a {@link AuthSchemeFactory} with  the given identifier. If a factory with the
     67      * given name already exists it will be overridden. This name is the same one used to
     68      * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}.
     69      *
     70      * <p>
     71      * Please note that custom authentication preferences, if used, need to be updated accordingly
     72      * for the new {@link AuthScheme authentication scheme} to take effect.
     73      * </p>
     74      *
     75      * @param name the identifier for this scheme
     76      * @param factory the {@link AuthSchemeFactory} class to register
     77      *
     78      * @see #getAuthScheme
     79      */
     80     public synchronized void register(
     81             final String name,
     82             final AuthSchemeFactory factory) {
     83          if (name == null) {
     84              throw new IllegalArgumentException("Name may not be null");
     85          }
     86         if (factory == null) {
     87             throw new IllegalArgumentException("Authentication scheme factory may not be null");
     88         }
     89         registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory);
     90     }
     91 
     92     /**
     93      * Unregisters the class implementing an {@link AuthScheme authentication scheme} with
     94      * the given name.
     95      *
     96      * @param name the identifier of the class to unregister
     97      */
     98     public synchronized void unregister(final String name) {
     99          if (name == null) {
    100              throw new IllegalArgumentException("Name may not be null");
    101          }
    102         registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH));
    103     }
    104 
    105     /**
    106      * Gets the {@link AuthScheme authentication scheme} with the given name.
    107      *
    108      * @param name the {@link AuthScheme authentication scheme} identifier
    109      * @param params the {@link HttpParams HTTP parameters} for the authentication
    110      *  scheme.
    111      *
    112      * @return {@link AuthScheme authentication scheme}
    113      *
    114      * @throws IllegalStateException if a scheme with the given name cannot be found
    115      */
    116     public synchronized AuthScheme getAuthScheme(final String name, final HttpParams params)
    117         throws IllegalStateException {
    118 
    119         if (name == null) {
    120             throw new IllegalArgumentException("Name may not be null");
    121         }
    122         AuthSchemeFactory factory = registeredSchemes.get(name.toLowerCase(Locale.ENGLISH));
    123         if (factory != null) {
    124             return factory.newInstance(params);
    125         } else {
    126             throw new IllegalStateException("Unsupported authentication scheme: " + name);
    127         }
    128     }
    129 
    130     /**
    131      * Obtains a list containing names of all registered {@link AuthScheme authentication
    132      * schemes} in their default order.
    133      *
    134      * @return list of registered scheme names
    135      */
    136     public synchronized List<String> getSchemeNames() {
    137         return new ArrayList<String>(registeredSchemes.keySet());
    138     }
    139 
    140     /**
    141      * Populates the internal collection of registered {@link AuthScheme authentication schemes}
    142      * with the content of the map passed as a parameter.
    143      *
    144      * @param map authentication schemes
    145      */
    146     public synchronized void setItems(final Map<String, AuthSchemeFactory> map) {
    147         if (map == null) {
    148             return;
    149         }
    150         registeredSchemes.clear();
    151         registeredSchemes.putAll(map);
    152     }
    153 
    154 }
    155