HTTPCLIENT-975: more refactoring. Handled ProtocolExceptions from
RequestWrapper instantiations further down in the call stack. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1051074 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
93603e5f19
commit
791bb497e2
|
@ -410,39 +410,23 @@ public class CachingHttpClient implements HttpClient {
|
||||||
log.warn("Unable to retrieve entries from cache", ioe);
|
log.warn("Unable to retrieve entries from cache", ioe);
|
||||||
}
|
}
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
cacheMisses.getAndIncrement();
|
recordCacheMiss(target, request);
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
RequestLine rl = request.getRequestLine();
|
|
||||||
log.debug("Cache miss [host: " + target + "; uri: " + rl.getUri() + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mayCallBackend(request)) {
|
if (!mayCallBackend(request)) {
|
||||||
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
|
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
|
||||||
"Gateway Timeout");
|
"Gateway Timeout");
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String,Variant> variants = null;
|
Map<String, Variant> variants =
|
||||||
try {
|
getExistingCacheVariants(target, request);
|
||||||
variants = responseCache.getVariantCacheEntriesWithEtags(target, request);
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
log.warn("Unable to retrieve variant entries from cache", ioe);
|
|
||||||
}
|
|
||||||
if (variants != null && variants.size() > 0) {
|
if (variants != null && variants.size() > 0) {
|
||||||
try {
|
return negotiateResponseFromVariants(target, request, context, variants);
|
||||||
return negotiateResponseFromVariants(target, request, context, variants);
|
|
||||||
} catch (ProtocolException e) {
|
|
||||||
throw new ClientProtocolException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return callBackend(target, request, context);
|
return callBackend(target, request, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (log.isDebugEnabled()) {
|
recordCacheHit(target, request);
|
||||||
RequestLine rl = request.getRequestLine();
|
|
||||||
log.debug("Cache hit [host: " + target + "; uri: " + rl.getUri() + "]");
|
|
||||||
}
|
|
||||||
cacheHits.getAndIncrement();
|
|
||||||
|
|
||||||
Date now = getCurrentDate();
|
Date now = getCurrentDate();
|
||||||
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
|
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
|
||||||
|
@ -475,6 +459,33 @@ public class CachingHttpClient implements HttpClient {
|
||||||
return callBackend(target, request, context);
|
return callBackend(target, request, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, Variant> getExistingCacheVariants(HttpHost target,
|
||||||
|
HttpRequest request) {
|
||||||
|
Map<String,Variant> variants = null;
|
||||||
|
try {
|
||||||
|
variants = responseCache.getVariantCacheEntriesWithEtags(target, request);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
log.warn("Unable to retrieve variant entries from cache", ioe);
|
||||||
|
}
|
||||||
|
return variants;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recordCacheMiss(HttpHost target, HttpRequest request) {
|
||||||
|
cacheMisses.getAndIncrement();
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
RequestLine rl = request.getRequestLine();
|
||||||
|
log.debug("Cache miss [host: " + target + "; uri: " + rl.getUri() + "]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recordCacheHit(HttpHost target, HttpRequest request) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
RequestLine rl = request.getRequestLine();
|
||||||
|
log.debug("Cache hit [host: " + target + "; uri: " + rl.getUri() + "]");
|
||||||
|
}
|
||||||
|
cacheHits.getAndIncrement();
|
||||||
|
}
|
||||||
|
|
||||||
private void flushEntriesInvalidatedByRequest(HttpHost target,
|
private void flushEntriesInvalidatedByRequest(HttpHost target,
|
||||||
HttpRequest request) {
|
HttpRequest request) {
|
||||||
try {
|
try {
|
||||||
|
@ -642,7 +653,7 @@ public class CachingHttpClient implements HttpClient {
|
||||||
|
|
||||||
HttpResponse negotiateResponseFromVariants(HttpHost target,
|
HttpResponse negotiateResponseFromVariants(HttpHost target,
|
||||||
HttpRequest request, HttpContext context,
|
HttpRequest request, HttpContext context,
|
||||||
Map<String, Variant> variants) throws IOException, ProtocolException {
|
Map<String, Variant> variants) throws IOException {
|
||||||
HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(request, variants);
|
HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(request, variants);
|
||||||
|
|
||||||
Date requestDate = getCurrentDate();
|
Date requestDate = getCurrentDate();
|
||||||
|
|
|
@ -27,6 +27,9 @@
|
||||||
package org.apache.http.impl.client.cache;
|
package org.apache.http.impl.client.cache;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HeaderElement;
|
import org.apache.http.HeaderElement;
|
||||||
import org.apache.http.HttpRequest;
|
import org.apache.http.HttpRequest;
|
||||||
|
@ -42,6 +45,8 @@ import org.apache.http.impl.client.RequestWrapper;
|
||||||
@Immutable
|
@Immutable
|
||||||
class ConditionalRequestBuilder {
|
class ConditionalRequestBuilder {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(ConditionalRequestBuilder.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When a {@link HttpCacheEntry} is stale but 'might' be used as a response
|
* When a {@link HttpCacheEntry} is stale but 'might' be used as a response
|
||||||
* to an {@link HttpRequest} we will attempt to revalidate the entry with
|
* to an {@link HttpRequest} we will attempt to revalidate the entry with
|
||||||
|
@ -90,12 +95,16 @@ class ConditionalRequestBuilder {
|
||||||
* @param request the original request from the caller
|
* @param request the original request from the caller
|
||||||
* @param cacheEntry the entry that needs to be revalidated
|
* @param cacheEntry the entry that needs to be revalidated
|
||||||
* @return the wrapped request
|
* @return the wrapped request
|
||||||
* @throws ProtocolException when I am unable to build a new origin request.
|
|
||||||
*/
|
*/
|
||||||
public HttpRequest buildConditionalRequestFromVariants(HttpRequest request,
|
public HttpRequest buildConditionalRequestFromVariants(HttpRequest request,
|
||||||
Map<String, Variant> variants)
|
Map<String, Variant> variants) {
|
||||||
throws ProtocolException {
|
RequestWrapper wrapperRequest;
|
||||||
RequestWrapper wrapperRequest = new RequestWrapper(request);
|
try {
|
||||||
|
wrapperRequest = new RequestWrapper(request);
|
||||||
|
} catch (ProtocolException pe) {
|
||||||
|
log.warn("unable to build conditional request", pe);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
wrapperRequest.resetHeaders();
|
wrapperRequest.resetHeaders();
|
||||||
|
|
||||||
// we do not support partial content so all etags are used
|
// we do not support partial content so all etags are used
|
||||||
|
@ -126,8 +135,14 @@ class ConditionalRequestBuilder {
|
||||||
* @throws ProtocolException
|
* @throws ProtocolException
|
||||||
*/
|
*/
|
||||||
public HttpRequest buildUnconditionalRequest(HttpRequest request,
|
public HttpRequest buildUnconditionalRequest(HttpRequest request,
|
||||||
HttpCacheEntry entry) throws ProtocolException {
|
HttpCacheEntry entry) {
|
||||||
RequestWrapper wrapped = new RequestWrapper(request);
|
RequestWrapper wrapped;
|
||||||
|
try {
|
||||||
|
wrapped = new RequestWrapper(request);
|
||||||
|
} catch (ProtocolException e) {
|
||||||
|
log.warn("unable to build proper unconditional request", e);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
wrapped.resetHeaders();
|
wrapped.resetHeaders();
|
||||||
wrapped.addHeader("Cache-Control","no-cache");
|
wrapped.addHeader("Cache-Control","no-cache");
|
||||||
wrapped.addHeader("Pragma","no-cache");
|
wrapped.addHeader("Pragma","no-cache");
|
||||||
|
|
Loading…
Reference in New Issue