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
28 package org.apache.hc.client5.http;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.apache.hc.client5.http.auth.AuthCache;
34 import org.apache.hc.client5.http.auth.AuthScheme;
35 import org.apache.hc.client5.http.auth.AuthSchemeFactory;
36 import org.apache.hc.client5.http.auth.CredentialsProvider;
37 import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
38 import org.apache.hc.client5.http.cookie.CookieSpecFactory;
39 import org.apache.hc.client5.http.cookie.CookieStore;
40 import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
41 import org.apache.hc.client5.http.impl.auth.BasicScheme;
42 import org.apache.hc.client5.http.protocol.HttpClientContext;
43 import org.apache.hc.client5.http.routing.RoutingSupport;
44 import org.apache.hc.core5.http.HttpHost;
45 import org.apache.hc.core5.http.config.Lookup;
46 import org.apache.hc.core5.util.Args;
47
48
49
50
51
52
53 public abstract class AbstractClientContextBuilder<T extends HttpClientContext> {
54
55 private final SchemePortResolver schemePortResolver;
56
57 private Lookup<CookieSpecFactory> cookieSpecRegistry;
58 private Lookup<AuthSchemeFactory> authSchemeRegistry;
59 private CookieStore cookieStore;
60 private CredentialsProvider credentialsProvider;
61 private AuthCache authCache;
62 private Map<HttpHost, AuthScheme> authSchemeMap;
63
64 protected AbstractClientContextBuilder(final SchemePortResolver schemePortResolver) {
65 this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
66 }
67
68 public AbstractClientContextBuilder<T> useCookieSpecRegistry(final Lookup<CookieSpecFactory> cookieSpecRegistry) {
69 this.cookieSpecRegistry = cookieSpecRegistry;
70 return this;
71 }
72
73 public AbstractClientContextBuilder<T> useAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
74 this.authSchemeRegistry = authSchemeRegistry;
75 return this;
76 }
77
78 public AbstractClientContextBuilder<T> useCookieStore(final CookieStore cookieStore) {
79 this.cookieStore = cookieStore;
80 return this;
81 }
82
83 public AbstractClientContextBuilder<T> useCredentialsProvider(final CredentialsProvider credentialsProvider) {
84 this.credentialsProvider = credentialsProvider;
85 return this;
86 }
87
88 public AbstractClientContextBuilder<T> useAuthCache(final AuthCache authCache) {
89 this.authCache = authCache;
90 return this;
91 }
92
93 public AbstractClientContextBuilder<T> preemptiveAuth(final HttpHost host, final AuthScheme authScheme) {
94 Args.notNull(host, "HTTP host");
95 if (authSchemeMap == null) {
96 authSchemeMap = new HashMap<>();
97 }
98 authSchemeMap.put(RoutingSupport.normalize(host, schemePortResolver), authScheme);
99 return this;
100 }
101
102 public AbstractClientContextBuilder<T> preemptiveBasicAuth(final HttpHost host, final UsernamePasswordCredentials credentials) {
103 Args.notNull(host, "HTTP host");
104 final BasicScheme authScheme = new BasicScheme();
105 authScheme.initPreemptive(credentials);
106 preemptiveAuth(host, authScheme);
107 return this;
108 }
109
110 protected abstract T createContext();
111
112 public T build() {
113 final T context = createContext();
114 context.setCookieSpecRegistry(cookieSpecRegistry);
115 context.setAuthSchemeRegistry(authSchemeRegistry);
116 context.setCookieStore(cookieStore);
117 context.setCredentialsProvider(credentialsProvider);
118 context.setAuthCache(authCache);
119 if (authSchemeMap != null) {
120 for (final Map.Entry<HttpHost, AuthScheme> entry : authSchemeMap.entrySet()) {
121 context.resetAuthExchange(entry.getKey(), entry.getValue());
122 }
123 }
124 return context;
125 }
126
127 }