hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1050878 - /httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
Date Sun, 19 Dec 2010 15:25:04 GMT
Author: jonm
Date: Sun Dec 19 15:25:04 2010
New Revision: 1050878

URL: http://svn.apache.org/viewvc?rev=1050878&view=rev
Log:
HTTPCLIENT-975: refactoring primary execute method in 
CachingHttpClient (mostly method extraction).


Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java?rev=1050878&r1=1050877&r2=1050878&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
Sun Dec 19 15:25:04 2010
@@ -397,11 +397,7 @@ public class CachingHttpClient implement
         }
         request.addHeader("Via",via);
 
-        try {
-            responseCache.flushInvalidatedCacheEntriesFor(target, request);
-        } catch (IOException ioe) {
-            log.warn("Unable to flush invalidated entries from cache", ioe);
-        }
+        flushEntriesInvalidatedByRequest(target, request);
 
         if (!cacheableRequestPolicy.isServableFromCache(request)) {
             return callBackend(target, request, context);
@@ -445,29 +441,16 @@ public class CachingHttpClient implement
         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;
+            return generateCachedResponse(request, context, entry, now);
         }
 
         if (!mayCallBackend(request)) {
-            return new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT,
-                    "Gateway Timeout");
+            return generateGatewayTimeout(context);
         }
 
         if (validityPolicy.isRevalidatable(entry)) {
@@ -484,18 +467,7 @@ public class CachingHttpClient implement
                 }
                 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;
-                }
+                return handleRevalidationFailure(request, context, entry, now);
             } catch (ProtocolException e) {
                 throw new ClientProtocolException(e);
             }
@@ -503,6 +475,61 @@ public class CachingHttpClient implement
         return callBackend(target, request, context);
     }
 
+    private void flushEntriesInvalidatedByRequest(HttpHost target,
+            HttpRequest request) {
+        try {
+            responseCache.flushInvalidatedCacheEntriesFor(target, request);
+        } catch (IOException ioe) {
+            log.warn("Unable to flush invalidated entries from cache", ioe);
+        }
+    }
+
+    private HttpResponse generateCachedResponse(HttpRequest request,
+            HttpContext context, HttpCacheEntry entry, Date 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;
+    }
+
+    private HttpResponse handleRevalidationFailure(HttpRequest request,
+            HttpContext context, HttpCacheEntry entry, Date now) {
+        if (staleResponseNotAllowed(request, entry, now)) {
+            return generateGatewayTimeout(context);
+        } else {
+            return unvalidatedCacheHit(context, entry);
+        }
+    }
+
+    private HttpResponse generateGatewayTimeout(HttpContext context) {
+        setResponseStatus(context, CacheResponseStatus.CACHE_MODULE_RESPONSE);
+        return new BasicHttpResponse(HttpVersion.HTTP_1_1,
+                HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
+    }
+
+    private HttpResponse unvalidatedCacheHit(HttpContext context,
+            HttpCacheEntry entry) {
+        final HttpResponse cachedResponse = responseGenerator.generateResponse(entry);
+        setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
+        cachedResponse.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\"");
+        return cachedResponse;
+    }
+
+    private boolean staleResponseNotAllowed(HttpRequest request,
+            HttpCacheEntry entry, Date now) {
+        return validityPolicy.mustRevalidate(entry)
+            || (isSharedCache() && validityPolicy.proxyRevalidate(entry))
+            || explicitFreshnessRequest(request, entry, now);
+    }
+
     private boolean mayCallBackend(HttpRequest request) {
         for (Header h: request.getHeaders("Cache-Control")) {
             for (HeaderElement elt : h.getElements()) {



Mime
View raw message