From a396bc4ac7aa5c5efc5a0e22a6f9ce93a723ad8f Mon Sep 17 00:00:00 2001 From: James Abley Date: Thu, 31 Mar 2011 23:50:52 +0100 Subject: [PATCH] HTTPCLIENT-1075: ContentEncodingHttpClient fails when used with BasicResponseHandler --- .../http/client/entity/DecompressingEntity.java | 6 ++++ .../client/entity/DeflateDecompressingEntity.java | 16 +++++++++++ .../client/entity/GzipDecompressingEntity.java | 9 +++--- .../http/impl/client/TestContentCodings.java | 28 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java index ebb7495..5c2d371 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java @@ -45,6 +45,12 @@ abstract class DecompressingEntity extends HttpEntityWrapper { private static final int BUFFER_SIZE = 1024 * 2; /** + * DecompressingEntities are not repeatable, so they will return the same + * InputStream instance when {@link #getContent()} is called. + */ + protected InputStream content; + + /** * Creates a new {@link DecompressingEntity}. * * @param wrapped diff --git a/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java index 9cef5eb..84df134 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java @@ -67,6 +67,22 @@ public class DeflateDecompressingEntity extends DecompressingEntity { */ @Override public InputStream getContent() throws IOException { + if (content == null) { + content = getDecompressingInputStream(); + } + + return content; + } + + /** + * Returns the non-null InputStream that should be returned to by all requests to + * {@link #getContent()}. + * + * @return a non-null InputStream + * @throws IOException if there was a problem + */ + private InputStream getDecompressingInputStream() throws IOException { + InputStream wrapped = this.wrappedEntity.getContent(); /* diff --git a/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java index a1dc7b9..d79b317 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java @@ -56,11 +56,12 @@ public class GzipDecompressingEntity extends DecompressingEntity { */ @Override public InputStream getContent() throws IOException, IllegalStateException { + if (content == null) { + InputStream wrappedin = wrappedEntity.getContent(); + content = new GZIPInputStream(wrappedin); + } - // the wrapped entity's getContent() decides about repeatability - InputStream wrappedin = wrappedEntity.getContent(); - - return new GZIPInputStream(wrappedin); + return content; } /** diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java b/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java index dff256e..0dfea64 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java @@ -313,6 +313,34 @@ public class TestContentCodings extends ServerTestBase { client.getConnectionManager().shutdown(); } + @Test + public void gzipResponsesWorkWithBasicResponseHandler() throws Exception { + final String entityText = "Hello, this is some plain text coming back."; + + this.localServer.register("*", createGzipEncodingRequestHandler(entityText)); + + DefaultHttpClient client = createHttpClient(); + HttpGet request = new HttpGet("/some-resource"); + String response = client.execute(getServerHttp(), request, new BasicResponseHandler()); + Assert.assertEquals("The entity text is correctly transported", entityText, response); + + client.getConnectionManager().shutdown(); + } + + @Test + public void deflateResponsesWorkWithBasicResponseHandler() throws Exception { + final String entityText = "Hello, this is some plain text coming back."; + + this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, false)); + + DefaultHttpClient client = createHttpClient(); + HttpGet request = new HttpGet("/some-resource"); + String response = client.execute(getServerHttp(), request, new BasicResponseHandler()); + Assert.assertEquals("The entity text is correctly transported", entityText, response); + + client.getConnectionManager().shutdown(); + } + /** * Creates a new {@link HttpRequestHandler} that will attempt to provide a deflate stream * Content-Coding. -- 1.7.3.4