hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1049045 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/impl/client/cache/
Date Tue, 14 Dec 2010 11:12:36 GMT
Author: jonm
Date: Tue Dec 14 11:12:36 2010
New Revision: 1049045

URL: http://svn.apache.org/viewvc?rev=1049045&view=rev
Log:
HTTPCLIENT-1032: variant information is now passed back from the HttpCache
as a Map<String,Variant> mapping Etags to specific information about the
variants (variant key, cache key, cache entry).

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
    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/ConditionalRequestBuilder.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HttpCache.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/AbstractProtocolTest.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java?rev=1049045&r1=1049044&r2=1049045&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
Tue Dec 14 11:12:36 2010
@@ -239,17 +239,19 @@ class BasicHttpCache implements HttpCach
         cacheInvalidator.flushInvalidatedCacheEntries(host, request);
     }
 
-    public Map<String, HttpCacheEntry> getVariantCacheEntriesWithEtags(HttpHost host,
HttpRequest request)
+    public Map<String, Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest
request)
             throws IOException {
-        Map<String,HttpCacheEntry> variants = new HashMap<String,HttpCacheEntry>();
+        Map<String,Variant> variants = new HashMap<String,Variant>();
         HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request));
         if (root == null || !root.hasVariants()) return variants;
-        for(String variantCacheKey : root.getVariantMap().values()) {
+        for(Map.Entry<String, String> variant : root.getVariantMap().entrySet()) {
+            String variantKey = variant.getKey();
+            String variantCacheKey = variant.getValue();
             HttpCacheEntry entry = storage.getEntry(variantCacheKey);
+            if (entry == null) continue;
             Header etagHeader = entry.getFirstHeader(HeaderConstants.ETAG);
-            if (etagHeader != null) {
-                variants.put(etagHeader.getValue(), entry);
-            }
+            if (etagHeader == null) continue;
+            variants.put(etagHeader.getValue(), new Variant(variantKey, variantCacheKey,
entry));
         }
         return variants;
     }

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=1049045&r1=1049044&r2=1049045&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
Tue Dec 14 11:12:36 2010
@@ -412,15 +412,15 @@ public class CachingHttpClient implement
                         "Gateway Timeout");
             }
 
-            Map<String,HttpCacheEntry> variantEntries = null;
+            Map<String,Variant> variants = null;
             try {
-                variantEntries = responseCache.getVariantCacheEntriesWithEtags(target, request);
+                variants = responseCache.getVariantCacheEntriesWithEtags(target, request);
             } catch (IOException ioe) {
                 log.warn("Unable to retrieve variant entries from cache", ioe);
             }
-            if (variantEntries != null && variantEntries.size() > 0) {
+            if (variants != null && variants.size() > 0) {
                 try {
-                    return negotiateResponseFromVariants(target, request, context, variantEntries);
+                    return negotiateResponseFromVariants(target, request, context, variants);
                 } catch (ProtocolException e) {
                     throw new ClientProtocolException(e);
                 }
@@ -594,8 +594,8 @@ public class CachingHttpClient implement
     
     HttpResponse negotiateResponseFromVariants(HttpHost target,
             HttpRequest request, HttpContext context,
-            Map<String, HttpCacheEntry> variantEntries) throws IOException, ProtocolException
{
-        HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(request,
variantEntries);
+            Map<String, Variant> variants) throws IOException, ProtocolException {
+        HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(request,
variants);
 
         Date requestDate = getCurrentDate();
         HttpResponse backendResponse = backend.execute(target, conditionalRequest, context);
@@ -605,7 +605,6 @@ public class CachingHttpClient implement
 
         if (backendResponse.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_MODIFIED)
{
             return handleBackendResponse(target, request, requestDate, responseDate, backendResponse);
-//            return handleBackendResponse(target, conditionalRequest, requestDate, responseDate,
backendResponse);
         }
 
         Header resultEtagHeader = backendResponse.getFirstHeader(HeaderConstants.ETAG);
@@ -615,13 +614,15 @@ public class CachingHttpClient implement
         }
 
         String resultEtag = resultEtagHeader.getValue();
-        HttpCacheEntry matchedEntry = variantEntries.get(resultEtag);
+        Variant matchingVariant = variants.get(resultEtag);
 
-        if (matchedEntry == null) {
+        if (matchingVariant == null) {
             log.debug("304 response did not contain ETag matching one sent in If-None-Match");
             return callBackend(target, request, context);
         }
 
+        HttpCacheEntry matchedEntry = matchingVariant.getEntry();
+        
         if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) {
         	HttpRequest unconditional = conditionalRequestBuilder
         		.buildUnconditionalRequest(request, matchedEntry);

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ConditionalRequestBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ConditionalRequestBuilder.java?rev=1049045&r1=1049044&r2=1049045&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ConditionalRequestBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ConditionalRequestBuilder.java
Tue Dec 14 11:12:36 2010
@@ -93,7 +93,7 @@ class ConditionalRequestBuilder {
      * @throws ProtocolException when I am unable to build a new origin request.
      */
     public HttpRequest buildConditionalRequestFromVariants(HttpRequest request,
-            Map<String, HttpCacheEntry> variantEntries)
+            Map<String, Variant> variants)
                 throws ProtocolException {
         RequestWrapper wrapperRequest = new RequestWrapper(request);
         wrapperRequest.resetHeaders();
@@ -101,7 +101,7 @@ class ConditionalRequestBuilder {
         // we do not support partial content so all etags are used
         StringBuilder etags = new StringBuilder();
         boolean first = true;
-        for(String etag : variantEntries.keySet()) {
+        for(String etag : variants.keySet()) {
             if (!first) {
                 etags.append(",");
             }

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HttpCache.java?rev=1049045&r1=1049044&r2=1049045&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HttpCache.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HttpCache.java
Tue Dec 14 11:12:36 2010
@@ -75,7 +75,7 @@ interface HttpCache {
      * @return a <code>Map</code> mapping Etags to variant cache entries
      * @throws IOException
      */
-    Map<String,HttpCacheEntry> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest
request)
+    Map<String,Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest
request)
         throws IOException;
 
     /**

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/AbstractProtocolTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/AbstractProtocolTest.java?rev=1049045&r1=1049044&r2=1049045&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/AbstractProtocolTest.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/AbstractProtocolTest.java
Tue Dec 14 11:12:36 2010
@@ -33,7 +33,6 @@ import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpVersion;
 import org.apache.http.client.HttpClient;
-import org.apache.http.client.cache.HttpCacheEntry;
 import org.apache.http.message.BasicHttpRequest;
 import org.apache.http.protocol.HttpContext;
 import org.easymock.IExpectationSetters;
@@ -104,7 +103,7 @@ public abstract class AbstractProtocolTe
         EasyMock.expect(mockCache.getCacheEntry(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class)))
             .andReturn(null).anyTimes();
         EasyMock.expect(mockCache.getVariantCacheEntriesWithEtags(EasyMock.isA(HttpHost.class),
EasyMock.isA(HttpRequest.class)))
-            .andReturn(new HashMap<String,HttpCacheEntry>()).anyTimes();
+            .andReturn(new HashMap<String,Variant>()).anyTimes();
 
         mockCache.flushCacheEntriesFor(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class));
         EasyMock.expectLastCall().anyTimes();

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java?rev=1049045&r1=1049044&r2=1049045&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
Tue Dec 14 11:12:36 2010
@@ -338,7 +338,7 @@ public class TestBasicHttpCache {
         HttpHost host = new HttpHost("foo.example.com");
         HttpRequest request = new HttpGet("http://foo.example.com/bar");
 
-        Map<String,HttpCacheEntry> variants = impl.getVariantCacheEntriesWithEtags(host,
request);
+        Map<String,Variant> variants = impl.getVariantCacheEntriesWithEtags(host, request);
 
         assertNotNull(variants);
         assertEquals(0, variants.size());
@@ -372,7 +372,7 @@ public class TestBasicHttpCache {
         impl.cacheAndReturnResponse(host, req1, resp1, new Date(), new Date());
         impl.cacheAndReturnResponse(host, req2, resp2, new Date(), new Date());
 
-        Map<String,HttpCacheEntry> variants = impl.getVariantCacheEntriesWithEtags(host,
req1);
+        Map<String,Variant> variants = impl.getVariantCacheEntriesWithEtags(host, req1);
 
         assertNotNull(variants);
         assertEquals(2, variants.size());

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=1049045&r1=1049044&r2=1049045&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
Tue Dec 14 11:12:36 2010
@@ -300,7 +300,7 @@ public class TestCachingHttpClient {
         cacheInvalidatorWasCalled();
         requestPolicyAllowsCaching(true);
         getCacheEntryReturns(null);
-        getVariantCacheEntriesReturns(new HashMap<String,HttpCacheEntry>());
+        getVariantCacheEntriesReturns(new HashMap<String,Variant>());
 
         requestProtocolValidationIsCalled();
         requestIsFatallyNonCompliant(null);
@@ -450,7 +450,7 @@ public class TestCachingHttpClient {
     @Test
     public void testRevalidationRetriesUnconditionallyIfOlderResponseReceived()
         throws Exception {
-// TODO
+
         mockImplMethods(GET_CURRENT_DATE);
 
         Date now = new Date();
@@ -1593,10 +1593,10 @@ public class TestCachingHttpClient {
         HttpCacheEntry variant3 = HttpTestUtils
                 .makeCacheEntry(new Header[] {new BasicHeader(HeaderConstants.ETAG, "\"etag3\"")
});
 
-        Map<String,HttpCacheEntry> variants = new HashMap<String,HttpCacheEntry>();
-        variants.put("\"etag1\"", variant1);
-        variants.put("\"etag2\"", variant2);
-        variants.put("\"etag3\"", variant3);
+        Map<String,Variant> variants = new HashMap<String,Variant>();
+        variants.put("\"etag1\"", new Variant("A","B",variant1));
+        variants.put("\"etag2\"", new Variant("C","D",variant2));
+        variants.put("\"etag3\"", new Variant("E","F",variant3));
 
         HttpRequest variantConditionalRequest = new BasicHttpRequest("GET", "http://foo.com/bar",
HttpVersion.HTTP_1_1);
         variantConditionalRequest.addHeader(new BasicHeader(HeaderConstants.IF_NONE_MATCH,
"etag1, etag2, etag3"));
@@ -1641,10 +1641,10 @@ public class TestCachingHttpClient {
 
         HttpCacheEntry cacheEntry = null;
 
-        Map<String,HttpCacheEntry> variants = new HashMap<String,HttpCacheEntry>();
-        variants.put("\"etag1\"", variant1);
-        variants.put("\"etag2\"", variant2);
-        variants.put("\"etag3\"", variant3);
+        Map<String,Variant> variants = new HashMap<String,Variant>();
+        variants.put("\"etag1\"", new Variant("A","B",variant1));
+        variants.put("\"etag2\"", new Variant("C","D",variant2));
+        variants.put("\"etag3\"", new Variant("E","F",variant3));
 
         HttpRequest variantConditionalRequest = new BasicHttpRequest("GET", "http://foo.com/bar",
HttpVersion.HTTP_1_1);
         variantConditionalRequest.addHeader(new BasicHeader(HeaderConstants.IF_NONE_MATCH,
"etag1, etag2, etag3"));
@@ -1957,7 +1957,7 @@ public class TestCachingHttpClient {
         EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(result);
     }
 
-    private void getVariantCacheEntriesReturns(Map<String,HttpCacheEntry> result) throws
IOException {
+    private void getVariantCacheEntriesReturns(Map<String,Variant> result) throws IOException
{
         EasyMock.expect(mockCache.getVariantCacheEntriesWithEtags(host, request)).andReturn(result);
     }
 
@@ -1989,7 +1989,7 @@ public class TestCachingHttpClient {
                 EasyMock.<HttpCacheEntry>anyObject())).andReturn(b);
     }
 
-    private void conditionalVariantRequestBuilderReturns(Map<String,HttpCacheEntry>
variantEntries, HttpRequest validate)
+    private void conditionalVariantRequestBuilderReturns(Map<String,Variant> variantEntries,
HttpRequest validate)
             throws Exception {
         EasyMock.expect(mockConditionalRequestBuilder.buildConditionalRequestFromVariants(request,
variantEntries))
             .andReturn(validate);

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java?rev=1049045&r1=1049044&r2=1049045&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java
Tue Dec 14 11:12:36 2010
@@ -307,10 +307,10 @@ public class TestConditionalRequestBuild
         String etag2 = "\"456\"";
         String etag3 = "\"789\"";
 
-        Map<String,HttpCacheEntry> variantEntries = new HashMap<String,HttpCacheEntry>();
-        variantEntries.put(etag1, HttpTestUtils.makeCacheEntry(new Header[] { new BasicHeader("ETag",
etag1) }));
-        variantEntries.put(etag2, HttpTestUtils.makeCacheEntry(new Header[] { new BasicHeader("ETag",
etag2) }));
-        variantEntries.put(etag3, HttpTestUtils.makeCacheEntry(new Header[] { new BasicHeader("ETag",
etag3) }));
+        Map<String,Variant> variantEntries = new HashMap<String,Variant>();
+        variantEntries.put(etag1, new Variant("A","B",HttpTestUtils.makeCacheEntry(new Header[]
{ new BasicHeader("ETag", etag1) })));
+        variantEntries.put(etag2, new Variant("C","D",HttpTestUtils.makeCacheEntry(new Header[]
{ new BasicHeader("ETag", etag2) })));
+        variantEntries.put(etag3, new Variant("E","F",HttpTestUtils.makeCacheEntry(new Header[]
{ new BasicHeader("ETag", etag3) })));
 
         HttpRequest conditional = impl.buildConditionalRequestFromVariants(request, variantEntries);
 



Mime
View raw message