You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by jo...@apache.org on 2010/12/20 13:12:55 UTC

svn commit: r1051076 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/impl/client/cache/

Author: jonm
Date: Mon Dec 20 12:12:55 2010
New Revision: 1051076

URL: http://svn.apache.org/viewvc?rev=1051076&view=rev
Log:
HTTPCLIENT-975: more refactoring/method-extraction

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.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=1051076&r1=1051075&r2=1051076&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 12:12:55 2010
@@ -390,11 +390,7 @@ public class CachingHttpClient implement
             return requestCompliance.getErrorForRequest(error);
         }
 
-        try {
-            request = requestCompliance.makeRequestCompliant(request);
-        } catch (ProtocolException e) {
-            throw new ClientProtocolException(e);
-        }
+        request = requestCompliance.makeRequestCompliant(request);
         request.addHeader("Via",via);
 
         flushEntriesInvalidatedByRequest(target, request);
@@ -674,7 +670,6 @@ public class CachingHttpClient implement
 
         String resultEtag = resultEtagHeader.getValue();
         Variant matchingVariant = variants.get(resultEtag);
-
         if (matchingVariant == null) {
             log.debug("304 response did not contain ETag matching one sent in If-None-Match");
             return callBackend(target, request, context);
@@ -683,14 +678,43 @@ public class CachingHttpClient implement
         HttpCacheEntry matchedEntry = matchingVariant.getEntry();
         
         if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) {
-        	HttpRequest unconditional = conditionalRequestBuilder
-        		.buildUnconditionalRequest(request, matchedEntry);
-        	return callBackend(target, unconditional, context);
+        	return retryRequestUnconditionally(target, request, context,
+                    matchedEntry);
         }
         
+        recordCacheUpdate(context);
+
+        HttpCacheEntry responseEntry = getUpdatedVariantEntry(target,
+                conditionalRequest, requestDate, responseDate, backendResponse,
+                matchingVariant, matchedEntry);
+
+        HttpResponse resp = responseGenerator.generateResponse(responseEntry);
+        tryToUpdateVariantMap(target, request, matchingVariant);
+
+        if (shouldSendNotModifiedResponse(request, responseEntry)) {
+            return responseGenerator.generateNotModifiedResponse(responseEntry);
+        }
+
+        return resp;
+    }
+
+    private HttpResponse retryRequestUnconditionally(HttpHost target,
+            HttpRequest request, HttpContext context,
+            HttpCacheEntry matchedEntry) throws IOException {
+        HttpRequest unconditional = conditionalRequestBuilder
+        	.buildUnconditionalRequest(request, matchedEntry);
+        return callBackend(target, unconditional, context);
+    }
+
+    private void recordCacheUpdate(HttpContext context) {
         cacheUpdates.getAndIncrement();
         setResponseStatus(context, CacheResponseStatus.VALIDATED);
+    }
 
+    private HttpCacheEntry getUpdatedVariantEntry(HttpHost target,
+            HttpRequest conditionalRequest, Date requestDate,
+            Date responseDate, HttpResponse backendResponse,
+            Variant matchingVariant, HttpCacheEntry matchedEntry) {
         HttpCacheEntry responseEntry = matchedEntry;
         try {
             responseEntry = responseCache.updateVariantCacheEntry(target, conditionalRequest,
@@ -698,19 +722,22 @@ public class CachingHttpClient implement
         } catch (IOException ioe) {
             log.warn("Could not update cache entry", ioe);
         }
+        return responseEntry;
+    }
 
-        HttpResponse resp = responseGenerator.generateResponse(responseEntry);
+    private void tryToUpdateVariantMap(HttpHost target, HttpRequest request,
+            Variant matchingVariant) {
         try {
             responseCache.reuseVariantEntryFor(target, request, matchingVariant);
         } catch (IOException ioe) {
             log.warn("Could not update cache entry to reuse variant", ioe);
         }
+    }
 
-        if (suitabilityChecker.isConditional(request) && suitabilityChecker.allConditionalsMatch(request, responseEntry, new Date())) {
-            return responseGenerator.generateNotModifiedResponse(responseEntry);
-        }
-
-        return resp;
+    private boolean shouldSendNotModifiedResponse(HttpRequest request,
+            HttpCacheEntry responseEntry) {
+        return (suitabilityChecker.isConditional(request)
+                && suitabilityChecker.allConditionalsMatch(request, responseEntry, new Date()));
     }
 
     HttpResponse revalidateCacheEntry(
@@ -737,8 +764,7 @@ public class CachingHttpClient implement
 
         int statusCode = backendResponse.getStatusLine().getStatusCode();
         if (statusCode == HttpStatus.SC_NOT_MODIFIED || statusCode == HttpStatus.SC_OK) {
-            cacheUpdates.getAndIncrement();
-            setResponseStatus(context, CacheResponseStatus.VALIDATED);
+            recordCacheUpdate(context);
         }
 
         if (statusCode == HttpStatus.SC_NOT_MODIFIED) {

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java?rev=1051076&r1=1051075&r2=1051076&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java Mon Dec 20 12:12:55 2010
@@ -39,6 +39,7 @@ import org.apache.http.HttpVersion;
 import org.apache.http.ProtocolException;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.annotation.Immutable;
+import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.cache.HeaderConstants;
 import org.apache.http.entity.AbstractHttpEntity;
 import org.apache.http.impl.client.RequestWrapper;
@@ -92,9 +93,11 @@ class RequestProtocolCompliance {
      *
      * @param request the request to check for compliance
      * @return the updated request
-     * @throws ProtocolException when we have trouble making the request compliant
+     * @throws ClientProtocolException when we have trouble making the request compliant
      */
-    public HttpRequest makeRequestCompliant(HttpRequest request) throws ProtocolException {
+    public HttpRequest makeRequestCompliant(HttpRequest request)
+        throws ClientProtocolException {
+    
         if (requestMustNotHaveEntity(request)) {
             ((HttpEntityEnclosingRequest) request).setEntity(null);
         }
@@ -212,16 +215,26 @@ class RequestProtocolCompliance {
     }
 
     private HttpRequest upgradeRequestTo(HttpRequest request, ProtocolVersion version)
-            throws ProtocolException {
-        RequestWrapper newRequest = new RequestWrapper(request);
+            throws ClientProtocolException {
+        RequestWrapper newRequest;
+        try {
+            newRequest = new RequestWrapper(request);
+        } catch (ProtocolException pe) {
+            throw new ClientProtocolException(pe);
+        }
         newRequest.setProtocolVersion(version);
 
         return newRequest;
     }
 
     private HttpRequest downgradeRequestTo(HttpRequest request, ProtocolVersion version)
-            throws ProtocolException {
-        RequestWrapper newRequest = new RequestWrapper(request);
+            throws ClientProtocolException {
+        RequestWrapper newRequest;
+        try {
+            newRequest = new RequestWrapper(request);
+        } catch (ProtocolException pe) {
+            throw new ClientProtocolException(pe);
+        }
         newRequest.setProtocolVersion(version);
 
         return newRequest;

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java?rev=1051076&r1=1051075&r2=1051076&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java Mon Dec 20 12:12:55 2010
@@ -868,7 +868,7 @@ public class TestCachingHttpClient {
     @Test
     public void testNonCompliantRequestWrapsAndReThrowsProtocolException() throws Exception {
 
-        ProtocolException expected = new ProtocolException("ouch");
+        ClientProtocolException expected = new ClientProtocolException("ouch");
 
         requestIsFatallyNonCompliant(null);
         requestCannotBeMadeCompliantThrows(expected);
@@ -878,7 +878,7 @@ public class TestCachingHttpClient {
         try {
             impl.execute(host, request, context);
         } catch (ClientProtocolException ex) {
-            Assert.assertTrue(ex.getCause().getMessage().equals(expected.getMessage()));
+            Assert.assertSame(expected, ex);
             gotException = true;
         }
         verifyMocks();
@@ -2039,7 +2039,7 @@ public class TestCachingHttpClient {
                         EasyMock.<HttpRequest>anyObject())).andReturn(request);
     }
 
-    private void requestCannotBeMadeCompliantThrows(ProtocolException exception) throws Exception {
+    private void requestCannotBeMadeCompliantThrows(ClientProtocolException exception) throws Exception {
         EasyMock.expect(
                 mockRequestProtocolCompliance.makeRequestCompliant(
                         EasyMock.<HttpRequest>anyObject())).andThrow(exception);