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     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      * @since 5.0
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      * @since 5.0
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      * @since 5.0
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      * @since 5.0
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 }