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.protocol;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.apache.hc.client5.http.HttpRoute;
34 import org.apache.hc.client5.http.RouteInfo;
35 import org.apache.hc.client5.http.auth.AuthCache;
36 import org.apache.hc.client5.http.auth.AuthExchange;
37 import org.apache.hc.client5.http.auth.AuthScheme;
38 import org.apache.hc.client5.http.auth.AuthSchemeFactory;
39 import org.apache.hc.client5.http.auth.CredentialsProvider;
40 import org.apache.hc.client5.http.config.RequestConfig;
41 import org.apache.hc.client5.http.cookie.CookieOrigin;
42 import org.apache.hc.client5.http.cookie.CookieSpec;
43 import org.apache.hc.client5.http.cookie.CookieSpecFactory;
44 import org.apache.hc.client5.http.cookie.CookieStore;
45 import org.apache.hc.core5.http.HttpHost;
46 import org.apache.hc.core5.http.config.Lookup;
47 import org.apache.hc.core5.http.protocol.BasicHttpContext;
48 import org.apache.hc.core5.http.protocol.HttpContext;
49 import org.apache.hc.core5.http.protocol.HttpCoreContext;
50 import org.apache.hc.core5.util.Args;
51
52
53
54
55
56
57
58
59 public class HttpClientContext extends HttpCoreContext {
60
61
62
63
64
65 public static final String HTTP_ROUTE = "http.route";
66
67
68
69
70
71 public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
72
73
74
75
76
77 public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry";
78
79
80
81
82
83 public static final String COOKIE_SPEC = "http.cookie-spec";
84
85
86
87
88
89 public static final String COOKIE_ORIGIN = "http.cookie-origin";
90
91
92
93
94
95 public static final String COOKIE_STORE = "http.cookie-store";
96
97
98
99
100
101 public static final String CREDS_PROVIDER = "http.auth.credentials-provider";
102
103
104
105
106
107 public static final String AUTH_CACHE = "http.auth.auth-cache";
108
109
110
111
112
113 public static final String AUTH_EXCHANGE_MAP = "http.auth.exchanges";
114
115
116
117
118
119 public static final String USER_TOKEN = "http.user-token";
120
121
122
123
124
125 public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry";
126
127
128
129
130
131 public static final String REQUEST_CONFIG = "http.request-config";
132
133
134
135
136
137 public static final String EXCHANGE_ID = "http.exchange-id";
138
139 public static HttpClientContext adapt(final HttpContext context) {
140 Args.notNull(context, "HTTP context");
141 if (context instanceof HttpClientContext) {
142 return (HttpClientContext) context;
143 }
144 return new HttpClientContext(context);
145 }
146
147 public static HttpClientContext create() {
148 return new HttpClientContext(new BasicHttpContext());
149 }
150
151 public HttpClientContext(final HttpContext context) {
152 super(context);
153 }
154
155 public HttpClientContext() {
156 super();
157 }
158
159 public RouteInfo getHttpRoute() {
160 return getAttribute(HTTP_ROUTE, HttpRoute.class);
161 }
162
163 public RedirectLocations getRedirectLocations() {
164 return getAttribute(REDIRECT_LOCATIONS, RedirectLocations.class);
165 }
166
167 public CookieStore getCookieStore() {
168 return getAttribute(COOKIE_STORE, CookieStore.class);
169 }
170
171 public void setCookieStore(final CookieStore cookieStore) {
172 setAttribute(COOKIE_STORE, cookieStore);
173 }
174
175 public CookieSpec getCookieSpec() {
176 return getAttribute(COOKIE_SPEC, CookieSpec.class);
177 }
178
179 public CookieOrigin getCookieOrigin() {
180 return getAttribute(COOKIE_ORIGIN, CookieOrigin.class);
181 }
182
183 @SuppressWarnings("unchecked")
184 private <T> Lookup<T> getLookup(final String name) {
185 return (Lookup<T>) getAttribute(name, Lookup.class);
186 }
187
188 public Lookup<CookieSpecFactory> getCookieSpecRegistry() {
189 return getLookup(COOKIESPEC_REGISTRY);
190 }
191
192 public void setCookieSpecRegistry(final Lookup<CookieSpecFactory> lookup) {
193 setAttribute(COOKIESPEC_REGISTRY, lookup);
194 }
195
196 public Lookup<AuthSchemeFactory> getAuthSchemeRegistry() {
197 return getLookup(AUTHSCHEME_REGISTRY);
198 }
199
200 public void setAuthSchemeRegistry(final Lookup<AuthSchemeFactory> lookup) {
201 setAttribute(AUTHSCHEME_REGISTRY, lookup);
202 }
203
204 public CredentialsProvider getCredentialsProvider() {
205 return getAttribute(CREDS_PROVIDER, CredentialsProvider.class);
206 }
207
208 public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
209 setAttribute(CREDS_PROVIDER, credentialsProvider);
210 }
211
212 public AuthCache getAuthCache() {
213 return getAttribute(AUTH_CACHE, AuthCache.class);
214 }
215
216 public void setAuthCache(final AuthCache authCache) {
217 setAttribute(AUTH_CACHE, authCache);
218 }
219
220
221
222
223 @SuppressWarnings("unchecked")
224 public Map<HttpHost, AuthExchange> getAuthExchanges() {
225 Map<HttpHost, AuthExchange> map = (Map<HttpHost, AuthExchange>) getAttribute(AUTH_EXCHANGE_MAP);
226 if (map == null) {
227 map = new HashMap<>();
228 setAttribute(AUTH_EXCHANGE_MAP, map);
229 }
230 return map;
231 }
232
233
234
235
236 public AuthExchange getAuthExchange(final HttpHost host) {
237 final Map<HttpHost, AuthExchange> authExchangeMap = getAuthExchanges();
238 AuthExchange authExchange = authExchangeMap.get(host);
239 if (authExchange == null) {
240 authExchange = new AuthExchange();
241 authExchangeMap.put(host, authExchange);
242 }
243 return authExchange;
244 }
245
246
247
248
249 public void setAuthExchange(final HttpHost host, final AuthExchange authExchange) {
250 final Map<HttpHost, AuthExchange> authExchangeMap = getAuthExchanges();
251 authExchangeMap.put(host, authExchange);
252 }
253
254
255
256
257 public void resetAuthExchange(final HttpHost host, final AuthScheme authScheme) {
258 final AuthExchange authExchange = new AuthExchange();
259 authExchange.select(authScheme);
260 final Map<HttpHost, AuthExchange> authExchangeMap = getAuthExchanges();
261 authExchangeMap.put(host, authExchange);
262 }
263
264 public <T> T getUserToken(final Class<T> clazz) {
265 return getAttribute(USER_TOKEN, clazz);
266 }
267
268 public Object getUserToken() {
269 return getAttribute(USER_TOKEN);
270 }
271
272 public void setUserToken(final Object obj) {
273 setAttribute(USER_TOKEN, obj);
274 }
275
276 public RequestConfig getRequestConfig() {
277 final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class);
278 return config != null ? config : RequestConfig.DEFAULT;
279 }
280
281 public void setRequestConfig(final RequestConfig config) {
282 setAttribute(REQUEST_CONFIG, config);
283 }
284
285
286
287
288 public String getExchangeId() {
289 return getAttribute(EXCHANGE_ID, String.class);
290 }
291
292
293
294
295 public void setExchangeId(final String id) {
296 setAttribute(EXCHANGE_ID, id);
297 }
298
299 }