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.http.impl.client.cache;
28  
29  import java.io.IOException;
30  import java.lang.reflect.UndeclaredThrowableException;
31  import java.util.concurrent.ExecutionException;
32  import java.util.concurrent.Future;
33  
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.client.methods.CloseableHttpResponse;
37  import org.apache.http.client.methods.HttpExecutionAware;
38  import org.apache.http.client.methods.HttpRequestWrapper;
39  import org.apache.http.client.protocol.HttpClientContext;
40  import org.apache.http.conn.routing.HttpRoute;
41  import org.apache.http.impl.execchain.ClientExecChain;
42  
43  public class CachingHttpAsyncClientExecChain implements ClientExecChain {
44  
45      private final CachingHttpAsyncClient client;
46  
47      public CachingHttpAsyncClientExecChain(final ClientExecChain backend) {
48          this(backend, new BasicHttpCache(), CacheConfig.DEFAULT);
49      }
50  
51      public CachingHttpAsyncClientExecChain(
52              final ClientExecChain backend,
53              final HttpCache cache,
54              final CacheConfig config) {
55          this.client = new CachingHttpAsyncClient(
56                  new ClientExecChainAsyncClient(backend), cache, config);
57      }
58  
59      CachingHttpAsyncClientExecChain(
60              final ClientExecChain backend, final HttpCache responseCache,
61              final CacheValidityPolicy validityPolicy,
62              final ResponseCachingPolicy responseCachingPolicy,
63              final CachedHttpResponseGenerator responseGenerator,
64              final CacheableRequestPolicy cacheableRequestPolicy,
65              final CachedResponseSuitabilityChecker suitabilityChecker,
66              final ConditionalRequestBuilder conditionalRequestBuilder,
67              final ResponseProtocolCompliance responseCompliance,
68              final RequestProtocolCompliance requestCompliance) {
69          this.client = new CachingHttpAsyncClient(
70                  new ClientExecChainAsyncClient(backend), validityPolicy,
71                  responseCachingPolicy, responseCache, responseGenerator,
72                  cacheableRequestPolicy, suitabilityChecker,
73                  conditionalRequestBuilder, responseCompliance,
74                  requestCompliance);
75      }
76  
77      public boolean supportsRangeAndContentRangeHeaders() {
78          return client.supportsRangeAndContentRangeHeaders();
79      }
80  
81      public CloseableHttpResponse execute(
82              final HttpRoute route,
83              final HttpRequestWrapper request) throws IOException, HttpException {
84          return execute(route, request, HttpClientContext.create(), null);
85      }
86  
87      public CloseableHttpResponse execute(
88              final HttpRoute route,
89              final HttpRequestWrapper request,
90              final HttpClientContext context) throws IOException, HttpException {
91          return execute(route, request, context, null);
92      }
93  
94      public CloseableHttpResponse execute(
95              final HttpRoute route,
96              final HttpRequestWrapper request,
97              final HttpClientContext context,
98              final HttpExecutionAware execAware) throws IOException, HttpException {
99          try {
100             final Future<HttpResponse> future = client.execute(route.getTargetHost(), request, context, null);
101             return Proxies.enhanceResponse(future.get());
102         } catch (InterruptedException e) {
103             Thread.currentThread().interrupt();
104             return null;
105         } catch (ExecutionException e) {
106             try {
107                 throw e.getCause();
108             } catch (IOException ex) {
109                 throw ex;
110             } catch (HttpException ex) {
111                 throw ex;
112             } catch (RuntimeException ex) {
113                 throw ex;
114             } catch (Error ex) {
115                 throw ex;
116             } catch (Throwable ex) {
117                 throw new UndeclaredThrowableException(ex);
118             }
119         }
120     }
121 
122     public long getCacheHits() {
123         return client.getCacheHits();
124     }
125 
126     public long getCacheMisses() {
127         return client.getCacheMisses();
128     }
129 
130     public long getCacheUpdates() {
131         return client.getCacheUpdates();
132     }
133 
134 }