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 org.apache.http.annotation.NotThreadSafe;
31  import org.apache.http.auth.AuthSchemeProvider;
32  import org.apache.http.auth.AuthState;
33  import org.apache.http.client.AuthCache;
34  import org.apache.http.client.CookieStore;
35  import org.apache.http.client.CredentialsProvider;
36  import org.apache.http.client.URICollection;
37  import org.apache.http.client.config.RequestConfig;
38  import org.apache.http.config.Lookup;
39  import org.apache.http.conn.routing.HttpRoute;
40  import org.apache.http.conn.routing.RouteInfo;
41  import org.apache.http.cookie.CookieOrigin;
42  import org.apache.http.cookie.CookieSpec;
43  import org.apache.http.cookie.CookieSpecProvider;
44  import org.apache.http.protocol.BasicHttpContext;
45  import org.apache.http.protocol.HttpContext;
46  import org.apache.http.protocol.HttpCoreContext;
47  
48  /**
49   * Implementation of {@link HttpContext} that provides convenience
50   * setters for user assignable attributes and getter for readable attributes.
51   *
52   * @since 4.3
53   */
54  @NotThreadSafe
55  public class HttpClientContext extends HttpCoreContext {
56  
57      /**
58       * Attribute name of a {@link org.apache.http.conn.routing.RouteInfo}
59       * object that represents the actual connection route.
60       */
61      public static final String HTTP_ROUTE   = "http.route";
62  
63      /**
64       * Attribute name of a {@link org.apache.http.client.URICollection} object that
65       * represents a collection of all redirect locations received in the process
66       * 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     public URICollection getRedirectLocations() {
161         return getAttribute(REDIRECT_LOCATIONS, URICollection.class);
162     }
163 
164     public CookieStore getCookieStore() {
165         return getAttribute(COOKIE_STORE, CookieStore.class);
166     }
167 
168     public void setCookieStore(final CookieStore cookieStore) {
169         setAttribute(COOKIE_STORE, cookieStore);
170     }
171 
172     public CookieSpec getCookieSpec() {
173         return getAttribute(COOKIE_SPEC, CookieSpec.class);
174     }
175 
176     public CookieOrigin getCookieOrigin() {
177         return getAttribute(COOKIE_ORIGIN, CookieOrigin.class);
178     }
179 
180     @SuppressWarnings("unchecked")
181     protected <T> Lookup<T> getLookup(final String name, final Class<T> clazz) {
182         return getAttribute(name, Lookup.class);
183     }
184 
185     public Lookup<CookieSpecProvider> getCookieSpecRegistry() {
186         return getLookup(COOKIESPEC_REGISTRY, CookieSpecProvider.class);
187     }
188 
189     public void setCookieSpecRegistry(final Lookup<CookieSpecProvider> lookup) {
190         setAttribute(COOKIESPEC_REGISTRY, lookup);
191     }
192 
193     public Lookup<AuthSchemeProvider> getAuthSchemeRegistry() {
194         return getLookup(AUTHSCHEME_REGISTRY, AuthSchemeProvider.class);
195     }
196 
197     public void setAuthSchemeRegistry(final Lookup<AuthSchemeProvider> lookup) {
198         setAttribute(AUTHSCHEME_REGISTRY, lookup);
199     }
200 
201     public CredentialsProvider getCredentialsProvider() {
202         return getAttribute(CREDS_PROVIDER, CredentialsProvider.class);
203     }
204 
205     public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
206         setAttribute(CREDS_PROVIDER, credentialsProvider);
207     }
208 
209     public AuthCache getAuthCache() {
210         return getAttribute(AUTH_CACHE, AuthCache.class);
211     }
212 
213     public void setAuthCache(final AuthCache authCache) {
214         setAttribute(AUTH_CACHE, authCache);
215     }
216 
217     public AuthState getTargetAuthState() {
218         return getAttribute(TARGET_AUTH_STATE, AuthState.class);
219     }
220 
221     public AuthState getProxyAuthState() {
222         return getAttribute(PROXY_AUTH_STATE, AuthState.class);
223     }
224 
225     public <T> T getUserToken(final Class<T> clazz) {
226         return getAttribute(USER_TOKEN, clazz);
227     }
228 
229     public Object getUserToken() {
230         return getAttribute(USER_TOKEN);
231     }
232 
233     public void setUserToken(final Object obj) {
234         setAttribute(USER_TOKEN, obj);
235     }
236 
237     public RequestConfig getRequestConfig() {
238         final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class);
239         return config != null ? config : RequestConfig.DEFAULT;
240     }
241 
242     public void setRequestConfig(final RequestConfig config) {
243         setAttribute(REQUEST_CONFIG, config);
244     }
245 
246 }