michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.auth; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.List; michael@0: import java.util.Locale; michael@0: import java.util.Map; michael@0: import java.util.concurrent.ConcurrentHashMap; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: michael@0: /** michael@0: * Authentication scheme registry that can be used to obtain the corresponding michael@0: * authentication scheme implementation for a given type of authorization challenge. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @ThreadSafe michael@0: public final class AuthSchemeRegistry { michael@0: michael@0: private final ConcurrentHashMap registeredSchemes; michael@0: michael@0: public AuthSchemeRegistry() { michael@0: super(); michael@0: this.registeredSchemes = new ConcurrentHashMap(); michael@0: } michael@0: michael@0: /** michael@0: * Registers a {@link AuthSchemeFactory} with the given identifier. If a factory with the michael@0: * given name already exists it will be overridden. This name is the same one used to michael@0: * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}. michael@0: * michael@0: *

michael@0: * Please note that custom authentication preferences, if used, need to be updated accordingly michael@0: * for the new {@link AuthScheme authentication scheme} to take effect. michael@0: *

michael@0: * michael@0: * @param name the identifier for this scheme michael@0: * @param factory the {@link AuthSchemeFactory} class to register michael@0: * michael@0: * @see #getAuthScheme michael@0: */ michael@0: public void register( michael@0: final String name, michael@0: final AuthSchemeFactory factory) { michael@0: if (name == null) { michael@0: throw new IllegalArgumentException("Name may not be null"); michael@0: } michael@0: if (factory == null) { michael@0: throw new IllegalArgumentException("Authentication scheme factory may not be null"); michael@0: } michael@0: registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory); michael@0: } michael@0: michael@0: /** michael@0: * Unregisters the class implementing an {@link AuthScheme authentication scheme} with michael@0: * the given name. michael@0: * michael@0: * @param name the identifier of the class to unregister michael@0: */ michael@0: public void unregister(final String name) { michael@0: if (name == null) { michael@0: throw new IllegalArgumentException("Name may not be null"); michael@0: } michael@0: registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH)); michael@0: } michael@0: michael@0: /** michael@0: * Gets the {@link AuthScheme authentication scheme} with the given name. michael@0: * michael@0: * @param name the {@link AuthScheme authentication scheme} identifier michael@0: * @param params the {@link HttpParams HTTP parameters} for the authentication michael@0: * scheme. michael@0: * michael@0: * @return {@link AuthScheme authentication scheme} michael@0: * michael@0: * @throws IllegalStateException if a scheme with the given name cannot be found michael@0: */ michael@0: public AuthScheme getAuthScheme(final String name, final HttpParams params) michael@0: throws IllegalStateException { michael@0: michael@0: if (name == null) { michael@0: throw new IllegalArgumentException("Name may not be null"); michael@0: } michael@0: AuthSchemeFactory factory = registeredSchemes.get(name.toLowerCase(Locale.ENGLISH)); michael@0: if (factory != null) { michael@0: return factory.newInstance(params); michael@0: } else { michael@0: throw new IllegalStateException("Unsupported authentication scheme: " + name); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Obtains a list containing the names of all registered {@link AuthScheme authentication michael@0: * schemes} michael@0: * michael@0: * @return list of registered scheme names michael@0: */ michael@0: public List getSchemeNames() { michael@0: return new ArrayList(registeredSchemes.keySet()); michael@0: } michael@0: michael@0: /** michael@0: * Populates the internal collection of registered {@link AuthScheme authentication schemes} michael@0: * with the content of the map passed as a parameter. michael@0: * michael@0: * @param map authentication schemes michael@0: */ michael@0: public void setItems(final Map map) { michael@0: if (map == null) { michael@0: return; michael@0: } michael@0: registeredSchemes.clear(); michael@0: registeredSchemes.putAll(map); michael@0: } michael@0: michael@0: }