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 public static HttpClientContext adapt(final HttpContext context) {
134 Args.notNull(context, "HTTP context");
135 if (context instanceof HttpClientContext) {
136 return (HttpClientContext) context;
137 }
138 return new HttpClientContext(context);
139 }
140
141 public static HttpClientContext create() {
142 return new HttpClientContext(new BasicHttpContext());
143 }
144
145 public HttpClientContext(final HttpContext context) {
146 super(context);
147 }
148
149 public HttpClientContext() {
150 super();
151 }
152
153 public RouteInfo getHttpRoute() {
154 return getAttribute(HTTP_ROUTE, HttpRoute.class);
155 }
156
157 public RedirectLocations getRedirectLocations() {
158 return getAttribute(REDIRECT_LOCATIONS, RedirectLocations.class);
159 }
160
161 public CookieStore getCookieStore() {
162 return getAttribute(COOKIE_STORE, CookieStore.class);
163 }
164
165 public void setCookieStore(final CookieStore cookieStore) {
166 setAttribute(COOKIE_STORE, cookieStore);
167 }
168
169 public CookieSpec getCookieSpec() {
170 return getAttribute(COOKIE_SPEC, CookieSpec.class);
171 }
172
173 public CookieOrigin getCookieOrigin() {
174 return getAttribute(COOKIE_ORIGIN, CookieOrigin.class);
175 }
176
177 private <T> Lookup<T> getLookup(final String name, final Class<T> clazz) {
178 return getAttribute(name, Lookup.class);
179 }
180
181 public Lookup<CookieSpecFactory> getCookieSpecRegistry() {
182 return getLookup(COOKIESPEC_REGISTRY, CookieSpecFactory.class);
183 }
184
185 public void setCookieSpecRegistry(final Lookup<CookieSpecFactory> lookup) {
186 setAttribute(COOKIESPEC_REGISTRY, lookup);
187 }
188
189 public Lookup<AuthSchemeFactory> getAuthSchemeRegistry() {
190 return getLookup(AUTHSCHEME_REGISTRY, AuthSchemeFactory.class);
191 }
192
193 public void setAuthSchemeRegistry(final Lookup<AuthSchemeFactory> lookup) {
194 setAttribute(AUTHSCHEME_REGISTRY, lookup);
195 }
196
197 public CredentialsProvider getCredentialsProvider() {
198 return getAttribute(CREDS_PROVIDER, CredentialsProvider.class);
199 }
200
201 public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
202 setAttribute(CREDS_PROVIDER, credentialsProvider);
203 }
204
205 public AuthCache getAuthCache() {
206 return getAttribute(AUTH_CACHE, AuthCache.class);
207 }
208
209 public void setAuthCache(final AuthCache authCache) {
210 setAttribute(AUTH_CACHE, authCache);
211 }
212
213
214
215
216 @SuppressWarnings("unchecked")
217 public Map<HttpHost, AuthExchange> getAuthExchanges() {
218 Map<HttpHost, AuthExchange> map = (Map<HttpHost, AuthExchange>) getAttribute(AUTH_EXCHANGE_MAP);
219 if (map == null) {
220 map = new HashMap<>();
221 setAttribute(AUTH_EXCHANGE_MAP, map);
222 }
223 return map;
224 }
225
226
227
228
229 public AuthExchange getAuthExchange(final HttpHost host) {
230 final Map<HttpHost, AuthExchange> authExchangeMap = getAuthExchanges();
231 AuthExchange authExchange = authExchangeMap.get(host);
232 if (authExchange == null) {
233 authExchange = new AuthExchange();
234 authExchangeMap.put(host, authExchange);
235 }
236 return authExchange;
237 }
238
239
240
241
242 public void setAuthExchange(final HttpHost host, final AuthExchange authExchange) {
243 final Map<HttpHost, AuthExchange> authExchangeMap = getAuthExchanges();
244 authExchangeMap.put(host, authExchange);
245 }
246
247
248
249
250 public void resetAuthExchange(final HttpHost host, final AuthScheme authScheme) {
251 final AuthExchangethExchange.html#AuthExchange">AuthExchange authExchange = new AuthExchange();
252 authExchange.select(authScheme);
253 final Map<HttpHost, AuthExchange> authExchangeMap = getAuthExchanges();
254 authExchangeMap.put(host, authExchange);
255 }
256
257 public <T> T getUserToken(final Class<T> clazz) {
258 return getAttribute(USER_TOKEN, clazz);
259 }
260
261 public Object getUserToken() {
262 return getAttribute(USER_TOKEN);
263 }
264
265 public void setUserToken(final Object obj) {
266 setAttribute(USER_TOKEN, obj);
267 }
268
269 public RequestConfig getRequestConfig() {
270 final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class);
271 return config != null ? config : RequestConfig.DEFAULT;
272 }
273
274 public void setRequestConfig(final RequestConfig config) {
275 setAttribute(REQUEST_CONFIG, config);
276 }
277
278 }