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  package org.apache.hc.client5.http.examples;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.util.List;
32  import java.util.concurrent.CountDownLatch;
33  import java.util.concurrent.TimeUnit;
34  
35  import org.apache.hc.client5.http.config.TlsConfig;
36  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
37  import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
38  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
39  import org.apache.hc.core5.http.ContentType;
40  import org.apache.hc.core5.http.EntityDetails;
41  import org.apache.hc.core5.http.Header;
42  import org.apache.hc.core5.http.HttpException;
43  import org.apache.hc.core5.http.HttpResponse;
44  import org.apache.hc.core5.http.config.Http1Config;
45  import org.apache.hc.core5.http.message.BasicHttpRequest;
46  import org.apache.hc.core5.http.message.StatusLine;
47  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
48  import org.apache.hc.core5.http.nio.CapacityChannel;
49  import org.apache.hc.core5.http.nio.DataStreamChannel;
50  import org.apache.hc.core5.http.nio.RequestChannel;
51  import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
52  import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
53  import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
54  import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
55  import org.apache.hc.core5.http.protocol.HttpContext;
56  import org.apache.hc.core5.http.support.BasicRequestBuilder;
57  import org.apache.hc.core5.http2.HttpVersionPolicy;
58  import org.apache.hc.core5.http2.config.H2Config;
59  import org.apache.hc.core5.io.CloseMode;
60  import org.apache.hc.core5.reactor.IOReactorConfig;
61  
62  /**
63   * This example demonstrates a full-duplex, streaming HTTP/2 message exchange.
64   */
65  public class AsyncClientH2FullDuplexExchange {
66  
67      public static void main(final String[] args) throws Exception {
68  
69          final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(
70                  H2Config.DEFAULT,
71                  Http1Config.DEFAULT,
72                  IOReactorConfig.DEFAULT,
73                  PoolingAsyncClientConnectionManagerBuilder.create()
74                          .setDefaultTlsConfig(TlsConfig.custom()
75                                  .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
76                                  .build())
77                          .build());
78  
79          client.start();
80  
81          final BasicHttpRequest request = BasicRequestBuilder.post("https://nghttp2.org/httpbin/post").build();
82          final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
83                  new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
84          final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
85                  new StringAsyncEntityConsumer());
86  
87          System.out.println("Executing request " + request);
88          final CountDownLatch latch = new CountDownLatch(1);
89          client.execute(new AsyncClientExchangeHandler() {
90  
91              @Override
92              public void releaseResources() {
93                  requestProducer.releaseResources();
94                  responseConsumer.releaseResources();
95                  latch.countDown();
96              }
97  
98              @Override
99              public void cancel() {
100                 System.out.println(request + " cancelled");
101             }
102 
103             @Override
104             public void failed(final Exception cause) {
105                 System.out.println(request + "->" + cause);
106             }
107 
108             @Override
109             public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
110                 requestProducer.sendRequest(channel, context);
111             }
112 
113             @Override
114             public int available() {
115                 return requestProducer.available();
116             }
117 
118             @Override
119             public void produce(final DataStreamChannel channel) throws IOException {
120                 requestProducer.produce(channel);
121             }
122 
123             @Override
124             public void consumeInformation(
125                     final HttpResponse response,
126                     final HttpContext context) throws HttpException, IOException {
127                 System.out.println(request + "->" + new StatusLine(response));
128             }
129 
130             @Override
131             public void consumeResponse(
132                     final HttpResponse response,
133                     final EntityDetails entityDetails,
134                     final HttpContext context) throws HttpException, IOException {
135                 System.out.println(request + "->" + new StatusLine(response));
136                 responseConsumer.consumeResponse(response, entityDetails, context, null);
137             }
138 
139             @Override
140             public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
141                 responseConsumer.updateCapacity(capacityChannel);
142             }
143 
144             @Override
145             public void consume(final ByteBuffer src) throws IOException {
146                 responseConsumer.consume(src);
147             }
148 
149             @Override
150             public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
151                 responseConsumer.streamEnd(trailers);
152             }
153 
154         });
155         latch.await(1, TimeUnit.MINUTES);
156 
157         System.out.println("Shutting down");
158         client.close(CloseMode.GRACEFUL);
159     }
160 
161 }