HTTPCLIENT-975: more method extraction; the body for

CachingHttpClient#execute now at least fits on one screen. :)


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1051133 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Moore 2010-12-20 14:42:34 +00:00
parent 65cb252c9b
commit 7573bf8284
1 changed files with 59 additions and 38 deletions

View File

@ -396,29 +396,17 @@ public class CachingHttpClient implements HttpClient {
return callBackend(target, request, context); return callBackend(target, request, context);
} }
HttpCacheEntry entry = null; HttpCacheEntry entry = satisfyFromCache(target, request);
try {
entry = responseCache.getCacheEntry(target, request);
} catch (IOException ioe) {
log.warn("Unable to retrieve entries from cache", ioe);
}
if (entry == null) { if (entry == null) {
recordCacheMiss(target, request); return handleCacheMiss(target, request, context);
if (!mayCallBackend(request)) {
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
"Gateway Timeout");
} }
Map<String, Variant> variants = return handleCacheHit(target, request, context, entry);
getExistingCacheVariants(target, request);
if (variants != null && variants.size() > 0) {
return negotiateResponseFromVariants(target, request, context, variants);
}
return callBackend(target, request, context);
} }
private HttpResponse handleCacheHit(HttpHost target, HttpRequest request,
HttpContext context, HttpCacheEntry entry)
throws ClientProtocolException, IOException {
recordCacheHit(target, request); recordCacheHit(target, request);
Date now = getCurrentDate(); Date now = getCurrentDate();
@ -431,6 +419,14 @@ public class CachingHttpClient implements HttpClient {
} }
if (validityPolicy.isRevalidatable(entry)) { if (validityPolicy.isRevalidatable(entry)) {
return revalidateCacheEntry(target, request, context, entry, now);
}
return callBackend(target, request, context);
}
private HttpResponse revalidateCacheEntry(HttpHost target,
HttpRequest request, HttpContext context, HttpCacheEntry entry,
Date now) throws ClientProtocolException {
log.debug("Revalidating the cache entry"); log.debug("Revalidating the cache entry");
try { try {
@ -449,8 +445,33 @@ public class CachingHttpClient implements HttpClient {
throw new ClientProtocolException(e); throw new ClientProtocolException(e);
} }
} }
return callBackend(target, request, context);
private HttpResponse handleCacheMiss(HttpHost target, HttpRequest request,
HttpContext context) throws IOException {
recordCacheMiss(target, request);
if (!mayCallBackend(request)) {
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
"Gateway Timeout");
}
Map<String, Variant> variants =
getExistingCacheVariants(target, request);
if (variants != null && variants.size() > 0) {
return negotiateResponseFromVariants(target, request, context, variants);
}
return callBackend(target, request, context);
}
private HttpCacheEntry satisfyFromCache(HttpHost target, HttpRequest request) {
HttpCacheEntry entry = null;
try {
entry = responseCache.getCacheEntry(target, request);
} catch (IOException ioe) {
log.warn("Unable to retrieve entries from cache", ioe);
}
return entry;
} }
private HttpResponse getFatallyNoncompliantResponse(HttpRequest request, private HttpResponse getFatallyNoncompliantResponse(HttpRequest request,