View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.examples;
29  
30  import java.io.IOException;
31  import java.util.concurrent.Future;
32  
33  import org.apache.hc.client5.http.async.AsyncExecCallback;
34  import org.apache.hc.client5.http.async.AsyncExecChain;
35  import org.apache.hc.client5.http.async.AsyncExecChainHandler;
36  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
37  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
38  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
39  import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
40  import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
41  import org.apache.hc.client5.http.impl.ChainElement;
42  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
43  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
44  import org.apache.hc.core5.concurrent.FutureCallback;
45  import org.apache.hc.core5.http.ContentType;
46  import org.apache.hc.core5.http.HttpException;
47  import org.apache.hc.core5.http.HttpRequest;
48  import org.apache.hc.core5.http.message.StatusLine;
49  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
50  import org.apache.hc.core5.http.nio.entity.DigestingEntityProducer;
51  import org.apache.hc.core5.io.CloseMode;
52  import org.apache.hc.core5.reactor.IOReactorConfig;
53  import org.apache.hc.core5.util.Timeout;
54  
55  /**
56   * This example demonstrates how to use a custom execution interceptor
57   * to add trailers to all outgoing request enclosing an entity.
58   */
59  public class AsyncClientMessageTrailers {
60  
61      public final static void main(final String[] args) throws Exception {
62  
63          final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
64                  .setSoTimeout(Timeout.ofSeconds(5))
65                  .build();
66  
67          final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
68                  .setIOReactorConfig(ioReactorConfig)
69                  .addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {
70  
71                      @Override
72                      public void execute(
73                              final HttpRequest request,
74                              final AsyncEntityProducer entityProducer,
75                              final AsyncExecChain.Scope scope,
76                              final AsyncExecChain chain,
77                              final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
78                          // Send MD5 hash in a trailer by decorating the original entity producer
79                          chain.proceed(
80                                  request,
81                                  entityProducer != null ? new DigestingEntityProducer("MD5", entityProducer) : null,
82                                  scope,
83                                  asyncExecCallback);
84                      }
85  
86                  })
87                  .build();
88  
89          client.start();
90  
91          final SimpleHttpRequest request = SimpleRequestBuilder.post("http://httpbin.org/post")
92                  .setBody("some stuff", ContentType.TEXT_PLAIN)
93                  .build();
94  
95          System.out.println("Executing request " + request);
96          final Future<SimpleHttpResponse> future = client.execute(
97                  SimpleRequestProducer.create(request),
98                  SimpleResponseConsumer.create(),
99                  new FutureCallback<SimpleHttpResponse>() {
100 
101                     @Override
102                     public void completed(final SimpleHttpResponse response) {
103                         System.out.println(request + "->" + new StatusLine(response));
104                         System.out.println(response.getBody());
105                     }
106 
107                     @Override
108                     public void failed(final Exception ex) {
109                         System.out.println(request + "->" + ex);
110                     }
111 
112                     @Override
113                     public void cancelled() {
114                         System.out.println(request + " cancelled");
115                     }
116 
117                 });
118         future.get();
119 
120         System.out.println("Shutting down");
121         client.close(CloseMode.GRACEFUL);
122     }
123 
124 }
125