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:
parent
2c04f2689b
commit
4e7d9fa5c8
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue