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