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.annotation.NotThreadSafe;
34  import org.apache.http.auth.AuthSchemeProvider;
35  import org.apache.http.auth.AuthState;
36  import org.apache.http.client.AuthCache;
37  import org.apache.http.client.CookieStore;
38  import org.apache.http.client.CredentialsProvider;
39  import org.apache.http.client.config.RequestConfig;
40  import org.apache.http.config.Lookup;
41  import org.apache.http.conn.routing.HttpRoute;
42  import org.apache.http.conn.routing.RouteInfo;
43  import org.apache.http.cookie.CookieOrigin;
44  import org.apache.http.cookie.CookieSpec;
45  import org.apache.http.cookie.CookieSpecProvider;
46  import org.apache.http.protocol.BasicHttpContext;
47  import org.apache.http.protocol.HttpContext;
48  import org.apache.http.protocol.HttpCoreContext;
49  
50  /**
51   * Adaptor class that provides convenience type safe setters and getters
52   * for common {@link HttpContext} attributes used in the course
53   * of HTTP request execution.
54   *
55   * @since 4.3
56   */
57  @NotThreadSafe
58  public class HttpClientContext extends HttpCoreContext {
59  
60      /**
61       * Attribute name of a {@link org.apache.http.conn.routing.RouteInfo}
62       * object that represents the actual connection route.
63       */
64      public static final String HTTP_ROUTE   = "http.route";
65  
66      /**
67       * Attribute name of a {@link List} object that represents a collection of all
68       * redirect locations received in the process of request execution.
69       */
70      public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
71  
72      /**
73       * Attribute name of a {@link org.apache.http.config.Lookup} object that represents
74       * the actual {@link CookieSpecProvider} registry.
75       */
76      public static final String COOKIESPEC_REGISTRY   = "http.cookiespec-registry";
77  
78      /**
79       * Attribute name of a {@link org.apache.http.cookie.CookieSpec}
80       * object that represents the actual cookie specification.
81       */
82      public static final String COOKIE_SPEC           = "http.cookie-spec";
83  
84      /**
85       * Attribute name of a {@link org.apache.http.cookie.CookieOrigin}
86       * object that represents the actual details of the origin server.
87       */
88      public static final String COOKIE_ORIGIN         = "http.cookie-origin";
89  
90      /**
91       * Attribute name of a {@link org.apache.http.client.CookieStore}
92       * object that represents the actual cookie store.
93       */
94      public static final String COOKIE_STORE          = "http.cookie-store";
95  
96      /**
97       * Attribute name of a {@link org.apache.http.client.CredentialsProvider}
98       * object that represents the actual credentials provider.
99       */
100     public static final String CREDS_PROVIDER        = "http.auth.credentials-provider";
101 
102     /**
103      * Attribute name of a {@link org.apache.http.client.AuthCache} object
104      * that represents the auth scheme cache.
105      */
106     public static final String AUTH_CACHE            = "http.auth.auth-cache";
107 
108     /**
109      * Attribute name of a {@link org.apache.http.auth.AuthState}
110      * object that represents the actual target authentication state.
111      */
112     public static final String TARGET_AUTH_STATE     = "http.auth.target-scope";
113 
114     /**
115      * Attribute name of a {@link org.apache.http.auth.AuthState}
116      * object that represents the actual proxy authentication state.
117      */
118     public static final String PROXY_AUTH_STATE      = "http.auth.proxy-scope";
119 
120     /**
121      * Attribute name of a {@link java.lang.Object} object that represents
122      * the actual user identity such as user {@link java.security.Principal}.
123      */
124     public static final String USER_TOKEN            = "http.user-token";
125 
126     /**
127      * Attribute name of a {@link org.apache.http.config.Lookup} object that represents
128      * the actual {@link AuthSchemeProvider} registry.
129      */
130     public static final String AUTHSCHEME_REGISTRY   = "http.authscheme-registry";
131 
132     /**
133      * Attribute name of a {@link org.apache.http.client.config.RequestConfig} object that
134      * represents the actual request configuration.
135      */
136     public static final String REQUEST_CONFIG = "http.request-config";
137 
138     public static HttpClientContext adapt(final HttpContext context) {
139         if (context instanceof HttpClientContext) {
140             return (HttpClientContext) context;
141         } else {
142             return new HttpClientContext(context);
143         }
144     }
145 
146     public static HttpClientContext create() {
147         return new HttpClientContext(new BasicHttpContext());
148     }
149 
150     public HttpClientContext(final HttpContext context) {
151         super(context);
152     }
153 
154     public HttpClientContext() {
155         super();
156     }
157 
158     public RouteInfo getHttpRoute() {
159         return getAttribute(HTTP_ROUTE, HttpRoute.class);
160     }
161 
162     @SuppressWarnings("unchecked")
163     public List<URI> getRedirectLocations() {
164         return getAttribute(REDIRECT_LOCATIONS, List.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, final Class<T> clazz) {
185         return getAttribute(name, Lookup.class);
186     }
187 
188     public Lookup<CookieSpecProvider> getCookieSpecRegistry() {
189         return getLookup(COOKIESPEC_REGISTRY, CookieSpecProvider.class);
190     }
191 
192     public void setCookieSpecRegistry(final Lookup<CookieSpecProvider> lookup) {
193         setAttribute(COOKIESPEC_REGISTRY, lookup);
194     }
195 
196     public Lookup<AuthSchemeProvider> getAuthSchemeRegistry() {
197         return getLookup(AUTHSCHEME_REGISTRY, AuthSchemeProvider.class);
198     }
199 
200     public void setAuthSchemeRegistry(final Lookup<AuthSchemeProvider> 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     public AuthState getTargetAuthState() {
221         return getAttribute(TARGET_AUTH_STATE, AuthState.class);
222     }
223 
224     public AuthState getProxyAuthState() {
225         return getAttribute(PROXY_AUTH_STATE, AuthState.class);
226     }
227 
228     public <T> T getUserToken(final Class<T> clazz) {
229         return getAttribute(USER_TOKEN, clazz);
230     }
231 
232     public Object getUserToken() {
233         return getAttribute(USER_TOKEN);
234     }
235 
236     public void setUserToken(final Object obj) {
237         setAttribute(USER_TOKEN, obj);
238     }
239 
240     public RequestConfig getRequestConfig() {
241         final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class);
242         return config != null ? config : RequestConfig.DEFAULT;
243     }
244 
245     public void setRequestConfig(final RequestConfig config) {
246         setAttribute(REQUEST_CONFIG, config);
247     }
248 
249 }