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.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   * Adaptor class that provides convenience type safe setters and getters
51   * for common {@link HttpContext} attributes used in the course
52   * of HTTP request execution.
53   *
54   * @since 4.3
55   */
56  public class HttpClientContext extends HttpCoreContext {
57  
58      /**
59       * Attribute name of a {@link org.apache.http.conn.routing.RouteInfo}
60       * object that represents the actual connection route.
61       */
62      public static final String HTTP_ROUTE   = "http.route";
63  
64      /**
65       * Attribute name of a {@link List} object that represents a collection of all
66       * redirect locations received in the process of request execution.
67       */
68      public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
69  
70      /**
71       * Attribute name of a {@link org.apache.http.config.Lookup} object that represents
72       * the actual {@link CookieSpecProvider} registry.
73       */
74      public static final String COOKIESPEC_REGISTRY   = "http.cookiespec-registry";
75  
76      /**
77       * Attribute name of a {@link org.apache.http.cookie.CookieSpec}
78       * object that represents the actual cookie specification.
79       */
80      public static final String COOKIE_SPEC           = "http.cookie-spec";
81  
82      /**
83       * Attribute name of a {@link org.apache.http.cookie.CookieOrigin}
84       * object that represents the actual details of the origin server.
85       */
86      public static final String COOKIE_ORIGIN         = "http.cookie-origin";
87  
88      /**
89       * Attribute name of a {@link org.apache.http.client.CookieStore}
90       * object that represents the actual cookie store.
91       */
92      public static final String COOKIE_STORE          = "http.cookie-store";
93  
94      /**
95       * Attribute name of a {@link org.apache.http.client.CredentialsProvider}
96       * object that represents the actual credentials provider.
97       */
98      public static final String CREDS_PROVIDER        = "http.auth.credentials-provider";
99  
100     /**
101      * Attribute name of a {@link org.apache.http.client.AuthCache} object
102      * that represents the auth scheme cache.
103      */
104     public static final String AUTH_CACHE            = "http.auth.auth-cache";
105 
106     /**
107      * Attribute name of a {@link org.apache.http.auth.AuthState}
108      * object that represents the actual target authentication state.
109      */
110     public static final String TARGET_AUTH_STATE     = "http.auth.target-scope";
111 
112     /**
113      * Attribute name of a {@link org.apache.http.auth.AuthState}
114      * object that represents the actual proxy authentication state.
115      */
116     public static final String PROXY_AUTH_STATE      = "http.auth.proxy-scope";
117 
118     /**
119      * Attribute name of a {@link java.lang.Object} object that represents
120      * the actual user identity such as user {@link java.security.Principal}.
121      */
122     public static final String USER_TOKEN            = "http.user-token";
123 
124     /**
125      * Attribute name of a {@link org.apache.http.config.Lookup} object that represents
126      * the actual {@link AuthSchemeProvider} registry.
127      */
128     public static final String AUTHSCHEME_REGISTRY   = "http.authscheme-registry";
129 
130     /**
131      * Attribute name of a {@link org.apache.http.client.config.RequestConfig} object that
132      * represents the actual request configuration.
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 }