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
This commit is contained in:
Jonathan Moore 2010-12-12 14:24:25 +00:00
parent 2c04f2689b
commit 4e7d9fa5c8
3 changed files with 29 additions and 25 deletions

View File

@ -235,22 +235,16 @@ class BasicHttpCache implements HttpCache {
public void flushInvalidatedCacheEntriesFor(HttpHost host, public void flushInvalidatedCacheEntriesFor(HttpHost host,
HttpRequest request) throws IOException { HttpRequest request) throws IOException {
cacheInvalidator.flushInvalidatedCacheEntries(host, request); cacheInvalidator.flushInvalidatedCacheEntries(host, request);
} }
public Set<HttpCacheEntry> getVariantCacheEntries(HttpHost host, HttpRequest request) public Set<HttpCacheEntry> getVariantCacheEntries(HttpHost host, HttpRequest request)
throws IOException { throws IOException {
Set<HttpCacheEntry> variants = new HashSet<HttpCacheEntry>(); Set<HttpCacheEntry> variants = new HashSet<HttpCacheEntry>();
HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request)); HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request));
if (root != null) { if (root == null || !root.hasVariants()) return variants;
if (root.hasVariants()) { for(String variantCacheKey : root.getVariantMap().values()) {
for(String variantUri : root.getVariantMap().values()) { variants.add(storage.getEntry(variantCacheKey));
variants.add(storage.getEntry(variantUri));
}
}
} }
return variants; return variants;
} }

View File

@ -136,16 +136,22 @@ class URIExtractor {
* @return String the extracted variant URI * @return String the extracted variant URI
*/ */
public String getVariantURI(HttpHost host, HttpRequest req, HttpCacheEntry entry) { public String getVariantURI(HttpHost host, HttpRequest req, HttpCacheEntry entry) {
Header[] varyHdrs = entry.getHeaders(HeaderConstants.VARY); if (!entry.hasVariants()) return getURI(host, req);
if (varyHdrs == null || varyHdrs.length == 0) { return getVariantKey(req, entry) + getURI(host, req);
return getURI(host, req);
}
return getVariantKey(req, varyHdrs) + 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 <code>String</code> variant key
*/
public String getVariantKey(HttpRequest req, HttpCacheEntry entry) {
List<String> variantHeaderNames = new ArrayList<String>(); List<String> variantHeaderNames = new ArrayList<String>();
for (Header varyHdr : varyHdrs) { for (Header varyHdr : entry.getHeaders(HeaderConstants.VARY)) {
for (HeaderElement elt : varyHdr.getElements()) { for (HeaderElement elt : varyHdr.getElements()) {
variantHeaderNames.add(elt.getName()); variantHeaderNames.add(elt.getName());
} }

View File

@ -123,8 +123,7 @@ public class TestURIExtractor {
@Test @Test
public void testGetVariantURIWithNoVaryHeaderReturnsNormalURI() { public void testGetVariantURIWithNoVaryHeaderReturnsNormalURI() {
final String theURI = "theURI"; final String theURI = "theURI";
Header[] noHdrs = new Header[0]; org.easymock.EasyMock.expect(mockEntry.hasVariants()).andReturn(false);
org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(noHdrs);
extractor = new URIExtractor() { extractor = new URIExtractor() {
@Override @Override
public String getURI(HttpHost h, HttpRequest req) { public String getURI(HttpHost h, HttpRequest req) {
@ -154,8 +153,9 @@ public class TestURIExtractor {
return theURI; return theURI;
} }
}; };
org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
org.easymock.EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn( EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(
encHeaders); encHeaders);
replayMocks(); replayMocks();
@ -178,8 +178,9 @@ public class TestURIExtractor {
return theURI; return theURI;
} }
}; };
org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
org.easymock.EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")) EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding"))
.andReturn(noHeaders); .andReturn(noHeaders);
replayMocks(); replayMocks();
@ -203,10 +204,11 @@ public class TestURIExtractor {
return theURI; return theURI;
} }
}; };
org.easymock.EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
org.easymock.EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn( EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(
encHeaders); encHeaders);
org.easymock.EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders); EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);
replayMocks(); replayMocks();
String result = extractor.getVariantURI(host, mockRequest, mockEntry); String result = extractor.getVariantURI(host, mockRequest, mockEntry);
@ -230,6 +232,7 @@ public class TestURIExtractor {
return theURI; return theURI;
} }
}; };
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders); EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders);
EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders); EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);
@ -256,6 +259,7 @@ public class TestURIExtractor {
return theURI; return theURI;
} }
}; };
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders); EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders); EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders);
EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders); EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);