HTTPCLIENT-937: CacheEntry made immutable; now uses immutable HttpEntity to store cached content

Contributed by David Mays <david_mays at comcast.com>


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@947501 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-05-23 21:55:20 +00:00
parent ec44b2c26e
commit ef2839b770
15 changed files with 274 additions and 90 deletions

View File

@ -1,3 +1,11 @@
Changes since 4.1 ALPHA2
-------------------
* [HTTPCLIENT-937] CacheEntry made immutable; now uses immutable HttpEntity
to store cached content.
Contributed by David Mays <david_mays at comcast.com> and
Oleg Kalnichevski <olegk at apache.org>
Release 4.1 ALPHA2
-------------------

View File

@ -0,0 +1,108 @@
/*
* ====================================================================
* 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.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.annotation.Immutable;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
@Immutable
class CacheEntity implements HttpEntity, Cloneable, Serializable {
private static final long serialVersionUID = -3467082284120936233L;
private final byte[] content;
private final String contentType;
private final String contentEncoding;
public CacheEntity(final byte[] b, final String contentType, final String contentEncoding) {
super();
this.content = b;
this.contentType = contentType;
this.contentEncoding = contentEncoding;
}
public Header getContentType() {
if (this.contentType != null) {
return new BasicHeader(HTTP.CONTENT_TYPE, this.contentType);
} else {
return null;
}
}
public Header getContentEncoding() {
if (this.contentEncoding != null) {
return new BasicHeader(HTTP.CONTENT_ENCODING, this.contentEncoding);
} else {
return null;
}
}
public boolean isChunked() {
return false;
}
public boolean isRepeatable() {
return true;
}
public long getContentLength() {
return this.content.length;
}
public InputStream getContent() {
return new ByteArrayInputStream(this.content);
}
public void writeTo(final OutputStream outstream) throws IOException {
if (outstream == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
outstream.write(this.content);
outstream.flush();
}
public boolean isStreaming() {
return false;
}
public void consumeContent() throws IOException {
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

View File

@ -37,6 +37,7 @@ import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
@ -62,9 +63,9 @@ public class CacheEntry implements Serializable {
private final ProtocolVersion version;
private final int status;
private final String reason;
private final CachedHeaderGroup responseHeaders = new CachedHeaderGroup();
private final byte[] body;
private final Set<String> variantURIs = new HashSet<String>();
private final CachedHeaderGroup responseHeaders;
private final HttpEntity body;
private final Set<String> variantURIs;
/**
*
@ -85,16 +86,18 @@ public class CacheEntry implements Serializable {
* @param reason
* String message from HTTP Status Line
*/
public CacheEntry(Date requestDate, Date responseDate, ProtocolVersion version, Header[] responseHeaders, byte[] responseBytes, int status, String reason){
public CacheEntry(Date requestDate, Date responseDate, ProtocolVersion version,
Header[] responseHeaders, HttpEntity body, int status, String reason) {
super();
this.requestDate = requestDate;
this.responseDate = responseDate;
this.version = version;
this.responseHeaders = new CachedHeaderGroup();
this.responseHeaders.setHeaders(responseHeaders);
this.status = status;
this.reason = reason;
this.body = responseBytes.clone();
this.body = body;
this.variantURIs = new HashSet<String>();
}
/**
@ -107,7 +110,7 @@ public class CacheEntry implements Serializable {
toCopy.getResponseDate(),
toCopy.getProtocolVersion(),
toCopy.getAllHeaders(),
toCopy.getBody(),
toCopy.body,
toCopy.getStatusCode(),
toCopy.getReasonPhrase());
@ -124,11 +127,11 @@ public class CacheEntry implements Serializable {
}
public String getReasonPhrase() {
return this.reason;
return reason;
}
public int getStatusCode() {
return this.status;
return status;
}
public Date getRequestDate() {
@ -136,11 +139,11 @@ public class CacheEntry implements Serializable {
}
public Date getResponseDate() {
return this.responseDate;
return responseDate;
}
public byte[] getBody() {
return body.clone();
public HttpEntity getBody() {
return body;
}
public Header[] getAllHeaders() {
@ -190,7 +193,7 @@ public class CacheEntry implements Serializable {
* @return boolean indicating whether actual length matches Content-Length
*/
protected boolean contentLengthHeaderMatchesActualLength() {
return getContentLengthValue() == body.length;
return getContentLengthValue() == body.getContentLength();
}
/**

View File

@ -28,8 +28,10 @@ package org.apache.http.impl.client.cache;
import java.util.Date;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.Immutable;
import org.apache.http.protocol.HTTP;
/**
* Generates a {@link CacheEntry} from a {@link HttpResponse}
@ -40,15 +42,19 @@ import org.apache.http.annotation.Immutable;
public class CacheEntryGenerator {
public CacheEntry generateEntry(Date requestDate, Date responseDate, HttpResponse response,
byte[] responseBytes) {
byte[] body) {
Header ct = response.getFirstHeader(HTTP.CONTENT_TYPE);
Header ce = response.getFirstHeader(HTTP.CONTENT_ENCODING);
CacheEntity entity = new CacheEntity(
body,
ct != null ? ct.getValue() : null,
ce != null ? ce.getValue() : null);
return new CacheEntry(requestDate,
responseDate,
response.getProtocolVersion(),
response.getAllHeaders(),
responseBytes,
entity,
response.getStatusLine().getStatusCode(),
response.getStatusLine().getReasonPhrase());
}
}

View File

@ -26,6 +26,7 @@
*/
package org.apache.http.impl.client.cache;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
@ -56,11 +57,15 @@ public class CacheEntryUpdater {
* @param responseDate When the response was gotten
* @param response The HttpResponse from the backend server call
* @return CacheEntry an updated version of the cache entry
* @throws java.io.IOException if something bad happens while trying to read the body from the original entry
*/
public CacheEntry updateCacheEntry(CacheEntry entry, Date requestDate, Date responseDate, HttpResponse response) {
public CacheEntry updateCacheEntry(
CacheEntry entry,
Date requestDate,
Date responseDate,
HttpResponse response) throws IOException {
Header[] mergedHeaders = mergeHeaders(entry, response);
CacheEntry updated = new CacheEntry(requestDate, responseDate,
entry.getProtocolVersion(),
mergedHeaders,

View File

@ -31,7 +31,6 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.annotation.Immutable;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
@ -54,7 +53,7 @@ public class CachedHttpResponseGenerator {
.getStatusCode(), entry.getReasonPhrase());
if (entry.getStatusCode() != HttpStatus.SC_NOT_MODIFIED) {
HttpEntity entity = new ByteArrayEntity(entry.getBody());
HttpEntity entity = entry.getBody();
response.setEntity(entity);
response.setHeaders(entry.getAllHeaders());
addMissingContentLengthHeader(response, entity);

View File

@ -44,9 +44,9 @@ public class SizeLimitedResponseReader {
private final int maxResponseSizeBytes;
private final HttpResponse response;
ByteArrayOutputStream outputStream;
InputStream contentInputStream;
private ByteArrayOutputStream outputStream;
private InputStream contentInputStream;
private boolean isTooLarge;
private boolean responseIsConsumed;
private byte[] sizeLimitedContent;

View File

@ -31,6 +31,7 @@ import java.util.Set;
import org.apache.http.Header;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.message.BasicHeader;
import org.junit.Assert;
@ -106,7 +107,8 @@ public class TestCacheEntry {
}
private CacheEntry getEntry(Date requestDate, Date responseDate, Header[] headers) {
return new CacheEntry(requestDate,responseDate,HTTP_1_1,headers,new byte[]{},200,"OK");
return new CacheEntry(requestDate, responseDate, HTTP_1_1, headers,
new ByteArrayEntity(new byte[] {}), 200, "OK");
}
@Test
@ -125,7 +127,9 @@ public class TestCacheEntry {
@Test
public void testCorrectedReceivedAgeIsAgeHeaderIfLarger() {
Header[] headers = new Header[] { new BasicHeader("Age", "10"), };
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,headers, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1,
headers, new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override
@ -141,7 +145,8 @@ public class TestCacheEntry {
@Test
public void testCorrectedReceivedAgeIsApparentAgeIfLarger() {
Header[] headers = new Header[] { new BasicHeader("Age", "6"), };
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,headers, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1,
headers, new ByteArrayEntity(new byte[] {}), 200 ,"OK") {
private static final long serialVersionUID = 1L;
@Override
@ -161,7 +166,9 @@ public class TestCacheEntry {
Header[] headers = new Header[]{};
CacheEntry entry = new CacheEntry(tenSecondsAgo,sixSecondsAgo,new ProtocolVersion("HTTP",1,1),headers,new byte[]{},200,"OK");
CacheEntry entry = new CacheEntry(tenSecondsAgo, sixSecondsAgo,
new ProtocolVersion("HTTP",1,1), headers, new ByteArrayEntity(new byte[] {}),
200, "OK");
Assert.assertEquals(4, entry.getResponseDelaySecs());
@ -169,7 +176,8 @@ public class TestCacheEntry {
@Test
public void testCorrectedInitialAgeIsCorrectedReceivedAgePlusResponseDelay() {
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,new Header[]{}, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1, new Header[] {},
new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override
@ -190,7 +198,8 @@ public class TestCacheEntry {
final Date now = new Date();
Date sixSecondsAgo = new Date(now.getTime() - 6 * 1000L);
CacheEntry entry = new CacheEntry(new Date(),sixSecondsAgo,HTTP_1_1,new Header[]{}, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), sixSecondsAgo, HTTP_1_1, new Header[]{},
new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override
@ -204,7 +213,8 @@ public class TestCacheEntry {
@Test
public void testCurrentAgeIsCorrectedInitialAgePlusResidentTime() {
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,new Header[]{}, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1, new Header[]{},
new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override
@ -288,7 +298,8 @@ public class TestCacheEntry {
@Test
public void testResponseIsFreshIfFreshnessLifetimeExceedsCurrentAge() {
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,new Header[]{}, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1, new Header[]{},
new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override
@ -307,7 +318,9 @@ public class TestCacheEntry {
@Test
public void testResponseIsNotFreshIfFreshnessLifetimeEqualsCurrentAge() {
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,new Header[]{}, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1, new Header[]{},
new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override
@ -326,7 +339,8 @@ public class TestCacheEntry {
@Test
public void testResponseIsNotFreshIfCurrentAgeExceedsFreshnessLifetime() {
CacheEntry entry = new CacheEntry(new Date(),new Date(),HTTP_1_1,new Header[]{}, new byte[]{},200,"OK") {
CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1, new Header[] {},
new ByteArrayEntity(new byte[] {}), 200, "OK") {
private static final long serialVersionUID = 1L;
@Override

View File

@ -26,15 +26,11 @@
*/
package org.apache.http.impl.client.cache;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotSame;
import java.util.Date;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
@ -44,6 +40,12 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotSame;
public class TestCacheEntryUpdater {
@ -86,7 +88,7 @@ public class TestCacheEntryUpdater {
}
@Test
public void testUpdateCacheEntryReturnsDifferentEntryInstance() {
public void testUpdateCacheEntryReturnsDifferentEntryInstance() throws IOException {
CacheEntry entry = getEntry(new Header[]{});
BasicHttpResponse response = new BasicHttpResponse(HTTP_1_1, 200, "OK");
@ -102,7 +104,7 @@ public class TestCacheEntryUpdater {
}
@Test
public void testHeadersAreMergedCorrectly() {
public void testHeadersAreMergedCorrectly() throws IOException {
Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(responseDate)),
@ -124,7 +126,7 @@ public class TestCacheEntryUpdater {
}
@Test
public void testNewerHeadersReplaceExistingHeaders() {
public void testNewerHeadersReplaceExistingHeaders() throws IOException {
Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(requestDate)),
@ -152,7 +154,7 @@ public class TestCacheEntryUpdater {
}
@Test
public void testNewHeadersAreAddedByMerge() {
public void testNewHeadersAreAddedByMerge() throws IOException {
Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(requestDate)),
@ -179,7 +181,7 @@ public class TestCacheEntryUpdater {
}
@Test
public void testUpdatedEntryHasLatestRequestAndResponseDates() {
public void testUpdatedEntryHasLatestRequestAndResponseDates() throws IOException {
Date now = new Date();
@ -191,7 +193,8 @@ public class TestCacheEntryUpdater {
Header[] headers = new Header[]{};
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1, headers, new byte[]{}, 200, "OK");
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1, headers,
new ByteArrayEntity(new byte[] {}), 200, "OK");
HttpResponse response = new BasicHttpResponse(HTTP_1_1, 200, "OK");
@ -222,6 +225,7 @@ public class TestCacheEntryUpdater {
}
private CacheEntry getEntry(Date requestDate, Date responseDate, Header[] headers) {
return new CacheEntry(requestDate, responseDate, HTTP_1_1, headers, new byte[]{}, 200, "OK");
return new CacheEntry(requestDate, responseDate, HTTP_1_1, headers,
new ByteArrayEntity(new byte[] {}), 200, "OK");
}
}

View File

@ -31,6 +31,7 @@ import java.util.Date;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.message.BasicHeader;
import org.junit.Assert;
@ -43,7 +44,9 @@ public class TestCachedHttpResponseGenerator {
Header[] hdrs = new Header[] {};
byte[] buf = new byte[] { 1, 2, 3, 4, 5 };
CacheEntry entry = new CacheEntry(new Date(),new Date(),new ProtocolVersion("HTTP", 1, 1),hdrs,buf,200,"OK");
CacheEntry entry = new CacheEntry(
new Date(), new Date(), new ProtocolVersion("HTTP", 1, 1), hdrs,
new ByteArrayEntity(buf), 200, "OK");
CachedHttpResponseGenerator gen = new CachedHttpResponseGenerator();
HttpResponse response = gen.generateResponse(entry);
@ -60,7 +63,9 @@ public class TestCachedHttpResponseGenerator {
Header[] hdrs = new Header[] { new BasicHeader("Transfer-Encoding", "chunked") };
byte[] buf = new byte[] { 1, 2, 3, 4, 5 };
CacheEntry entry = new CacheEntry(new Date(),new Date(),new ProtocolVersion("HTTP", 1, 1),hdrs,buf,200,"OK");
CacheEntry entry = new CacheEntry(
new Date(), new Date(), new ProtocolVersion("HTTP", 1, 1), hdrs,
new ByteArrayEntity(buf), 200, "OK");
CachedHttpResponseGenerator gen = new CachedHttpResponseGenerator();
@ -145,7 +150,8 @@ public class TestCachedHttpResponseGenerator {
new BasicHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)),
new BasicHeader("Content-Length", "150") };
return new CacheEntry(tenSecondsAgo,sixSecondsAgo,new ProtocolVersion("HTTP", 1, 1),hdrs,new byte[]{},200,"OK");
return new CacheEntry(tenSecondsAgo, sixSecondsAgo, new ProtocolVersion("HTTP", 1, 1),
hdrs, new ByteArrayEntity(new byte[] {}), 200, "OK");
}
@ -160,8 +166,10 @@ public class TestCachedHttpResponseGenerator {
new BasicHeader("Content-Length", "150") };
return new CacheEntry(tenSecondsAgo,sixSecondsAgo,new ProtocolVersion("HTTP", 1, 1),hdrs,new byte[]{},200,"OK"){
private static final long serialVersionUID = 1L;
return new CacheEntry(tenSecondsAgo, sixSecondsAgo, new ProtocolVersion("HTTP", 1, 1),
hdrs, new ByteArrayEntity(new byte[] {}), 200, "OK"){
private static final long serialVersionUID = 1L;
@Override
public long getCurrentAgeSecs() {

View File

@ -26,24 +26,7 @@
*/
package org.apache.http.impl.client.cache;
import static junit.framework.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolException;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
@ -55,6 +38,7 @@ import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpParams;
@ -65,6 +49,15 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static junit.framework.Assert.assertTrue;
public class TestCachingHttpClient {
private static final ProtocolVersion HTTP_1_1 = new ProtocolVersion("HTTP",1,1);
@ -313,7 +306,8 @@ public class TestCachingHttpClient {
final String variantURI = "variantURI";
final CacheEntry entry = new CacheEntry(new Date(), new Date(),HTTP_1_1,new Header[]{},new byte[]{},200,"OK");
final CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1,
new Header[] {}, new ByteArrayEntity(new byte[] {}), 200, "OK");
extractVariantURI(variantURI, entry);
putInCache(variantURI, entry);
@ -965,7 +959,7 @@ public class TestCachingHttpClient {
org.easymock.EasyMock.expect(mockCacheEntry.isRevalidatable()).andReturn(b);
}
private void cacheEntryUpdaterCalled() {
private void cacheEntryUpdaterCalled() throws IOException {
EasyMock.expect(
mockCacheEntryUpdater.updateCacheEntry(mockCacheEntry, requestDate, responseDate,
mockBackendResponse)).andReturn(mockUpdatedCacheEntry);

View File

@ -32,6 +32,7 @@ import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.ProtocolException;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpRequest;
@ -61,7 +62,9 @@ public class TestConditionalRequestBuilder {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("Last-Modified", lastModified) };
CacheEntry cacheEntry = new CacheEntry(new Date(),new Date(),new ProtocolVersion("HTTP",1,1),headers, new byte[]{},200,"OK");
CacheEntry cacheEntry = new CacheEntry(new Date(), new Date(),
new ProtocolVersion("HTTP",1,1), headers,
new ByteArrayEntity(new byte[] {}), 200, "OK");
HttpRequest newRequest = impl.buildConditionalRequest(request, cacheEntry);
Assert.assertNotSame(request, newRequest);
@ -93,7 +96,9 @@ public class TestConditionalRequestBuilder {
new BasicHeader("Last-Modified", DateUtils.formatDate(new Date())),
new BasicHeader("ETag", theETag) };
CacheEntry cacheEntry = new CacheEntry(new Date(),new Date(),new ProtocolVersion("HTTP",1,1),headers, new byte[]{},200,"OK");
CacheEntry cacheEntry = new CacheEntry(new Date(), new Date(),
new ProtocolVersion("HTTP",1,1), headers, new ByteArrayEntity(new byte[] {}),
200, "OK");
HttpRequest newRequest = impl.buildConditionalRequest(request, cacheEntry);

View File

@ -26,11 +26,6 @@
*/
package org.apache.http.impl.client.cache;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Date;
import org.apache.http.Header;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
@ -39,6 +34,13 @@ import org.apache.http.message.BasicHeader;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
public class TestDefaultCacheEntrySerializer {
@Test
@ -60,7 +62,7 @@ public class TestDefaultCacheEntrySerializer {
}
private CacheEntry newCacheEntry() {
private CacheEntry newCacheEntry() throws UnsupportedEncodingException {
Header[] headers = new Header[5];
@ -70,13 +72,14 @@ public class TestDefaultCacheEntrySerializer {
ProtocolVersion version = new HttpVersion(1, 1);
String body = "Lorem ipsum dolor sit amet";
CacheEntry cacheEntry = new CacheEntry(new Date(),new Date(), version, headers, body.getBytes(),200,"OK");
CacheEntry cacheEntry = new CacheEntry(new Date(), new Date(), version, headers,
new CacheEntity(body.getBytes("US-ASCII"), null, null), 200, "OK");
return cacheEntry;
}
private boolean areEqual(CacheEntry one, CacheEntry two) {
private boolean areEqual(CacheEntry one, CacheEntry two) throws IOException {
if (!one.getRequestDate().equals(two.getRequestDate()))
return false;
@ -84,7 +87,20 @@ public class TestDefaultCacheEntrySerializer {
return false;
if (!one.getProtocolVersion().equals(two.getProtocolVersion()))
return false;
if (!Arrays.equals(one.getBody(), two.getBody()))
byte[] bytesOne, bytesTwo;
ByteArrayOutputStream streamOne = new ByteArrayOutputStream();
one.getBody().writeTo(streamOne);
bytesOne = streamOne.toByteArray();
ByteArrayOutputStream streamTwo = new ByteArrayOutputStream();
two.getBody().writeTo(streamTwo);
bytesTwo = streamTwo.toByteArray();
if (!Arrays.equals(bytesOne, bytesTwo))
return false;
Header[] oneHeaders = one.getAllHeaders();

View File

@ -2308,7 +2308,8 @@ public class TestProtocolRequirements {
byte[] bytes = new byte[128];
(new Random()).nextBytes(bytes);
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1, headerGroup.getAllHeaders(),bytes,200,"OK");
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1,
headerGroup.getAllHeaders(), new ByteArrayEntity(bytes), 200, "OK");
mockCache.putEntry(EasyMock.eq("http://foo.example.com/thing"), EasyMock.isA(CacheEntry.class));
@ -2352,7 +2353,9 @@ public class TestProtocolRequirements {
(new Random()).nextBytes(bytes);
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1, headerGroup.getAllHeaders(),bytes,200,"OK");
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1,
headerGroup.getAllHeaders(),
new ByteArrayEntity(bytes), 200, "OK");
impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
@ -2393,7 +2396,8 @@ public class TestProtocolRequirements {
byte[] bytes = new byte[128];
(new Random()).nextBytes(bytes);
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1, headerGroup.getAllHeaders(),bytes,200,"OK");
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1,
headerGroup.getAllHeaders(), new ByteArrayEntity(bytes), 200, "OK");
@ -2596,7 +2600,8 @@ public class TestProtocolRequirements {
byte[] bytes = new byte[128];
(new Random()).nextBytes(bytes);
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1, headerGroup.getAllHeaders(),bytes,200,"OK");
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, HTTP_1_1,
headerGroup.getAllHeaders(), new ByteArrayEntity(bytes), 200, "OK");
impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
@ -2639,8 +2644,8 @@ public class TestProtocolRequirements {
byte[] bytes = new byte[128];
(new Random()).nextBytes(bytes);
CacheEntry entry = new CacheEntry(requestTime, responseTime, HTTP_1_1, headerGroup.getAllHeaders(),bytes,200,"OK");
CacheEntry entry = new CacheEntry(requestTime, responseTime, HTTP_1_1,
headerGroup.getAllHeaders(), new ByteArrayEntity(bytes), 200, "OK");
impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);

View File

@ -28,12 +28,16 @@ package org.apache.http.impl.client.cache;
import org.apache.http.client.cache.HttpCacheOperationException;
import org.apache.http.client.cache.HttpCacheUpdateCallback;
import org.apache.http.entity.ByteArrayEntity;
import org.easymock.classextension.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class TestResponseCache {
private BasicHttpCache cache;
@ -133,7 +137,7 @@ public class TestResponseCache {
@Test
@Ignore
public void testCacheEntryCallbackUpdatesCacheEntry() throws HttpCacheOperationException {
public void testCacheEntryCallbackUpdatesCacheEntry() throws HttpCacheOperationException, IOException {
final byte[] expectedArray = new byte[] { 1, 2, 3, 4, 5 };
@ -151,7 +155,7 @@ public class TestResponseCache {
existing.getRequestDate(),
existing.getProtocolVersion(),
existing.getAllHeaders(),
expectedArray,
new ByteArrayEntity(expectedArray),
existing.getStatusCode(),
existing.getReasonPhrase());
cache.removeEntry("bar");
@ -164,7 +168,12 @@ public class TestResponseCache {
Assert.assertNull(bar);
Assert.assertArrayEquals(expectedArray, afterUpdate.getBody());
byte[] bytes;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
afterUpdate.getBody().writeTo(stream);
bytes = stream.toByteArray();
Assert.assertArrayEquals(expectedArray,bytes);
}
}