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  package org.apache.hc.client5.http.cache;
28  
29  import java.util.Map;
30  
31  import javax.net.ssl.SSLSession;
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.client5.http.protocol.HttpClientContext;
46  import org.apache.hc.client5.http.protocol.RedirectLocations;
47  import org.apache.hc.core5.annotation.Internal;
48  import org.apache.hc.core5.http.EndpointDetails;
49  import org.apache.hc.core5.http.HttpHost;
50  import org.apache.hc.core5.http.HttpRequest;
51  import org.apache.hc.core5.http.HttpResponse;
52  import org.apache.hc.core5.http.ProtocolVersion;
53  import org.apache.hc.core5.http.config.Lookup;
54  import org.apache.hc.core5.http.protocol.HttpContext;
55  
56  /**
57   * Cache execution {@link HttpContext}. This class can be re-used for
58   * multiple consecutive logically related request executions that represent
59   * a single communication session. This context may not be used concurrently.
60   * <p>
61   * IMPORTANT: This class is NOT thread-safe and MUST NOT be used concurrently by
62   * multiple message exchanges.
63   *
64   * @since 4.3
65   */
66  public class HttpCacheContext extends HttpClientContext {
67  
68      /**
69       * @deprecated Use getter methods
70       */
71      @Deprecated
72      public static final String CACHE_RESPONSE_STATUS = "http.cache.response.status";
73      static final String REQUEST_CACHE_CONTROL = "http.cache.request-control";
74      static final String RESPONSE_CACHE_CONTROL = "http.cache.response-control";
75      static final String CACHE_ENTRY = "http.cache.entry";
76  
77      /**
78       * @deprecated Use {@link #castOrCreate(HttpContext)}.
79       */
80      @Deprecated
81      public static HttpCacheContext adapt(final HttpContext context) {
82          if (context instanceof HttpCacheContext) {
83              return (HttpCacheContext) context;
84          }
85          return new Delegate(HttpClientContext.castOrCreate(context));
86      }
87  
88      /**
89       * Casts the given generic {@link HttpContext} as {@link HttpCacheContext} or
90       * throws an {@link IllegalStateException} if the given context is not suitable.
91       *
92       * @since 5.4
93       */
94      public static HttpCacheContext cast(final HttpContext context) {
95          if (context == null) {
96              return null;
97          }
98          if (context instanceof HttpCacheContext) {
99              return (HttpCacheContext) context;
100         } else if (context instanceof HttpClientContext) {
101             return new Delegate((HttpClientContext) context);
102         } else {
103             return new Delegate(HttpClientContext.cast(context));
104         }
105     }
106 
107     /**
108      * Casts the given generic {@link HttpContext} as {@link HttpCacheContext} or
109      * creates new {@link HttpCacheContext} if the given context is null..
110      *
111      * @since 5.4
112      */
113     public static HttpCacheContext castOrCreate(final HttpContext context) {
114         return context != null ? cast(context) : create();
115     }
116 
117     public static HttpCacheContext create() {
118         return new HttpCacheContext();
119     }
120 
121     private CacheResponseStatus responseStatus;
122     private RequestCacheControl requestCacheControl;
123     private ResponseCacheControl responseCacheControl;
124     private HttpCacheEntry cacheEntry;
125 
126     public HttpCacheContext(final HttpContext context) {
127         super(context);
128     }
129 
130     public HttpCacheContext() {
131         super();
132     }
133 
134     /**
135      * Represents an outcome of the cache operation and the way the response has been
136      * generated.
137      * <p>
138      * This context attribute is expected to be populated by the protocol handler.
139      */
140     public CacheResponseStatus getCacheResponseStatus() {
141         return responseStatus;
142     }
143 
144     /**
145      * @since 5.4
146      */
147     @Internal
148     public void setCacheResponseStatus(final CacheResponseStatus responseStatus) {
149         this.responseStatus = responseStatus;
150     }
151 
152     /**
153      * Represents cache control requested by the client.
154      * <p>
155      * This context attribute is expected to be set by the caller.
156      *
157      * @since 5.4
158      */
159     public RequestCacheControl getRequestCacheControl() {
160         return requestCacheControl;
161     }
162 
163     /**
164      * Returns cache control requested by the client or {@link RequestCacheControl#DEFAULT}
165      * if not explicitly set in the context.
166      *
167      * @since 5.4
168      */
169     public final RequestCacheControl getRequestCacheControlOrDefault() {
170         final RequestCacheControl cacheControl = getRequestCacheControl();
171         return cacheControl != null ? cacheControl : RequestCacheControl.DEFAULT;
172     }
173 
174     /**
175      * @since 5.4
176      */
177     @Internal
178     public void setRequestCacheControl(final RequestCacheControl requestCacheControl) {
179         this.requestCacheControl = requestCacheControl;
180     }
181 
182     /**
183      * Represents cache control enforced by the server.
184      * <p>
185      * This context attribute is expected to be populated by the protocol handler.
186      *
187      * @since 5.4
188      */
189     public ResponseCacheControl getResponseCacheControl() {
190         return responseCacheControl;
191     }
192 
193     /**
194      * Represents cache control enforced by the server or {@link ResponseCacheControl#DEFAULT}
195      * if not explicitly set in the context.
196      *
197      * @since 5.4
198      */
199     public final ResponseCacheControl getResponseCacheControlOrDefault() {
200         final ResponseCacheControl cacheControl = getResponseCacheControl();
201         return cacheControl != null ? cacheControl : ResponseCacheControl.DEFAULT;
202     }
203 
204     /**
205      * @since 5.4
206      */
207     @Internal
208     public void setResponseCacheControl(final ResponseCacheControl responseCacheControl) {
209         this.responseCacheControl = responseCacheControl;
210     }
211 
212     /**
213      * Represents the cache entry the resource of which has been used to generate the response.
214      * <p>
215      * This context attribute is expected to be populated by the protocol handler.
216      *
217      * @since 5.4
218      */
219     public HttpCacheEntry getCacheEntry() {
220         return cacheEntry;
221     }
222 
223     /**
224      * @since 5.4
225      */
226     @Internal
227     public void setCacheEntry(final HttpCacheEntry cacheEntry) {
228         this.cacheEntry = cacheEntry;
229     }
230 
231     /**
232      * Internal adaptor class that delegates all its method calls to {@link HttpClientContext}.
233      * To be removed in the future.
234      */
235     @SuppressWarnings("deprecation")
236     @Internal
237     static class Delegate extends HttpCacheContext {
238 
239         private final HttpClientContext clientContext;
240 
241         Delegate(final HttpClientContext clientContext) {
242             super(null);
243             this.clientContext = clientContext;
244         }
245 
246         @Override
247         public CacheResponseStatus getCacheResponseStatus() {
248             return clientContext.getAttribute(CACHE_RESPONSE_STATUS, CacheResponseStatus.class);
249         }
250 
251         @Override
252         public void setCacheResponseStatus(final CacheResponseStatus responseStatus) {
253             clientContext.setAttribute(CACHE_RESPONSE_STATUS, responseStatus);
254         }
255 
256         @Override
257         public RequestCacheControl getRequestCacheControl() {
258             return clientContext.getAttribute(REQUEST_CACHE_CONTROL, RequestCacheControl.class);
259         }
260 
261         @Override
262         public void setRequestCacheControl(final RequestCacheControl requestCacheControl) {
263             clientContext.setAttribute(REQUEST_CACHE_CONTROL, requestCacheControl);
264         }
265 
266         @Override
267         public ResponseCacheControl getResponseCacheControl() {
268             return clientContext.getAttribute(RESPONSE_CACHE_CONTROL, ResponseCacheControl.class);
269         }
270 
271         @Override
272         public void setResponseCacheControl(final ResponseCacheControl responseCacheControl) {
273             clientContext.setAttribute(RESPONSE_CACHE_CONTROL, responseCacheControl);
274         }
275 
276         @Override
277         public HttpCacheEntry getCacheEntry() {
278             return clientContext.getAttribute(CACHE_ENTRY, HttpCacheEntry.class);
279         }
280 
281         @Override
282         public void setCacheEntry(final HttpCacheEntry cacheEntry) {
283             clientContext.setAttribute(CACHE_ENTRY, cacheEntry);
284         }
285 
286         @Override
287         public RouteInfo getHttpRoute() {
288             return clientContext.getHttpRoute();
289         }
290 
291         @Override
292         @Internal
293         public void setRoute(final HttpRoute route) {
294             clientContext.setRoute(route);
295         }
296 
297         @Override
298         public RedirectLocations getRedirectLocations() {
299             return clientContext.getRedirectLocations();
300         }
301 
302         @Override
303         @Internal
304         public void setRedirectLocations(final RedirectLocations redirectLocations) {
305             clientContext.setRedirectLocations(redirectLocations);
306         }
307 
308         @Override
309         public CookieStore getCookieStore() {
310             return clientContext.getCookieStore();
311         }
312 
313         @Override
314         public void setCookieStore(final CookieStore cookieStore) {
315             clientContext.setCookieStore(cookieStore);
316         }
317 
318         @Override
319         public CookieSpec getCookieSpec() {
320             return clientContext.getCookieSpec();
321         }
322 
323         @Override
324         @Internal
325         public void setCookieSpec(final CookieSpec cookieSpec) {
326             clientContext.setCookieSpec(cookieSpec);
327         }
328 
329         @Override
330         public CookieOrigin getCookieOrigin() {
331             return clientContext.getCookieOrigin();
332         }
333 
334         @Override
335         @Internal
336         public void setCookieOrigin(final CookieOrigin cookieOrigin) {
337             clientContext.setCookieOrigin(cookieOrigin);
338         }
339 
340         @Override
341         public Lookup<CookieSpecFactory> getCookieSpecRegistry() {
342             return clientContext.getCookieSpecRegistry();
343         }
344 
345         @Override
346         public void setCookieSpecRegistry(final Lookup<CookieSpecFactory> lookup) {
347             clientContext.setCookieSpecRegistry(lookup);
348         }
349 
350         @Override
351         public Lookup<AuthSchemeFactory> getAuthSchemeRegistry() {
352             return clientContext.getAuthSchemeRegistry();
353         }
354 
355         @Override
356         public void setAuthSchemeRegistry(final Lookup<AuthSchemeFactory> lookup) {
357             clientContext.setAuthSchemeRegistry(lookup);
358         }
359 
360         @Override
361         public CredentialsProvider getCredentialsProvider() {
362             return clientContext.getCredentialsProvider();
363         }
364 
365         @Override
366         public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
367             clientContext.setCredentialsProvider(credentialsProvider);
368         }
369 
370         @Override
371         public AuthCache getAuthCache() {
372             return clientContext.getAuthCache();
373         }
374 
375         @Override
376         public void setAuthCache(final AuthCache authCache) {
377             clientContext.setAuthCache(authCache);
378         }
379 
380         @Override
381         public Map<HttpHost, AuthExchange> getAuthExchanges() {
382             return clientContext.getAuthExchanges();
383         }
384 
385         @Override
386         public AuthExchange getAuthExchange(final HttpHost host) {
387             return clientContext.getAuthExchange(host);
388         }
389 
390         @Override
391         public void setAuthExchange(final HttpHost host, final AuthExchange authExchange) {
392             clientContext.setAuthExchange(host, authExchange);
393         }
394 
395         @Override
396         public void resetAuthExchange(final HttpHost host, final AuthScheme authScheme) {
397             clientContext.resetAuthExchange(host, authScheme);
398         }
399 
400         @Override
401         public Object getUserToken() {
402             return clientContext.getUserToken();
403         }
404 
405         @Override
406         public void setUserToken(final Object userToken) {
407             clientContext.setUserToken(userToken);
408         }
409 
410         @Override
411         public RequestConfig getRequestConfig() {
412             return clientContext.getRequestConfig();
413         }
414 
415         @Override
416         public void setRequestConfig(final RequestConfig requestConfig) {
417             clientContext.setRequestConfig(requestConfig);
418         }
419 
420         @Override
421         public String getExchangeId() {
422             return clientContext.getExchangeId();
423         }
424 
425         @Override
426         public void setExchangeId(final String exchangeId) {
427             clientContext.setExchangeId(exchangeId);
428         }
429 
430         @Override
431         public HttpRequest getRequest() {
432             return clientContext.getRequest();
433         }
434 
435         @Override
436         public void setRequest(final HttpRequest request) {
437             clientContext.setRequest(request);
438         }
439 
440         @Override
441         public HttpResponse getResponse() {
442             return clientContext.getResponse();
443         }
444 
445         @Override
446         public void setResponse(final HttpResponse response) {
447             clientContext.setResponse(response);
448         }
449 
450         @Override
451         public EndpointDetails getEndpointDetails() {
452             return clientContext.getEndpointDetails();
453         }
454 
455         @Override
456         public void setEndpointDetails(final EndpointDetails endpointDetails) {
457             clientContext.setEndpointDetails(endpointDetails);
458         }
459 
460         @Override
461         public SSLSession getSSLSession() {
462             return clientContext.getSSLSession();
463         }
464 
465         @Override
466         public void setSSLSession(final SSLSession sslSession) {
467             clientContext.setSSLSession(sslSession);
468         }
469 
470         @Override
471         public ProtocolVersion getProtocolVersion() {
472             return clientContext.getProtocolVersion();
473         }
474 
475         @Override
476         public void setProtocolVersion(final ProtocolVersion version) {
477             clientContext.setProtocolVersion(version);
478         }
479 
480         @Override
481         public Object getAttribute(final String id) {
482             return clientContext.getAttribute(id);
483         }
484 
485         @Override
486         public Object setAttribute(final String id, final Object obj) {
487             return clientContext.setAttribute(id, obj);
488         }
489 
490         @Override
491         public Object removeAttribute(final String id) {
492             return clientContext.removeAttribute(id);
493         }
494 
495         @Override
496         public <T> T getAttribute(final String id, final Class<T> clazz) {
497             return clientContext.getAttribute(id, clazz);
498         }
499 
500         @Override
501         public String toString() {
502             return clientContext.toString();
503         }
504 
505     }
506 
507 }