From 4e7d9fa5c8e6cf0a12aed9a7d55cd6c4c27bb3eb Mon Sep 17 00:00:00 2001 From: Jonathan Moore Date: Sun, 12 Dec 2010 14:24:25 +0000 Subject: [PATCH] HTTPCLIENT-1032: minor refactoring on URIExtractor to clean up internal-only API for getVariantKey() git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1044824 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/client/cache/BasicHttpCache.java | 12 +++------- .../http/impl/client/cache/URIExtractor.java | 20 +++++++++++------ .../impl/client/cache/TestURIExtractor.java | 22 +++++++++++-------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java index 205fa8f6d..106f6db1c 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java @@ -235,22 +235,16 @@ class BasicHttpCache implements HttpCache { public void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request) throws IOException { cacheInvalidator.flushInvalidatedCacheEntries(host, request); - } public Set getVariantCacheEntries(HttpHost host, HttpRequest request) throws IOException { Set variants = new HashSet(); - HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request)); - if (root != null) { - if (root.hasVariants()) { - for(String variantUri : root.getVariantMap().values()) { - variants.add(storage.getEntry(variantUri)); - } - } + if (root == null || !root.hasVariants()) return variants; + for(String variantCacheKey : root.getVariantMap().values()) { + variants.add(storage.getEntry(variantCacheKey)); } - return variants; } diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java index 447930565..229ae7806 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java @@ -136,16 +136,22 @@ class URIExtractor { * @return String the extracted variant URI */ public String getVariantURI(HttpHost host, HttpRequest req, HttpCacheEntry entry) { - Header[] varyHdrs = entry.getHeaders(HeaderConstants.VARY); - if (varyHdrs == null || varyHdrs.length == 0) { - return getURI(host, req); - } - return getVariantKey(req, varyHdrs) + getURI(host, req); + if (!entry.hasVariants()) return getURI(host, req); + return getVariantKey(req, entry) + getURI(host, req); } - public String getVariantKey(HttpRequest req, Header[] varyHdrs) { + /** + * Compute a "variant key" from the headers of a given request that are + * covered by the Vary header of a given cache entry. Any request whose + * varying headers match those of this request should have the same + * variant key. + * @param req originating request + * @param entry cache entry in question that has variants + * @return a String variant key + */ + public String getVariantKey(HttpRequest req, HttpCacheEntry entry) { List variantHeaderNames = new ArrayList(); - for (Header varyHdr : varyHdrs) { + for (Header varyHdr : entry.getHeaders(HeaderConstants.VARY)) { for (HeaderElement elt : varyHdr.getElements()) { variantHeaderNames.add(elt.getName()); } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestURIExtractor.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestURIExtractor.java index b6914ddf9..0b603b5e0 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestURIExtractor.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestURIExtractor.java @@ -123,8 +123,7 @@ public class TestURIExtractor { @Test public void testGetVariantURIWithNoVaryHeaderReturnsNormalURI() { final String theURI = "theURI"; - Header[] noHdrs = new Header[0]; - org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(noHdrs); + org.easymock.EasyMock.expect(mockEntry.hasVariants()).andReturn(false); extractor = new URIExtractor() { @Override public String getURI(HttpHost h, HttpRequest req) { @@ -154,8 +153,9 @@ public class TestURIExtractor { return theURI; } }; - org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); - org.easymock.EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn( + EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes(); + EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); + EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn( encHeaders); replayMocks(); @@ -178,8 +178,9 @@ public class TestURIExtractor { return theURI; } }; - org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); - org.easymock.EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")) + EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes(); + EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); + EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")) .andReturn(noHeaders); replayMocks(); @@ -203,10 +204,11 @@ public class TestURIExtractor { return theURI; } }; - org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); - org.easymock.EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn( + EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes(); + EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); + EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn( encHeaders); - org.easymock.EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders); + EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders); replayMocks(); String result = extractor.getVariantURI(host, mockRequest, mockEntry); @@ -230,6 +232,7 @@ public class TestURIExtractor { return theURI; } }; + EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes(); EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders); EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders); @@ -256,6 +259,7 @@ public class TestURIExtractor { return theURI; } }; + EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes(); EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders); EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);