hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1462070 - in /httpcomponents/httpclient/branches/4.2.x: ./ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/
Date Thu, 28 Mar 2013 12:36:34 GMT
Author: olegk
Date: Thu Mar 28 12:36:34 2013
New Revision: 1462070

URL: http://svn.apache.org/r1462070
Log:
HTTPCLIENT-1299: (regression) cache incorrectly disposes of the underlying cache resource
when storing variant entry

Modified:
    httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt
    httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
    httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java
    httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java

Modified: httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt?rev=1462070&r1=1462069&r2=1462070&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt Thu Mar 28 12:36:34 2013
@@ -1,6 +1,10 @@
 Changes since Release 4.2.3
 -------------------
 
+* [HTTPCLIENT-1299] (regression) cache incorrectly disposes of the underlying cache resource

+  when storing variant entry.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCLIENT-1320] Leverage javax.net.ssl.SSLSocketFactory#getDefault() to initialize SSL
context 
   based on system defaults instead of using an internal custom routine.
   Contributed by Abe Backus <abraham at backus.com> and Oleg Kalnichevski <olegk
at apache.org>

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java?rev=1462070&r1=1462069&r2=1462070&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
Thu Mar 28 12:36:34 2013
@@ -200,7 +200,6 @@ class BasicHttpCache implements HttpCach
         Resource resource = null;
         if (oldResource != null) {
             resource = resourceFactory.copy(requestId, entry.getResource());
-            oldResource.dispose();
         }
         Map<String,String> variantMap = new HashMap<String,String>(src.getVariantMap());
         variantMap.put(variantKey, variantCacheKey);

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java?rev=1462070&r1=1462069&r2=1462070&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/HeapResourceFactory.java
Thu Mar 28 12:36:34 2013
@@ -59,7 +59,7 @@ public class HeapResourceFactory impleme
                 break;
             }
         }
-        return new HeapResource(outstream.toByteArray());
+        return createResource(outstream.toByteArray());
     }
 
     public Resource copy(
@@ -73,7 +73,11 @@ public class HeapResourceFactory impleme
             IOUtils.copyAndClose(resource.getInputStream(), outstream);
             body = outstream.toByteArray();
         }
-        return new HeapResource(body);
+        return createResource(body);
+    }
+
+    Resource createResource(byte[] buf) {
+        return new HeapResource(buf);
     }
 
 }

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java?rev=1462070&r1=1462069&r2=1462070&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java
Thu Mar 28 12:36:34 2013
@@ -34,6 +34,8 @@ import static org.junit.Assert.assertSam
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Date;
 import java.util.HashMap;
@@ -237,7 +239,7 @@ public class TestBasicHttpCache {
         Date responseReceived = new Date(now.getTime() - 1 * 1000L);
 
         HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
-        originResponse.setEntity(HttpTestUtils.makeBody((int) CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES
+ 1));
+        originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES
+ 1));
         originResponse.setHeader("Cache-Control","public, max-age=3600");
         originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
         originResponse.setHeader("ETag", "\"etag\"");
@@ -259,7 +261,7 @@ public class TestBasicHttpCache {
         Date responseReceived = new Date(now.getTime() - 1 * 1000L);
 
         HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
-        originResponse.setEntity(HttpTestUtils.makeBody((int) CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES
- 1));
+        originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES
- 1));
         originResponse.setHeader("Cache-Control","public, max-age=3600");
         originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
         originResponse.setHeader("ETag", "\"etag\"");
@@ -406,4 +408,71 @@ public class TestBasicHttpCache {
         assertTrue(inputStream.wasClosed());
     }
 
+    static class DisposableResource implements Resource {
+
+        private static final long serialVersionUID = 1L;
+
+        private final byte[] b;
+        private boolean dispoased;
+
+        public DisposableResource(final byte[] b) {
+            super();
+            this.b = b;
+        }
+
+        byte[] getByteArray() {
+            return this.b;
+        }
+
+        public InputStream getInputStream() throws IOException {
+            if (dispoased) {
+                throw new IOException("Already dispoased");
+            }
+            return new ByteArrayInputStream(this.b);
+        }
+
+        public long length() {
+            return this.b.length;
+        }
+
+        public void dispose() {
+            this.dispoased = true;
+        }
+
+    }
+
+    @Test
+    public void testEntryUpdate() throws Exception {
+
+        HeapResourceFactory rf = new HeapResourceFactory() {
+
+            @Override
+            Resource createResource(byte[] buf) {
+                return new DisposableResource(buf);
+            }
+
+        };
+
+        impl = new BasicHttpCache(rf, backing, new CacheConfig());
+
+        HttpHost host = new HttpHost("foo.example.com");
+
+        HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+        origRequest.setHeader("Accept-Encoding","gzip");
+
+        HttpResponse origResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
+        origResponse.setEntity(HttpTestUtils.makeBody(128));
+        origResponse.setHeader("Date", DateUtils.formatDate(new Date()));
+        origResponse.setHeader("Cache-Control", "max-age=3600, public");
+        origResponse.setHeader("ETag", "\"etag\"");
+        origResponse.setHeader("Vary", "Accept-Encoding");
+        origResponse.setHeader("Content-Encoding","gzip");
+
+        HttpResponse response = impl.cacheAndReturnResponse(
+                host, origRequest, origResponse, new Date(), new Date());
+        HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(entity);
+        IOUtils.copyAndClose(entity.getContent(), new ByteArrayOutputStream());
+    }
+
 }



Mime
View raw message