1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/BasicCredentialsProvider.java $
3 * $Revision: 653041 $
4 * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 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
31package org.apache.http.impl.client;
32
33import java.util.HashMap;
34
35import org.apache.http.auth.AuthScope;
36import org.apache.http.auth.Credentials;
37import org.apache.http.client.CredentialsProvider;
38
39/**
40 * Default implementation of {@link CredentialsProvider}
41 *
42 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
43 * @author Rodney Waldhoff
44 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
45 * @author Sean C. Sullivan
46 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
47 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49 * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
50 *
51 * @since 4.0
52 *
53 * @deprecated Please use {@link java.net.URL#openConnection} instead.
54 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
55 *     for further details.
56 */
57@Deprecated
58public class BasicCredentialsProvider implements CredentialsProvider {
59
60    private final HashMap<AuthScope, Credentials> credMap;
61
62    /**
63     * Default constructor.
64     */
65    public BasicCredentialsProvider() {
66        super();
67        this.credMap = new HashMap<AuthScope, Credentials>();
68    }
69
70    /**
71     * Sets the {@link Credentials credentials} for the given authentication
72     * scope. Any previous credentials for the given scope will be overwritten.
73     *
74     * @param authscope the {@link AuthScope authentication scope}
75     * @param credentials the authentication {@link Credentials credentials}
76     * for the given scope.
77     *
78     * @see #getCredentials(AuthScope)
79     */
80    public synchronized void setCredentials(
81            final AuthScope authscope,
82            final Credentials credentials) {
83        if (authscope == null) {
84            throw new IllegalArgumentException("Authentication scope may not be null");
85        }
86        credMap.put(authscope, credentials);
87    }
88
89    /**
90     * Find matching {@link Credentials credentials} for the given authentication scope.
91     *
92     * @param map the credentials hash map
93     * @param authscope the {@link AuthScope authentication scope}
94     * @return the credentials
95     *
96     */
97    private static Credentials matchCredentials(
98            final HashMap<AuthScope, Credentials> map,
99            final AuthScope authscope) {
100        // see if we get a direct hit
101        Credentials creds = map.get(authscope);
102        if (creds == null) {
103            // Nope.
104            // Do a full scan
105            int bestMatchFactor  = -1;
106            AuthScope bestMatch  = null;
107            for (AuthScope current: map.keySet()) {
108                int factor = authscope.match(current);
109                if (factor > bestMatchFactor) {
110                    bestMatchFactor = factor;
111                    bestMatch = current;
112                }
113            }
114            if (bestMatch != null) {
115                creds = map.get(bestMatch);
116            }
117        }
118        return creds;
119    }
120
121    /**
122     * Get the {@link Credentials credentials} for the given authentication scope.
123     *
124     * @param authscope the {@link AuthScope authentication scope}
125     * @return the credentials
126     *
127     * @see #setCredentials(AuthScope, Credentials)
128     */
129    public synchronized Credentials getCredentials(final AuthScope authscope) {
130        if (authscope == null) {
131            throw new IllegalArgumentException("Authentication scope may not be null");
132        }
133        return matchCredentials(this.credMap, authscope);
134    }
135
136    @Override
137    public String toString() {
138        return credMap.toString();
139    }
140
141    /**
142     * Clears all credentials.
143     */
144    public synchronized void clear() {
145        this.credMap.clear();
146    }
147
148}
149