-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathJdkWebSocketAdapter.java
More file actions
314 lines (277 loc) · 10.2 KB
/
JdkWebSocketAdapter.java
File metadata and controls
314 lines (277 loc) · 10.2 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
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.websocket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Builder;
import java.nio.ByteBuffer;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.htmlunit.WebClient;
import org.htmlunit.WebClientOptions;
/**
* JDK based implementation of the {@link WebSocketAdapter}.
* Uses the {@link java.net.http.HttpClient} and {@link java.net.http.WebSocket}
* APIs available since JDK 11.
*
* @author Ronald Brill
*/
public final class JdkWebSocketAdapter implements WebSocketAdapter {
/**
* Our {@link WebSocketAdapterFactory}.
*/
public static final class JdkWebSocketAdapterFactory implements WebSocketAdapterFactory {
/**
* {@inheritDoc}
*/
@Override
public WebSocketAdapter buildWebSocketAdapter(final WebClient webClient,
final WebSocketListener webSocketListener) {
return new JdkWebSocketAdapter(webClient, webSocketListener);
}
}
private final Object clientLock_ = new Object();
private HttpClient httpClient_;
private final WebClient webClient_;
private final WebSocketListener listener_;
private volatile java.net.http.WebSocket incomingSession_;
private java.net.http.WebSocket outgoingSession_;
/**
* Ctor.
* @param webClient the {@link WebClient}
* @param listener the {@link WebSocketListener}
*/
public JdkWebSocketAdapter(final WebClient webClient, final WebSocketListener listener) {
super();
webClient_ = webClient;
listener_ = listener;
}
/**
* {@inheritDoc}
*/
@Override
public void start() throws Exception {
synchronized (clientLock_) {
final WebClientOptions options = webClient_.getOptions();
final Executor executor = webClient_.getExecutor();
final Builder builder = HttpClient.newBuilder()
.executor(executor)
.cookieHandler(new WebSocketCookieHandler(webClient_));
if (options.isUseInsecureSSL()) {
builder.sslContext(createInsecureSslContext());
}
httpClient_ = builder.build();
}
}
/**
* {@inheritDoc}
*/
@Override
public void connect(final URI url) throws Exception {
synchronized (clientLock_) {
final Executor executor = webClient_.getExecutor();
final CompletableFuture<java.net.http.WebSocket> connectFuture =
httpClient_.newWebSocketBuilder()
.buildAsync(url, new JdkWebSocketListenerImpl());
executor.execute(() -> {
try {
listener_.onWebSocketConnecting();
incomingSession_ = connectFuture.join();
}
catch (final Exception e) {
listener_.onWebSocketConnectError(e);
}
});
}
}
/**
* {@inheritDoc}
*/
@Override
public void send(final Object content) throws IOException {
try {
if (content instanceof String string) {
outgoingSession_.sendText(string, true).join();
}
else if (content instanceof ByteBuffer buffer) {
outgoingSession_.sendBinary(buffer, true).join();
}
else {
throw new IllegalStateException(
"Unsupported content type for WebSocket.send(): expected String or ByteBuffer");
}
}
catch (final IllegalStateException e) {
throw e;
}
catch (final Exception e) {
throw new IOException(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public void closeIncomingSession() {
if (incomingSession_ != null) {
incomingSession_.sendClose(java.net.http.WebSocket.NORMAL_CLOSURE, "").join();
}
}
/**
* {@inheritDoc}
*/
@Override
public void closeOutgoingSession() {
if (outgoingSession_ != null) {
outgoingSession_.sendClose(java.net.http.WebSocket.NORMAL_CLOSURE, "").join();
}
}
/**
* {@inheritDoc}
*/
@Override
public void closeClient() throws Exception {
synchronized (clientLock_) {
// HttpClient does not have a close() in Java 17;
// simply drop the reference so it can be garbage-collected
httpClient_ = null;
}
}
private static SSLContext createInsecureSslContext()
throws NoSuchAlgorithmException, KeyManagementException {
final TrustManager[] trustAllCerts = {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
// trust all
}
@Override
public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
// trust all
}
}
};
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
return sslContext;
}
/**
* A {@link CookieHandler} that bridges to the {@link WebClient} cookie store.
*/
private static class WebSocketCookieHandler extends CookieHandler {
private final WebSocketCookieStore cookieStore_;
WebSocketCookieHandler(final WebClient webClient) {
cookieStore_ = new WebSocketCookieStore(webClient);
}
@Override
public java.util.Map<String, java.util.List<String>> get(
final URI uri,
final java.util.Map<String, java.util.List<String>> requestHeaders) {
final java.util.List<java.net.HttpCookie> cookies = cookieStore_.get(uri);
final java.util.Map<String, java.util.List<String>> result = new java.util.HashMap<>();
if (!cookies.isEmpty()) {
final java.util.List<String> cookieValues = new java.util.ArrayList<>();
for (final java.net.HttpCookie cookie : cookies) {
cookieValues.add(cookie.toString());
}
result.put("Cookie", cookieValues);
}
return result;
}
@Override
public void put(final URI uri,
final java.util.Map<String, java.util.List<String>> responseHeaders) {
// not needed for WebSocket connections
}
}
private class JdkWebSocketListenerImpl implements java.net.http.WebSocket.Listener {
private StringBuilder textAccumulator_;
private ByteArrayOutputStream binaryAccumulator_;
JdkWebSocketListenerImpl() {
super();
}
@Override
public void onOpen(final java.net.http.WebSocket webSocket) {
outgoingSession_ = webSocket;
listener_.onWebSocketConnect();
webSocket.request(1);
}
@Override
public CompletionStage<?> onText(final java.net.http.WebSocket webSocket,
final CharSequence data, final boolean last) {
if (textAccumulator_ == null) {
textAccumulator_ = new StringBuilder();
}
textAccumulator_.append(data);
if (last) {
final String message = textAccumulator_.toString();
textAccumulator_ = null;
listener_.onWebSocketText(message);
}
webSocket.request(1);
return null;
}
@Override
public CompletionStage<?> onBinary(final java.net.http.WebSocket webSocket,
final ByteBuffer data, final boolean last) {
if (binaryAccumulator_ == null) {
binaryAccumulator_ = new ByteArrayOutputStream();
}
if (data.hasArray()) {
binaryAccumulator_.write(data.array(),
data.arrayOffset() + data.position(), data.remaining());
}
else {
final byte[] temp = new byte[data.remaining()];
data.get(temp);
binaryAccumulator_.write(temp, 0, temp.length);
}
if (last) {
final byte[] bytes = binaryAccumulator_.toByteArray();
binaryAccumulator_ = null;
listener_.onWebSocketBinary(bytes, 0, bytes.length);
}
webSocket.request(1);
return null;
}
@Override
public CompletionStage<?> onClose(final java.net.http.WebSocket webSocket,
final int statusCode, final String reason) {
outgoingSession_ = null;
listener_.onWebSocketClose(statusCode, reason);
return null;
}
@Override
public void onError(final java.net.http.WebSocket webSocket, final Throwable error) {
outgoingSession_ = null;
listener_.onWebSocketError(error);
}
}
}