From fd30e924982a61dcde244b812f3561cdd23563d8 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Tue, 15 Jul 2025 11:01:29 +0200 Subject: [PATCH 1/2] Deprecate setContentDecoderRegistry to populate contentDecoder Adapt InputStreamFactory map into contentDecoder via DecompressingEntity adapters --- .../http/impl/classic/HttpClientBuilder.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java index d3c8b05edd..ef0b4e0979 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java @@ -28,6 +28,7 @@ package org.apache.hc.client5.http.impl.classic; import java.io.Closeable; +import java.io.InputStream; import java.net.ProxySelector; import java.security.AccessController; import java.security.PrivilegedAction; @@ -58,6 +59,8 @@ import org.apache.hc.client5.http.entity.InputStreamFactory; import org.apache.hc.client5.http.entity.compress.ContentCodecRegistry; import org.apache.hc.client5.http.entity.compress.ContentCoding; +import org.apache.hc.client5.http.entity.compress.DecompressingEntity; +import org.apache.hc.client5.http.entity.compress.IOFunction; import org.apache.hc.client5.http.impl.ChainElement; import org.apache.hc.client5.http.impl.CookieSpecSupport; import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy; @@ -215,8 +218,6 @@ private ExecInterceptorEntry( private BackoffManager backoffManager; private Lookup authSchemeRegistry; private Lookup cookieSpecRegistry; - @Deprecated - private LinkedHashMap contentDecoderMap; private CookieStore cookieStore; private CredentialsProvider credentialsProvider; private String userAgent; @@ -706,11 +707,24 @@ public final HttpClientBuilder setDefaultCookieSpecRegistry( * Sets a map of {@link org.apache.hc.client5.http.entity.InputStreamFactory}s * to be used for automatic content decompression. * + * @deprecated Populate via {@link #setContentDecoder(LinkedHashMap)} + * * @return this instance. */ + @Deprecated public final HttpClientBuilder setContentDecoderRegistry( final LinkedHashMap contentDecoderMap) { - this.contentDecoderMap = contentDecoderMap; + if (contentDecoderMap != null) { + final LinkedHashMap> map = new LinkedHashMap<>(); + for (final Map.Entry e : contentDecoderMap.entrySet()) { + final ContentCoding coding = ContentCoding.fromToken(e.getKey()); + if (coding != null) { + final IOFunction decoderFunc = e.getValue()::create; + map.put(coding, srcEntity -> new DecompressingEntity(srcEntity, decoderFunc)); + } + } + this.contentDecoder = map; + } return this; } From 1520bb2582696ca6e5277d6abf1b6af87fb9f67d Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Wed, 16 Jul 2025 12:49:23 +0200 Subject: [PATCH 2/2] Allow cannot plug-in 3rd party coders using String as a key instead. --- .../http/impl/classic/HttpClientBuilder.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java index ef0b4e0979..92475b9cad 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java @@ -244,7 +244,7 @@ private ExecInterceptorEntry( * Custom decoders keyed by {@link ContentCoding}. * */ - private LinkedHashMap> contentDecoder; + private LinkedHashMap> contentDecoder; public static HttpClientBuilder create() { return new HttpClientBuilder(); @@ -715,13 +715,10 @@ public final HttpClientBuilder setDefaultCookieSpecRegistry( public final HttpClientBuilder setContentDecoderRegistry( final LinkedHashMap contentDecoderMap) { if (contentDecoderMap != null) { - final LinkedHashMap> map = new LinkedHashMap<>(); + final LinkedHashMap> map = new LinkedHashMap<>(); for (final Map.Entry e : contentDecoderMap.entrySet()) { - final ContentCoding coding = ContentCoding.fromToken(e.getKey()); - if (coding != null) { - final IOFunction decoderFunc = e.getValue()::create; - map.put(coding, srcEntity -> new DecompressingEntity(srcEntity, decoderFunc)); - } + final IOFunction decoderFunc = e.getValue()::create; + map.put(e.getKey(), srcEntity -> new DecompressingEntity(srcEntity, decoderFunc)); } this.contentDecoder = map; } @@ -739,7 +736,7 @@ public final HttpClientBuilder setContentDecoderRegistry( * @since 5.6 */ public final HttpClientBuilder setContentDecoder( - final LinkedHashMap> contentDecoder) { + final LinkedHashMap> contentDecoder) { this.contentDecoder = contentDecoder; return this; } @@ -1009,8 +1006,8 @@ public CloseableHttpClient build() { if (contentDecoder != null) { final List encodings = new ArrayList<>(contentDecoder.size()); final RegistryBuilder> b2 = RegistryBuilder.create(); - for (final Map.Entry> entry : contentDecoder.entrySet()) { - final String token = entry.getKey().token(); + for (final Map.Entry> entry : contentDecoder.entrySet()) { + final String token = entry.getKey(); encodings.add(token); b2.register(token, entry.getValue()); }