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         if (context instanceof HttpClientContext) {
138             return (HttpClientContext) context;
139         } else {
140             return new HttpClientContext(context);
141         }
142     }
143 
144     public static HttpClientContext create() {
145         return new HttpClientContext(new BasicHttpContext());
146     }
147 
148     public HttpClientContext(final HttpContext context) {
149         super(context);
150     }
151 
152     public HttpClientContext() {
153         super();
154     }
155 
156     public RouteInfo getHttpRoute() {
157         return getAttribute(HTTP_ROUTE, HttpRoute.class);
158     }
159 
160     @SuppressWarnings("unchecked")
161     public List<URI> getRedirectLocations() {
162         return getAttribute(REDIRECT_LOCATIONS, List.class);
163     }
164 
165     public CookieStore getCookieStore() {
166         return getAttribute(COOKIE_STORE, CookieStore.class);
167     }
168 
169     public void setCookieStore(final CookieStore cookieStore) {
170         setAttribute(COOKIE_STORE, cookieStore);
171     }
172 
173     public CookieSpec getCookieSpec() {
174         return getAttribute(COOKIE_SPEC, CookieSpec.class);
175     }
176 
177     public CookieOrigin getCookieOrigin() {
178         return getAttribute(COOKIE_ORIGIN, CookieOrigin.class);
179     }
180 
181     @SuppressWarnings("unchecked")
182     private <T> Lookup<T> getLookup(final String name, final Class<T> clazz) {
183         return getAttribute(name, Lookup.class);
184     }
185 
186     public Lookup<CookieSpecProvider> getCookieSpecRegistry() {
187         return getLookup(COOKIESPEC_REGISTRY, CookieSpecProvider.class);
188     }
189 
190     public void setCookieSpecRegistry(final Lookup<CookieSpecProvider> lookup) {
191         setAttribute(COOKIESPEC_REGISTRY, lookup);
192     }
193 
194     public Lookup<AuthSchemeProvider> getAuthSchemeRegistry() {
195         return getLookup(AUTHSCHEME_REGISTRY, AuthSchemeProvider.class);
196     }
197 
198     public void setAuthSchemeRegistry(final Lookup<AuthSchemeProvider> lookup) {
199         setAttribute(AUTHSCHEME_REGISTRY, lookup);
200     }
201 
202     public CredentialsProvider getCredentialsProvider() {
203         return getAttribute(CREDS_PROVIDER, CredentialsProvider.class);
204     }
205 
206     public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
207         setAttribute(CREDS_PROVIDER, credentialsProvider);
208     }
209 
210     public AuthCache getAuthCache() {
211         return getAttribute(AUTH_CACHE, AuthCache.class);
212     }
213 
214     public void setAuthCache(final AuthCache authCache) {
215         setAttribute(AUTH_CACHE, authCache);
216     }
217 
218     public AuthState getTargetAuthState() {
219         return getAttribute(TARGET_AUTH_STATE, AuthState.class);
220     }
221 
222     public AuthState getProxyAuthState() {
223         return getAttribute(PROXY_AUTH_STATE, AuthState.class);
224     }
225 
226     public <T> T getUserToken(final Class<T> clazz) {
227         return getAttribute(USER_TOKEN, clazz);
228     }
229 
230     public Object getUserToken() {
231         return getAttribute(USER_TOKEN);
232     }
233 
234     public void setUserToken(final Object obj) {
235         setAttribute(USER_TOKEN, obj);
236     }
237 
238     public RequestConfig getRequestConfig() {
239         final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class);
240         return config != null ? config : RequestConfig.DEFAULT;
241     }
242 
243     public void setRequestConfig(final RequestConfig config) {
244         setAttribute(REQUEST_CONFIG, config);
245     }
246 
247 }