hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1571635 - in /httpcomponents/httpclient/trunk: ./ httpclient/src/main/java/org/apache/http/client/entity/ httpclient/src/main/java/org/apache/http/client/protocol/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/test/j...
Date Tue, 25 Feb 2014 09:49:47 GMT
Author: olegk
Date: Tue Feb 25 09:49:47 2014
New Revision: 1571635

URL: http://svn.apache.org/r1571635
Log:
HTTPCLIENT-1403: Pluggable content decoders

Added:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/InputStreamFactory.java
      - copied, changed from r1571537, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Tue Feb 25 09:49:47 2014
@@ -4,7 +4,11 @@ Changes for 4.4-alpha1
 Changelog:
 -------------------
 
-* [HTTPCLIENT-1470] CachingExec(ClientExecChain, HttpCache, CacheConfig, AsynchronousValidator)
throws NPE if config is null
+* [HTTPCLIENT-1403] Pluggable content decoders.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
+* [HTTPCLIENT-1470] CachingExec(ClientExecChain, HttpCache, CacheConfig, AsynchronousValidator)

+  throws NPE if config is null.
 
 * [HTTPCLIENT-1466] FileBodyPart#generateContentType() ignores custom ContentType values.
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
Tue Feb 25 09:49:47 2014
@@ -30,6 +30,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.entity.HttpEntityWrapper;
 import org.apache.http.util.Args;
@@ -37,15 +38,16 @@ import org.apache.http.util.Args;
 /**
  * Common base class for decompressing {@link HttpEntity} implementations.
  *
- * @since 4.1
+ * @since 4.4
  */
-abstract class DecompressingEntity extends HttpEntityWrapper {
+public class DecompressingEntity extends HttpEntityWrapper {
 
     /**
      * Default buffer size.
      */
     private static final int BUFFER_SIZE = 1024 * 2;
 
+    private final InputStreamFactory inputStreamFactory;
     /**
      * {@link #getContent()} method must return the same {@link InputStream}
      * instance when DecompressingEntity is wrapping a streaming entity.
@@ -55,23 +57,21 @@ abstract class DecompressingEntity exten
     /**
      * Creates a new {@link DecompressingEntity}.
      *
-     * @param wrapped
-     *            the non-null {@link HttpEntity} to be wrapped
+     * @param wrapped the non-null {@link HttpEntity} to be wrapped
+     * @param inputStreamFactory factory to create decompressing stream.
      */
-    public DecompressingEntity(final HttpEntity wrapped) {
+    public DecompressingEntity(
+            final HttpEntity wrapped,
+            final InputStreamFactory inputStreamFactory) {
         super(wrapped);
+        this.inputStreamFactory = inputStreamFactory;
     }
 
-    abstract InputStream decorate(final InputStream wrapped) throws IOException;
-
     private InputStream getDecompressingStream() throws IOException {
         final InputStream in = wrappedEntity.getContent();
-        return new LazyDecompressingInputStream(in, this);
+        return new LazyDecompressingInputStream(in, inputStreamFactory);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public InputStream getContent() throws IOException {
         if (wrappedEntity.isStreaming()) {
@@ -84,9 +84,6 @@ abstract class DecompressingEntity exten
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void writeTo(final OutputStream outstream) throws IOException {
         Args.notNull(outstream, "Output stream");
@@ -102,4 +99,16 @@ abstract class DecompressingEntity exten
         }
     }
 
+    @Override
+    public Header getContentEncoding() {
+        /* Content encoding is now 'identity' */
+        return null;
+    }
+
+    @Override
+    public long getContentLength() {
+        /* length of decompressed content is not known */
+        return -1;
+    }
+
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
Tue Feb 25 09:49:47 2014
@@ -29,7 +29,6 @@ package org.apache.http.client.entity;
 import java.io.IOException;
 import java.io.InputStream;
 
-import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 
 /**
@@ -58,39 +57,14 @@ public class DeflateDecompressingEntity 
      *            a non-null {@link HttpEntity} to be wrapped
      */
     public DeflateDecompressingEntity(final HttpEntity entity) {
-        super(entity);
-    }
+        super(entity, new InputStreamFactory() {
 
-    /**
-     * Returns the non-null InputStream that should be returned to by all requests to
-     * {@link #getContent()}.
-     *
-     * @return a non-null InputStream
-     * @throws IOException if there was a problem
-     */
-    @Override
-    InputStream decorate(final InputStream wrapped) throws IOException {
-        return new DeflateInputStream(wrapped);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Header getContentEncoding() {
-
-        /* This HttpEntityWrapper has dealt with the Content-Encoding. */
-        return null;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public long getContentLength() {
+            @Override
+            public InputStream create(final InputStream instream) throws IOException {
+                return new DeflateInputStream(instream);
+            }
 
-        /* Length of inflated content is unknown. */
-        return -1;
+        });
     }
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
Tue Feb 25 09:49:47 2014
@@ -30,7 +30,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.zip.GZIPInputStream;
 
-import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 
 /**
@@ -49,32 +48,14 @@ public class GzipDecompressingEntity ext
      *            the non-null {@link HttpEntity} to be wrapped
      */
     public GzipDecompressingEntity(final HttpEntity entity) {
-        super(entity);
-    }
-
-    @Override
-    InputStream decorate(final InputStream wrapped) throws IOException {
-        return new GZIPInputStream(wrapped);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Header getContentEncoding() {
-
-        /* This HttpEntityWrapper has dealt with the Content-Encoding. */
-        return null;
-    }
+        super(entity, new InputStreamFactory() {
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public long getContentLength() {
+            @Override
+            public InputStream create(final InputStream instream) throws IOException {
+                return new GZIPInputStream(instream);
+            }
 
-        /* length of ungzipped content is not known */
-        return -1;
+        });
     }
 
 }

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/InputStreamFactory.java
(from r1571537, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/InputStreamFactory.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/InputStreamFactory.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java&r1=1571537&r2=1571635&rev=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/InputStreamFactory.java
Tue Feb 25 09:49:47 2014
@@ -28,53 +28,14 @@ package org.apache.http.client.entity;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.zip.GZIPInputStream;
-
-import org.apache.http.Header;
-import org.apache.http.HttpEntity;
 
 /**
- * {@link org.apache.http.entity.HttpEntityWrapper} for handling gzip
- * Content Coded responses.
+ * Factory for decorated {@link java.io.InputStream}s.
  *
- * @since 4.1
+ * @since 4.4
  */
-public class GzipDecompressingEntity extends DecompressingEntity {
-
-    /**
-     * Creates a new {@link GzipDecompressingEntity} which will wrap the specified
-     * {@link HttpEntity}.
-     *
-     * @param entity
-     *            the non-null {@link HttpEntity} to be wrapped
-     */
-    public GzipDecompressingEntity(final HttpEntity entity) {
-        super(entity);
-    }
-
-    @Override
-    InputStream decorate(final InputStream wrapped) throws IOException {
-        return new GZIPInputStream(wrapped);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Header getContentEncoding() {
-
-        /* This HttpEntityWrapper has dealt with the Content-Encoding. */
-        return null;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public long getContentLength() {
+public interface InputStreamFactory {
 
-        /* length of ungzipped content is not known */
-        return -1;
-    }
+    InputStream create(InputStream instream) throws IOException;
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java
Tue Feb 25 09:49:47 2014
@@ -26,11 +26,11 @@
  */
 package org.apache.http.client.entity;
 
-import org.apache.http.annotation.NotThreadSafe;
-
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.http.annotation.NotThreadSafe;
+
 /**
  * Lazy init InputStream wrapper.
  */
@@ -38,21 +38,20 @@ import java.io.InputStream;
 class LazyDecompressingInputStream extends InputStream {
 
     private final InputStream wrappedStream;
-
-    private final DecompressingEntity decompressingEntity;
+    private final InputStreamFactory inputStreamFactory;
 
     private InputStream wrapperStream;
 
     public LazyDecompressingInputStream(
             final InputStream wrappedStream,
-            final DecompressingEntity decompressingEntity) {
+            final InputStreamFactory inputStreamFactory) {
         this.wrappedStream = wrappedStream;
-        this.decompressingEntity = decompressingEntity;
+        this.inputStreamFactory = inputStreamFactory;
     }
 
     private void initWrapper() throws IOException {
         if (wrapperStream == null) {
-            wrapperStream = decompressingEntity.decorate(wrappedStream);
+            wrapperStream = inputStreamFactory.create(wrappedStream);
         }
     }
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAcceptEncoding.java
Tue Feb 25 09:49:47 2014
@@ -27,6 +27,7 @@
 package org.apache.http.client.protocol;
 
 import java.io.IOException;
+import java.util.List;
 
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
@@ -46,9 +47,30 @@ import org.apache.http.protocol.HttpCont
 @Immutable
 public class RequestAcceptEncoding implements HttpRequestInterceptor {
 
+    private final String acceptEncoding;
+
     /**
-     * Adds the header {@code "Accept-Encoding: gzip,deflate"} to the request.
+     * @since 4.4
      */
+    public RequestAcceptEncoding(final List<String> encodings) {
+        if (encodings != null && !encodings.isEmpty()) {
+            final StringBuilder buf = new StringBuilder();
+            for (int i = 0; i < encodings.size(); i++) {
+                if (i > 0) {
+                    buf.append(",");
+                }
+                buf.append(encodings.get(i));
+            }
+            this.acceptEncoding = buf.toString();
+        } else {
+            this.acceptEncoding = "gzip,deflate";
+        }
+    }
+
+    public RequestAcceptEncoding() {
+        this(null);
+    }
+
     @Override
     public void process(
             final HttpRequest request,
@@ -56,7 +78,7 @@ public class RequestAcceptEncoding imple
 
         /* Signal support for Accept-Encoding transfer encodings. */
         if (!request.containsHeader("Accept-Encoding")) {
-            request.addHeader("Accept-Encoding", "gzip,deflate");
+            request.addHeader("Accept-Encoding", acceptEncoding);
         }
     }
 

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=1571635&r1=1571634&r2=1571635&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
Tue Feb 25 09:49:47 2014
@@ -27,7 +27,9 @@
 package org.apache.http.client.protocol;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Locale;
+import java.util.zip.GZIPInputStream;
 
 import org.apache.http.Header;
 import org.apache.http.HeaderElement;
@@ -37,8 +39,11 @@ 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.client.entity.DecompressingEntity;
+import org.apache.http.client.entity.DeflateInputStream;
+import org.apache.http.client.entity.InputStreamFactory;
+import org.apache.http.config.Lookup;
+import org.apache.http.config.RegistryBuilder;
 import org.apache.http.protocol.HttpContext;
 
 /**
@@ -55,20 +60,49 @@ public class ResponseContentEncoding imp
 
     public static final String UNCOMPRESSED = "http.client.response.uncompressed";
 
+    private final static InputStreamFactory GZIP = new InputStreamFactory() {
+
+        @Override
+        public InputStream create(final InputStream instream) throws IOException {
+            return new GZIPInputStream(instream);
+        }
+    };
+
+    private final static InputStreamFactory DEFLATE = new InputStreamFactory() {
+
+        @Override
+        public InputStream create(final InputStream instream) throws IOException {
+            return new DeflateInputStream(instream);
+        }
+
+    };
+
+    private final Lookup<InputStreamFactory> decoderRegistry;
+
     /**
-     * Handles the following {@code Content-Encoding}s by
-     * using the appropriate decompressor to wrap the response Entity:
+     * @since 4.4
+     */
+    public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry)
{
+        this.decoderRegistry = decoderRegistry != null ? decoderRegistry :
+            RegistryBuilder.<InputStreamFactory>create()
+                    .register("gzip", GZIP)
+                    .register("x-gzip", GZIP)
+                    .register("deflate", DEFLATE)
+                    .build();
+    }
+
+    /**
+     * Handles <tt>gzip</tt> and <tt>deflate</tt> compressed entities
by using the following
+     * decoders:
      * <ul>
-     * <li>gzip - see {@link GzipDecompressingEntity}</li>
-     * <li>deflate - see {@link DeflateDecompressingEntity}</li>
-     * <li>identity - no action needed</li>
+     * <li>gzip - see {@link GZIPInputStream}</li>
+     * <li>deflate - see {@link DeflateInputStream}</li>
      * </ul>
-     *
-     * @param response the response which contains the entity
-     * @param  context not currently used
-     *
-     * @throws HttpException if the {@code Content-Encoding} is none of the above
      */
+    public ResponseContentEncoding() {
+        this(null);
+    }
+
     @Override
     public void process(
             final HttpResponse response,
@@ -83,30 +117,20 @@ public class ResponseContentEncoding imp
             final Header ceheader = entity.getContentEncoding();
             if (ceheader != null) {
                 final HeaderElement[] codecs = ceheader.getElements();
-                boolean uncompressed = false;
                 for (final HeaderElement codec : codecs) {
-                    final String codecname = codec.getName().toLowerCase(Locale.US);
-                    if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
-                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
-                        uncompressed = true;
-                        break;
-                    } else if ("deflate".equals(codecname)) {
-                        response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
-                        uncompressed = true;
-                        break;
-                    } else if ("identity".equals(codecname)) {
-
-                        /* Don't need to transform the content - no-op */
-                        return;
+                    final String codecname = codec.getName().toLowerCase(Locale.ROOT);
+                    final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname);
+                    if (decoderFactory != null) {
+                        response.setEntity(new DecompressingEntity(response.getEntity(),
decoderFactory));
+                        response.removeHeaders("Content-Length");
+                        response.removeHeaders("Content-Encoding");
+                        response.removeHeaders("Content-MD5");
                     } else {
-                        throw new HttpException("Unsupported Content-Coding: " + codec.getName());
+                        if (!"identity".equals(codecname)) {
+                            throw new HttpException("Unsupported Content-Coding: " + codec.getName());
+                        }
                     }
                 }
-                if (uncompressed) {
-                    response.removeHeaders("Content-Length");
-                    response.removeHeaders("Content-Encoding");
-                    response.removeHeaders("Content-MD5");
-                }
             }
         }
     }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
Tue Feb 25 09:49:47 2014
@@ -31,8 +31,10 @@ import java.io.Closeable;
 import java.net.ProxySelector;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSocketFactory;
@@ -56,6 +58,7 @@ import org.apache.http.client.UserTokenH
 import org.apache.http.client.config.AuthSchemes;
 import org.apache.http.client.config.CookieSpecs;
 import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.entity.InputStreamFactory;
 import org.apache.http.client.protocol.RequestAcceptEncoding;
 import org.apache.http.client.protocol.RequestAddCookies;
 import org.apache.http.client.protocol.RequestAuthCache;
@@ -177,6 +180,7 @@ public class HttpClientBuilder {
     private ServiceUnavailableRetryStrategy serviceUnavailStrategy;
     private Lookup<AuthSchemeProvider> authSchemeRegistry;
     private Lookup<CookieSpecProvider> cookieSpecRegistry;
+    private Map<String, InputStreamFactory> contentDecoderMap;
     private CookieStore cookieStore;
     private CredentialsProvider credentialsProvider;
     private String userAgent;
@@ -636,6 +640,17 @@ public class HttpClientBuilder {
         return this;
     }
 
+
+    /**
+     * Assigns a map of {@link org.apache.http.client.entity.InputStreamFactory}s
+     * to be used for automatic content decompression.
+     */
+    public final HttpClientBuilder setContentDecoderRegistry(
+            final Map<String, InputStreamFactory> contentDecoderMap) {
+        this.contentDecoderMap = contentDecoderMap;
+        return this;
+    }
+
     /**
      * Assigns default {@link RequestConfig} instance which will be used
      * for request execution if not explicitly set in the client execution
@@ -831,7 +846,13 @@ public class HttpClientBuilder {
                 b.add(new RequestAddCookies());
             }
             if (!contentCompressionDisabled) {
-                b.add(new RequestAcceptEncoding());
+                if (contentDecoderMap != null) {
+                    final List<String> encodings = new ArrayList<String>(contentDecoderMap.keySet());
+                    Collections.sort(encodings);
+                    b.add(new RequestAcceptEncoding(encodings));
+                } else {
+                    b.add(new RequestAcceptEncoding());
+                }
             }
             if (!authCachingDisabled) {
                 b.add(new RequestAuthCache());
@@ -840,7 +861,15 @@ public class HttpClientBuilder {
                 b.add(new ResponseProcessCookies());
             }
             if (!contentCompressionDisabled) {
-                b.add(new ResponseContentEncoding());
+                if (contentDecoderMap != null) {
+                    final RegistryBuilder<InputStreamFactory> b2 = RegistryBuilder.create();
+                    for (Map.Entry<String, InputStreamFactory> entry: contentDecoderMap.entrySet())
{
+                        b2.register(entry.getKey(), entry.getValue());
+                    }
+                    b.add(new ResponseContentEncoding(b2.build()));
+                } else {
+                    b.add(new ResponseContentEncoding());
+                }
             }
             if (requestLast != null) {
                 for (final HttpRequestInterceptor i: requestLast) {

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java?rev=1571635&r1=1571634&r2=1571635&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
Tue Feb 25 09:49:47 2014
@@ -93,16 +93,15 @@ public class TestDecompressingEntity {
 
     static class ChecksumEntity extends DecompressingEntity {
 
-        private final Checksum checksum;
-
         public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
-            super(wrapped);
-            this.checksum = checksum;
-        }
+            super(wrapped, new InputStreamFactory() {
+
+                @Override
+                public InputStream create(final InputStream instream) throws IOException
{
+                    return new CheckedInputStream(instream, checksum);
+                }
 
-        @Override
-        InputStream decorate(final InputStream wrapped) throws IOException {
-            return new CheckedInputStream(wrapped, this.checksum);
+            });
         }
 
     }

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=1571635&r1=1571634&r2=1571635&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
Tue Feb 25 09:49:47 2014
@@ -32,7 +32,7 @@ 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.DecompressingEntity;
 import org.apache.http.client.entity.GzipDecompressingEntity;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.message.BasicHttpResponse;
@@ -80,7 +80,7 @@ public class TestResponseContentEncoding
         interceptor.process(response, context);
         final HttpEntity entity = response.getEntity();
         Assert.assertNotNull(entity);
-        Assert.assertTrue(entity instanceof GzipDecompressingEntity);
+        Assert.assertTrue(entity instanceof DecompressingEntity);
     }
 
     @Test
@@ -110,7 +110,7 @@ public class TestResponseContentEncoding
         interceptor.process(response, context);
         final HttpEntity entity = response.getEntity();
         Assert.assertNotNull(entity);
-        Assert.assertTrue(entity instanceof GzipDecompressingEntity);
+        Assert.assertTrue(entity instanceof DecompressingEntity);
     }
 
     @Test
@@ -125,7 +125,7 @@ public class TestResponseContentEncoding
         interceptor.process(response, context);
         final HttpEntity entity = response.getEntity();
         Assert.assertNotNull(entity);
-        Assert.assertTrue(entity instanceof DeflateDecompressingEntity);
+        Assert.assertTrue(entity instanceof DecompressingEntity);
     }
 
     @Test



Mime
View raw message