-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathWebClient.java
More file actions
473 lines (394 loc) · 14 KB
/
Copy pathWebClient.java
File metadata and controls
473 lines (394 loc) · 14 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.test;
import static okhttp3.RequestBody.create;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.jspecify.annotations.Nullable;
import io.jooby.Server;
import io.jooby.ServerSentMessage;
import io.jooby.SneakyThrows;
import io.jooby.WebSocketCloseStatus;
import okhttp3.*;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import okhttp3.sse.EventSources;
import okio.ByteString;
public class WebClient implements AutoCloseable {
private class SyncWebSocketListener extends WebSocketListener {
private CountDownLatch opened = new CountDownLatch(1);
private AtomicBoolean closed = new AtomicBoolean(false);
private BlockingQueue messages = new LinkedBlockingQueue();
private String testName;
public SyncWebSocketListener(String testName) {
this.testName = testName;
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
opened.countDown();
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
closed.set(true);
}
@Override
public void onFailure(WebSocket webSocket, Throwable e, @Nullable Response response) {
if (!Server.connectionLost(e)) {
System.err.println("Unexpected web socket error: " + testName);
e.printStackTrace();
}
}
@Override
public void onMessage(WebSocket webSocket, String text) {
messages.offer(text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
messages.offer(new String(bytes.toByteArray(), StandardCharsets.UTF_8));
}
public String lastMessage() {
try {
return (String) messages.poll(10, TimeUnit.SECONDS);
} catch (Exception x) {
throw SneakyThrows.propagate(x);
}
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
super.onClosing(webSocket, code, reason);
}
}
public class BlockingWebSocket {
private WebSocket ws;
private SyncWebSocketListener listener;
public BlockingWebSocket(WebSocket ws, SyncWebSocketListener listener) {
this.ws = ws;
this.listener = listener;
try {
this.listener.opened.await(5, TimeUnit.SECONDS);
} catch (Exception x) {
throw SneakyThrows.propagate(x);
}
}
public String send(String message) {
ws.send(message);
return lastMessage();
}
public String sendBytes(byte[] message) {
ws.send(ByteString.of(message));
return lastMessage();
}
public String lastMessage() {
return listener.lastMessage();
}
public void close() {
if (listener.closed.compareAndSet(false, true)) {
ws.close(WebSocketCloseStatus.NORMAL_CODE, WebSocketCloseStatus.NORMAL.getReason());
}
}
}
public class Request {
private final okhttp3.Request.Builder req;
private SneakyThrows.Consumer<okhttp3.Request.Builder> configurer;
public Request(okhttp3.Request.Builder req) {
this.req = req;
}
public Request prepare(SneakyThrows.Consumer<okhttp3.Request.Builder> configurer) {
this.configurer = configurer;
return this;
}
public void execute(SneakyThrows.Consumer<Response> callback) {
execute(1, callback);
}
public void execute(int concurrency, SneakyThrows.Consumer<Response> callback) {
if (configurer != null) {
configurer.accept(req);
}
if (concurrency > 1) {
var futures = new ArrayList<CompletableFuture<String>>();
for (var i = 0; i < concurrency; i++) {
futures.add(
CompletableFuture.supplyAsync(
() -> {
executeCall(callback);
return "success";
}));
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
} catch (CompletionException x) {
throw SneakyThrows.propagate(x.getCause());
}
}
} else {
executeCall(callback);
}
}
private void executeCall(SneakyThrows.Consumer<Response> callback) {
okhttp3.Request r = req.build();
try (Response rsp = client.newCall(r).execute()) {
callback.accept(rsp);
} catch (SocketTimeoutException x) {
SocketTimeoutException timeout = new SocketTimeoutException(r.toString());
timeout.addSuppressed(x);
throw SneakyThrows.propagate(timeout);
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
}
private static RequestBody EMPTY_BODY = RequestBody.create(new byte[0], null);
private String scheme;
private final int port;
private OkHttpClient client;
private Headers.Builder headers;
public WebClient(String scheme, int port, boolean followRedirects) {
try {
this.scheme = scheme;
this.port = port;
OkHttpClient.Builder builder =
new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.followRedirects(followRedirects);
if (scheme.equalsIgnoreCase("https")) {
configureSelfSigned(builder);
}
this.client = builder.build();
} catch (Exception x) {
throw SneakyThrows.propagate(x);
}
}
public WebClient header(String name, String value) {
if (headers == null) {
headers = new Headers.Builder();
}
if (value != null && !value.trim().isEmpty()) {
headers.add(name, value);
}
return this;
}
public Request invoke(String method, String path) {
return invoke(method, path, EMPTY_BODY);
}
public Request invoke(String method, String path, RequestBody body) {
return invoke(method, path, Map.of(), body);
}
public Request invoke(String method, String path, Map<String, Object> query, RequestBody body) {
var req = new okhttp3.Request.Builder();
req.method(method, body);
setRequestHeaders(req);
var url = HttpUrl.parse(scheme + "://localhost:" + port + path).newBuilder();
query.forEach((name, value) -> url.addQueryParameter(name, value.toString()));
req.url(url.build());
return new Request(req);
}
private void setRequestHeaders(okhttp3.Request.Builder req) {
if (headers == null) {
// set default headers:
header(
"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
}
if (headers != null) {
req.headers(headers.build());
headers = null;
}
}
public Request get(String path) {
return get(path, Map.of());
}
public Request get(String path, Map<String, Object> query) {
return invoke("GET", path, query, null);
}
public ServerSentMessageIterator sse(String path) {
okhttp3.Request.Builder req = new okhttp3.Request.Builder();
setRequestHeaders(req);
req.url(scheme + "://localhost:" + port + path);
EventSource.Factory factory = EventSources.createFactory(client);
BlockingQueue<ServerSentMessage> messages = new LinkedBlockingQueue();
EventSource eventSource =
factory.newEventSource(
req.build(),
new EventSourceListener() {
@Override
public void onClosed(EventSource eventSource) {
eventSource.cancel();
}
@Override
public void onEvent(
EventSource eventSource,
@Nullable String id,
@Nullable String type,
String data) {
// retry is not part of public API
ServerSentMessage message = new ServerSentMessage(data).setId(id).setEvent(type);
messages.offer(message);
}
@Override
public void onFailure(
EventSource eventSource, @Nullable Throwable t, @Nullable Response response) {
super.onFailure(eventSource, t, response);
}
@Override
public void onOpen(EventSource eventSource, Response response) {
super.onOpen(eventSource, response);
}
});
return new ServerSentMessageIterator(eventSource, messages);
}
public void get(String path, SneakyThrows.Consumer<Response> callback) {
get(path).execute(callback);
}
public void get(
String path, Map<String, Object> query, SneakyThrows.Consumer<Response> callback) {
get(path, query).execute(callback);
}
public void syncWebSocket(String path, SneakyThrows.Consumer<BlockingWebSocket> consumer) {
okhttp3.Request.Builder req = new okhttp3.Request.Builder();
req.url("ws://localhost:" + port + path);
setRequestHeaders(req);
okhttp3.Request r = req.build();
SyncWebSocketListener listener =
new SyncWebSocketListener(
System.getProperty("___app_name__")
+ "("
+ System.getProperty("___server_name__")
+ ")");
WebSocket webSocket = client.newWebSocket(r, listener);
BlockingWebSocket blockingWebSocket = new BlockingWebSocket(webSocket, listener);
consumer.accept(blockingWebSocket);
blockingWebSocket.close();
}
public WebSocket webSocket(String path, WebSocketListener listener) {
okhttp3.Request.Builder req = new okhttp3.Request.Builder();
req.url("ws://localhost:" + port + path);
setRequestHeaders(req);
okhttp3.Request r = req.build();
return client.newWebSocket(r, listener);
}
public Request options(String path) {
return invoke("OPTIONS", path, null);
}
public void options(String path, SneakyThrows.Consumer<Response> callback) {
options(path).execute(callback);
}
public Request trace(String path) {
return invoke("TRACE", path, null);
}
public void trace(String path, SneakyThrows.Consumer<Response> callback) {
trace(path).execute(callback);
}
public Request head(String path) {
return invoke("HEAD", path, null);
}
public void head(String path, SneakyThrows.Consumer<Response> callback) {
head(path).execute(callback);
}
public Request post(String path) {
return post(path, EMPTY_BODY);
}
public void post(String path, SneakyThrows.Consumer<Response> callback) {
post(path).execute(callback);
}
public Request post(String path, RequestBody body) {
return invoke("POST", path, body);
}
public void post(String path, RequestBody form, SneakyThrows.Consumer<Response> callback) {
post(path, form).execute(callback);
}
public void postJson(String path, String json, SneakyThrows.Consumer<Response> callback) {
post(path, create(json, MediaType.parse("application/json"))).execute(callback);
}
public Request put(String path) {
return invoke("put", path, EMPTY_BODY);
}
public void put(String path, SneakyThrows.Consumer<Response> callback) {
put(path).execute(callback);
}
public Request delete(String path) {
return invoke("delete", path, EMPTY_BODY);
}
public void delete(String path, SneakyThrows.Consumer<Response> callback) {
delete(path).execute(callback);
}
public Request patch(String path) {
return invoke("patch", path, EMPTY_BODY);
}
public void patch(String path, SneakyThrows.Consumer<Response> callback) {
patch(path).execute(callback);
}
public int getPort() {
return port;
}
public void close() {
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
}
private static void configureSelfSigned(OkHttpClient.Builder builder)
throws NoSuchAlgorithmException, KeyManagementException {
X509TrustManager trustManager =
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkServerTrusted(final X509Certificate[] chain, final String authType)
throws CertificateException {}
@Override
public void checkClientTrusted(final X509Certificate[] chain, final String authType)
throws CertificateException {}
};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] {trustManager}, new java.security.SecureRandom());
builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
builder.hostnameVerifier((hostname, session) -> true);
}
public static class ServerSentMessageIterator {
private final EventSource source;
private List<BiConsumer<ServerSentMessage, EventSource>> consumers = new ArrayList<>();
private BlockingQueue<ServerSentMessage> messages;
public ServerSentMessageIterator(
EventSource source, BlockingQueue<ServerSentMessage> messages) {
this.source = source;
this.messages = messages;
}
public ServerSentMessageIterator next(Consumer<ServerSentMessage> consumer) {
return next((message, source) -> consumer.accept(message));
}
public ServerSentMessageIterator next(BiConsumer<ServerSentMessage, EventSource> consumer) {
consumers.add(consumer);
return this;
}
public void verify() {
int i = 0;
while (i < consumers.size()) {
try {
ServerSentMessage message = messages.take();
consumers.get(i).accept(message, source);
} catch (InterruptedException e) {
e.printStackTrace();
}
i += 1;
}
}
}
}