1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.core5.http2.examples;
28
29 import java.util.List;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.Future;
32
33 import javax.net.ssl.SSLContext;
34
35 import org.apache.hc.core5.concurrent.FutureCallback;
36 import org.apache.hc.core5.http.Header;
37 import org.apache.hc.core5.http.HttpConnection;
38 import org.apache.hc.core5.http.HttpHost;
39 import org.apache.hc.core5.http.HttpResponse;
40 import org.apache.hc.core5.http.Message;
41 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
42 import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
43 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
44 import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
45 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
46 import org.apache.hc.core5.http2.config.H2Config;
47 import org.apache.hc.core5.http2.frame.RawFrame;
48 import org.apache.hc.core5.http2.impl.nio.H2StreamListener;
49 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2RequesterBootstrap;
50 import org.apache.hc.core5.http2.ssl.ConscryptClientTlsStrategy;
51 import org.apache.hc.core5.io.CloseMode;
52 import org.apache.hc.core5.ssl.SSLContexts;
53 import org.apache.hc.core5.util.Timeout;
54 import org.conscrypt.Conscrypt;
55
56
57
58
59
60 public class H2ConscriptRequestExecutionExample {
61
62 public final static void main(final String[] args) throws Exception {
63
64
65 final H2Config h2Config = H2Config.custom()
66 .setPushEnabled(false)
67 .build();
68
69 final SSLContext sslContext = SSLContexts.custom()
70 .setProvider(Conscrypt.newProvider())
71 .build();
72
73 final HttpAsyncRequester requester = H2RequesterBootstrap.bootstrap()
74 .setH2Config(h2Config)
75 .setTlsStrategy(new ConscryptClientTlsStrategy(sslContext))
76 .setStreamListener(new H2StreamListener() {
77
78 @Override
79 public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
80 for (int i = 0; i < headers.size(); i++) {
81 System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
82 }
83 }
84
85 @Override
86 public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
87 for (int i = 0; i < headers.size(); i++) {
88 System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
89 }
90 }
91
92 @Override
93 public void onFrameInput(final HttpConnection connection, final int streamId, final RawFrame frame) {
94 }
95
96 @Override
97 public void onFrameOutput(final HttpConnection connection, final int streamId, final RawFrame frame) {
98 }
99
100 @Override
101 public void onInputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
102 }
103
104 @Override
105 public void onOutputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
106 }
107
108 })
109 .create();
110 Runtime.getRuntime().addShutdownHook(new Thread() {
111 @Override
112 public void run() {
113 System.out.println("HTTP requester shutting down");
114 requester.close(CloseMode.GRACEFUL);
115 }
116 });
117 requester.start();
118
119 final HttpHost target = new HttpHost("https", "nghttp2.org", 443);
120 final String[] requestUris = new String[] {"/httpbin/ip", "/httpbin/user-agent", "/httpbin/headers"};
121
122 final CountDownLatch latch = new CountDownLatch(requestUris.length);
123 for (final String requestUri: requestUris) {
124 final Future<AsyncClientEndpoint> future = requester.connect(target, Timeout.ofDays(5));
125 final AsyncClientEndpoint clientEndpoint = future.get();
126 clientEndpoint.execute(
127 AsyncRequestBuilder.get()
128 .setHttpHost(target)
129 .setPath(requestUri)
130 .build(),
131 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
132 new FutureCallback<Message<HttpResponse, String>>() {
133
134 @Override
135 public void completed(final Message<HttpResponse, String> message) {
136 clientEndpoint.releaseAndReuse();
137 final HttpResponse response = message.getHead();
138 final String body = message.getBody();
139 System.out.println(requestUri + "->" + response.getCode() + " " + response.getVersion());
140 System.out.println(body);
141 latch.countDown();
142 }
143
144 @Override
145 public void failed(final Exception ex) {
146 clientEndpoint.releaseAndDiscard();
147 System.out.println(requestUri + "->" + ex);
148 latch.countDown();
149 }
150
151 @Override
152 public void cancelled() {
153 clientEndpoint.releaseAndDiscard();
154 System.out.println(requestUri + " cancelled");
155 latch.countDown();
156 }
157
158 });
159 }
160
161 latch.await();
162 System.out.println("Shutting down I/O reactor");
163 requester.initiateShutdown();
164 }
165
166 }