hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1570142 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/client/config/ main/java/org/apache/http/client/protocol/ test/java/org/apache/http/client/protocol/
Date Thu, 20 Feb 2014 10:53:52 GMT
Author: olegk
Date: Thu Feb 20 10:53:52 2014
New Revision: 1570142

URL: http://svn.apache.org/r1570142
Log:
HTTPCLIENT-1464: ability to enable / disable content decompression at request level

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java?rev=1570142&r1=1570141&r2=1570142&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
Thu Feb 20 10:53:52 2014
@@ -51,6 +51,7 @@ public class RequestConfig implements Cl
     private final int connectionRequestTimeout;
     private final int connectTimeout;
     private final int socketTimeout;
+    private final boolean decompressionEnabled;
 
     RequestConfig(
             final boolean expectContinueEnabled,
@@ -67,7 +68,8 @@ public class RequestConfig implements Cl
             final Collection<String> proxyPreferredAuthSchemes,
             final int connectionRequestTimeout,
             final int connectTimeout,
-            final int socketTimeout) {
+            final int socketTimeout,
+            final boolean decompressionEnabled) {
         super();
         this.expectContinueEnabled = expectContinueEnabled;
         this.proxy = proxy;
@@ -84,6 +86,7 @@ public class RequestConfig implements Cl
         this.connectionRequestTimeout = connectionRequestTimeout;
         this.connectTimeout = connectTimeout;
         this.socketTimeout = socketTimeout;
+        this.decompressionEnabled = decompressionEnabled;
     }
 
     /**
@@ -263,6 +266,17 @@ public class RequestConfig implements Cl
         return socketTimeout;
     }
 
+    /**
+     * Determines whether compressed entities should be decompressed automatically.
+     * <p/>
+     * Default: <code>true</code>
+     *
+     * @since 4.4
+     */
+    public boolean isDecompressionEnabled() {
+        return decompressionEnabled;
+    }
+
     @Override
     protected RequestConfig clone() throws CloneNotSupportedException {
         return (RequestConfig) super.clone();
@@ -286,6 +300,7 @@ public class RequestConfig implements Cl
         builder.append(", connectionRequestTimeout=").append(connectionRequestTimeout);
         builder.append(", connectTimeout=").append(connectTimeout);
         builder.append(", socketTimeout=").append(socketTimeout);
+        builder.append(", decompressionEnabled=").append(decompressionEnabled);
         builder.append("]");
         return builder.toString();
     }
@@ -310,7 +325,8 @@ public class RequestConfig implements Cl
             .setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
             .setConnectionRequestTimeout(config.getConnectionRequestTimeout())
             .setConnectTimeout(config.getConnectTimeout())
-            .setSocketTimeout(config.getSocketTimeout());
+            .setSocketTimeout(config.getSocketTimeout())
+            .setDecompressionEnabled(config.isDecompressionEnabled());
     }
 
     public static class Builder {
@@ -330,6 +346,7 @@ public class RequestConfig implements Cl
         private int connectionRequestTimeout;
         private int connectTimeout;
         private int socketTimeout;
+        private boolean decompressionEnabled;
 
         Builder() {
             super();
@@ -341,6 +358,7 @@ public class RequestConfig implements Cl
             this.connectionRequestTimeout = -1;
             this.connectTimeout = -1;
             this.socketTimeout = -1;
+            this.decompressionEnabled = true;
         }
 
         public Builder setExpectContinueEnabled(final boolean expectContinueEnabled) {
@@ -418,6 +436,11 @@ public class RequestConfig implements Cl
             return this;
         }
 
+        public Builder setDecompressionEnabled(final boolean decompressionEnabled) {
+            this.decompressionEnabled = decompressionEnabled;
+            return this;
+        }
+
         public RequestConfig build() {
             return new RequestConfig(
                     expectContinueEnabled,
@@ -434,7 +457,8 @@ public class RequestConfig implements Cl
                     proxyPreferredAuthSchemes,
                     connectionRequestTimeout,
                     connectTimeout,
-                    socketTimeout);
+                    socketTimeout,
+                    decompressionEnabled);
         }
 
     }

Modified: 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=1570142&r1=1570141&r2=1570142&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java
Thu Feb 20 10:53:52 2014
@@ -36,6 +36,7 @@ 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.config.RequestConfig;
 import org.apache.http.client.entity.DeflateDecompressingEntity;
 import org.apache.http.client.entity.GzipDecompressingEntity;
 import org.apache.http.protocol.HttpContext;
@@ -74,9 +75,11 @@ public class ResponseContentEncoding imp
             final HttpContext context) throws HttpException, IOException {
         final HttpEntity entity = response.getEntity();
 
+        final HttpClientContext clientContext = HttpClientContext.adapt(context);
+        final RequestConfig requestConfig = clientContext.getRequestConfig();
         // entity can be null in case of 304 Not Modified, 204 No Content or similar
         // check for zero length entity.
-        if (entity != null && entity.getContentLength() != 0) {
+        if (requestConfig.isDecompressionEnabled() && entity != null && entity.getContentLength()
!= 0) {
             final Header ceheader = entity.getContentEncoding();
             if (ceheader != null) {
                 final HeaderElement[] codecs = ceheader.getElements();

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java?rev=1570142&r1=1570141&r2=1570142&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
Thu Feb 20 10:53:52 2014
@@ -31,6 +31,7 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpResponseInterceptor;
 import org.apache.http.HttpVersion;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.entity.DeflateDecompressingEntity;
 import org.apache.http.client.entity.GzipDecompressingEntity;
 import org.apache.http.entity.StringEntity;
@@ -154,4 +155,25 @@ public class TestResponseContentEncoding
         interceptor.process(response, context);
     }
 
+    @Test
+    public void testContentEncodingRequestParameter() throws Exception {
+        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final StringEntity original = new StringEntity("encoded stuff");
+        original.setContentEncoding("GZip");
+        response.setEntity(original);
+
+        final RequestConfig config = RequestConfig.custom()
+                .setDecompressionEnabled(false)
+                .build();
+
+        final HttpContext context = new BasicHttpContext();
+        context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
+
+        final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
+        interceptor.process(response, context);
+        final HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(entity);
+        Assert.assertFalse(entity instanceof GzipDecompressingEntity);
+    }
+
 }



Mime
View raw message