ok2c commented on code in PR #681:
URL: 
https://github.com/apache/httpcomponents-client/pull/681#discussion_r2250022183


##########
httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientDeflatePostExample.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * ====================================================================
+ * 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.hc.client5.http.examples;
+
+import java.util.concurrent.Future;
+
+import org.apache.hc.client5.http.async.methods.DeflatingAsyncEntityProducer;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
+import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
+import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
+import org.apache.hc.core5.concurrent.FutureCallback;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.Message;
+import org.apache.hc.core5.http.message.StatusLine;
+import org.apache.hc.core5.http.nio.AsyncEntityProducer;
+import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
+import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
+import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
+import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
+import org.apache.hc.core5.io.CloseMode;
+
+/**
+ * Simple command-line tool that sends a {@code deflate}-compressed JSON
+ * payload to <a href="https://httpbin.org/post";>httpbin.org/post</a> and
+ * prints the echoed response.
+ *
+ * <p>The example demonstrates how to:</p>
+ * <ol>
+ *   <li>Wrap a regular {@link AsyncEntityProducer} with
+ *       {@link DeflatingAsyncEntityProducer}.</li>
+ *   <li>Add the mandatory {@code Content-Encoding} request header.</li>
+ *   <li>Consume the JSON echo using {@link StringAsyncEntityConsumer}.</li>
+ * </ol>
+ *
+ * <p>Swap {@code DeflatingAsyncEntityProducer} for your future
+ * {@code GzipAsyncEntityProducer} once gzip support is added.</p>
+ *
+ * @since 5.6
+ */
+public final class AsyncClientDeflatePostExample {

Review Comment:
   @arturobernalg I do not quite understand how this example differs from 
`AsyncClientDeflateCompressionExample` and what additional value it provides. I 
would drop it.



##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/ContentCompressionAsyncExec.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * ====================================================================
+ * 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.hc.client5.http.impl.async;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.function.UnaryOperator;
+
+import org.apache.hc.client5.http.async.AsyncExecCallback;
+import org.apache.hc.client5.http.async.AsyncExecChain;
+import org.apache.hc.client5.http.async.AsyncExecChainHandler;
+import org.apache.hc.client5.http.async.methods.InflatingAsyncDataConsumer;
+import org.apache.hc.client5.http.entity.compress.ContentCoding;
+import org.apache.hc.client5.http.protocol.HttpClientContext;
+import org.apache.hc.core5.annotation.Contract;
+import org.apache.hc.core5.annotation.Internal;
+import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HeaderElement;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.config.Lookup;
+import org.apache.hc.core5.http.config.RegistryBuilder;
+import org.apache.hc.core5.http.message.BasicHeaderValueParser;
+import org.apache.hc.core5.http.message.MessageSupport;
+import org.apache.hc.core5.http.message.ParserCursor;
+import org.apache.hc.core5.http.nio.AsyncDataConsumer;
+import org.apache.hc.core5.http.nio.AsyncEntityProducer;
+import org.apache.hc.core5.util.Args;
+
+@Contract(threading = ThreadingBehavior.STATELESS)
+@Internal
+public final class ContentCompressionAsyncExec implements 
AsyncExecChainHandler {
+
+    private final Lookup<UnaryOperator<AsyncDataConsumer>> decoders;
+    private final Header acceptEncoding;
+    private final boolean ignoreUnknown;
+
+    public ContentCompressionAsyncExec(
+            final LinkedHashMap<String, UnaryOperator<AsyncDataConsumer>> 
decoderMap,
+            final boolean ignoreUnknown) {
+
+        Args.notEmpty(decoderMap, "Decoder map");
+
+        this.acceptEncoding = MessageSupport.headerOfTokens(
+                HttpHeaders.ACCEPT_ENCODING, new 
ArrayList<>(decoderMap.keySet()));
+
+        final RegistryBuilder<UnaryOperator<AsyncDataConsumer>> rb = 
RegistryBuilder.create();
+        decoderMap.forEach(rb::register);
+        this.decoders = rb.build();
+        this.ignoreUnknown = ignoreUnknown;
+    }
+
+    /**
+     * default = DEFLATE only
+     */
+    public ContentCompressionAsyncExec() {
+        final LinkedHashMap<String, UnaryOperator<AsyncDataConsumer>> map = 
new LinkedHashMap<>();
+        map.put(ContentCoding.DEFLATE.token(),
+                d -> new InflatingAsyncDataConsumer(d, null));
+        this.acceptEncoding = MessageSupport.headerOfTokens(
+                HttpHeaders.ACCEPT_ENCODING, 
Collections.singletonList(ContentCoding.DEFLATE.token()));
+        this.decoders = 
RegistryBuilder.<UnaryOperator<AsyncDataConsumer>>create()
+                .register(ContentCoding.DEFLATE.token(), 
map.get(ContentCoding.DEFLATE.token()))
+                .build();
+        this.ignoreUnknown = true;
+    }
+
+
+    @Override
+    public void execute(final HttpRequest request,
+                        final AsyncEntityProducer producer,
+                        final AsyncExecChain.Scope scope,
+                        final AsyncExecChain chain,
+                        final AsyncExecCallback cb) throws IOException, 
HttpException {
+
+        final HttpClientContext ctx = scope != null ? scope.clientContext : 
HttpClientContext.create();
+        final boolean enabled = 
ctx.getRequestConfigOrDefault().isContentCompressionEnabled();
+
+        if (enabled && !request.containsHeader(HttpHeaders.ACCEPT_ENCODING)) {
+            request.addHeader(acceptEncoding);
+        }
+
+        chain.proceed(request, producer, scope, new AsyncExecCallback() {
+
+            @Override
+            public AsyncDataConsumer handleResponse(final HttpResponse rsp, 
final EntityDetails details)
+                    throws HttpException, IOException {
+
+                if (!enabled) {
+                    return cb.handleResponse(rsp, details);
+                }
+
+                /* header lookup – unchanged from previous version */
+                String raw = details != null ? details.getContentEncoding() : 
null;
+                if (raw == null) {
+                    final Header h = 
rsp.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
+                    if (h != null) raw = h.getValue();
+                }
+
+                AsyncDataConsumer downstream = cb.handleResponse(rsp, details);

Review Comment:
   @arturobernalg Careful here. We should rewrite `EntityDetails` when we 
actually about to do automatic content decompression and make it return 
content-length as `-1` and content-encoding as `null` to signal downstream the 
content is going to be transformed on the fly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to