1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
41
42
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 }