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.http.client.protocol;
29
30 import java.net.URI;
31 import java.util.List;
32
33 import org.apache.http.auth.AuthSchemeProvider;
34 import org.apache.http.auth.AuthState;
35 import org.apache.http.client.AuthCache;
36 import org.apache.http.client.CookieStore;
37 import org.apache.http.client.CredentialsProvider;
38 import org.apache.http.client.config.RequestConfig;
39 import org.apache.http.config.Lookup;
40 import org.apache.http.conn.routing.HttpRoute;
41 import org.apache.http.conn.routing.RouteInfo;
42 import org.apache.http.cookie.CookieOrigin;
43 import org.apache.http.cookie.CookieSpec;
44 import org.apache.http.cookie.CookieSpecProvider;
45 import org.apache.http.protocol.BasicHttpContext;
46 import org.apache.http.protocol.HttpContext;
47 import org.apache.http.protocol.HttpCoreContext;
48
49
50
51
52
53
54
55
56 public class HttpClientContext extends HttpCoreContext {
57
58
59
60
61
62 public static final String HTTP_ROUTE = "http.route";
63
64
65
66
67
68 public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
69
70
71
72
73
74 public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry";
75
76
77
78
79
80 public static final String COOKIE_SPEC = "http.cookie-spec";
81
82
83
84
85
86 public static final String COOKIE_ORIGIN = "http.cookie-origin";
87
88
89
90
91
92 public static final String COOKIE_STORE = "http.cookie-store";
93
94
95
96
97
98 public static final String CREDS_PROVIDER = "http.auth.credentials-provider";
99
100
101
102
103
104 public static final String AUTH_CACHE = "http.auth.auth-cache";
105
106
107
108
109
110 public static final String TARGET_AUTH_STATE = "http.auth.target-scope";
111
112
113
114
115
116 public static final String PROXY_AUTH_STATE = "http.auth.proxy-scope";
117
118
119
120
121
122 public static final String USER_TOKEN = "http.user-token";
123
124
125
126
127
128 public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry";
129
130
131
132
133
134 public static final String REQUEST_CONFIG = "http.request-config";
135
136 public static HttpClientContext adapt(final HttpContext context) {
137 return context instanceof HttpClientContext
138 ? (HttpClientContext) context
139 : new HttpClientContext(context);
140 }
141
142 public static HttpClientContext create() {
143 return new HttpClientContext(new BasicHttpContext());
144 }
145
146 public HttpClientContext(final HttpContext context) {
147 super(context);
148 }
149
150 public HttpClientContext() {
151 super();
152 }
153
154 public RouteInfo getHttpRoute() {
155 return getAttribute(HTTP_ROUTE, HttpRoute.class);
156 }
157
158 @SuppressWarnings("unchecked")
159 public List<URI> getRedirectLocations() {
160 return getAttribute(REDIRECT_LOCATIONS, List.class);
161 }
162
163 public CookieStore getCookieStore() {
164 return getAttribute(COOKIE_STORE, CookieStore.class);
165 }
166
167 public void setCookieStore(final CookieStore cookieStore) {
168 setAttribute(COOKIE_STORE, cookieStore);
169 }
170
171 public CookieSpec getCookieSpec() {
172 return getAttribute(COOKIE_SPEC, CookieSpec.class);
173 }
174
175 public CookieOrigin getCookieOrigin() {
176 return getAttribute(COOKIE_ORIGIN, CookieOrigin.class);
177 }
178
179 @SuppressWarnings("unchecked")
180 private <T> Lookup<T> getLookup(final String name, final Class<T> clazz) {
181 return getAttribute(name, Lookup.class);
182 }
183
184 public Lookup<CookieSpecProvider> getCookieSpecRegistry() {
185 return getLookup(COOKIESPEC_REGISTRY, CookieSpecProvider.class);
186 }
187
188 public void setCookieSpecRegistry(final Lookup<CookieSpecProvider> lookup) {
189 setAttribute(COOKIESPEC_REGISTRY, lookup);
190 }
191
192 public Lookup<AuthSchemeProvider> getAuthSchemeRegistry() {
193 return getLookup(AUTHSCHEME_REGISTRY, AuthSchemeProvider.class);
194 }
195
196 public void setAuthSchemeRegistry(final Lookup<AuthSchemeProvider> lookup) {
197 setAttribute(AUTHSCHEME_REGISTRY, lookup);
198 }
199
200 public CredentialsProvider getCredentialsProvider() {
201 return getAttribute(CREDS_PROVIDER, CredentialsProvider.class);
202 }
203
204 public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
205 setAttribute(CREDS_PROVIDER, credentialsProvider);
206 }
207
208 public AuthCache getAuthCache() {
209 return getAttribute(AUTH_CACHE, AuthCache.class);
210 }
211
212 public void setAuthCache(final AuthCache authCache) {
213 setAttribute(AUTH_CACHE, authCache);
214 }
215
216 public AuthState getTargetAuthState() {
217 return getAttribute(TARGET_AUTH_STATE, AuthState.class);
218 }
219
220 public AuthState getProxyAuthState() {
221 return getAttribute(PROXY_AUTH_STATE, AuthState.class);
222 }
223
224 public <T> T getUserToken(final Class<T> clazz) {
225 return getAttribute(USER_TOKEN, clazz);
226 }
227
228 public Object getUserToken() {
229 return getAttribute(USER_TOKEN);
230 }
231
232 public void setUserToken(final Object obj) {
233 setAttribute(USER_TOKEN, obj);
234 }
235
236 public RequestConfig getRequestConfig() {
237 final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class);
238 return config != null ? config : RequestConfig.DEFAULT;
239 }
240
241 public void setRequestConfig(final RequestConfig config) {
242 setAttribute(REQUEST_CONFIG, config);
243 }
244
245 }