You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2009/09/18 12:59:35 UTC
svn commit: r816583 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/client/protocol/
main/java/org/apache/http/impl/client/ test/java/org/apache/http/impl/client/
Author: olegk
Date: Fri Sep 18 10:59:35 2009
New Revision: 816583
URL: http://svn.apache.org/viewvc?rev=816583&view=rev
Log:
HTTPCLIENT-834: Transparent Content Coding support
* ContentEncodingProcessor split into RequestAcceptEncoding and ResponseContentEncoding protocol interceptors
Contributed by James Abley <james.abley at gmail.com>
Added:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java
- copied, changed from r816293, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java (with props)
Removed:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java
Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java (from r816293, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java&r1=816293&r2=816583&rev=816583&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java Fri Sep 18 10:59:35 2009
@@ -21,86 +21,38 @@
* 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.annotation.Immutable;
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.
+ * 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
*/
-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);
- }
- }
-
+@Immutable
+public class RequestAcceptEncoding implements HttpRequestInterceptor {
+
/**
* {@inheritDoc}
*/
public void process(
- HttpResponse response, HttpContext context) throws HttpException, IOException {
-
- if (context.getAttribute(ContentEncodingProcessor.class.getName()) != null) {
- HttpEntity entity = response.getEntity();
+ final HttpRequest request,
+ final HttpContext context) throws HttpException, IOException {
- 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.
- }
- }
- }
- }
+ /* Signal support for Accept-Encoding transfer encodings. */
+ request.addHeader("Accept-Encoding", "gzip,deflate");
}
}
Added: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java?rev=816583&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java Fri Sep 18 10:59:35 2009
@@ -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());
+ }
+ }
+ }
+ }
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java?rev=816583&r1=816582&r2=816583&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java Fri Sep 18 10:59:35 2009
@@ -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 @@
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;
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java?rev=816583&r1=816582&r2=816583&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java Fri Sep 18 10:59:35 2009
@@ -54,7 +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.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 @@
AbstractHttpClient client = createHttpClient();
HttpGet request = new HttpGet("/some-resource");
- client.removeRequestInterceptorByClass(ContentEncodingProcessor.class);
+ client.removeRequestInterceptorByClass(RequestAcceptEncoding.class);
HttpResponse response = client.execute(getServerHttp(), request);