HTTPCLIENT-834: Added ContentEncodingHttpClient

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

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@814281 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-09-13 08:32:12 +00:00
parent 2055915036
commit 7a959d6306
5 changed files with 146 additions and 24 deletions

View File

@ -21,11 +21,8 @@
* 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.impl.client;
*/
package org.apache.http.client.entity;
import java.io.IOException;
import java.io.InputStream;
@ -47,7 +44,7 @@
* <code>deflate</code> streams. We handle both types in here, since that's what is seen on the
* internet. Moral - prefer <code>gzip</code>!
*/
class DeflateDecompressingEntity extends HttpEntityWrapper {
public class DeflateDecompressingEntity extends HttpEntityWrapper {
/**
* Creates a new {@link DeflateDecompressingEntity} which will wrap the specified

View File

@ -21,11 +21,8 @@
* 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.impl.client;
*/
package org.apache.http.client.entity;
import java.io.IOException;
import java.io.InputStream;
@ -38,7 +35,7 @@
/**
* {@link HttpEntityWrapper} for handling gzip Content Coded responses.
*/
class GzipDecompressingEntity extends HttpEntityWrapper {
public class GzipDecompressingEntity extends HttpEntityWrapper {
/**
* Creates a new {@link GzipDecompressingEntity} which will wrap the specified

View File

@ -21,11 +21,8 @@
* 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.impl.client;
*/
package org.apache.http.client.protocol;
import java.io.IOException;
@ -37,6 +34,8 @@
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.entity.DeflateDecompressingEntity;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.protocol.HttpContext;
/**
@ -50,7 +49,8 @@
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5
*/
class ContentEncodingProcessor implements HttpResponseInterceptor, HttpRequestInterceptor {
public class ContentEncodingProcessor
implements HttpResponseInterceptor, HttpRequestInterceptor {
/**
* {@inheritDoc}

View File

@ -0,0 +1,77 @@
/*
* ====================================================================
* 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.impl.client;
import org.apache.http.client.protocol.ContentEncodingProcessor;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
/**
* {@link DefaultHttpClient} sub-class which includes a {@link ContentEncodingProcessor}
* for the request and response.
*/
public class ContentEncodingHttpClient extends DefaultHttpClient {
/**
* Creates a new HTTP client from parameters and a connection manager.
*
* @param params the parameters
* @param conman the connection manager
*/
public ContentEncodingHttpClient(ClientConnectionManager conman, HttpParams params) {
super(conman, params);
}
/**
* @param params
*/
public ContentEncodingHttpClient(HttpParams params) {
this(null, params);
}
/**
*
*/
public ContentEncodingHttpClient() {
this(null);
}
/**
* {@inheritDoc}
*/
@Override
protected BasicHttpProcessor createHttpProcessor() {
BasicHttpProcessor result = super.createHttpProcessor();
ContentEncodingProcessor ceProcessor = new ContentEncodingProcessor();
result.addRequestInterceptor(ceProcessor);
result.addResponseInterceptor(ceProcessor);
return result;
}
}

View File

@ -54,6 +54,7 @@
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ContentEncodingProcessor;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
@ -102,7 +103,7 @@ public void handle(
}
});
DefaultHttpClient client = new DefaultHttpClient();
DefaultHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
HttpResponse response = client.execute(getServerHttp(), request);
@ -124,7 +125,7 @@ public void testDeflateSupportForServerReturningRfc1950Stream() throws Exception
this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, false));
DefaultHttpClient client = new DefaultHttpClient();
DefaultHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
HttpResponse response = client.execute(getServerHttp(), request);
@ -146,7 +147,7 @@ public void testDeflateSupportForServerReturningRfc1951Stream() throws Exception
this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, true));
DefaultHttpClient client = new DefaultHttpClient();
DefaultHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
HttpResponse response = client.execute(getServerHttp(), request);
assertEquals("The entity text is correctly transported", entityText,
@ -165,7 +166,7 @@ public void testGzipSupport() throws Exception {
this.localServer.register("*", createGzipEncodingRequestHandler(entityText));
DefaultHttpClient client = new DefaultHttpClient();
DefaultHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
HttpResponse response = client.execute(getServerHttp(), request);
assertEquals("The entity text is correctly transported", entityText,
@ -231,12 +232,18 @@ public void testThreadSafetyOfContentCodings() throws Exception {
}
}
public void testExistingProtocolInterceptorsAreNotAffected() throws Exception {
/**
* This test is no longer relevant, since the functionality has been added via a new subclass of
* {@link DefaultHttpClient} rather than changing the existing class.
*
* @throws Exception
*/
public void removedTestExistingProtocolInterceptorsAreNotAffected() throws Exception {
final String entityText = "Hello, this is some plain text coming back.";
this.localServer.register("*", createGzipEncodingRequestHandler(entityText));
DefaultHttpClient client = new DefaultHttpClient();
DefaultHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
client.addRequestInterceptor(new HttpRequestInterceptor() {
@ -285,7 +292,47 @@ public void process(
client.getConnectionManager().shutdown();
}
/**
* Checks that we can turn off the new Content-Coding support. The default is that it's on, but that is a change
* to existing behaviour and might not be desirable in some situations.
*
* @throws Exception
*/
public void testCanBeDisabledAtRequestTime() throws Exception {
final String entityText = "Hello, this is some plain text coming back.";
/* Assume that server will see an Accept-Encoding header. */
final boolean [] sawAcceptEncodingHeader = { true };
this.localServer.register("*", new HttpRequestHandler() {
/**
* {@inheritDoc}
*/
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
response.setEntity(new StringEntity(entityText));
response.addHeader("Content-Type", "text/plain");
Header[] acceptEncodings = request.getHeaders("Accept-Encoding");
sawAcceptEncodingHeader[0] = acceptEncodings.length > 0;
}
});
AbstractHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
client.removeRequestInterceptorByClass(ContentEncodingProcessor.class);
HttpResponse response = client.execute(getServerHttp(), request);
assertFalse("The Accept-Encoding header was not there", sawAcceptEncodingHeader[0]);
assertEquals("The entity isn't treated as gzip or zip content", entityText,
EntityUtils.toString(response.getEntity()));
client.getConnectionManager().shutdown();
}
/**
* Creates a new {@link HttpRequestHandler} that will attempt to provide a deflate stream
* Content-Coding.
@ -402,6 +449,10 @@ public void handle(
};
}
private DefaultHttpClient createHttpClient() {
return new ContentEncodingHttpClient();
}
/**
* Sub-ordinate task passed off to a different thread to be executed.
*