From 20db76c05a95092b85bf25a1386ec49d77629e2b Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 4 Nov 2009 19:16:48 +0000 Subject: [PATCH] HTTPCLIENT-834: GzipDecompressingEntity and DeflateDecompressingEntity classes now override the#writeTo() Contributed by James Abley git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@832847 13f79535-47bb-0310-9956-ffa450edef68 --- .../client/entity/DecompressingEntity.java | 75 +++++++++++++++++++ .../entity/DeflateDecompressingEntity.java | 2 +- .../entity/GzipDecompressingEntity.java | 4 +- .../http/impl/client/TestContentCodings.java | 46 ++++++++++++ 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java 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 new file mode 100644 index 000000000..4107ddd5a --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java @@ -0,0 +1,75 @@ +/* + * ==================================================================== + * + * 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 + * . + * + */ +package org.apache.http.client.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.entity.HttpEntityWrapper; + +/** + * Common base class for decompressing {@link HttpEntity} implementations. + */ +abstract class DecompressingEntity extends HttpEntityWrapper { + + /** + * Default buffer size. + */ + private static final int BUFFER_SIZE = 1024 * 2; + + /** + * Creates a new {@link DecompressingEntity}. + * + * @param wrapped + * the non-null {@link HttpEntity} to be wrapped + */ + public DecompressingEntity(final HttpEntity wrapped) { + super(wrapped); + } + + /** + * {@inheritDoc} + */ + @Override + public void writeTo(OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + + InputStream instream = getContent(); + + byte[] buffer = new byte[BUFFER_SIZE]; + + int l; + + while ((l = instream.read(buffer)) != -1) { + outstream.write(buffer, 0, l); + } + } + +} 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 7e50dd98b..79fee1a5b 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 @@ -45,7 +45,7 @@ import org.apache.http.entity.HttpEntityWrapper; * deflate streams. We handle both types in here, since that's what is seen on the * internet. Moral - prefer gzip! */ -public class DeflateDecompressingEntity extends HttpEntityWrapper { +public class DeflateDecompressingEntity extends DecompressingEntity { /** * Creates a new {@link DeflateDecompressingEntity} which will wrap the specified 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 1aa37f1a5..2c9dd1e7f 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 @@ -22,7 +22,7 @@ * information on the Apache Software Foundation, please see * . * -*/ + */ package org.apache.http.client.entity; import java.io.IOException; @@ -36,7 +36,7 @@ import org.apache.http.entity.HttpEntityWrapper; /** * {@link HttpEntityWrapper} for handling gzip Content Coded responses. */ -public class GzipDecompressingEntity extends HttpEntityWrapper { +public class GzipDecompressingEntity extends DecompressingEntity { /** * Creates a new {@link GzipDecompressingEntity} which will wrap the specified 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 c547786d4..1b917036d 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 @@ -322,6 +322,52 @@ public class TestContentCodings extends ServerTestBase { client.getConnectionManager().shutdown(); } + + /** + * Test that the returned {@link HttpEntity} in the response correctly overrides + * {@link HttpEntity#writeTo(OutputStream)} for gzip-encoding. + * + * @throws Exception + */ + public void testHttpEntityWriteToForGzip() 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"); + HttpResponse response = client.execute(getServerHttp(), request); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + response.getEntity().writeTo(out); + + assertEquals(entityText, out.toString("utf-8")); + + client.getConnectionManager().shutdown(); + } + + /** + * Test that the returned {@link HttpEntity} in the response correctly overrides + * {@link HttpEntity#writeTo(OutputStream)} for deflate-encoding. + * + * @throws Exception + */ + public void testHttpEntityWriteToForDeflate() throws Exception { + final String entityText = "Hello, this is some plain text coming back."; + + this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, true)); + + DefaultHttpClient client = createHttpClient(); + HttpGet request = new HttpGet("/some-resource"); + HttpResponse response = client.execute(getServerHttp(), request); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + response.getEntity().writeTo(out); + + assertEquals(entityText, out.toString("utf-8")); + + client.getConnectionManager().shutdown(); + } /** * Creates a new {@link HttpRequestHandler} that will attempt to provide a deflate stream