HTTPCLIENT-1116: ResponseCachingPolicy uses integers for sizes

Contributed by Greg Bowyer <gbowyer at fastmail.co.uk >


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1172302 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-09-18 16:32:53 +00:00
parent e5ea32b76d
commit 72a41afd5e
11 changed files with 50 additions and 17 deletions

View File

@ -1,5 +1,8 @@
Changes since 4.1.2 Changes since 4.1.2
* [HTTPCLIENT-1116] ResponseCachingPolicy uses integers for sizes
Contributed by Greg Bowyer <gbowyer at fastmail.co.uk >
* [HTTPCLIENT-1123] Support for pluggable DNS resolvers. * [HTTPCLIENT-1123] Support for pluggable DNS resolvers.
Contributed by Alin Vasile <alinachegalati at yahoo dot com> Contributed by Alin Vasile <alinachegalati at yahoo dot com>

View File

@ -52,7 +52,7 @@ class BasicHttpCache implements HttpCache {
private final CacheKeyGenerator uriExtractor; private final CacheKeyGenerator uriExtractor;
private final ResourceFactory resourceFactory; private final ResourceFactory resourceFactory;
private final int maxObjectSizeBytes; private final long maxObjectSizeBytes;
private final CacheEntryUpdater cacheEntryUpdater; private final CacheEntryUpdater cacheEntryUpdater;
private final CachedHttpResponseGenerator responseGenerator; private final CachedHttpResponseGenerator responseGenerator;
private final CacheInvalidator cacheInvalidator; private final CacheInvalidator cacheInvalidator;
@ -64,7 +64,7 @@ class BasicHttpCache implements HttpCache {
this.resourceFactory = resourceFactory; this.resourceFactory = resourceFactory;
this.uriExtractor = new CacheKeyGenerator(); this.uriExtractor = new CacheKeyGenerator();
this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory); this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory);
this.maxObjectSizeBytes = config.getMaxObjectSizeBytes(); this.maxObjectSizeBytes = config.getMaxObjectSize();
this.responseGenerator = new CachedHttpResponseGenerator(); this.responseGenerator = new CachedHttpResponseGenerator();
this.storage = storage; this.storage = storage;
this.cacheInvalidator = new CacheInvalidator(this.uriExtractor, this.storage); this.cacheInvalidator = new CacheInvalidator(this.uriExtractor, this.storage);

View File

@ -131,7 +131,7 @@ public class CacheConfig {
*/ */
public static final int DEFAULT_REVALIDATION_QUEUE_SIZE = 100; public static final int DEFAULT_REVALIDATION_QUEUE_SIZE = 100;
private int maxObjectSizeBytes = DEFAULT_MAX_OBJECT_SIZE_BYTES; private long maxObjectSize = DEFAULT_MAX_OBJECT_SIZE_BYTES;
private int maxCacheEntries = DEFAULT_MAX_CACHE_ENTRIES; private int maxCacheEntries = DEFAULT_MAX_CACHE_ENTRIES;
private int maxUpdateRetries = DEFAULT_MAX_UPDATE_RETRIES; private int maxUpdateRetries = DEFAULT_MAX_UPDATE_RETRIES;
private boolean heuristicCachingEnabled = false; private boolean heuristicCachingEnabled = false;
@ -146,17 +146,47 @@ public class CacheConfig {
/** /**
* Returns the current maximum response body size that will be cached. * Returns the current maximum response body size that will be cached.
* @return size in bytes * @return size in bytes
*
* @deprecated use {@link #getMaxObjectSize()}
*/ */
@Deprecated
public int getMaxObjectSizeBytes() { public int getMaxObjectSizeBytes() {
return maxObjectSizeBytes; return maxObjectSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) maxObjectSize;
} }
/** /**
* Specifies the maximum response body size that will be eligible for caching. * Specifies the maximum response body size that will be eligible for caching.
* @param maxObjectSizeBytes size in bytes * @param maxObjectSizeBytes size in bytes
*
* @deprecated use {@link #setMaxObjectSize(long)}
*/ */
@Deprecated
public void setMaxObjectSizeBytes(int maxObjectSizeBytes) { public void setMaxObjectSizeBytes(int maxObjectSizeBytes) {
this.maxObjectSizeBytes = maxObjectSizeBytes; if (maxObjectSizeBytes > Integer.MAX_VALUE) {
this.maxObjectSize = Integer.MAX_VALUE;
} else {
this.maxObjectSize = maxObjectSizeBytes;
}
}
/**
* Returns the current maximum response body size that will be cached.
* @return size in bytes
*
* @since 4.2
*/
public long getMaxObjectSize() {
return maxObjectSize;
}
/**
* Specifies the maximum response body size that will be eligible for caching.
* @param maxObjectSize size in bytes
*
* @since 4.2
*/
public void setMaxObjectSize(long maxObjectSize) {
this.maxObjectSize = maxObjectSize;
} }
/** /**

View File

@ -130,7 +130,7 @@ public class CachingHttpClient implements HttpClient {
private final ConditionalRequestBuilder conditionalRequestBuilder; private final ConditionalRequestBuilder conditionalRequestBuilder;
private final int maxObjectSizeBytes; private final long maxObjectSizeBytes;
private final boolean sharedCache; private final boolean sharedCache;
private final ResponseProtocolCompliance responseCompliance; private final ResponseProtocolCompliance responseCompliance;
@ -154,7 +154,7 @@ public class CachingHttpClient implements HttpClient {
if (config == null) { if (config == null) {
throw new IllegalArgumentException("CacheConfig may not be null"); throw new IllegalArgumentException("CacheConfig may not be null");
} }
this.maxObjectSizeBytes = config.getMaxObjectSizeBytes(); this.maxObjectSizeBytes = config.getMaxObjectSize();
this.sharedCache = config.isSharedCache(); this.sharedCache = config.isSharedCache();
this.backend = client; this.backend = client;
this.responseCache = cache; this.responseCache = cache;
@ -268,7 +268,7 @@ public class CachingHttpClient implements HttpClient {
ResponseProtocolCompliance responseCompliance, ResponseProtocolCompliance responseCompliance,
RequestProtocolCompliance requestCompliance) { RequestProtocolCompliance requestCompliance) {
CacheConfig config = new CacheConfig(); CacheConfig config = new CacheConfig();
this.maxObjectSizeBytes = config.getMaxObjectSizeBytes(); this.maxObjectSizeBytes = config.getMaxObjectSize();
this.sharedCache = config.isSharedCache(); this.sharedCache = config.isSharedCache();
this.backend = backend; this.backend = backend;
this.validityPolicy = validityPolicy; this.validityPolicy = validityPolicy;

View File

@ -51,7 +51,7 @@ import org.apache.http.protocol.HTTP;
@Immutable @Immutable
class ResponseCachingPolicy { class ResponseCachingPolicy {
private final int maxObjectSizeBytes; private final long maxObjectSizeBytes;
private final boolean sharedCache; private final boolean sharedCache;
private final Log log = LogFactory.getLog(getClass()); private final Log log = LogFactory.getLog(getClass());
@ -63,7 +63,7 @@ class ResponseCachingPolicy {
* @param sharedCache whether to behave as a shared cache (true) or a * @param sharedCache whether to behave as a shared cache (true) or a
* non-shared/private cache (false) * non-shared/private cache (false)
*/ */
public ResponseCachingPolicy(int maxObjectSizeBytes, boolean sharedCache) { public ResponseCachingPolicy(long maxObjectSizeBytes, boolean sharedCache) {
this.maxObjectSizeBytes = maxObjectSizeBytes; this.maxObjectSizeBytes = maxObjectSizeBytes;
this.sharedCache = sharedCache; this.sharedCache = sharedCache;
} }

View File

@ -71,7 +71,7 @@ public abstract class AbstractProtocolTest {
params = new CacheConfig(); params = new CacheConfig();
params.setMaxCacheEntries(MAX_ENTRIES); params.setMaxCacheEntries(MAX_ENTRIES);
params.setMaxObjectSizeBytes(MAX_BYTES); params.setMaxObjectSize(MAX_BYTES);
cache = new BasicHttpCache(params); cache = new BasicHttpCache(params);
mockBackend = EasyMock.createMock(HttpClient.class); mockBackend = EasyMock.createMock(HttpClient.class);
mockCache = EasyMock.createMock(HttpCache.class); mockCache = EasyMock.createMock(HttpCache.class);

View File

@ -75,7 +75,7 @@ public class DoNotTestProtocolRequirements {
originResponse = make200Response(); originResponse = make200Response();
CacheConfig params = new CacheConfig(); CacheConfig params = new CacheConfig();
params.setMaxObjectSizeBytes(MAX_BYTES); params.setMaxObjectSize(MAX_BYTES);
params.setMaxCacheEntries(MAX_ENTRIES); params.setMaxCacheEntries(MAX_ENTRIES);
HttpCache cache = new BasicHttpCache(params); HttpCache cache = new BasicHttpCache(params);

View File

@ -234,7 +234,7 @@ public class TestBasicHttpCache {
Date responseReceived = new Date(now.getTime() - 1 * 1000L); Date responseReceived = new Date(now.getTime() - 1 * 1000L);
HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES + 1)); originResponse.setEntity(HttpTestUtils.makeBody((int) CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES + 1));
originResponse.setHeader("Cache-Control","public, max-age=3600"); originResponse.setHeader("Cache-Control","public, max-age=3600");
originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated)); originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
originResponse.setHeader("ETag", "\"etag\""); originResponse.setHeader("ETag", "\"etag\"");
@ -256,7 +256,7 @@ public class TestBasicHttpCache {
Date responseReceived = new Date(now.getTime() - 1 * 1000L); Date responseReceived = new Date(now.getTime() - 1 * 1000L);
HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES - 1)); originResponse.setEntity(HttpTestUtils.makeBody((int) CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES - 1));
originResponse.setHeader("Cache-Control","public, max-age=3600"); originResponse.setHeader("Cache-Control","public, max-age=3600");
originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated)); originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
originResponse.setHeader("ETag", "\"etag\""); originResponse.setHeader("ETag", "\"etag\"");

View File

@ -94,7 +94,7 @@ public class TestProtocolDeviations {
originResponse = make200Response(); originResponse = make200Response();
CacheConfig params = new CacheConfig(); CacheConfig params = new CacheConfig();
params.setMaxObjectSizeBytes(MAX_BYTES); params.setMaxObjectSize(MAX_BYTES);
params.setMaxCacheEntries(MAX_ENTRIES); params.setMaxCacheEntries(MAX_ENTRIES);
HttpCache cache = new BasicHttpCache(params); HttpCache cache = new BasicHttpCache(params);

View File

@ -118,7 +118,7 @@ public class TestSizeLimitedResponseReader {
@Test @Test
public void testTooLargeEntityHasOriginalContentTypes() throws Exception { public void testTooLargeEntityHasOriginalContentTypes() throws Exception {
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
StringEntity entity = new StringEntity("large entity content", "text/plain", "utf-8"); StringEntity entity = new StringEntity("large entity content");
response.setEntity(entity); response.setEntity(entity);
impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response); impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response);

View File

@ -64,7 +64,7 @@ public class TestEhcacheProtocolRequirements extends TestProtocolRequirements{
public void setUp() { public void setUp() {
super.setUp(); super.setUp();
params = new CacheConfig(); params = new CacheConfig();
params.setMaxObjectSizeBytes(MAX_BYTES); params.setMaxObjectSize(MAX_BYTES);
if (CACHE_MANAGER.cacheExists(TEST_EHCACHE_NAME)){ if (CACHE_MANAGER.cacheExists(TEST_EHCACHE_NAME)){
CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME); CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);