View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.auth;
28  
29  import java.util.Map;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  import org.apache.hc.client5.http.auth.AuthScope;
33  import org.apache.hc.client5.http.auth.Credentials;
34  import org.apache.hc.client5.http.auth.CredentialsStore;
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.http.protocol.HttpContext;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * Default implementation of {@link CredentialsStore}.
42   *
43   * @since 4.0
44   */
45  @Contract(threading = ThreadingBehavior.SAFE)
46  public class BasicCredentialsProvider implements CredentialsStore {
47  
48      private final ConcurrentHashMap<AuthScope, Credentials> credMap;
49  
50      /**
51       * Default constructor.
52       */
53      public BasicCredentialsProvider() {
54          super();
55          this.credMap = new ConcurrentHashMap<>();
56      }
57  
58      @Override
59      public void setCredentials(
60              final AuthScope authScope,
61              final Credentials credentials) {
62          Args.notNull(authScope, "Authentication scope");
63          credMap.put(authScope, credentials);
64      }
65  
66      /**
67       * Find matching {@link Credentials credentials} for the given authentication scope.
68       *
69       * @param map the credentials hash map
70       * @param authScope the {@link AuthScope authentication scope}
71       * @return the credentials
72       *
73       */
74      private static Credentials matchCredentials(
75              final Map<AuthScope, Credentials> map,
76              final AuthScope authScope) {
77          // see if we get a direct hit
78          Credentials creds = map.get(authScope);
79          if (creds == null) {
80              // Nope.
81              // Do a full scan
82              int bestMatchFactor  = -1;
83              AuthScope bestMatch  = null;
84              for (final AuthScope current: map.keySet()) {
85                  final int factor = authScope.match(current);
86                  if (factor > bestMatchFactor) {
87                      bestMatchFactor = factor;
88                      bestMatch = current;
89                  }
90              }
91              if (bestMatch != null) {
92                  creds = map.get(bestMatch);
93              }
94          }
95          return creds;
96      }
97  
98      @Override
99      public Credentials getCredentials(final AuthScope authScope,
100                                       final HttpContext httpContext) {
101         Args.notNull(authScope, "Authentication scope");
102         return matchCredentials(this.credMap, authScope);
103     }
104 
105     @Override
106     public void clear() {
107         this.credMap.clear();
108     }
109 
110     @Override
111     public String toString() {
112         return credMap.toString();
113     }
114 
115 }