Removed deprecated HttpCacheEntry#getVariantURIs and associated

constructors and fields. Also removed code in surrounding classes
that was providing backwards compatibility for the deprecated
Set-based variant data structure. Made a note in the ChangeLog
about the change to the public API between 4.1-beta and 4.1.


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1058153 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Moore 2011-01-12 14:21:32 +00:00
parent 3c0bc58a30
commit bac69b368f
7 changed files with 40 additions and 311 deletions

View File

@ -32,6 +32,9 @@ maintained and supported by Apache HttpComponents project.
Changelog
-------------------
* The public API for the caching module had a minor change between 4.1-beta and 4.1-GA to the
HttpCacheEntry class. This will likely not affect you unless you are implementing new
storage backends and/or implementing custom serializers for cache entries.
* Changed Browser-Compatibility and Best-Match cookie policies to emulate the behaviour of FireFox
more closely when parsing Netscape style cookies. Comma will no longer be treated as a header
@ -43,7 +46,7 @@ Changelog
to US-ASCII)
Contributed by Sebastian Bazley <sebb at apache.org>
* [HTTPCLIENT-975] Support stale-if-error extension directive (RFC5861).
* [HTTPCLIENT-975] Support stale-if-error and stale-while-revalidate extension directive (RFC5861).
Contributed by Mohammed Azeem Uddin <mohammedazeem_uddin at comcast.com>,
Michajlo Matijkiw <michajlo_matijkiw at comcast.com>, and
Matthew Hawthorne <matthew_hawthorne at comcast.com>.

View File

@ -30,10 +30,7 @@ import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
@ -60,16 +57,32 @@ public class HttpCacheEntry implements Serializable {
private final StatusLine statusLine;
private final HeaderGroup responseHeaders;
private final Resource resource;
private final Set<String> variantURIs;
private final Map<String,String> variantMap;
private HttpCacheEntry(
/**
* Create a new {@link HttpCacheEntry} with variants.
* @param requestDate
* Date/time when the request was made (Used for age
* calculations)
* @param responseDate
* Date/time that the response came back (Used for age
* calculations)
* @param statusLine
* HTTP status line from origin response
* @param responseHeaders
* Header[] from original HTTP Response
* @param resource representing origin response body
* @param variantMap describing cache entries that are variants
* of this parent entry; this maps a "variant key" (derived
* from the varying request headers) to a "cache key" (where
* in the cache storage the particular variant is located)
*/
public HttpCacheEntry(
final Date requestDate,
final Date responseDate,
final StatusLine statusLine,
final Header[] responseHeaders,
final Resource resource,
final Set<String> variants,
final Map<String,String> variantMap) {
super();
if (requestDate == null) {
@ -93,43 +106,11 @@ public class HttpCacheEntry implements Serializable {
this.responseHeaders = new HeaderGroup();
this.responseHeaders.setHeaders(responseHeaders);
this.resource = resource;
this.variantURIs = variants != null
? new HashSet<String>(variants) : null;
this.variantMap = variantMap != null
? new HashMap<String,String>(variantMap)
: null;
}
/**
* Create a new {@link HttpCacheEntry} with a set of variant
* URIs.
*
* @param requestDate
* Date/time when the request was made (Used for age
* calculations)
* @param responseDate
* Date/time that the response came back (Used for age
* calculations)
* @param statusLine
* HTTP status line from origin response
* @param responseHeaders
* Header[] from original HTTP Response
* @param resource representing origin response body
* @param variants set of cache keys that are variants of this
* "parent" entry
*/
@Deprecated
public HttpCacheEntry(
final Date requestDate,
final Date responseDate,
final StatusLine statusLine,
final Header[] responseHeaders,
final Resource resource,
final Set<String> variants) {
this(requestDate, responseDate, statusLine, responseHeaders,
resource, variants, null);
}
/**
* Create a new {@link HttpCacheEntry}.
*
@ -147,35 +128,10 @@ public class HttpCacheEntry implements Serializable {
*/
public HttpCacheEntry(Date requestDate, Date responseDate, StatusLine statusLine,
Header[] responseHeaders, Resource resource) {
this(requestDate, responseDate, statusLine, responseHeaders, resource, null,
this(requestDate, responseDate, statusLine, responseHeaders, resource,
new HashMap<String,String>());
}
/**
* Create a new {@link HttpCacheEntry} with variants.
* @param requestDate
* Date/time when the request was made (Used for age
* calculations)
* @param responseDate
* Date/time that the response came back (Used for age
* calculations)
* @param statusLine
* HTTP status line from origin response
* @param responseHeaders
* Header[] from original HTTP Response
* @param resource representing origin response body
* @param variantMap describing cache entries that are variants
* of this parent entry; this maps a "variant key" (derived
* from the varying request headers) to a "cache key" (where
* in the cache storage the particular variant is located)
*/
public HttpCacheEntry(Date requestDate, Date responseDate,
StatusLine statusLine, Header[] responseHeaders,
Resource resource, Map<String, String> variantMap) {
this(requestDate, responseDate, statusLine, responseHeaders,
resource, null, variantMap);
}
/**
* Returns the {@link StatusLine} from the origin {@link HttpResponse}.
*/
@ -262,14 +218,6 @@ public class HttpCacheEntry implements Serializable {
return getFirstHeader(HeaderConstants.VARY) != null;
}
@Deprecated
public Set<String> getVariantURIs() {
if (variantMap != null) {
return Collections.unmodifiableSet(new HashSet<String>(variantMap.values()));
}
return Collections.unmodifiableSet(variantURIs);
}
/**
* Returns an index about where in the cache different variants for
* a given resource are stored. This maps "variant keys" to "cache keys",
@ -280,10 +228,6 @@ public class HttpCacheEntry implements Serializable {
* the "parent" entry to hold this index of the other variants.
*/
public Map<String, String> getVariantMap() {
if (variantMap == null) {
throw new UnsupportedOperationException("variant maps not" +
"supported if constructed with deprecated variant set");
}
return Collections.unmodifiableMap(variantMap);
}

View File

@ -29,10 +29,7 @@ package org.apache.http.impl.client.cache;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
@ -53,14 +50,6 @@ import org.apache.http.message.BasicHttpResponse;
class BasicHttpCache implements HttpCache {
static final String DEPRECATED_VARIANT_SET_MSG =
"It looks like you have a custom HttpCacheEntrySerializer " +
"that was written against the 4.1-beta API and is using the " +
"old (and deprecated) variant tracking mechanism of a Set " +
"instead of a Map. Everything is still working, just not as " +
"efficiently as possible. Please update your serializer" +
"appropriately to use the non-deprecated API for HttpCacheEntry.";
private final CacheKeyGenerator uriExtractor;
private final ResourceFactory resourceFactory;
private final int maxObjectSizeBytes;
@ -194,7 +183,6 @@ class BasicHttpCache implements HttpCache {
return error;
}
@SuppressWarnings("deprecation")
HttpCacheEntry doGetUpdatedParentEntry(
final String requestId,
final HttpCacheEntry existing,
@ -207,29 +195,15 @@ class BasicHttpCache implements HttpCache {
}
Resource resource = resourceFactory.copy(requestId, src.getResource());
try {
Map<String,String> variantMap = new HashMap<String,String>(src.getVariantMap());
variantMap.put(variantKey, variantCacheKey);
return new HttpCacheEntry(
src.getRequestDate(),
src.getResponseDate(),
src.getStatusLine(),
src.getAllHeaders(),
resource,
variantMap);
} catch (UnsupportedOperationException uoe) {
// TODO: to be removed once HttpCacheEntry#getVariantURIs is removed
log.warn(DEPRECATED_VARIANT_SET_MSG);
Set<String> variantURIs = new HashSet<String>(src.getVariantURIs());
variantURIs.add(variantCacheKey);
return new HttpCacheEntry(
src.getRequestDate(),
src.getResponseDate(),
src.getStatusLine(),
src.getAllHeaders(),
resource,
variantURIs);
}
Map<String,String> variantMap = new HashMap<String,String>(src.getVariantMap());
variantMap.put(variantKey, variantCacheKey);
return new HttpCacheEntry(
src.getRequestDate(),
src.getResponseDate(),
src.getStatusLine(),
src.getAllHeaders(),
resource,
variantMap);
}
public HttpCacheEntry updateCacheEntry(HttpHost target, HttpRequest request,
@ -293,14 +267,7 @@ class BasicHttpCache implements HttpCache {
HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request));
if (root == null) return null;
if (!root.hasVariants()) return root;
String variantCacheKey = null;
try {
variantCacheKey = root.getVariantMap().get(uriExtractor.getVariantKey(request, root));
} catch (UnsupportedOperationException uoe) {
// TODO: to be removed once HttpCacheEntry#getVariantURIs is removed
log.warn(DEPRECATED_VARIANT_SET_MSG);
variantCacheKey = uriExtractor.getVariantURI(host, request, root);
}
String variantCacheKey = root.getVariantMap().get(uriExtractor.getVariantKey(request, root));
if (variantCacheKey == null) return null;
return storage.getEntry(variantCacheKey);
}
@ -310,25 +277,15 @@ class BasicHttpCache implements HttpCache {
cacheInvalidator.flushInvalidatedCacheEntries(host, request);
}
@SuppressWarnings("deprecation")
public Map<String, Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest request)
throws IOException {
Map<String,Variant> variants = new HashMap<String,Variant>();
HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request));
if (root == null || !root.hasVariants()) return variants;
try {
for(Map.Entry<String, String> variant : root.getVariantMap().entrySet()) {
String variantKey = variant.getKey();
String variantCacheKey = variant.getValue();
addVariantWithEtag(variantKey, variantCacheKey, variants);
}
} catch (UnsupportedOperationException uoe) {
// TODO: to be removed once HttpCacheEntry#getVariantURIs is removed
log.warn(DEPRECATED_VARIANT_SET_MSG);
for(String variantCacheKey : root.getVariantURIs()) {
String variantKey = uriExtractor.getVariantKey(request, root);
addVariantWithEtag(variantKey, variantCacheKey, variants);
}
for(Map.Entry<String, String> variant : root.getVariantMap().entrySet()) {
String variantKey = variant.getKey();
String variantCacheKey = variant.getValue();
addVariantWithEtag(variantKey, variantCacheKey, variants);
}
return variants;
}

View File

@ -29,7 +29,6 @@ package org.apache.http.impl.client.cache;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Date;
import org.apache.commons.logging.Log;
@ -80,7 +79,6 @@ class CacheInvalidator {
* @param host The backend host we are talking to
* @param req The HttpRequest to that host
*/
@SuppressWarnings("deprecation")
public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req) {
if (requestShouldNotBeCached(req)) {
log.debug("Request should not be cached");
@ -92,15 +90,7 @@ class CacheInvalidator {
log.debug("parent entry: " + parent);
if (parent != null) {
Collection<String> variantURIs = null;
try {
variantURIs = parent.getVariantMap().values();
} catch (UnsupportedOperationException uoe) {
// TODO: to be removed once HttpCacheEntry#getVariantURIs is removed
log.warn(BasicHttpCache.DEPRECATED_VARIANT_SET_MSG);
variantURIs = parent.getVariantURIs();
}
for (String variantURI : variantURIs) {
for (String variantURI : parent.getVariantMap().values()) {
flushEntry(variantURI);
}
flushEntry(theUri);

View File

@ -31,10 +31,7 @@ import static org.junit.Assert.*;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
@ -256,58 +253,7 @@ public class TestHttpCacheEntry {
assertEquals(headers[i], result[i]);
}
}
@SuppressWarnings("deprecation")
@Test
public void canRetrieveOriginalVariantSet() {
Set<String> variants = new HashSet<String>();
variants.add("variant1");
variants.add("variant2");
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
new Header[]{}, mockResource, variants);
Set<String> result = entry.getVariantURIs();
assertEquals(variants.size(), result.size());
for(String variant : variants) {
assertTrue(result.contains(variant));
}
}
@SuppressWarnings("deprecation")
@Test
public void throwsExceptionWhenRetrievingVariantMapIfConstructedWithVariantSet() {
Set<String> variants = new HashSet<String>();
variants.add("variant1");
variants.add("variant2");
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
new Header[]{}, mockResource, variants);
try {
entry.getVariantMap();
fail("should have thrown exception");
} catch (UnsupportedOperationException expected) {
}
}
@SuppressWarnings("deprecation")
@Test
public void variantSetIsNotModifiable() {
Set<String> variants = new HashSet<String>();
variants.add("variant1");
variants.add("variant2");
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
new Header[]{}, mockResource, variants);
Set<String> result = entry.getVariantURIs();
try {
result.remove("variant1");
fail("Should have thrown exception");
} catch (UnsupportedOperationException expected) {
}
try {
result.add("variant3");
fail("Should have thrown exception");
} catch (UnsupportedOperationException expected) {
}
}
@Test
public void canConstructWithoutVariants() {
new HttpCacheEntry(new Date(), new Date(), statusLine,
@ -334,21 +280,6 @@ public class TestHttpCacheEntry {
assertEquals("B", result.get("A"));
assertEquals("D", result.get("C"));
}
@SuppressWarnings("deprecation")
@Test
public void returnsVariantMapValuesForVariantSet() {
Map<String,String> variantMap = new HashMap<String,String>();
variantMap.put("A","B");
variantMap.put("C","D");
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
new Header[]{}, mockResource,
variantMap);
Set<String> result = entry.getVariantURIs();
assertEquals(2, result.size());
assertTrue(result.contains("B"));
assertTrue(result.contains("D"));
}
@Test
public void retrievedVariantMapIsNotModifiable() {

View File

@ -35,10 +35,7 @@ import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.ProtocolVersion;
@ -63,11 +60,6 @@ public class TestHttpCacheEntrySerializers {
impl = new DefaultHttpCacheEntrySerializer();
}
@Test
public void canSerializeOldEntriesWithVariantSets() throws Exception {
readWriteVerify(makeCacheEntryWithOldVariantSet());
}
@Test
public void canSerializeEntriesWithVariantMaps() throws Exception {
readWriteVerify(makeCacheEntryWithVariantMap());
@ -86,26 +78,6 @@ public class TestHttpCacheEntrySerializers {
assertTrue(areEqual(readEntry, writeEntry));
}
@SuppressWarnings("deprecation")
private HttpCacheEntry makeCacheEntryWithOldVariantSet() throws UnsupportedEncodingException {
Header[] headers = new Header[5];
for (int i = 0; i < headers.length; i++) {
headers[i] = new BasicHeader("header" + i, "value" + i);
}
String body = "Lorem ipsum dolor sit amet";
ProtocolVersion pvObj = new ProtocolVersion("HTTP", 1, 1);
StatusLine slObj = new BasicStatusLine(pvObj, 200, "ok");
Set<String> variants = new HashSet<String>();
variants.add("test variant 1");
variants.add("test variant 2");
HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(),
slObj, headers, new HeapResource(Base64.decodeBase64(body
.getBytes(UTF8.name()))), variants);
return cacheEntry;
}
private HttpCacheEntry makeCacheEntryWithVariantMap() throws UnsupportedEncodingException {
Header[] headers = new Header[5];
for (int i = 0; i < headers.length; i++) {

View File

@ -1,68 +0,0 @@
package org.apache.http.impl.client.cache.ehcache;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.StatusLine;
import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.client.cache.HttpCacheEntrySerializer;
import org.apache.http.client.cache.Resource;
import org.apache.http.impl.client.cache.CachingHttpClient;
import org.apache.http.impl.client.cache.HeapResourceFactory;
import org.junit.Before;
public class TestProtocolRequirementsWithDeprecatedSerializer
extends TestEhcacheProtocolRequirements {
private static class OldSerializer implements HttpCacheEntrySerializer {
@SuppressWarnings("deprecation")
public void writeTo(HttpCacheEntry entry, OutputStream os)
throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(entry.getRequestDate());
oos.writeObject(entry.getResponseDate());
oos.writeObject(entry.getStatusLine());
oos.writeObject(entry.getAllHeaders());
oos.writeObject(entry.getResource());
oos.writeObject(entry.getVariantURIs());
}
@SuppressWarnings({ "deprecation", "unchecked" })
public HttpCacheEntry readFrom(InputStream is) throws IOException {
ObjectInputStream ois = new ObjectInputStream(is);
try {
Date requestDate = (Date)ois.readObject();
Date responseDate = (Date)ois.readObject();
StatusLine statusLine = (StatusLine)ois.readObject();
Header[] responseHeaders = (Header[])ois.readObject();
Resource resource = (Resource)ois.readObject();
Set<String> variants = (Set<String>)ois.readObject();
return new HttpCacheEntry(requestDate, responseDate, statusLine,
responseHeaders, resource, variants);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
} finally {
ois.close();
}
}
}
@Override
@Before
public void setUp() {
super.setUp();
HttpCacheStorage storage = new EhcacheHttpCacheStorage(CACHE_MANAGER.getCache(TEST_EHCACHE_NAME), params,
new OldSerializer());
impl = new CachingHttpClient(mockBackend, new HeapResourceFactory(), storage, params);
}
}