View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Adaptor class that provides convenience type safe setters and getters
54   * for common {@link HttpContext} attributes used in the course
55   * of HTTP request execution.
56   *
57   * @since 4.3
58   */
59  public class HttpClientContext extends HttpCoreContext {
60  
61      /**
62       * Attribute name of a {@link RouteInfo}
63       * object that represents the actual connection route.
64       */
65      public static final String HTTP_ROUTE   = "http.route";
66  
67      /**
68       * Attribute name of a {@link RedirectLocations} object that represents a collection of all
69       * redirect locations received in the process of request execution.
70       */
71      public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
72  
73      /**
74       * Attribute name of a {@link org.apache.hc.core5.http.config.Lookup} object that represents
75       * the actual {@link CookieSpecFactory} registry.
76       */
77      public static final String COOKIESPEC_REGISTRY   = "http.cookiespec-registry";
78  
79      /**
80       * Attribute name of a {@link org.apache.hc.client5.http.cookie.CookieSpec}
81       * object that represents the actual cookie specification.
82       */
83      public static final String COOKIE_SPEC           = "http.cookie-spec";
84  
85      /**
86       * Attribute name of a {@link org.apache.hc.client5.http.cookie.CookieOrigin}
87       * object that represents the actual details of the origin server.
88       */
89      public static final String COOKIE_ORIGIN         = "http.cookie-origin";
90  
91      /**
92       * Attribute name of a {@link CookieStore}
93       * object that represents the actual cookie store.
94       */
95      public static final String COOKIE_STORE          = "http.cookie-store";
96  
97      /**
98       * Attribute name of a {@link CredentialsProvider}
99       * object that represents the actual credentials provider.
100      */
101     public static final String CREDS_PROVIDER        = "http.auth.credentials-provider";
102 
103     /**
104      * Attribute name of a {@link AuthCache} object
105      * that represents the auth scheme cache.
106      */
107     public static final String AUTH_CACHE            = "http.auth.auth-cache";
108 
109     /**
110      * Attribute name of a map containing actual {@link AuthExchange}s keyed by their respective
111      * {@link org.apache.hc.core5.http.HttpHost}.
112      */
113     public static final String AUTH_EXCHANGE_MAP     = "http.auth.exchanges";
114 
115     /**
116      * Attribute name of a {@link java.lang.Object} object that represents
117      * the actual user identity such as user {@link java.security.Principal}.
118      */
119     public static final String USER_TOKEN            = "http.user-token";
120 
121     /**
122      * Attribute name of a {@link org.apache.hc.core5.http.config.Lookup} object that represents
123      * the actual {@link AuthSchemeFactory} registry.
124      */
125     public static final String AUTHSCHEME_REGISTRY   = "http.authscheme-registry";
126 
127     /**
128      * Attribute name of a {@link org.apache.hc.client5.http.config.RequestConfig} object that
129      * represents the actual request configuration.
130      */
131     public static final String REQUEST_CONFIG = "http.request-config";
132 
133     /**
134      * Attribute name of a {@link java.lang.String} object that represents the ID of the
135      * current message exchange.
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      * @since 5.0
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      * @since 5.0
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      * @since 5.0
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      * @since 5.0
256      */
257     public void resetAuthExchange(final HttpHost host, final AuthScheme authScheme) {
258         final AuthExchangethExchange.html#AuthExchange">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      * @since 5.1
287      */
288     public String getExchangeId() {
289         return getAttribute(EXCHANGE_ID, String.class);
290     }
291 
292     /**
293      * @since 5.1
294      */
295     public void setExchangeId(final String id) {
296         setAttribute(EXCHANGE_ID, id);
297     }
298 
299 }