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.HashMap;
30  import java.util.Map;
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.CredentialsProvider;
35  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
36  import org.apache.hc.core5.http.HttpHost;
37  import org.apache.hc.core5.util.Args;
38  
39  /**
40   * {@link CredentialsProvider} builder.
41   *
42   * @since 5.2
43   */
44  public final class CredentialsProviderBuilder {
45  
46      private final Map<AuthScope, Credentials> credMap;
47  
48      public static CredentialsProviderBuilder create() {
49          return new CredentialsProviderBuilder();
50      }
51  
52      public CredentialsProviderBuilder() {
53          super();
54          this.credMap = new HashMap<>();
55      }
56  
57      public CredentialsProviderBuilder add(final AuthScope authScope, final Credentials credentials) {
58          Args.notNull(authScope, "Host");
59          credMap.put(authScope, credentials);
60          return this;
61      }
62  
63      public CredentialsProviderBuilder add(final AuthScope authScope, final String username, final char[] password) {
64          Args.notNull(authScope, "Host");
65          credMap.put(authScope, new UsernamePasswordCredentials(username, password));
66          return this;
67      }
68  
69      public CredentialsProviderBuilder add(final HttpHost httpHost, final Credentials credentials) {
70          Args.notNull(httpHost, "Host");
71          credMap.put(new AuthScope(httpHost), credentials);
72          return this;
73      }
74  
75      public CredentialsProviderBuilder add(final HttpHost httpHost, final String username, final char[] password) {
76          Args.notNull(httpHost, "Host");
77          credMap.put(new AuthScope(httpHost), new UsernamePasswordCredentials(username, password));
78          return this;
79      }
80  
81      public CredentialsProvider build() {
82          if (credMap.size() == 0) {
83              return new BasicCredentialsProvider();
84          } else if (credMap.size() == 1) {
85              final Map.Entry<AuthScope, Credentials> entry = credMap.entrySet().iterator().next();
86              return new SingleCredentialsProvider(entry.getKey(), entry.getValue());
87          } else {
88              return new FixedCredentialsProvider(credMap);
89          }
90      }
91  
92      static class Entry {
93  
94          final AuthScope authScope;
95          final Credentials credentials;
96  
97          Entry(final AuthScope authScope, final Credentials credentials) {
98              this.authScope = authScope;
99              this.credentials = credentials;
100         }
101 
102     }
103 
104 }