| 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 |
@Override |
| 95 |
public CloseableHttpResponse execute( |
| 96 |
final HttpRoute route, |
| 97 |
final HttpRequestWrapper request, |
| 98 |
final HttpClientContext context, |
| 99 |
final HttpExecutionAware execAware) throws IOException, HttpException { |
| 100 |
try { |
| 101 |
final Future<HttpResponse> future = client.execute(route.getTargetHost(), request, context, null); |
| 102 |
return Proxies.enhanceResponse(future.get()); |
| 103 |
} catch (final InterruptedException e) { |
| 104 |
Thread.currentThread().interrupt(); |
| 105 |
return null; |
| 106 |
} catch (final ExecutionException e) { |
| 107 |
try { |
| 108 |
throw e.getCause(); |
| 109 |
} catch (final IOException ex) { |
| 110 |
throw ex; |
| 111 |
} catch (final HttpException ex) { |
| 112 |
throw ex; |
| 113 |
} catch (final RuntimeException ex) { |
| 114 |
throw ex; |
| 115 |
} catch (final Error ex) { |
| 116 |
throw ex; |
| 117 |
} catch (final Throwable ex) { |
| 118 |
throw new UndeclaredThrowableException(ex); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public long getCacheHits() { |
| 124 |
return client.getCacheHits(); |
| 125 |
} |
| 126 |
|
| 127 |
public long getCacheMisses() { |
| 128 |
return client.getCacheMisses(); |
| 129 |
} |
| 130 |
|
| 131 |
public long getCacheUpdates() { |
| 132 |
return client.getCacheUpdates(); |
| 133 |
} |
| 134 |
|
| 135 |
} |