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,
HttpRequest request) throws IOException {
cacheInvalidator.flushInvalidatedCacheEntries(host, request);
}
public Set<HttpCacheEntry> getVariantCacheEntries(HttpHost host, HttpRequest request)
throws IOException {
Set<HttpCacheEntry> variants = new HashSet<HttpCacheEntry>();
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;
}

View File

@ -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 <code>String</code> variant key
*/
public String getVariantKey(HttpRequest req, HttpCacheEntry entry) {
List<String> variantHeaderNames = new ArrayList<String>();
for (Header varyHdr : varyHdrs) {
for (Header varyHdr : entry.getHeaders(HeaderConstants.VARY)) {
for (HeaderElement elt : varyHdr.getElements()) {
variantHeaderNames.add(elt.getName());
}

View File

@ -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);