Use ResourceFactory to handle system resource allocation
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@984346 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cb4f012fa7
commit
6f2036bde2
|
@ -27,29 +27,16 @@
|
|||
package org.apache.http.client.cache;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.StatusLine;
|
||||
|
||||
/**
|
||||
* Generates {@link HttpCacheEntry} instances.
|
||||
* Generates {@link Resource} instances.
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
public interface HttpCacheEntryFactory {
|
||||
public interface ResourceFactory {
|
||||
|
||||
HttpCacheEntry generate(
|
||||
String requestId,
|
||||
Date requestDate,
|
||||
Date responseDate,
|
||||
StatusLine statusLine,
|
||||
Header[] headers,
|
||||
byte[] body) throws IOException;
|
||||
Resource generate(String requestId, byte[] body) throws IOException;
|
||||
|
||||
HttpCacheEntry copyVariant(
|
||||
String requestId,
|
||||
HttpCacheEntry entry,
|
||||
String variantURI) throws IOException;
|
||||
Resource copy(String requestId, Resource resource) throws IOException;
|
||||
|
||||
}
|
|
@ -26,39 +26,25 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.cache.HttpCacheEntryFactory;
|
||||
import org.apache.http.client.cache.Resource;
|
||||
import org.apache.http.client.cache.ResourceFactory;
|
||||
|
||||
/**
|
||||
* Generates {@link HttpCacheEntry} instances stored entirely in memory.
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
@Immutable
|
||||
public class MemCacheEntryFactory implements HttpCacheEntryFactory {
|
||||
class CacheEntryFactory {
|
||||
|
||||
public HttpCacheEntry generateEntry(
|
||||
final Date requestDate,
|
||||
final Date responseDate,
|
||||
final HttpResponse response,
|
||||
final byte[] body) {
|
||||
return new HttpCacheEntry(requestDate,
|
||||
responseDate,
|
||||
response.getStatusLine(),
|
||||
response.getAllHeaders(),
|
||||
new HeapResource(body),
|
||||
null);
|
||||
private final ResourceFactory resourceFactory;
|
||||
|
||||
public CacheEntryFactory(final ResourceFactory resourceFactory) {
|
||||
super();
|
||||
this.resourceFactory = resourceFactory;
|
||||
}
|
||||
|
||||
public HttpCacheEntry generate(
|
||||
|
@ -72,7 +58,7 @@ public class MemCacheEntryFactory implements HttpCacheEntryFactory {
|
|||
responseDate,
|
||||
statusLine,
|
||||
headers,
|
||||
new HeapResource(body),
|
||||
resourceFactory.generate(requestId, body),
|
||||
null);
|
||||
}
|
||||
|
||||
|
@ -80,26 +66,14 @@ public class MemCacheEntryFactory implements HttpCacheEntryFactory {
|
|||
final String requestId,
|
||||
final HttpCacheEntry entry,
|
||||
final String variantURI) throws IOException {
|
||||
byte[] body;
|
||||
|
||||
Resource orig = entry.getResource();
|
||||
if (orig instanceof HeapResource) {
|
||||
body = ((HeapResource) orig).getByteArray();
|
||||
} else {
|
||||
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
|
||||
IOUtils.copyAndClose(orig.getInputStream(), outstream);
|
||||
body = outstream.toByteArray();
|
||||
}
|
||||
|
||||
Set<String> variants = new HashSet<String>(entry.getVariantURIs());
|
||||
variants.add(variantURI);
|
||||
|
||||
return new HttpCacheEntry(
|
||||
entry.getRequestDate(),
|
||||
entry.getResponseDate(),
|
||||
entry.getStatusLine(),
|
||||
entry.getAllHeaders(),
|
||||
new HeapResource(body),
|
||||
resourceFactory.copy(requestId, entry.getResource()),
|
||||
variants);
|
||||
}
|
||||
|
|
@ -40,7 +40,6 @@ import org.apache.http.HttpResponse;
|
|||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.cache.HeaderConstants;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.cache.HttpCacheEntryFactory;
|
||||
import org.apache.http.impl.cookie.DateParseException;
|
||||
import org.apache.http.impl.cookie.DateUtils;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
@ -55,13 +54,13 @@ import org.apache.http.protocol.HTTP;
|
|||
@Immutable
|
||||
class CacheEntryUpdater {
|
||||
|
||||
private final HttpCacheEntryFactory cacheEntryFactory;
|
||||
private final CacheEntryFactory cacheEntryFactory;
|
||||
|
||||
CacheEntryUpdater() {
|
||||
this(new MemCacheEntryFactory());
|
||||
this(new CacheEntryFactory(new HeapResourceFactory()));
|
||||
}
|
||||
|
||||
CacheEntryUpdater(final HttpCacheEntryFactory cacheEntryFactory) {
|
||||
CacheEntryUpdater(final CacheEntryFactory cacheEntryFactory) {
|
||||
super();
|
||||
this.cacheEntryFactory = cacheEntryFactory;
|
||||
}
|
||||
|
|
|
@ -50,8 +50,8 @@ import org.apache.http.client.ResponseHandler;
|
|||
import org.apache.http.client.cache.HeaderConstants;
|
||||
import org.apache.http.client.cache.HttpCache;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.cache.HttpCacheEntryFactory;
|
||||
import org.apache.http.client.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.http.client.cache.ResourceFactory;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
|
@ -76,7 +76,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
|
||||
private final HttpClient backend;
|
||||
private final HttpCache responseCache;
|
||||
private final HttpCacheEntryFactory cacheEntryFactory;
|
||||
private final CacheEntryFactory cacheEntryFactory;
|
||||
private final CacheValidityPolicy validityPolicy;
|
||||
private final ResponseCachingPolicy responseCachingPolicy;
|
||||
private final URIExtractor uriExtractor;
|
||||
|
@ -100,7 +100,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
public CachingHttpClient(
|
||||
HttpClient client,
|
||||
HttpCache cache,
|
||||
HttpCacheEntryFactory cacheEntryFactory,
|
||||
ResourceFactory resourceFactory,
|
||||
CacheConfig config) {
|
||||
super();
|
||||
if (client == null) {
|
||||
|
@ -109,8 +109,8 @@ public class CachingHttpClient implements HttpClient {
|
|||
if (cache == null) {
|
||||
throw new IllegalArgumentException("HttpCache may not be null");
|
||||
}
|
||||
if (cacheEntryFactory == null) {
|
||||
throw new IllegalArgumentException("HttpCacheEntryFactory may not be null");
|
||||
if (resourceFactory == null) {
|
||||
throw new IllegalArgumentException("ResourceFactory may not be null");
|
||||
}
|
||||
if (config == null) {
|
||||
throw new IllegalArgumentException("CacheConfig may not be null");
|
||||
|
@ -119,7 +119,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
this.sharedCache = config.isSharedCache();
|
||||
this.backend = client;
|
||||
this.responseCache = cache;
|
||||
this.cacheEntryFactory = cacheEntryFactory;
|
||||
this.cacheEntryFactory = new CacheEntryFactory(resourceFactory);
|
||||
|
||||
this.validityPolicy = new CacheValidityPolicy();
|
||||
this.responseCachingPolicy = new ResponseCachingPolicy(maxObjectSizeBytes, sharedCache);
|
||||
|
@ -138,62 +138,62 @@ public class CachingHttpClient implements HttpClient {
|
|||
public CachingHttpClient() {
|
||||
this(new DefaultHttpClient(),
|
||||
new BasicHttpCache(MAX_CACHE_ENTRIES),
|
||||
new MemCacheEntryFactory(),
|
||||
new HeapResourceFactory(),
|
||||
new CacheConfig());
|
||||
}
|
||||
|
||||
public CachingHttpClient(CacheConfig config) {
|
||||
this(new DefaultHttpClient(),
|
||||
new BasicHttpCache(MAX_CACHE_ENTRIES),
|
||||
new MemCacheEntryFactory(),
|
||||
new HeapResourceFactory(),
|
||||
config);
|
||||
}
|
||||
|
||||
public CachingHttpClient(HttpClient client) {
|
||||
this(client,
|
||||
new BasicHttpCache(MAX_CACHE_ENTRIES),
|
||||
new MemCacheEntryFactory(),
|
||||
new HeapResourceFactory(),
|
||||
new CacheConfig());
|
||||
}
|
||||
|
||||
public CachingHttpClient(HttpClient client, CacheConfig config) {
|
||||
this(client,
|
||||
new BasicHttpCache(MAX_CACHE_ENTRIES),
|
||||
new MemCacheEntryFactory(),
|
||||
new HeapResourceFactory(),
|
||||
config);
|
||||
}
|
||||
|
||||
public CachingHttpClient(
|
||||
HttpCache cache,
|
||||
HttpCacheEntryFactory cacheEntryFactory) {
|
||||
ResourceFactory resourceFactory) {
|
||||
this(new DefaultHttpClient(),
|
||||
cache,
|
||||
cacheEntryFactory,
|
||||
resourceFactory,
|
||||
new CacheConfig());
|
||||
}
|
||||
|
||||
public CachingHttpClient(
|
||||
HttpCache cache,
|
||||
HttpCacheEntryFactory cacheEntryFactory,
|
||||
ResourceFactory resourceFactory,
|
||||
CacheConfig config) {
|
||||
this(new DefaultHttpClient(),
|
||||
cache,
|
||||
cacheEntryFactory,
|
||||
resourceFactory,
|
||||
config);
|
||||
}
|
||||
|
||||
public CachingHttpClient(
|
||||
HttpClient client,
|
||||
HttpCache cache,
|
||||
HttpCacheEntryFactory cacheEntryFactory) {
|
||||
ResourceFactory resourceFactory) {
|
||||
this(client,
|
||||
cache,
|
||||
cacheEntryFactory,
|
||||
resourceFactory,
|
||||
new CacheConfig());
|
||||
}
|
||||
|
||||
CachingHttpClient(HttpClient backend, CacheValidityPolicy validityPolicy, ResponseCachingPolicy responseCachingPolicy,
|
||||
HttpCacheEntryFactory cacheEntryFactory, URIExtractor uriExtractor,
|
||||
CacheEntryFactory cacheEntryFactory, URIExtractor uriExtractor,
|
||||
HttpCache responseCache, CachedHttpResponseGenerator responseGenerator,
|
||||
CacheInvalidator cacheInvalidator, CacheableRequestPolicy cacheableRequestPolicy,
|
||||
CachedResponseSuitabilityChecker suitabilityChecker,
|
||||
|
|
|
@ -29,29 +29,23 @@ package org.apache.http.impl.client.cache;
|
|||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.cache.HttpCacheEntryFactory;
|
||||
import org.apache.http.client.cache.Resource;
|
||||
import org.apache.http.client.cache.ResourceFactory;
|
||||
|
||||
/**
|
||||
* Generates {@link HttpCacheEntry} instances whose body is stored in a temporary file.
|
||||
* Generates {@link Resource} instances whose body is stored in a temporary file.
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
@Immutable
|
||||
public class FileCacheEntryFactory implements HttpCacheEntryFactory {
|
||||
public class FileResourceFactory implements ResourceFactory {
|
||||
|
||||
private final File cacheDir;
|
||||
private final BasicIdGenerator idgen;
|
||||
|
||||
public FileCacheEntryFactory(final File cacheDir) {
|
||||
public FileResourceFactory(final File cacheDir) {
|
||||
super();
|
||||
this.cacheDir = cacheDir;
|
||||
this.idgen = new BasicIdGenerator();
|
||||
|
@ -73,13 +67,7 @@ public class FileCacheEntryFactory implements HttpCacheEntryFactory {
|
|||
return new File(this.cacheDir, buffer.toString());
|
||||
}
|
||||
|
||||
public HttpCacheEntry generate(
|
||||
final String requestId,
|
||||
final Date requestDate,
|
||||
final Date responseDate,
|
||||
final StatusLine statusLine,
|
||||
final Header[] headers,
|
||||
byte[] body) throws IOException {
|
||||
public Resource generate(final String requestId, final byte[] body) throws IOException {
|
||||
File file = generateUniqueCacheFile(requestId);
|
||||
FileOutputStream outstream = new FileOutputStream(file);
|
||||
try {
|
||||
|
@ -87,40 +75,20 @@ public class FileCacheEntryFactory implements HttpCacheEntryFactory {
|
|||
} finally {
|
||||
outstream.close();
|
||||
}
|
||||
return new HttpCacheEntry(
|
||||
requestDate,
|
||||
responseDate,
|
||||
statusLine,
|
||||
headers,
|
||||
new FileResource(file),
|
||||
null);
|
||||
return new FileResource(file);
|
||||
}
|
||||
|
||||
public HttpCacheEntry copyVariant(
|
||||
final String requestId,
|
||||
final HttpCacheEntry entry,
|
||||
final String variantURI) throws IOException {
|
||||
|
||||
public Resource copy(final String requestId, final Resource resource) throws IOException {
|
||||
File file = generateUniqueCacheFile(requestId);
|
||||
|
||||
Set<String> variants = new HashSet<String>(entry.getVariantURIs());
|
||||
variants.add(variantURI);
|
||||
|
||||
Resource orig = entry.getResource();
|
||||
if (orig instanceof FileResource) {
|
||||
File src = ((FileResource) orig).getFile();
|
||||
if (resource instanceof FileResource) {
|
||||
File src = ((FileResource) resource).getFile();
|
||||
IOUtils.copyFile(src, file);
|
||||
} else {
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
IOUtils.copyAndClose(orig.getInputStream(), out);
|
||||
IOUtils.copyAndClose(resource.getInputStream(), out);
|
||||
}
|
||||
return new HttpCacheEntry(
|
||||
entry.getRequestDate(),
|
||||
entry.getResponseDate(),
|
||||
entry.getStatusLine(),
|
||||
entry.getAllHeaders(),
|
||||
new FileResource(file),
|
||||
variants);
|
||||
return new FileResource(file);
|
||||
}
|
||||
|
||||
}
|
60
httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java
vendored
Normal file
60
httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java
vendored
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.cache.Resource;
|
||||
import org.apache.http.client.cache.ResourceFactory;
|
||||
|
||||
/**
|
||||
* Generates {@link Resource} instances stored entirely in heap.
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
@Immutable
|
||||
public class HeapResourceFactory implements ResourceFactory {
|
||||
|
||||
public Resource generate(final String requestId, final byte[] body) throws IOException {
|
||||
return new HeapResource(body);
|
||||
}
|
||||
|
||||
public Resource copy(final String requestId, final Resource resource) throws IOException {
|
||||
byte[] body;
|
||||
if (resource instanceof HeapResource) {
|
||||
body = ((HeapResource) resource).getByteArray();
|
||||
} else {
|
||||
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
|
||||
IOUtils.copyAndClose(resource.getInputStream(), outstream);
|
||||
body = outstream.toByteArray();
|
||||
}
|
||||
return new HeapResource(body);
|
||||
}
|
||||
|
||||
}
|
|
@ -10,7 +10,6 @@ import org.apache.http.HttpStatus;
|
|||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.cache.HttpCache;
|
||||
import org.apache.http.client.cache.HttpCacheEntryFactory;
|
||||
import org.apache.http.impl.cookie.DateUtils;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
|
@ -26,7 +25,6 @@ public abstract class AbstractProtocolTest {
|
|||
protected int entityLength = 128;
|
||||
protected HttpHost host;
|
||||
protected HttpEntity body;
|
||||
protected HttpEntity mockEntity;
|
||||
protected HttpClient mockBackend;
|
||||
protected HttpCache mockCache;
|
||||
protected HttpRequest request;
|
||||
|
@ -34,7 +32,6 @@ public abstract class AbstractProtocolTest {
|
|||
protected CacheConfig params;
|
||||
protected CachingHttpClient impl;
|
||||
protected HttpCache cache;
|
||||
protected HttpCacheEntryFactory cacheEntryFactory;
|
||||
|
||||
public static HttpRequest eqRequest(HttpRequest in) {
|
||||
EasyMock.reportMatcher(new RequestEquivalent(in));
|
||||
|
@ -52,25 +49,21 @@ public abstract class AbstractProtocolTest {
|
|||
originResponse = make200Response();
|
||||
|
||||
cache = new BasicHttpCache(MAX_ENTRIES);
|
||||
cacheEntryFactory = new MemCacheEntryFactory();
|
||||
mockBackend = EasyMock.createMock(HttpClient.class);
|
||||
mockEntity = EasyMock.createMock(HttpEntity.class);
|
||||
mockCache = EasyMock.createMock(HttpCache.class);
|
||||
params = new CacheConfig();
|
||||
params.setMaxObjectSizeBytes(MAX_BYTES);
|
||||
impl = new CachingHttpClient(mockBackend, cache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params);
|
||||
}
|
||||
|
||||
protected void replayMocks() {
|
||||
EasyMock.replay(mockBackend);
|
||||
EasyMock.replay(mockCache);
|
||||
EasyMock.replay(mockEntity);
|
||||
}
|
||||
|
||||
protected void verifyMocks() {
|
||||
EasyMock.verify(mockBackend);
|
||||
EasyMock.verify(mockCache);
|
||||
EasyMock.verify(mockEntity);
|
||||
}
|
||||
|
||||
protected HttpResponse make200Response() {
|
||||
|
@ -95,9 +88,8 @@ public abstract class AbstractProtocolTest {
|
|||
protected void emptyMockCacheExpectsNoPuts() throws Exception {
|
||||
mockBackend = EasyMock.createMock(HttpClient.class);
|
||||
mockCache = EasyMock.createMock(HttpCache.class);
|
||||
mockEntity = EasyMock.createMock(HttpEntity.class);
|
||||
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
|
||||
|
||||
EasyMock.expect(mockCache.getEntry((String) EasyMock.anyObject())).andReturn(null)
|
||||
.anyTimes();
|
||||
|
@ -108,7 +100,7 @@ public abstract class AbstractProtocolTest {
|
|||
|
||||
protected void behaveAsNonSharedCache() {
|
||||
params.setSharedCache(false);
|
||||
impl = new CachingHttpClient(mockBackend, cache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params);
|
||||
}
|
||||
|
||||
public AbstractProtocolTest() {
|
||||
|
|
|
@ -82,7 +82,7 @@ public class DoNotTestProtocolRequirements {
|
|||
mockCache = EasyMock.createMock(HttpCache.class);
|
||||
CacheConfig params = new CacheConfig();
|
||||
params.setMaxObjectSizeBytes(MAX_BYTES);
|
||||
impl = new CachingHttpClient(mockBackend, cache, new MemCacheEntryFactory(), params);
|
||||
impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params);
|
||||
}
|
||||
|
||||
private HttpResponse make200Response() {
|
||||
|
|
|
@ -108,7 +108,7 @@ public class TestCacheEntry {
|
|||
Header[] headers = new Header[]{};
|
||||
CacheEntry entry = new CacheEntry(headers);
|
||||
|
||||
MemCacheEntryFactory entryGenerator = new MemCacheEntryFactory();
|
||||
CacheEntryFactory entryGenerator = new CacheEntryFactory(new HeapResourceFactory());
|
||||
|
||||
HttpCacheEntry addedOne = entryGenerator.copyVariant(null, entry, "foo");
|
||||
HttpCacheEntry addedTwo = entryGenerator.copyVariant(null, addedOne, "bar");
|
||||
|
|
|
@ -28,31 +28,28 @@ package org.apache.http.impl.client.cache;
|
|||
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestCacheEntryGenerator {
|
||||
|
||||
@Test
|
||||
public void testEntryMatchesInputs() {
|
||||
public void testEntryMatchesInputs() throws Exception {
|
||||
|
||||
MemCacheEntryFactory gen = new MemCacheEntryFactory();
|
||||
CacheEntryFactory gen = new CacheEntryFactory(new HeapResourceFactory());
|
||||
|
||||
HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
|
||||
HttpStatus.SC_OK, "Success");
|
||||
HttpEntity entity = new ByteArrayEntity(new byte[] {});
|
||||
response.setEntity(entity);
|
||||
Header h = new BasicHeader("fooHeader", "fooHeaderValue");
|
||||
|
||||
response.setHeader("fooHeader", "fooHeaderValue");
|
||||
|
||||
HttpCacheEntry entry = gen.generateEntry(new Date(), new Date(), response, new byte[] {});
|
||||
HttpCacheEntry entry = gen.generate(
|
||||
null,
|
||||
new Date(),
|
||||
new Date(),
|
||||
new OKStatus(),
|
||||
new Header[] { h },
|
||||
new byte[] {});
|
||||
|
||||
Assert.assertEquals("HTTP", entry.getProtocolVersion().getProtocol());
|
||||
Assert.assertEquals(1, entry.getProtocolVersion().getMajor());
|
||||
|
|
|
@ -48,7 +48,6 @@ import org.apache.http.client.HttpClient;
|
|||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.client.cache.HttpCache;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.cache.HttpCacheEntryFactory;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
|
@ -98,7 +97,7 @@ public class TestCachingHttpClient {
|
|||
private CacheEntry mockVariantCacheEntry;
|
||||
private CacheEntry mockUpdatedCacheEntry;
|
||||
private URIExtractor mockExtractor;
|
||||
private HttpCacheEntryFactory mockEntryGenerator;
|
||||
private CacheEntryFactory mockEntryGenerator;
|
||||
private CachedHttpResponseGenerator mockResponseGenerator;
|
||||
private SizeLimitedResponseReader mockResponseReader;
|
||||
private ClientConnectionManager mockConnectionManager;
|
||||
|
@ -141,7 +140,7 @@ public class TestCachingHttpClient {
|
|||
mockUpdatedCacheEntry = EasyMock.createMock(CacheEntry.class);
|
||||
mockVariantCacheEntry = EasyMock.createMock(CacheEntry.class);
|
||||
mockExtractor = EasyMock.createMock(URIExtractor.class);
|
||||
mockEntryGenerator = EasyMock.createMock(HttpCacheEntryFactory.class);
|
||||
mockEntryGenerator = EasyMock.createMock(CacheEntryFactory.class);
|
||||
mockResponseGenerator = EasyMock.createMock(CachedHttpResponseGenerator.class);
|
||||
mockCachedResponse = EasyMock.createMock(HttpResponse.class);
|
||||
mockConditionalRequestBuilder = EasyMock.createMock(ConditionalRequestBuilder.class);
|
||||
|
|
|
@ -100,7 +100,7 @@ public class TestProtocolDeviations {
|
|||
|
||||
CacheConfig params = new CacheConfig();
|
||||
params.setMaxObjectSizeBytes(MAX_BYTES);
|
||||
impl = new CachingHttpClient(mockBackend, cache, new MemCacheEntryFactory(), params);
|
||||
impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params);
|
||||
}
|
||||
|
||||
private HttpResponse make200Response() {
|
||||
|
|
|
@ -43,6 +43,7 @@ import org.apache.http.HttpVersion;
|
|||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.entity.BasicHttpEntity;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.impl.client.RequestWrapper;
|
||||
import org.apache.http.impl.cookie.DateUtils;
|
||||
|
@ -548,7 +549,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
BasicHttpEntityEnclosingRequest post = EasyMock.createMockBuilder(
|
||||
BasicHttpEntityEnclosingRequest.class).withConstructor("POST", "/", HttpVersion.HTTP_1_1)
|
||||
.addMockedMethods("expectContinue").createMock();
|
||||
post.setEntity(mockEntity);
|
||||
post.setEntity(new BasicHttpEntity());
|
||||
post.setHeader("Content-Length", "128");
|
||||
|
||||
Capture<HttpEntityEnclosingRequest> reqCap = new Capture<HttpEntityEnclosingRequest>();
|
||||
|
@ -593,7 +594,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
BasicHttpEntityEnclosingRequest post = EasyMock.createMockBuilder(
|
||||
BasicHttpEntityEnclosingRequest.class).withConstructor("POST", "/", HttpVersion.HTTP_1_1)
|
||||
.addMockedMethods("expectContinue").createMock();
|
||||
post.setEntity(mockEntity);
|
||||
post.setEntity(new BasicHttpEntity());
|
||||
post.setHeader("Content-Length", "128");
|
||||
|
||||
Capture<HttpEntityEnclosingRequest> reqCap = new Capture<HttpEntityEnclosingRequest>();
|
||||
|
@ -2221,7 +2222,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
|
||||
mockCache.putEntry(EasyMock.eq("http://foo.example.com/thing"), EasyMock.isA(HttpCacheEntry.class));
|
||||
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
|
||||
|
||||
HttpRequest validate = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
|
||||
validate.setHeader("If-None-Match", "\"etag\"");
|
||||
|
@ -2263,7 +2264,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
|
||||
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
|
||||
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
|
||||
|
||||
EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
|
||||
|
||||
|
@ -2304,7 +2305,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
|
||||
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
|
||||
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
|
||||
|
||||
EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
|
||||
EasyMock.expect(
|
||||
|
@ -2505,7 +2506,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
|
||||
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
|
||||
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
|
||||
|
||||
EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
|
||||
|
||||
|
@ -2549,7 +2550,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
|
|||
|
||||
CacheEntry entry = new CacheEntry(requestTime, responseTime, hdrs, bytes);
|
||||
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, cacheEntryFactory, params);
|
||||
impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
|
||||
|
||||
HttpResponse validated = make200Response();
|
||||
validated.setHeader("Cache-Control", "public");
|
||||
|
|
Loading…
Reference in New Issue