You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2010/10/19 21:54:29 UTC

svn commit: r1024383 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/CachingHttpClient.java test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java

Author: olegk
Date: Tue Oct 19 19:54:29 2010
New Revision: 1024383

URL: http://svn.apache.org/viewvc?rev=1024383&view=rev
Log:
HTTPCLIENT-1015: Support only-if-cached directive
Contributed by Michajlo Matijkiw <michajlo_matijkiw at comcast.com>

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.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=1024383&r1=1024382&r2=1024383&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 Oct 19 19:54:29 2010
@@ -407,6 +407,11 @@ public class CachingHttpClient implement
                 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);
@@ -447,6 +452,11 @@ public class CachingHttpClient implement
             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");
 
@@ -472,6 +482,17 @@ public class CachingHttpClient implement
         return callBackend(target, request, context);
     }
 
+    private boolean mayCallBackend(HttpRequest request) {
+        for (Header h: request.getHeaders("Cache-Control")) {
+            for (HeaderElement elt : h.getElements()) {
+                if ("only-if-cached".equals(elt.getName())) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
     private boolean explicitFreshnessRequest(HttpRequest request, HttpCacheEntry entry, Date now) {
         for(Header h : request.getHeaders("Cache-Control")) {
             for(HeaderElement elt : h.getElements()) {

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=1024383&r1=1024382&r2=1024383&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 Oct 19 19:54:29 2010
@@ -1931,6 +1931,59 @@ public class TestCachingHttpClient {
         Assert.assertSame(resp, result);
     }
 
+    @Test
+    public void testIfOnlyIfCachedAndNoCacheEntryBackendNotCalled() throws IOException {
+        impl = new CachingHttpClient(mockBackend);
+
+        request.addHeader("Cache-Control", "only-if-cached");
+
+        HttpResponse resp = impl.execute(host, request);
+
+        Assert.assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, resp.getStatusLine().getStatusCode());
+    }
+
+    @Test
+    public void testIfOnlyIfCachedAndEntryNotSuitableBackendNotCalled() throws Exception {
+
+        request.setHeader("Cache-Control", "only-if-cached");
+
+        entry = HttpTestUtils.makeCacheEntry(new Header[]{new BasicHeader("Cache-Control", "must-revalidate")});
+
+        requestIsFatallyNonCompliant(null);
+        requestProtocolValidationIsCalled();
+        cacheInvalidatorWasCalled();
+        requestPolicyAllowsCaching(true);
+        getCacheEntryReturns(entry);
+        cacheEntrySuitable(false);
+
+        replayMocks();
+        HttpResponse resp = impl.execute(host, request);
+        verifyMocks();
+
+        Assert.assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, resp.getStatusLine().getStatusCode());
+    }
+
+    @Test
+    public void testIfOnlyIfCachedAndEntryExistsAndIsSuitableReturnsEntry() throws Exception {
+
+        request.setHeader("Cache-Control", "only-if-cached");
+
+        requestIsFatallyNonCompliant(null);
+        requestProtocolValidationIsCalled();
+        cacheInvalidatorWasCalled();
+        requestPolicyAllowsCaching(true);
+        getCacheEntryReturns(entry);
+        cacheEntrySuitable(true);
+        responseIsGeneratedFromCache();
+        entryHasStaleness(0);
+
+        replayMocks();
+        HttpResponse resp = impl.execute(host, request);
+        verifyMocks();
+
+        Assert.assertSame(mockCachedResponse, resp);
+    }
+
     private void getCacheEntryReturns(HttpCacheEntry result) throws IOException {
         EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(result);
     }