ok2c commented on code in PR #681: URL: https://github.com/apache/httpcomponents-client/pull/681#discussion_r2239596409
########## httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientDeflateCompressionExample.java: ########## @@ -0,0 +1,106 @@ +/* + * ==================================================================== + * 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.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; + +/** + * Demonstrates how to POST a JSON body compressed on-the-fly with + * {@link DeflatingAsyncEntityProducer}. The program talks to + * <a href="https://httpbin.org/post">httpbin</a>, which echoes your request, + * making it easy to verify that the {@code Content-Encoding: deflate} + * header was honoured. + * + * <p>Key take-aways:</p> + * <ol> + * <li>Wrap any existing {@code AsyncEntityProducer} in the compressor.</li> + * <li>Add the encoding header yourself – HttpClient does not do that for you.</li> + * <li>The producer is streaming: huge payloads are never fully buffered.</li> + * </ol> + * + * @since 5.6 + */ +public class AsyncClientDeflateCompressionExample { + + public static void main(final String[] args) throws Exception { + try (final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault()) { + client.start(); + + final String json = "{\"msg\":\"hello deflated world\"}"; + final AsyncEntityProducer raw = + new StringAsyncEntityProducer(json, ContentType.APPLICATION_JSON); + + final AsyncEntityProducer deflated = new DeflatingAsyncEntityProducer(raw); + + final SimpleHttpRequest request = SimpleRequestBuilder + .post("https://httpbin.org/post") + .addHeader("Content-Encoding", "deflate") Review Comment: @arturobernalg [RequestContent](https://github.com/apache/httpcomponents-core/blob/master/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestContent.java#L136) request interceptor should do that automatically. This should not be needed. Please double-check. ########## httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientDeflatePostExample.java: ########## @@ -0,0 +1,111 @@ +/* + * ==================================================================== + * 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 { + + private AsyncClientDeflatePostExample() { + } + + public static void main(final String[] args) throws Exception { + + try (final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault()) { + client.start(); + + /* ------------- original payload ------------------ */ + final String json = "{\"msg\":\"hello compressed world\"}"; + final AsyncEntityProducer raw = new StringAsyncEntityProducer(json, ContentType.APPLICATION_JSON); + + /* ------------- wrap with DEFLATE ------------------ */ + final AsyncEntityProducer deflated = new DeflatingAsyncEntityProducer(raw); + + /* ------------- build request ---------------------- */ + final SimpleHttpRequest req = SimpleRequestBuilder.post("https://httpbin.org/post") + .addHeader("Content-Encoding", "deflate") Review Comment: @arturobernalg Same. -- 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