HTTPCLIENT-834: GzipDecompressingEntity and DeflateDecompressingEntity classes now override the#writeTo()

Contributed by James Abley <james.abley at gmail.com>

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@832847 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-11-04 19:16:48 +00:00
parent 5c9850a7a9
commit 20db76c05a
4 changed files with 124 additions and 3 deletions

View File

@ -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
* <http://www.apache.org/>.
*
*/
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);
}
}
}

View File

@ -45,7 +45,7 @@ import org.apache.http.entity.HttpEntityWrapper;
* <code>deflate</code> streams. We handle both types in here, since that's what is seen on the
* internet. Moral - prefer <code>gzip</code>!
*/
public class DeflateDecompressingEntity extends HttpEntityWrapper {
public class DeflateDecompressingEntity extends DecompressingEntity {
/**
* Creates a new {@link DeflateDecompressingEntity} which will wrap the specified

View File

@ -22,7 +22,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
*/
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

View File

@ -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