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.SimpleHttpResponse;
37  import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
38  import org.apache.hc.client5.http.impl.ChainElement;
39  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
40  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
41  import org.apache.hc.core5.concurrent.FutureCallback;
42  import org.apache.hc.core5.http.ContentType;
43  import org.apache.hc.core5.http.HttpException;
44  import org.apache.hc.core5.http.HttpRequest;
45  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
46  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
47  import org.apache.hc.core5.http.nio.entity.DigestingEntityProducer;
48  import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
49  import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
50  import org.apache.hc.core5.io.CloseMode;
51  import org.apache.hc.core5.reactor.IOReactorConfig;
52  import org.apache.hc.core5.util.Timeout;
53  
54  /**
55   * This example demonstrates how to use a custom execution interceptor
56   * to add trailers to all outgoing request enclosing an entity.
57   */
58  public class AsyncClientMessageTrailers {
59  
60      public final static void main(final String[] args) throws Exception {
61  
62          final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
63                  .setSoTimeout(Timeout.ofSeconds(5))
64                  .build();
65  
66          final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
67                  .setIOReactorConfig(ioReactorConfig)
68                  .addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {
69  
70                      @Override
71                      public void execute(
72                              final HttpRequest request,
73                              final AsyncEntityProducer entityProducer,
74                              final AsyncExecChain.Scope scope,
75                              final AsyncExecChain chain,
76                              final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
77                          // Send MD5 hash in a trailer by decorating the original entity producer
78                          chain.proceed(
79                                  request,
80                                  entityProducer != null ? new DigestingEntityProducer("MD5", entityProducer) : null,
81                                  scope,
82                                  asyncExecCallback);
83                      }
84  
85                  })
86                  .build();
87  
88          client.start();
89  
90          final String requestUri = "http://httpbin.org/post";
91          final AsyncRequestProducer requestProducer = AsyncRequestBuilder.post(requestUri)
92                  .setEntity(new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN))
93                  .build();
94          final Future<SimpleHttpResponse> future = client.execute(
95                  requestProducer,
96                  SimpleResponseConsumer.create(),
97                  new FutureCallback<SimpleHttpResponse>() {
98  
99                      @Override
100                     public void completed(final SimpleHttpResponse response) {
101                         System.out.println(requestUri + "->" + response.getCode());
102                         System.out.println(response.getBody());
103                     }
104 
105                     @Override
106                     public void failed(final Exception ex) {
107                         System.out.println(requestUri + "->" + ex);
108                     }
109 
110                     @Override
111                     public void cancelled() {
112                         System.out.println(requestUri + " cancelled");
113                     }
114 
115                 });
116         future.get();
117 
118         System.out.println("Shutting down");
119         client.close(CloseMode.GRACEFUL);
120     }
121 
122 }
123