hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1051133 - /httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
Date Mon, 20 Dec 2010 14:42:34 GMT
Author: jonm
Date: Mon Dec 20 14:42:34 2010
New Revision: 1051133

URL: http://svn.apache.org/viewvc?rev=1051133&view=rev
Log:
HTTPCLIENT-975: more method extraction; the body for
CachingHttpClient#execute now at least fits on one screen. :)

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=1051133&r1=1051132&r2=1051133&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
Mon Dec 20 14:42:34 2010
@@ -396,29 +396,17 @@ public class CachingHttpClient implement
             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);
-        }
+        HttpCacheEntry entry = satisfyFromCache(target, request);
         if (entry == null) {
-            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);
+            return handleCacheMiss(target, request, context);
         }
 
+        return handleCacheHit(target, request, context, entry); 
+    }
+
+    private HttpResponse handleCacheHit(HttpHost target, HttpRequest request,
+            HttpContext context, HttpCacheEntry entry)
+            throws ClientProtocolException, IOException {
         recordCacheHit(target, request);
 
         Date now = getCurrentDate();
@@ -431,26 +419,59 @@ public class CachingHttpClient implement
         }
 
         if (validityPolicy.isRevalidatable(entry)) {
-            log.debug("Revalidating the cache entry");
+            return revalidateCacheEntry(target, request, context, entry, now);
+        }
+        return callBackend(target, request, context);
+    }
 
-            try {
-                if (asynchRevalidator != null && validityPolicy.mayReturnStaleWhileRevalidating(entry,
now)) {
-                    final HttpResponse resp = responseGenerator.generateResponse(entry);
-                    resp.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is
stale\"");
-                    
-                    asynchRevalidator.revalidateCacheEntry(target, request, context, entry);
-                    
-                    return resp;
-                }
-                return revalidateCacheEntry(target, request, context, entry);
-            } catch (IOException ioex) {
-                return handleRevalidationFailure(request, context, entry, now);
-            } catch (ProtocolException e) {
-                throw new ClientProtocolException(e);
+    private HttpResponse revalidateCacheEntry(HttpHost target,
+            HttpRequest request, HttpContext context, HttpCacheEntry entry,
+            Date now) throws ClientProtocolException {
+        log.debug("Revalidating the cache entry");
+
+        try {
+            if (asynchRevalidator != null && validityPolicy.mayReturnStaleWhileRevalidating(entry,
now)) {
+                final HttpResponse resp = responseGenerator.generateResponse(entry);
+                resp.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"");
+                
+                asynchRevalidator.revalidateCacheEntry(target, request, context, entry);
+                
+                return resp;
             }
+            return revalidateCacheEntry(target, request, context, entry);
+        } catch (IOException ioex) {
+            return handleRevalidationFailure(request, context, entry, now);
+        } catch (ProtocolException e) {
+            throw new ClientProtocolException(e);
+        }
+    }
+
+    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); 
 
+        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,



Mime
View raw message