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