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.impl.client; michael@0: 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.auth.AuthScope; michael@0: import ch.boye.httpclientandroidlib.auth.Credentials; michael@0: import ch.boye.httpclientandroidlib.client.CredentialsProvider; michael@0: michael@0: /** michael@0: * Default implementation of {@link CredentialsProvider}. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @ThreadSafe michael@0: public class BasicCredentialsProvider implements CredentialsProvider { michael@0: michael@0: private final ConcurrentHashMap credMap; michael@0: michael@0: /** michael@0: * Default constructor. michael@0: */ michael@0: public BasicCredentialsProvider() { michael@0: super(); michael@0: this.credMap = new ConcurrentHashMap(); michael@0: } michael@0: michael@0: public void setCredentials( michael@0: final AuthScope authscope, michael@0: final Credentials credentials) { michael@0: if (authscope == null) { michael@0: throw new IllegalArgumentException("Authentication scope may not be null"); michael@0: } michael@0: credMap.put(authscope, credentials); michael@0: } michael@0: michael@0: /** michael@0: * Find matching {@link Credentials credentials} for the given authentication scope. michael@0: * michael@0: * @param map the credentials hash map michael@0: * @param authscope the {@link AuthScope authentication scope} michael@0: * @return the credentials michael@0: * michael@0: */ michael@0: private static Credentials matchCredentials( michael@0: final Map map, michael@0: final AuthScope authscope) { michael@0: // see if we get a direct hit michael@0: Credentials creds = map.get(authscope); michael@0: if (creds == null) { michael@0: // Nope. michael@0: // Do a full scan michael@0: int bestMatchFactor = -1; michael@0: AuthScope bestMatch = null; michael@0: for (AuthScope current: map.keySet()) { michael@0: int factor = authscope.match(current); michael@0: if (factor > bestMatchFactor) { michael@0: bestMatchFactor = factor; michael@0: bestMatch = current; michael@0: } michael@0: } michael@0: if (bestMatch != null) { michael@0: creds = map.get(bestMatch); michael@0: } michael@0: } michael@0: return creds; michael@0: } michael@0: michael@0: public Credentials getCredentials(final AuthScope authscope) { michael@0: if (authscope == null) { michael@0: throw new IllegalArgumentException("Authentication scope may not be null"); michael@0: } michael@0: return matchCredentials(this.credMap, authscope); michael@0: } michael@0: michael@0: public void clear() { michael@0: this.credMap.clear(); michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: return credMap.toString(); michael@0: } michael@0: michael@0: }