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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/auth/AuthSchemeRegistry.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.auth;
    1.31 +
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.List;
    1.34 +import java.util.Locale;
    1.35 +import java.util.Map;
    1.36 +import java.util.concurrent.ConcurrentHashMap;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
    1.39 +
    1.40 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.41 +
    1.42 +/**
    1.43 + * Authentication scheme registry that can be used to obtain the corresponding
    1.44 + * authentication scheme implementation for a given type of authorization challenge.
    1.45 + *
    1.46 + * @since 4.0
    1.47 + */
    1.48 +@ThreadSafe
    1.49 +public final class AuthSchemeRegistry {
    1.50 +
    1.51 +    private final ConcurrentHashMap<String,AuthSchemeFactory> registeredSchemes;
    1.52 +
    1.53 +    public AuthSchemeRegistry() {
    1.54 +        super();
    1.55 +        this.registeredSchemes = new ConcurrentHashMap<String,AuthSchemeFactory>();
    1.56 +    }
    1.57 +
    1.58 +    /**
    1.59 +     * Registers a {@link AuthSchemeFactory} with  the given identifier. If a factory with the
    1.60 +     * given name already exists it will be overridden. This name is the same one used to
    1.61 +     * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}.
    1.62 +     *
    1.63 +     * <p>
    1.64 +     * Please note that custom authentication preferences, if used, need to be updated accordingly
    1.65 +     * for the new {@link AuthScheme authentication scheme} to take effect.
    1.66 +     * </p>
    1.67 +     *
    1.68 +     * @param name the identifier for this scheme
    1.69 +     * @param factory the {@link AuthSchemeFactory} class to register
    1.70 +     *
    1.71 +     * @see #getAuthScheme
    1.72 +     */
    1.73 +    public void register(
    1.74 +            final String name,
    1.75 +            final AuthSchemeFactory factory) {
    1.76 +         if (name == null) {
    1.77 +             throw new IllegalArgumentException("Name may not be null");
    1.78 +         }
    1.79 +        if (factory == null) {
    1.80 +            throw new IllegalArgumentException("Authentication scheme factory may not be null");
    1.81 +        }
    1.82 +        registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory);
    1.83 +    }
    1.84 +
    1.85 +    /**
    1.86 +     * Unregisters the class implementing an {@link AuthScheme authentication scheme} with
    1.87 +     * the given name.
    1.88 +     *
    1.89 +     * @param name the identifier of the class to unregister
    1.90 +     */
    1.91 +    public void unregister(final String name) {
    1.92 +         if (name == null) {
    1.93 +             throw new IllegalArgumentException("Name may not be null");
    1.94 +         }
    1.95 +        registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH));
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Gets the {@link AuthScheme authentication scheme} with the given name.
   1.100 +     *
   1.101 +     * @param name the {@link AuthScheme authentication scheme} identifier
   1.102 +     * @param params the {@link HttpParams HTTP parameters} for the authentication
   1.103 +     *  scheme.
   1.104 +     *
   1.105 +     * @return {@link AuthScheme authentication scheme}
   1.106 +     *
   1.107 +     * @throws IllegalStateException if a scheme with the given name cannot be found
   1.108 +     */
   1.109 +    public AuthScheme getAuthScheme(final String name, final HttpParams params)
   1.110 +        throws IllegalStateException {
   1.111 +
   1.112 +        if (name == null) {
   1.113 +            throw new IllegalArgumentException("Name may not be null");
   1.114 +        }
   1.115 +        AuthSchemeFactory factory = registeredSchemes.get(name.toLowerCase(Locale.ENGLISH));
   1.116 +        if (factory != null) {
   1.117 +            return factory.newInstance(params);
   1.118 +        } else {
   1.119 +            throw new IllegalStateException("Unsupported authentication scheme: " + name);
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * Obtains a list containing the names of all registered {@link AuthScheme authentication
   1.125 +     * schemes}
   1.126 +     *
   1.127 +     * @return list of registered scheme names
   1.128 +     */
   1.129 +    public List<String> getSchemeNames() {
   1.130 +        return new ArrayList<String>(registeredSchemes.keySet());
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Populates the internal collection of registered {@link AuthScheme authentication schemes}
   1.135 +     * with the content of the map passed as a parameter.
   1.136 +     *
   1.137 +     * @param map authentication schemes
   1.138 +     */
   1.139 +    public void setItems(final Map<String, AuthSchemeFactory> map) {
   1.140 +        if (map == null) {
   1.141 +            return;
   1.142 +        }
   1.143 +        registeredSchemes.clear();
   1.144 +        registeredSchemes.putAll(map);
   1.145 +    }
   1.146 +
   1.147 +}

mercurial