HTTPCLIENT-834: Transparent Content Coding support

* ContentEncodingProcessor split into RequestAcceptEncoding and ResponseContentEncoding protocol interceptors 
 
Contributed by James Abley <james.abley at gmail.com>


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@816583 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-09-18 10:59:35 +00:00
parent fb6d73ef38
commit 39f46a9c2b
5 changed files with 154 additions and 113 deletions

View File

@ -1,106 +0,0 @@
/*
* ====================================================================
*
* 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.protocol;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
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;
/**
* Class responsible for handling Content Encoding in HTTP. This takes the form of
* an {@link HttpRequestInterceptor} that will modify the {@link HttpRequest} if the client hasn't
* already specified an <code>Accept-Encoding</code> header. There is an accompanying
* {@link HttpResponseInterceptor} implementation that will only examine the {@link HttpResponse}
* if the {@link HttpRequestInterceptor} implementation did any modifications.
* <p>
* Instances of this class are stateless, therefore they're thread-safe and immutable.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5
*/
public class ContentEncodingProcessor
implements HttpResponseInterceptor, HttpRequestInterceptor {
/**
* {@inheritDoc}
*/
public void process(
HttpRequest request, HttpContext context) throws HttpException, IOException {
/*
* If a client of this library has already set this header, presume that they did so for
* a reason and so this instance shouldn't handle the response at all.
*/
if (!request.containsHeader("Accept-Encoding")) {
/* Signal support for Accept-Encoding transfer encodings. */
// TODO add compress support.
request.addHeader("Accept-Encoding", "gzip,deflate");
/* Store the fact that the request was modified, so that we can potentially handle
* the response. */
context.setAttribute(ContentEncodingProcessor.class.getName(), Boolean.TRUE);
}
}
/**
* {@inheritDoc}
*/
public void process(
HttpResponse response, HttpContext context) throws HttpException, IOException {
if (context.getAttribute(ContentEncodingProcessor.class.getName()) != null) {
HttpEntity entity = response.getEntity();
if (entity != null) { // It wasn't a 304 Not Modified response, 204 No Content or similar
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0, n = codecs.length; i < n; ++i) {
if ("gzip".equalsIgnoreCase(codecs[i].getName())) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
} else if ("deflate".equalsIgnoreCase(codecs[i].getName())) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
return;
}
// TODO add compress. identity is a no-op.
}
}
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* ====================================================================
*
* 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.protocol;
import java.io.IOException;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.annotation.Immutable;
import org.apache.http.protocol.HttpContext;
/**
* Class responsible for handling Content Encoding requests in HTTP.
* <p>
* Instances of this class are stateless, therefore they're thread-safe and immutable.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5
*
* @since 4.1
*/
@Immutable
public class RequestAcceptEncoding implements HttpRequestInterceptor {
/**
* {@inheritDoc}
*/
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
/* Signal support for Accept-Encoding transfer encodings. */
request.addHeader("Accept-Encoding", "gzip,deflate");
}
}

View File

@ -0,0 +1,86 @@
/*
* ====================================================================
*
* 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.protocol;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.annotation.Immutable;
import org.apache.http.client.entity.DeflateDecompressingEntity;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.protocol.HttpContext;
/**
* {@link HttpResponseInterceptor} responsible for processing Content-Encoding
* responses.
* <p>
* Instances of this class are stateless and immutable, therefore threadsafe.
*
* @since 4.1
*
*/
@Immutable
public class ResponseContentEncoding implements HttpResponseInterceptor {
/**
* {@inheritDoc}
*/
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
// It wasn't a 304 Not Modified response, 204 No Content or similar
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (HeaderElement codec : codecs) {
if ("gzip".equalsIgnoreCase(codec.getName())) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
} else if ("deflate".equalsIgnoreCase(codec.getName())) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
return;
} else if ("identity".equalsIgnoreCase(codec.getName())) {
/* Don't need to transform the content - no-op */
return;
} else {
throw new HttpException("Unsupported Content-Coding: " + codec.getName());
}
}
}
}
}
}

View File

@ -26,14 +26,17 @@
package org.apache.http.impl.client;
import org.apache.http.client.protocol.ContentEncodingProcessor;
import org.apache.http.client.protocol.RequestAcceptEncoding;
import org.apache.http.client.protocol.ResponseContentEncoding;
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}
* {@link DefaultHttpClient} sub-class which includes a {@link RequestAcceptEncoding}
* for the request and response.
*
* @since 4.1
*/
public class ContentEncodingHttpClient extends DefaultHttpClient {
@ -68,9 +71,9 @@ public class ContentEncodingHttpClient extends DefaultHttpClient {
protected BasicHttpProcessor createHttpProcessor() {
BasicHttpProcessor result = super.createHttpProcessor();
ContentEncodingProcessor ceProcessor = new ContentEncodingProcessor();
result.addRequestInterceptor(ceProcessor);
result.addResponseInterceptor(ceProcessor);
result.addRequestInterceptor(new RequestAcceptEncoding());
result.addResponseInterceptor(new ResponseContentEncoding());
return result;
}

View File

@ -54,7 +54,7 @@ import org.apache.http.HttpStatus;
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.client.protocol.RequestAcceptEncoding;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
@ -322,7 +322,7 @@ public class TestContentCodings extends ServerTestBase {
AbstractHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
client.removeRequestInterceptorByClass(ContentEncodingProcessor.class);
client.removeRequestInterceptorByClass(RequestAcceptEncoding.class);
HttpResponse response = client.execute(getServerHttp(), request);