diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/OKStatus.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/OKStatus.java index eeebb295a..c62e8dad1 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/OKStatus.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/OKStatus.java @@ -32,6 +32,8 @@ public class OKStatus extends BasicStatusLine { + private static final long serialVersionUID = -1639872615816850272L; + public OKStatus() { super(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); } diff --git a/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java b/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java index a998dfb42..81df17e1b 100644 --- a/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java +++ b/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java @@ -35,6 +35,7 @@ import org.apache.http.HttpEntity; import org.apache.http.entity.HttpEntityWrapper; +import org.apache.http.util.EntityUtils; /** * An entity that releases a {@link ManagedClientConnection connection}. @@ -87,15 +88,14 @@ public InputStream getContent() throws IOException { return new EofSensorInputStream(wrappedEntity.getContent(), this); } - @Override - public void consumeContent() throws IOException { + private void ensureConsumed() throws IOException { if (managedConn == null) return; try { if (attemptReuse) { // this will not trigger a callback from EofSensorInputStream - wrappedEntity.consumeContent(); + EntityUtils.consume(wrappedEntity); managedConn.markReusable(); } } finally { @@ -103,14 +103,20 @@ public void consumeContent() throws IOException { } } + @Deprecated + @Override + public void consumeContent() throws IOException { + ensureConsumed(); + } + @Override public void writeTo(final OutputStream outstream) throws IOException { super.writeTo(outstream); - consumeContent(); + ensureConsumed(); } public void releaseConnection() throws IOException { - this.consumeContent(); + ensureConsumed(); } public void abortConnection() throws IOException { diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java index 180a853ed..1054152d0 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java @@ -89,6 +89,7 @@ import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpRequestExecutor; +import org.apache.http.util.EntityUtils; /** * Default implementation of {@link RequestDirector}. @@ -485,9 +486,7 @@ public HttpResponse execute(HttpHost target, HttpRequest request, if (reuse) { // Make sure the response body is fully consumed, if present HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); // entity consumed above is not an auto-release entity, // need to mark the connection re-usable explicitly managedConn.markReusable(); @@ -875,9 +874,7 @@ protected boolean createTunnelToTarget(HttpRoute route, this.log.debug("Connection kept alive"); // Consume response content HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); } else { this.managedConn.close(); } diff --git a/httpclient/src/main/java/org/apache/http/impl/client/EntityEnclosingRequestWrapper.java b/httpclient/src/main/java/org/apache/http/impl/client/EntityEnclosingRequestWrapper.java index 2f1d3d1bb..3c393c659 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/EntityEnclosingRequestWrapper.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/EntityEnclosingRequestWrapper.java @@ -57,7 +57,7 @@ public class EntityEnclosingRequestWrapper extends RequestWrapper private HttpEntity entity; private boolean consumed; - + public EntityEnclosingRequestWrapper(final HttpEntityEnclosingRequest request) throws ProtocolException { super(request); @@ -84,11 +84,12 @@ public boolean isRepeatable() { } class EntityWrapper extends HttpEntityWrapper { - + EntityWrapper(final HttpEntity entity) { super(entity); } + @Deprecated @Override public void consumeContent() throws IOException { consumed = true; @@ -106,7 +107,7 @@ public void writeTo(final OutputStream outstream) throws IOException { consumed = true; super.writeTo(outstream); } - + } - + } diff --git a/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java b/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java index 63a637b00..573345a22 100644 --- a/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java +++ b/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java @@ -52,6 +52,7 @@ import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -98,9 +99,7 @@ public void testCookieVersionSupportHeader1() throws Exception { HttpResponse response1 = client.execute(getServerHttp(), httpget, context); HttpEntity e1 = response1.getEntity(); - if (e1 != null) { - e1.consumeContent(); - } + EntityUtils.consume(e1); List cookies = cookieStore.getCookies(); Assert.assertNotNull(cookies); @@ -108,9 +107,7 @@ public void testCookieVersionSupportHeader1() throws Exception { HttpResponse response2 = client.execute(getServerHttp(), httpget, context); HttpEntity e2 = response2.getEntity(); - if (e2 != null) { - e2.consumeContent(); - } + EntityUtils.consume(e2); HttpRequest reqWrapper = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); @@ -150,9 +147,7 @@ public void testCookieVersionSupportHeader2() throws Exception { HttpResponse response1 = client.execute(getServerHttp(), httpget, context); HttpEntity e1 = response1.getEntity(); - if (e1 != null) { - e1.consumeContent(); - } + EntityUtils.consume(e1); List cookies = cookieStore.getCookies(); Assert.assertNotNull(cookies); @@ -160,9 +155,7 @@ public void testCookieVersionSupportHeader2() throws Exception { HttpResponse response2 = client.execute(getServerHttp(), httpget, context); HttpEntity e2 = response2.getEntity(); - if (e2 != null) { - e2.consumeContent(); - } + EntityUtils.consume(e2); HttpRequest reqWrapper = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); @@ -201,9 +194,7 @@ public void testCookieVersionSupportHeader3() throws Exception { HttpResponse response1 = client.execute(getServerHttp(), httpget, context); HttpEntity e1 = response1.getEntity(); - if (e1 != null) { - e1.consumeContent(); - } + EntityUtils.consume(e1); List cookies = cookieStore.getCookies(); Assert.assertNotNull(cookies); @@ -211,9 +202,7 @@ public void testCookieVersionSupportHeader3() throws Exception { HttpResponse response2 = client.execute(getServerHttp(), httpget, context); HttpEntity e2 = response2.getEntity(); - if (e2 != null) { - e2.consumeContent(); - } + EntityUtils.consume(e2); HttpRequest reqWrapper = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); @@ -253,9 +242,7 @@ public void testSetCookieVersionMix() throws Exception { HttpResponse response1 = client.execute(getServerHttp(), httpget, context); HttpEntity e1 = response1.getEntity(); - if (e1 != null) { - e1.consumeContent(); - } + EntityUtils.consume(e1); List cookies = cookieStore.getCookies(); Assert.assertNotNull(cookies); diff --git a/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java b/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java index e43986270..611ef348a 100644 --- a/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java +++ b/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java @@ -32,7 +32,6 @@ import java.util.List; import org.apache.http.Header; -import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -60,6 +59,7 @@ import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -231,10 +231,7 @@ public void testBasicRedirect300() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -257,10 +254,7 @@ public void testBasicRedirect301() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -287,10 +281,7 @@ public void testBasicRedirect302() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -325,10 +316,7 @@ public void handle( HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -355,10 +343,7 @@ public void testBasicRedirect303() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -385,10 +370,7 @@ public void testBasicRedirect304() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -411,10 +393,7 @@ public void testBasicRedirect305() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -437,10 +416,7 @@ public void testBasicRedirect307() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -501,10 +477,7 @@ public void testPostNoRedirect() throws Exception { httppost.setEntity(new StringEntity("stuff")); HttpResponse response = client.execute(getServerHttp(), httppost, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -529,10 +502,7 @@ public void testPostRedirectSeeOther() throws Exception { httppost.setEntity(new StringEntity("stuff")); HttpResponse response = client.execute(getServerHttp(), httppost, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -557,10 +527,7 @@ public void testRelativeRedirect() throws Exception { HttpGet httpget = new HttpGet("/oldlocation/"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -588,10 +555,7 @@ public void testRelativeRedirect2() throws Exception { HttpGet httpget = new HttpGet("/test/oldlocation"); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -678,10 +642,7 @@ public void testRedirectWithCookie() throws Exception { HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -714,10 +675,7 @@ public void testDefaultHeadersRedirect() throws Exception { HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); diff --git a/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java b/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java index d21851da4..96032a822 100644 --- a/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java +++ b/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java @@ -42,6 +42,7 @@ import org.apache.http.localserver.LocalTestServer; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -89,12 +90,12 @@ private void doTest(String uri, boolean relative) throws Exception { String request = "http://" + host + ":" + port + uri; HttpGet httpget = new HttpGet(request); response = client.execute(httpget); - response.getEntity().consumeContent(); + EntityUtils.consume(response.getEntity()); } else { HttpHost target = new HttpHost(host, port); HttpGet httpget = new HttpGet(uri); response = client.execute(target, httpget); - response.getEntity().consumeContent(); + EntityUtils.consume(response.getEntity()); } Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); diff --git a/httpclient/src/test/java/org/apache/http/conn/TestConnectionAutoRelease.java b/httpclient/src/test/java/org/apache/http/conn/TestConnectionAutoRelease.java index 9069698e9..c77b2f71a 100644 --- a/httpclient/src/test/java/org/apache/http/conn/TestConnectionAutoRelease.java +++ b/httpclient/src/test/java/org/apache/http/conn/TestConnectionAutoRelease.java @@ -87,7 +87,7 @@ public void testReleaseOnEntityConsumeContent() throws Exception { HttpEntity e = response.getEntity(); Assert.assertNotNull(e); - e.consumeContent(); + EntityUtils.consume(e); // Expect one connection in the pool Assert.assertEquals(1, mgr.getConnectionsInPool()); diff --git a/httpclient/src/test/java/org/apache/http/conn/TestConnectionReuse.java b/httpclient/src/test/java/org/apache/http/conn/TestConnectionReuse.java index a0866c764..d7e7522bc 100644 --- a/httpclient/src/test/java/org/apache/http/conn/TestConnectionReuse.java +++ b/httpclient/src/test/java/org/apache/http/conn/TestConnectionReuse.java @@ -32,7 +32,6 @@ import java.net.URI; import org.apache.http.Header; -import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; @@ -59,6 +58,7 @@ import org.apache.http.protocol.ResponseContent; import org.apache.http.protocol.ResponseDate; import org.apache.http.protocol.ResponseServer; +import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Assert; import org.junit.Test; @@ -301,15 +301,13 @@ public void testKeepAliveHeaderRespected() throws Exception { HttpHost target = new HttpHost(saddress.getHostName(), saddress.getPort(), "http"); HttpResponse response = client.execute(target, new HttpGet("/random/2000")); - if(response.getEntity() != null) - response.getEntity().consumeContent(); + EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, mgr.getConnectionsInPool()); Assert.assertEquals(1, localServer.getAcceptedConnectionCount()); response = client.execute(target, new HttpGet("/random/2000")); - if(response.getEntity() != null) - response.getEntity().consumeContent(); + EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, mgr.getConnectionsInPool()); Assert.assertEquals(1, localServer.getAcceptedConnectionCount()); @@ -317,8 +315,7 @@ public void testKeepAliveHeaderRespected() throws Exception { // Now sleep for 1.1 seconds and let the timeout do its work Thread.sleep(1100); response = client.execute(target, new HttpGet("/random/2000")); - if(response.getEntity() != null) - response.getEntity().consumeContent(); + EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, mgr.getConnectionsInPool()); Assert.assertEquals(2, localServer.getAcceptedConnectionCount()); @@ -327,8 +324,7 @@ public void testKeepAliveHeaderRespected() throws Exception { // sure we reuse that connection. Thread.sleep(500); response = client.execute(target, new HttpGet("/random/2000")); - if(response.getEntity() != null) - response.getEntity().consumeContent(); + EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, mgr.getConnectionsInPool()); Assert.assertEquals(2, localServer.getAcceptedConnectionCount()); @@ -372,10 +368,7 @@ public void run() { if (this.forceClose) { httpget.abort(); } else { - HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(response.getEntity()); } } } catch (Exception ex) { diff --git a/httpclient/src/test/java/org/apache/http/impl/auth/TestNegotiateScheme.java b/httpclient/src/test/java/org/apache/http/impl/auth/TestNegotiateScheme.java index b2dae780f..da43412bc 100644 --- a/httpclient/src/test/java/org/apache/http/impl/auth/TestNegotiateScheme.java +++ b/httpclient/src/test/java/org/apache/http/impl/auth/TestNegotiateScheme.java @@ -29,9 +29,6 @@ import java.io.IOException; import java.security.Principal; -import junit.framework.Assert; - -import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -51,12 +48,14 @@ import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.ietf.jgss.GSSContext; import org.ietf.jgss.GSSCredential; import org.ietf.jgss.GSSManager; import org.ietf.jgss.GSSName; import org.ietf.jgss.Oid; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -188,10 +187,7 @@ public void testDontTryToAuthenticateEndlessly() throws Exception { String s = "/path"; HttpGet httpget = new HttpGet(s); HttpResponse response = client.execute(httpget); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); ((NegotiateSchemeFactoryWithMockGssManager)nsf).scheme.verify(); @@ -222,10 +218,7 @@ public void testNoTokenGeneratedError() throws Exception { String s = "/path"; HttpGet httpget = new HttpGet(s); HttpResponse response = client.execute(httpget); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); nsf.scheme.verify(); Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java b/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java index a7431cb3e..5caa4c4db 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java @@ -60,6 +60,7 @@ import org.apache.http.protocol.ResponseContent; import org.apache.http.protocol.ResponseDate; import org.apache.http.protocol.ResponseServer; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -103,12 +104,12 @@ public void handle( static class AuthExpectationVerifier implements HttpExpectationVerifier { private final BasicAuthTokenExtractor authTokenExtractor; - + public AuthExpectationVerifier() { super(); this.authTokenExtractor = new BasicAuthTokenExtractor(); } - + public void verify( final HttpRequest request, final HttpResponse response, @@ -166,7 +167,7 @@ public void testBasicAuthenticationNoCreds() throws Exception { HttpEntity entity = response.getEntity(); Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); Assert.assertNotNull(entity); - entity.consumeContent(); + EntityUtils.consume(entity); AuthScope authscope = credsProvider.getAuthScope(); Assert.assertNotNull(authscope); Assert.assertEquals("test realm", authscope.getRealm()); @@ -189,7 +190,7 @@ public void testBasicAuthenticationFailure() throws Exception { HttpEntity entity = response.getEntity(); Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); Assert.assertNotNull(entity); - entity.consumeContent(); + EntityUtils.consume(entity); AuthScope authscope = credsProvider.getAuthScope(); Assert.assertNotNull(authscope); Assert.assertEquals("test realm", authscope.getRealm()); @@ -212,7 +213,7 @@ public void testBasicAuthenticationSuccess() throws Exception { HttpEntity entity = response.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); Assert.assertNotNull(entity); - entity.consumeContent(); + EntityUtils.consume(entity); AuthScope authscope = credsProvider.getAuthScope(); Assert.assertNotNull(authscope); Assert.assertEquals("test realm", authscope.getRealm()); @@ -298,7 +299,7 @@ public void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception { HttpEntity entity = response.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); Assert.assertNotNull(entity); - entity.consumeContent(); + EntityUtils.consume(entity); AuthScope authscope = credsProvider.getAuthScope(); Assert.assertNotNull(authscope); Assert.assertEquals("test realm", authscope.getRealm()); @@ -384,13 +385,13 @@ public void testBasicAuthenticationCredentialsCaching() throws Exception { HttpEntity entity1 = response1.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode()); Assert.assertNotNull(entity1); - entity1.consumeContent(); + EntityUtils.consume(entity1); HttpResponse response2 = httpclient.execute(getServerHttp(), httpget, context); HttpEntity entity2 = response1.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode()); Assert.assertNotNull(entity2); - entity1.consumeContent(); + EntityUtils.consume(entity2); Assert.assertEquals(1, authHandler.getCount()); } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java index 966ed6693..149a85903 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java @@ -34,7 +34,6 @@ import org.apache.http.Header; import org.apache.http.HttpClientConnection; -import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -74,6 +73,7 @@ import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -607,10 +607,7 @@ public void testDefaultHostAtClientLevel() throws Exception { HttpGet httpget = new HttpGet(s); HttpResponse response = client.execute(httpget); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); } @@ -630,10 +627,7 @@ public void testDefaultHostAtRequestLevel() throws Exception { httpget.getParams().setParameter(ClientPNames.DEFAULT_HOST, target2); HttpResponse response = client.execute(httpget); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); } @@ -717,10 +711,7 @@ public boolean retryRequest( HttpGet httpget = new HttpGet(s); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java b/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java index a83e9afe1..17bcaacd5 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java @@ -28,7 +28,6 @@ import java.io.IOException; -import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; @@ -41,6 +40,7 @@ import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -85,10 +85,7 @@ public void testRequestURIRewriting() throws Exception { HttpGet httpget = new HttpGet(s); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); @@ -111,10 +108,7 @@ public void testRequestURIRewritingEmptyPath() throws Exception { HttpGet httpget = new HttpGet(s); HttpResponse response = client.execute(getServerHttp(), httpget, context); - HttpEntity e = response.getEntity(); - if (e != null) { - e.consumeContent(); - } + EntityUtils.consume(response.getEntity()); HttpRequest reqWrapper = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestStatefulConnManagement.java b/httpclient/src/test/java/org/apache/http/impl/client/TestStatefulConnManagement.java index 85ac46f7d..24b87149d 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestStatefulConnManagement.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestStatefulConnManagement.java @@ -27,7 +27,6 @@ import java.io.IOException; -import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -47,6 +46,7 @@ import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Test; @@ -185,10 +185,7 @@ public void run() { this.context.setAttribute("r" + r, conn.getState()); - HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(response.getEntity()); } } catch (Exception ex) { @@ -229,10 +226,7 @@ public Object getUserToken(final HttpContext context) { context1.setAttribute("user", "stuff"); HttpResponse response1 = client.execute( new HttpHost("localhost", port), new HttpGet("/"), context1); - HttpEntity entity1 = response1.getEntity(); - if (entity1 != null) { - entity1.consumeContent(); - } + EntityUtils.consume(response1.getEntity()); // The ConnPoolByRoute now has 1 free connection, out of 2 max // The ConnPoolByRoute has one RouteSpcfcPool, that has one free connection @@ -245,10 +239,7 @@ public Object getUserToken(final HttpContext context) { HttpContext context2 = new BasicHttpContext(); HttpResponse response2 = client.execute( new HttpHost("127.0.0.1", port), new HttpGet("/"), context2); - HttpEntity entity2 = response2.getEntity(); - if (entity2 != null) { - entity2.consumeContent(); - } + EntityUtils.consume(response2.getEntity()); // ConnPoolByRoute now has 2 free connexions, out of its 2 max. // The [localhost][stuff] RouteSpcfcPool is the same as earlier // And there is a [127.0.0.1][null] pool with 1 free connection @@ -267,10 +258,7 @@ public Object getUserToken(final HttpContext context) { // If the ConnPoolByRoute did not behave coherently with the RouteSpecificPool // this may fail. Ex : if the ConnPool discared the route pool because it was empty, // but still used it to build the request3 connection. - HttpEntity entity3 = response3.getEntity(); - if (entity3 != null) { - entity3.consumeContent(); - } + EntityUtils.consume(response3.getEntity()); } diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestIdleConnectionEviction.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestIdleConnectionEviction.java index 5bc71750b..180ffeb99 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestIdleConnectionEviction.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestIdleConnectionEviction.java @@ -30,7 +30,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; -import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; @@ -48,6 +47,7 @@ import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; +import org.apache.http.util.EntityUtils; import org.junit.Before; import org.junit.Test; @@ -128,10 +128,7 @@ public void run() { this.request.abort(); throw new ClientProtocolException("Unexpected status code: " + status); } - HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(response.getEntity()); Thread.sleep(10); } } catch (Exception ex) { diff --git a/pom.xml b/pom.xml index 4d42d22ff..63f230a1c 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,7 @@ UTF-8 UTF-8 - 4.1-beta1 + 4.1-beta2 1.1.1 1.4 2.2.0 diff --git a/src/docbkx/fundamentals.xml b/src/docbkx/fundamentals.xml index 6d13bf6cd..01c5239ed 100644 --- a/src/docbkx/fundamentals.xml +++ b/src/docbkx/fundamentals.xml @@ -296,16 +296,30 @@ important message
Ensuring release of low level resources - When finished with a response entity, it's important to ensure that all entity - content has been fully consumed, so that the connection could be safely returned to - the connection pool and re-used by the connection manager for subsequent requests. - The easiest way to do so is to call the - HttpEntity#consumeContent() method to consume any - available content on the stream. HttpClient will automatically release the - underlying connection back to the connection manager as soon as it detects that the - end of the content stream has been reached. The - HttpEntity#consumeContent() method is safe to call more - than once. + In order to ensure proper release of system resources one must close the content + stream associated with the entity. + + Please note that HttpEntity#writeTo(OutputStream) + method is also required to ensure proper release of system resources once the + entity has been fully written out. If this method obtains an instance of + java.io.InputStream by calling + HttpEntity#getContent(), it is also expected to close + the stream in a finally clause. + When working with streaming entities, one can use the + EntityUtils#consume(HttpEntity) method to ensure that + the entity content has been fully consumed and the underlying stream has been + closed. There can be situations, however, when only a small portion of the entire response content needs to be retrieved and the performance penalty for consuming the remaining content and making the connection reusable is too high, one can simply