}
try {
request = requestCompliance.makeRequestCompliant(request);
} catch (ProtocolException e) {
throw new ClientProtocolException(e);
}
request.addHeader("Via",via);
try {
responseCache.flushInvalidatedCacheEntriesFor(target, request);
} catch (IOException ioe) {
log.warn("Unable to flush invalidated entries from cache", ioe);
}
if (!cacheableRequestPolicy.isServableFromCache(request)) {
return callBackend(target, request, context);
}
HttpCacheEntry entry = null;
try {
entry = responseCache.getCacheEntry(target, request);
} catch (IOException ioe) {
log.warn("Unable to retrieve entries from cache", ioe);
}
if (entry == null) {
cacheMisses.getAndIncrement();
if (log.isDebugEnabled()) {
RequestLine rl = request.getRequestLine();
log.debug("Cache miss [host: " + target + "; uri: " + rl.getUri() + "]");
}
if (!mayCallBackend(request)) {
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
"Gateway Timeout");
}
Set<HttpCacheEntry> variantEntries = null;
try {
responseCache.getVariantCacheEntries(target, request);
} catch (IOException ioe) {
log.warn("Unable to retrieve variant entries from cache", ioe);
}
if (variantEntries != null && variantEntries.size() > 0) {
try {
return negotiateResponseFromVariants(target, request, context, variantEntries);
} catch (ProtocolException e) {
throw new ClientProtocolException(e);
}
}
return callBackend(target, request, context);
}
if (log.isDebugEnabled()) {
RequestLine rl = request.getRequestLine();
log.debug("Cache hit [host: " + target + "; uri: " + rl.getUri() + "]");
}
cacheHits.getAndIncrement();
Date now = getCurrentDate();
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
final HttpResponse cachedResponse;
if (request.containsHeader(HeaderConstants.IF_NONE_MATCH)
|| request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) {
cachedResponse = responseGenerator.generateNotModifiedResponse(entry);
} else {
cachedResponse = responseGenerator.generateResponse(entry);
}
setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
if (validityPolicy.getStalenessSecs(entry, now) > 0L) {
cachedResponse.addHeader("Warning","110 localhost \"Response is stale\"");
}
return cachedResponse;
}
if (!mayCallBackend(request)) {
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
"Gateway Timeout");
}
if (validityPolicy.isRevalidatable(entry)) {
log.debug("Revalidating the cache entry");
try {
return revalidateCacheEntry(target, request, context, entry);
} catch (IOException ioex) {
if (validityPolicy.mustRevalidate(entry)
|| (isSharedCache() && validityPolicy.proxyRevalidate(entry))
|| explicitFreshnessRequest(request, entry, now)) {
setResponseStatus(context, CacheResponseStatus.CACHE_MODULE_RESPONSE);
return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
} else {
final HttpResponse cachedResponse = responseGenerator.generateResponse(entry);
setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
cachedResponse.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\"");
log.debug("111 revalidation failed due to exception: " + ioex);
return cachedResponse;
}
} catch (ProtocolException e) {
throw new ClientProtocolException(e);
}
}
return callBackend(target, request, context);
}