author | prappo |
Wed, 08 Jun 2016 15:19:58 +0100 | |
changeset 38864 | bf2b41533aed |
parent 38856 | cc3a0d1e96e0 |
child 39133 | b5641ce64cf7 |
permissions | -rw-r--r-- |
37874 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.net.http; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.net.ProtocolException; |
|
29 |
import java.net.URI; |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Objects; |
|
33 |
import java.util.Optional; |
|
34 |
import java.util.concurrent.CompletableFuture; |
|
35 |
import java.util.concurrent.CompletionStage; |
|
36 |
import java.util.concurrent.TimeUnit; |
|
37 |
import java.util.stream.Stream; |
|
38 |
||
39 |
/** |
|
40 |
* A WebSocket client conforming to RFC 6455. |
|
41 |
* |
|
42 |
* <p> A {@code WebSocket} provides full-duplex communication over a TCP |
|
43 |
* connection. |
|
44 |
* |
|
45 |
* <p> To create a {@code WebSocket} use a {@linkplain #newBuilder(URI, Listener) |
|
46 |
* builder}. Once a {@code WebSocket} is obtained, it's ready to send and |
|
47 |
* receive messages. When the {@code WebSocket} is no longer |
|
48 |
* needed it must be closed: a Close message must both be {@linkplain |
|
49 |
* #sendClose() sent} and {@linkplain Listener#onClose(WebSocket, Optional, |
|
50 |
* String) received}. Or to close abruptly, {@link #abort()} is called. Once |
|
51 |
* closed it remains closed, cannot be reopened. |
|
52 |
* |
|
53 |
* <p> Messages of type {@code X} are sent through the {@code WebSocket.sendX} |
|
54 |
* methods and received through {@link WebSocket.Listener}{@code .onX} methods |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
55 |
* asynchronously. Each of the methods returns a {@link CompletionStage} which |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
56 |
* completes when the operation has completed. |
37874 | 57 |
* |
58 |
* <p> Messages are received only if {@linkplain #request(long) requested}. |
|
59 |
* |
|
60 |
* <p> One outstanding send operation is permitted: if another send operation is |
|
61 |
* initiated before the previous one has completed, an {@link |
|
62 |
* IllegalStateException IllegalStateException} will be thrown. When sending, a |
|
63 |
* message should not be modified until the returned {@code CompletableFuture} |
|
64 |
* completes (either normally or exceptionally). |
|
65 |
* |
|
66 |
* <p> Messages can be sent and received as a whole or in parts. A whole message |
|
67 |
* is a sequence of one or more messages in which the last message is marked |
|
68 |
* when it is sent or received. |
|
69 |
* |
|
70 |
* <p> If the message is contained in a {@link ByteBuffer}, bytes are considered |
|
71 |
* arranged from the {@code buffer}'s {@link ByteBuffer#position() position} to |
|
72 |
* the {@code buffer}'s {@link ByteBuffer#limit() limit}. |
|
73 |
* |
|
74 |
* <p> All message exchange is run by the threads belonging to the {@linkplain |
|
75 |
* HttpClient#executorService() executor service} of {@code WebSocket}'s {@link |
|
76 |
* HttpClient}. |
|
77 |
* |
|
78 |
* <p> Unless otherwise noted, passing a {@code null} argument to a constructor |
|
79 |
* or method of this type will cause a {@link NullPointerException |
|
80 |
* NullPointerException} to be thrown. |
|
81 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
82 |
* @implNote The default implementation's methods do not block before returning |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
83 |
* a {@code CompletableFuture}. |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
84 |
* |
37874 | 85 |
* @since 9 |
86 |
*/ |
|
87 |
public interface WebSocket { |
|
88 |
||
89 |
/** |
|
90 |
* Creates a builder of {@code WebSocket}s connected to the given URI and |
|
91 |
* receiving events with the given {@code Listener}. |
|
92 |
* |
|
93 |
* <p> Equivalent to: |
|
94 |
* <pre>{@code |
|
95 |
* WebSocket.newBuilder(uri, HttpClient.getDefault()) |
|
96 |
* }</pre> |
|
97 |
* |
|
98 |
* @param uri |
|
99 |
* the WebSocket URI as defined in the WebSocket Protocol |
|
100 |
* (with "ws" or "wss" scheme) |
|
101 |
* |
|
102 |
* @param listener |
|
103 |
* the listener |
|
104 |
* |
|
105 |
* @throws IllegalArgumentException |
|
106 |
* if the {@code uri} is not a WebSocket URI |
|
107 |
* @throws SecurityException |
|
108 |
* if running under a security manager and the caller does |
|
109 |
* not have permission to access the |
|
110 |
* {@linkplain HttpClient#getDefault() default HttpClient} |
|
111 |
* |
|
112 |
* @return a builder |
|
113 |
*/ |
|
114 |
static Builder newBuilder(URI uri, Listener listener) { |
|
115 |
return newBuilder(uri, HttpClient.getDefault(), listener); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Creates a builder of {@code WebSocket}s connected to the given URI and |
|
120 |
* receiving events with the given {@code Listener}. |
|
121 |
* |
|
122 |
* <p> Providing a custom {@code client} allows for finer control over the |
|
123 |
* opening handshake. |
|
124 |
* |
|
125 |
* <p> <b>Example</b> |
|
126 |
* <pre>{@code |
|
127 |
* HttpClient client = HttpClient.create() |
|
128 |
* .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) |
|
129 |
* .build(); |
|
130 |
* ... |
|
131 |
* WebSocket.newBuilder(URI.create("ws://websocket.example.com"), client, listener)... |
|
132 |
* }</pre> |
|
133 |
* |
|
134 |
* @param uri |
|
135 |
* the WebSocket URI as defined in the WebSocket Protocol |
|
136 |
* (with "ws" or "wss" scheme) |
|
137 |
* |
|
138 |
* @param client |
|
139 |
* the HttpClient |
|
140 |
* @param listener |
|
141 |
* the listener |
|
142 |
* |
|
143 |
* @throws IllegalArgumentException |
|
144 |
* if the uri is not a WebSocket URI |
|
145 |
* |
|
146 |
* @return a builder |
|
147 |
*/ |
|
148 |
static Builder newBuilder(URI uri, HttpClient client, Listener listener) { |
|
149 |
return new WSBuilder(uri, client, listener); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* A builder for creating {@code WebSocket} instances. |
|
154 |
* |
|
155 |
* <p> To build a {@code WebSocket}, instantiate a builder, configure it |
|
156 |
* as required by calling intermediate methods (the ones that return the |
|
157 |
* builder itself), then finally call {@link #buildAsync()} to get a {@link |
|
158 |
* CompletableFuture} with resulting {@code WebSocket}. |
|
159 |
* |
|
160 |
* <p> If an intermediate method has not been called, an appropriate |
|
161 |
* default value (or behavior) will be used. Unless otherwise noted, a |
|
162 |
* repeated call to an intermediate method overwrites the previous value (or |
|
163 |
* overrides the previous behaviour), if no exception is thrown. |
|
164 |
* |
|
165 |
* <p> Instances of {@code Builder} may not be safe for use by multiple |
|
166 |
* threads. |
|
167 |
* |
|
168 |
* @since 9 |
|
169 |
*/ |
|
170 |
interface Builder { |
|
171 |
||
172 |
/** |
|
173 |
* Adds the given name-value pair to the list of additional headers for |
|
174 |
* the opening handshake. |
|
175 |
* |
|
176 |
* <p> Headers defined in WebSocket Protocol are not allowed to be added. |
|
177 |
* |
|
178 |
* @param name |
|
179 |
* the header name |
|
180 |
* @param value |
|
181 |
* the header value |
|
182 |
* |
|
183 |
* @return this builder |
|
184 |
* |
|
185 |
* @throws IllegalArgumentException |
|
186 |
* if the {@code name} is a WebSocket defined header name |
|
187 |
*/ |
|
188 |
Builder header(String name, String value); |
|
189 |
||
190 |
/** |
|
191 |
* Includes a request for the given subprotocols during the opening |
|
192 |
* handshake. |
|
193 |
* |
|
194 |
* <p> Among the requested subprotocols at most one will be chosen by |
|
195 |
* the server. When the {@code WebSocket} is connected, the subprotocol |
|
196 |
* in use is available from {@link WebSocket#getSubprotocol}. |
|
197 |
* Subprotocols may be specified in the order of preference. |
|
198 |
* |
|
199 |
* <p> Each of the given subprotocols must conform to the relevant |
|
200 |
* rules defined in the WebSocket Protocol. |
|
201 |
* |
|
202 |
* @param mostPreferred |
|
203 |
* the most preferred subprotocol |
|
204 |
* @param lesserPreferred |
|
205 |
* the lesser preferred subprotocols, with the least preferred |
|
206 |
* at the end |
|
207 |
* |
|
208 |
* @return this builder |
|
209 |
* |
|
210 |
* @throws IllegalArgumentException |
|
211 |
* if any of the WebSocket Protocol rules relevant to |
|
212 |
* subprotocols are violated |
|
213 |
*/ |
|
214 |
Builder subprotocols(String mostPreferred, String... lesserPreferred); |
|
215 |
||
216 |
/** |
|
217 |
* Sets a timeout for the opening handshake. |
|
218 |
* |
|
219 |
* <p> If the opening handshake is not finished within the specified |
|
220 |
* timeout then {@link #buildAsync()} completes exceptionally with a |
|
221 |
* {@code HttpTimeoutException}. |
|
222 |
* |
|
223 |
* <p> If the timeout is not specified then it's deemed infinite. |
|
224 |
* |
|
225 |
* @param timeout |
|
226 |
* the maximum time to wait |
|
227 |
* @param unit |
|
228 |
* the time unit of the timeout argument |
|
229 |
* |
|
230 |
* @return this builder |
|
231 |
* |
|
232 |
* @throws IllegalArgumentException |
|
233 |
* if the {@code timeout} is negative |
|
234 |
*/ |
|
235 |
Builder connectTimeout(long timeout, TimeUnit unit); |
|
236 |
||
237 |
/** |
|
238 |
* Builds a {@code WebSocket}. |
|
239 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
240 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
241 |
* normally with the {@code WebSocket} when it is connected or completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
242 |
* exceptionally if an error occurs. |
37874 | 243 |
* |
244 |
* <p> {@code CompletableFuture} may complete exceptionally with the |
|
245 |
* following errors: |
|
246 |
* <ul> |
|
247 |
* <li> {@link IOException} |
|
248 |
* if an I/O error occurs |
|
249 |
* <li> {@link InterruptedException} |
|
250 |
* if the operation was interrupted |
|
251 |
* <li> {@link SecurityException} |
|
252 |
* if a security manager is set, and the caller does not |
|
253 |
* have a {@link java.net.URLPermission} for the WebSocket URI |
|
254 |
* <li> {@link WebSocketHandshakeException} |
|
255 |
* if the opening handshake fails |
|
256 |
* </ul> |
|
257 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
258 |
* @return a {@code CompletableFuture} with the {@code WebSocket} |
37874 | 259 |
*/ |
260 |
CompletableFuture<WebSocket> buildAsync(); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* A listener for events and messages on a {@code WebSocket}. |
|
265 |
* |
|
266 |
* <p> Each method below corresponds to a type of event. |
|
267 |
* <ul> |
|
268 |
* <li> {@link #onOpen onOpen} <br> |
|
269 |
* This method is always the first to be invoked. |
|
38856
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
270 |
* <li> {@link #onText(WebSocket, CharSequence, WebSocket.MessagePart) |
37874 | 271 |
* onText}, {@link #onBinary(WebSocket, ByteBuffer, WebSocket.MessagePart) |
272 |
* onBinary}, {@link #onPing(WebSocket, ByteBuffer) onPing} and {@link |
|
273 |
* #onPong(WebSocket, ByteBuffer) onPong} <br> |
|
274 |
* These methods are invoked zero or more times after {@code onOpen}. |
|
275 |
* <li> {@link #onClose(WebSocket, Optional, String) onClose}, {@link |
|
276 |
* #onError(WebSocket, Throwable) onError} <br> |
|
277 |
* Only one of these methods is invoked, and that method is invoked last and |
|
278 |
* at most once. |
|
279 |
* </ul> |
|
280 |
* |
|
281 |
* <pre><code> |
|
282 |
* onOpen (onText|onBinary|onPing|onPong)* (onClose|onError)? |
|
283 |
* </code></pre> |
|
284 |
* |
|
285 |
* <p> Messages received by the {@code Listener} conform to the WebSocket |
|
286 |
* Protocol, otherwise {@code onError} with a {@link ProtocolException} is |
|
287 |
* invoked. |
|
288 |
* |
|
289 |
* <p> If a whole message is received, then the corresponding method |
|
290 |
* ({@code onText} or {@code onBinary}) will be invoked with {@link |
|
291 |
* WebSocket.MessagePart#WHOLE WHOLE} marker. Otherwise the method will be |
|
292 |
* invoked with {@link WebSocket.MessagePart#FIRST FIRST}, zero or more |
|
293 |
* times with {@link WebSocket.MessagePart#FIRST PART} and, finally, with |
|
294 |
* {@link WebSocket.MessagePart#LAST LAST} markers. |
|
295 |
* |
|
296 |
* <pre><code> |
|
297 |
* WHOLE|(FIRST PART* LAST) |
|
298 |
* </code></pre> |
|
299 |
* |
|
300 |
* <p> All methods are invoked in a sequential (and |
|
301 |
* <a href="../../../java/util/concurrent/package-summary.html#MemoryVisibility"> |
|
302 |
* happens-before</a>) order, one after another, possibly by different |
|
303 |
* threads. If any of the methods above throws an exception, {@code onError} |
|
304 |
* is then invoked with that exception. Exceptions thrown from {@code |
|
305 |
* onError} or {@code onClose} are ignored. |
|
306 |
* |
|
307 |
* <p> When the method returns, the message is deemed received. After this |
|
308 |
* another messages may be received. |
|
309 |
* |
|
310 |
* <p> These invocations begin asynchronous processing which might not end |
|
311 |
* with the invocation. To provide coordination, methods of {@code |
|
312 |
* Listener} return a {@link CompletionStage CompletionStage}. The {@code |
|
313 |
* CompletionStage} signals the {@code WebSocket} that the |
|
314 |
* processing of a message has ended. For |
|
315 |
* convenience, methods may return {@code null}, which means |
|
316 |
* the same as returning an already completed {@code CompletionStage}. If |
|
317 |
* the returned {@code CompletionStage} completes exceptionally, then {@link |
|
318 |
* #onError(WebSocket, Throwable) onError} will be invoked with the |
|
319 |
* exception. |
|
320 |
* |
|
321 |
* <p> Control of the message passes to the {@code Listener} with the |
|
322 |
* invocation of the method. Control of the message returns to the {@code |
|
323 |
* WebSocket} at the earliest of, either returning {@code null} from the |
|
324 |
* method, or the completion of the {@code CompletionStage} returned from |
|
325 |
* the method. The {@code WebSocket} does not access the message while it's |
|
326 |
* not in its control. The {@code Listener} must not access the message |
|
327 |
* after its control has been returned to the {@code WebSocket}. |
|
328 |
* |
|
329 |
* <p> It is the responsibility of the listener to make additional |
|
330 |
* {@linkplain WebSocket#request(long) message requests}, when ready, so |
|
331 |
* that messages are received eventually. |
|
332 |
* |
|
333 |
* <p> Methods above are never invoked with {@code null}s as their |
|
334 |
* arguments. |
|
335 |
* |
|
336 |
* @since 9 |
|
337 |
*/ |
|
338 |
interface Listener { |
|
339 |
||
340 |
/** |
|
341 |
* Notifies the {@code Listener} that it is connected to the provided |
|
342 |
* {@code WebSocket}. |
|
343 |
* |
|
344 |
* <p> The {@code onOpen} method does not correspond to any message |
|
345 |
* from the WebSocket Protocol. It is a synthetic event. It is the first |
|
346 |
* {@code Listener}'s method to be invoked. No other {@code Listener}'s |
|
347 |
* methods are invoked before this one. The method is usually used to |
|
348 |
* make an initial {@linkplain WebSocket#request(long) request} for |
|
349 |
* messages. |
|
350 |
* |
|
351 |
* <p> If an exception is thrown from this method then {@link |
|
352 |
* #onError(WebSocket, Throwable) onError} will be invoked with the |
|
353 |
* exception. |
|
354 |
* |
|
355 |
* @implSpec The default implementation {@linkplain WebSocket#request(long) |
|
356 |
* requests one message}. |
|
357 |
* |
|
358 |
* @param webSocket |
|
359 |
* the WebSocket |
|
360 |
*/ |
|
361 |
default void onOpen(WebSocket webSocket) { webSocket.request(1); } |
|
362 |
||
363 |
/** |
|
364 |
* Receives a Text message. |
|
365 |
* |
|
366 |
* <p> The {@code onText} method is invoked zero or more times between |
|
367 |
* {@code onOpen} and ({@code onClose} or {@code onError}). |
|
368 |
* |
|
369 |
* <p> This message may be a partial UTF-16 sequence. However, the |
|
370 |
* concatenation of all messages through the last will be a whole UTF-16 |
|
371 |
* sequence. |
|
372 |
* |
|
373 |
* <p> If an exception is thrown from this method or the returned {@code |
|
374 |
* CompletionStage} completes exceptionally, then {@link |
|
375 |
* #onError(WebSocket, Throwable) onError} will be invoked with the |
|
376 |
* exception. |
|
377 |
* |
|
378 |
* @implSpec The default implementation {@linkplain WebSocket#request(long) |
|
379 |
* requests one more message}. |
|
380 |
* |
|
38856
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
381 |
* @implNote This implementation passes only complete UTF-16 sequences |
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
382 |
* to the {@code onText} method. |
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
383 |
* |
37874 | 384 |
* @param webSocket |
385 |
* the WebSocket |
|
386 |
* @param message |
|
387 |
* the message |
|
388 |
* @param part |
|
389 |
* the part |
|
390 |
* |
|
391 |
* @return a CompletionStage that completes when the message processing |
|
392 |
* is done; or {@code null} if already done |
|
393 |
*/ |
|
394 |
default CompletionStage<?> onText(WebSocket webSocket, |
|
38856
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
395 |
CharSequence message, |
37874 | 396 |
MessagePart part) { |
397 |
webSocket.request(1); |
|
398 |
return null; |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* Receives a Binary message. |
|
403 |
* |
|
404 |
* <p> The {@code onBinary} method is invoked zero or more times |
|
405 |
* between {@code onOpen} and ({@code onClose} or {@code onError}). |
|
406 |
* |
|
407 |
* <p> If an exception is thrown from this method or the returned {@code |
|
408 |
* CompletionStage} completes exceptionally, then {@link |
|
409 |
* #onError(WebSocket, Throwable) onError} will be invoked with this |
|
410 |
* exception. |
|
411 |
* |
|
412 |
* @implSpec The default implementation {@linkplain WebSocket#request(long) |
|
413 |
* requests one more message}. |
|
414 |
* |
|
415 |
* @param webSocket |
|
416 |
* the WebSocket |
|
417 |
* @param message |
|
418 |
* the message |
|
419 |
* @param part |
|
420 |
* the part |
|
421 |
* |
|
422 |
* @return a CompletionStage that completes when the message processing |
|
423 |
* is done; or {@code null} if already done |
|
424 |
*/ |
|
425 |
default CompletionStage<?> onBinary(WebSocket webSocket, |
|
426 |
ByteBuffer message, |
|
427 |
MessagePart part) { |
|
428 |
webSocket.request(1); |
|
429 |
return null; |
|
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Receives a Ping message. |
|
434 |
* |
|
435 |
* <p> A Ping message may be sent or received by either client or |
|
436 |
* server. It may serve either as a keepalive or as a means to verify |
|
437 |
* that the remote endpoint is still responsive. |
|
438 |
* |
|
439 |
* <p> The message will consist of not more than {@code 125} bytes: |
|
440 |
* {@code message.remaining() <= 125}. |
|
441 |
* |
|
442 |
* <p> The {@code onPing} is invoked zero or more times in between |
|
443 |
* {@code onOpen} and ({@code onClose} or {@code onError}). |
|
444 |
* |
|
445 |
* <p> If an exception is thrown from this method or the returned {@code |
|
446 |
* CompletionStage} completes exceptionally, then {@link |
|
447 |
* #onError(WebSocket, Throwable) onError} will be invoked with this |
|
448 |
* exception. |
|
449 |
* |
|
450 |
* @implNote |
|
451 |
* |
|
452 |
* <p> Replies with a Pong message and requests one more message when |
|
453 |
* the Pong has been sent. |
|
454 |
* |
|
455 |
* @param webSocket |
|
456 |
* the WebSocket |
|
457 |
* @param message |
|
458 |
* the message |
|
459 |
* |
|
460 |
* @return a CompletionStage that completes when the message processing |
|
461 |
* is done; or {@code null} if already done |
|
462 |
*/ |
|
463 |
default CompletionStage<?> onPing(WebSocket webSocket, |
|
464 |
ByteBuffer message) { |
|
465 |
return webSocket.sendPong(message).thenRun(() -> webSocket.request(1)); |
|
466 |
} |
|
467 |
||
468 |
/** |
|
469 |
* Receives a Pong message. |
|
470 |
* |
|
471 |
* <p> A Pong message may be unsolicited or may be received in response |
|
472 |
* to a previously sent Ping. In the latter case, the contents of the |
|
473 |
* Pong is identical to the originating Ping. |
|
474 |
* |
|
475 |
* <p> The message will consist of not more than {@code 125} bytes: |
|
476 |
* {@code message.remaining() <= 125}. |
|
477 |
* |
|
478 |
* <p> The {@code onPong} method is invoked zero or more times in |
|
479 |
* between {@code onOpen} and ({@code onClose} or {@code onError}). |
|
480 |
* |
|
481 |
* <p> If an exception is thrown from this method or the returned {@code |
|
482 |
* CompletionStage} completes exceptionally, then {@link |
|
483 |
* #onError(WebSocket, Throwable) onError} will be invoked with this |
|
484 |
* exception. |
|
485 |
* |
|
486 |
* @implSpec The default implementation {@linkplain WebSocket#request(long) |
|
487 |
* requests one more message}. |
|
488 |
* |
|
489 |
* @param webSocket |
|
490 |
* the WebSocket |
|
491 |
* @param message |
|
492 |
* the message |
|
493 |
* |
|
494 |
* @return a CompletionStage that completes when the message processing |
|
495 |
* is done; or {@code null} if already done |
|
496 |
*/ |
|
497 |
default CompletionStage<?> onPong(WebSocket webSocket, |
|
498 |
ByteBuffer message) { |
|
499 |
webSocket.request(1); |
|
500 |
return null; |
|
501 |
} |
|
502 |
||
503 |
/** |
|
504 |
* Receives a Close message. |
|
505 |
* |
|
506 |
* <p> Once a Close message is received, the server will not send any |
|
507 |
* more messages. |
|
508 |
* |
|
509 |
* <p> A Close message may consist of a close code and a reason for |
|
510 |
* closing. The reason will have a UTF-8 representation not longer than |
|
511 |
* {@code 123} bytes. The reason may be useful for debugging or passing |
|
512 |
* information relevant to the connection but is not necessarily human |
|
513 |
* readable. |
|
514 |
* |
|
515 |
* <p> {@code onClose} is the last invocation on the {@code Listener}. |
|
516 |
* It is invoked at most once, but after {@code onOpen}. If an exception |
|
517 |
* is thrown from this method, it is ignored. |
|
518 |
* |
|
519 |
* @implSpec The default implementation does nothing. |
|
520 |
* |
|
521 |
* @param webSocket |
|
522 |
* the WebSocket |
|
523 |
* @param code |
|
524 |
* an {@code Optional} describing the close code, or |
|
525 |
* an empty {@code Optional} if the message doesn't contain it |
|
526 |
* @param reason |
|
527 |
* the reason of close; can be empty |
|
528 |
*/ |
|
529 |
default void onClose(WebSocket webSocket, Optional<CloseCode> code, |
|
530 |
String reason) { } |
|
531 |
||
532 |
/** |
|
533 |
* Notifies an I/O or protocol error has occurred on the {@code |
|
534 |
* WebSocket}. |
|
535 |
* |
|
536 |
* <p> The {@code onError} method does not correspond to any message |
|
537 |
* from the WebSocket Protocol. It is a synthetic event. {@code onError} |
|
538 |
* is the last invocation on the {@code Listener}. It is invoked at most |
|
539 |
* once but after {@code onOpen}. If an exception is thrown from this |
|
540 |
* method, it is ignored. |
|
541 |
* |
|
542 |
* <p> The WebSocket Protocol requires some errors occurs in the |
|
543 |
* incoming destination must be fatal to the connection. In such cases |
|
544 |
* the implementation takes care of closing the {@code WebSocket}. By |
|
545 |
* the time {@code onError} is invoked, no more messages can be sent on |
|
546 |
* this {@code WebSocket}. |
|
547 |
* |
|
548 |
* @apiNote Errors associated with send operations ({@link |
|
549 |
* WebSocket#sendText(CharSequence, boolean) sendText}, {@link |
|
550 |
* #sendBinary(ByteBuffer, boolean) sendBinary}, {@link |
|
551 |
* #sendPing(ByteBuffer) sendPing}, {@link #sendPong(ByteBuffer) |
|
552 |
* sendPong} and {@link #sendClose(CloseCode, CharSequence) sendClose}) |
|
553 |
* are reported to the {@code CompletionStage} operations return. |
|
554 |
* |
|
555 |
* @implSpec The default implementation does nothing. |
|
556 |
* |
|
557 |
* @param webSocket |
|
558 |
* the WebSocket |
|
559 |
* @param error |
|
560 |
* the error |
|
561 |
*/ |
|
562 |
default void onError(WebSocket webSocket, Throwable error) { } |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* A marker used by {@link WebSocket.Listener} for partial message |
|
567 |
* receiving. |
|
568 |
* |
|
569 |
* @since 9 |
|
570 |
*/ |
|
571 |
enum MessagePart { |
|
572 |
||
573 |
/** |
|
574 |
* The first part of a message in a sequence. |
|
575 |
*/ |
|
576 |
FIRST, |
|
577 |
||
578 |
/** |
|
579 |
* A middle part of a message in a sequence. |
|
580 |
*/ |
|
581 |
PART, |
|
582 |
||
583 |
/** |
|
584 |
* The last part of a message in a sequence. |
|
585 |
*/ |
|
586 |
LAST, |
|
587 |
||
588 |
/** |
|
589 |
* A whole message. The message consists of a single part. |
|
590 |
*/ |
|
591 |
WHOLE; |
|
592 |
||
593 |
/** |
|
594 |
* Tells whether a part of a message received with this marker is the |
|
595 |
* last part. |
|
596 |
* |
|
597 |
* @return {@code true} if LAST or WHOLE, {@code false} otherwise |
|
598 |
*/ |
|
599 |
public boolean isLast() { |
|
600 |
return this == LAST || this == WHOLE; |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
/** |
|
38856
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
605 |
* Sends a Text message with characters from the given {@code CharSequence}. |
37874 | 606 |
* |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
607 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
608 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
609 |
* error occurs. |
37874 | 610 |
* |
611 |
* <p> The {@code CharSequence} should not be modified until the returned |
|
612 |
* {@code CompletableFuture} completes (either normally or exceptionally). |
|
613 |
* |
|
614 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
615 |
* with: |
|
616 |
* <ul> |
|
617 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
618 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
619 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
620 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
621 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
622 |
* or if there is an outstanding send operation; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
623 |
* or if a previous Binary message was not sent with {@code isLast == true} |
37874 | 624 |
* </ul> |
625 |
* |
|
38856
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
626 |
* @implNote This implementation does not accept partial UTF-16 |
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
627 |
* sequences. In case such a sequence is passed, a returned {@code |
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
628 |
* CompletableFuture} completes exceptionally. |
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
629 |
* |
37874 | 630 |
* @param message |
631 |
* the message |
|
632 |
* @param isLast |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
633 |
* {@code true} if this is the final part of the message, |
37874 | 634 |
* {@code false} otherwise |
635 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
636 |
* @return a CompletableFuture with this WebSocket |
37874 | 637 |
* |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
638 |
* @throws IllegalArgumentException |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
639 |
* if {@code message} is a malformed (or an incomplete) UTF-16 sequence |
37874 | 640 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
641 |
CompletableFuture<WebSocket> sendText(CharSequence message, boolean isLast); |
37874 | 642 |
|
643 |
/** |
|
644 |
* Sends a whole Text message with characters from the given {@code |
|
645 |
* CharSequence}. |
|
646 |
* |
|
647 |
* <p> This is a convenience method. For the general case, use {@link |
|
648 |
* #sendText(CharSequence, boolean)}. |
|
649 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
650 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
651 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
652 |
* error occurs. |
37874 | 653 |
* |
654 |
* <p> The {@code CharSequence} should not be modified until the returned |
|
655 |
* {@code CompletableFuture} completes (either normally or exceptionally). |
|
656 |
* |
|
657 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
658 |
* with: |
|
659 |
* <ul> |
|
660 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
661 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
662 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
663 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
664 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
665 |
* or if there is an outstanding send operation; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
666 |
* or if a previous Binary message was not sent with {@code isLast == true} |
37874 | 667 |
* </ul> |
668 |
* |
|
669 |
* @param message |
|
670 |
* the message |
|
671 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
672 |
* @return a CompletableFuture with this WebSocket |
37874 | 673 |
* |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
674 |
* @throws IllegalArgumentException |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
675 |
* if {@code message} is a malformed (or an incomplete) UTF-16 sequence |
37874 | 676 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
677 |
default CompletableFuture<WebSocket> sendText(CharSequence message) { |
37874 | 678 |
return sendText(message, true); |
679 |
} |
|
680 |
||
681 |
/** |
|
682 |
* Sends a whole Text message with characters from {@code |
|
683 |
* CharacterSequence}s provided by the given {@code Stream}. |
|
684 |
* |
|
685 |
* <p> This is a convenience method. For the general case use {@link |
|
686 |
* #sendText(CharSequence, boolean)}. |
|
687 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
688 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
689 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
690 |
* error occurs. |
37874 | 691 |
* |
692 |
* <p> Streamed character sequences should not be modified until the |
|
693 |
* returned {@code CompletableFuture} completes (either normally or |
|
694 |
* exceptionally). |
|
695 |
* |
|
696 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
697 |
* with: |
|
698 |
* <ul> |
|
699 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
700 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
701 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
702 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
703 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
704 |
* or if there is an outstanding send operation; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
705 |
* or if a previous Binary message was not sent with {@code isLast == true} |
37874 | 706 |
* </ul> |
707 |
* |
|
708 |
* @param message |
|
709 |
* the message |
|
710 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
711 |
* @return a CompletableFuture with this WebSocket |
37874 | 712 |
* |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
713 |
* @throws IllegalArgumentException |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
714 |
* if {@code message} is a malformed (or an incomplete) UTF-16 sequence |
37874 | 715 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
716 |
CompletableFuture<WebSocket> sendText(Stream<? extends CharSequence> message); |
37874 | 717 |
|
718 |
/** |
|
719 |
* Sends a Binary message with bytes from the given {@code ByteBuffer}. |
|
720 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
721 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
722 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
723 |
* error occurs. |
37874 | 724 |
* |
725 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
726 |
* with: |
|
727 |
* <ul> |
|
728 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
729 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
730 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
731 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
732 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
733 |
* or if there is an outstanding send operation; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
734 |
* or if a previous Text message was not sent with {@code isLast == true} |
37874 | 735 |
* </ul> |
736 |
* |
|
737 |
* @param message |
|
738 |
* the message |
|
739 |
* @param isLast |
|
740 |
* {@code true} if this is the final part of the message, |
|
741 |
* {@code false} otherwise |
|
742 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
743 |
* @return a CompletableFuture with this WebSocket |
37874 | 744 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
745 |
CompletableFuture<WebSocket> sendBinary(ByteBuffer message, boolean isLast); |
37874 | 746 |
|
747 |
/** |
|
748 |
* Sends a Binary message with bytes from the given {@code byte[]}. |
|
749 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
750 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
751 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
752 |
* error occurs. |
37874 | 753 |
* |
754 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
755 |
* with: |
|
756 |
* <ul> |
|
757 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
758 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
759 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
760 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
761 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
762 |
* or if there is an outstanding send operation; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
763 |
* or if a previous Text message was not sent with {@code isLast == true} |
37874 | 764 |
* </ul> |
765 |
* |
|
766 |
* @implSpec This is equivalent to: |
|
767 |
* <pre>{@code |
|
768 |
* sendBinary(ByteBuffer.wrap(message), isLast) |
|
769 |
* }</pre> |
|
770 |
* |
|
771 |
* @param message |
|
772 |
* the message |
|
773 |
* @param isLast |
|
774 |
* {@code true} if this is the final part of the message, |
|
775 |
* {@code false} otherwise |
|
776 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
777 |
* @return a CompletableFuture with this WebSocket |
37874 | 778 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
779 |
default CompletableFuture<WebSocket> sendBinary(byte[] message, boolean isLast) { |
37874 | 780 |
Objects.requireNonNull(message, "message"); |
781 |
return sendBinary(ByteBuffer.wrap(message), isLast); |
|
782 |
} |
|
783 |
||
784 |
/** |
|
785 |
* Sends a Ping message. |
|
786 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
787 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
788 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
789 |
* error occurs. |
37874 | 790 |
* |
791 |
* <p> A Ping message may be sent or received by either client or server. |
|
792 |
* It may serve either as a keepalive or as a means to verify that the |
|
793 |
* remote endpoint is still responsive. |
|
794 |
* |
|
795 |
* <p> The message must consist of not more than {@code 125} bytes: {@code |
|
796 |
* message.remaining() <= 125}. |
|
797 |
* |
|
798 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
799 |
* with: |
|
800 |
* <ul> |
|
801 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
802 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
803 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
804 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
805 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
806 |
* or if there is an outstanding send operation |
37874 | 807 |
* </ul> |
808 |
* |
|
809 |
* @param message |
|
810 |
* the message |
|
811 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
812 |
* @return a CompletableFuture with this WebSocket |
37874 | 813 |
* |
814 |
* @throws IllegalArgumentException |
|
815 |
* if {@code message.remaining() > 125} |
|
816 |
*/ |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
817 |
CompletableFuture<WebSocket> sendPing(ByteBuffer message); |
37874 | 818 |
|
819 |
/** |
|
820 |
* Sends a Pong message. |
|
821 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
822 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
823 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
824 |
* error occurs. |
37874 | 825 |
* |
826 |
* <p> A Pong message may be unsolicited or may be sent in response to a |
|
827 |
* previously received Ping. In latter case the contents of the Pong is |
|
828 |
* identical to the originating Ping. |
|
829 |
* |
|
830 |
* <p> The message must consist of not more than {@code 125} bytes: {@code |
|
831 |
* message.remaining() <= 125}. |
|
832 |
* |
|
833 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
834 |
* with: |
|
835 |
* <ul> |
|
836 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
837 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
838 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
839 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
840 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
841 |
* or if there is an outstanding send operation |
37874 | 842 |
* </ul> |
843 |
* |
|
844 |
* @param message |
|
845 |
* the message |
|
846 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
847 |
* @return a CompletableFuture with this WebSocket |
37874 | 848 |
* |
849 |
* @throws IllegalArgumentException |
|
850 |
* if {@code message.remaining() > 125} |
|
851 |
*/ |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
852 |
CompletableFuture<WebSocket> sendPong(ByteBuffer message); |
37874 | 853 |
|
854 |
/** |
|
855 |
* Sends a Close message with the given close code and the reason. |
|
856 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
857 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
858 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
859 |
* error occurs. |
37874 | 860 |
* |
861 |
* <p> A Close message may consist of a close code and a reason for closing. |
|
862 |
* The reason must have a valid UTF-8 representation not longer than {@code |
|
863 |
* 123} bytes. The reason may be useful for debugging or passing information |
|
864 |
* relevant to the connection but is not necessarily human readable. |
|
865 |
* |
|
866 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
867 |
* with: |
|
868 |
* <ul> |
|
869 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
870 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
871 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
872 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
873 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
874 |
* or if there is an outstanding send operation |
37874 | 875 |
* </ul> |
876 |
* |
|
877 |
* @param code |
|
878 |
* the close code |
|
879 |
* @param reason |
|
880 |
* the reason; can be empty |
|
881 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
882 |
* @return a CompletableFuture with this WebSocket |
37874 | 883 |
* |
884 |
* @throws IllegalArgumentException |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
885 |
* if {@code reason} doesn't have an UTF-8 representation not longer |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
886 |
* than {@code 123} bytes |
37874 | 887 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
888 |
CompletableFuture<WebSocket> sendClose(CloseCode code, CharSequence reason); |
37874 | 889 |
|
890 |
/** |
|
891 |
* Sends an empty Close message. |
|
892 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
893 |
* <p> Returns a {@code CompletableFuture<WebSocket>} which completes |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
894 |
* normally when the message has been sent or completes exceptionally if an |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
895 |
* error occurs. |
37874 | 896 |
* |
897 |
* <p> The returned {@code CompletableFuture} can complete exceptionally |
|
898 |
* with: |
|
899 |
* <ul> |
|
900 |
* <li> {@link IOException} |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
901 |
* if an I/O error occurs during this operation |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
902 |
* <li> {@link IllegalStateException} |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
903 |
* if the {@code WebSocket} closes while this operation is in progress; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
904 |
* or if a Close message has been sent already; |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
905 |
* or if there is an outstanding send operation |
37874 | 906 |
* </ul> |
907 |
* |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
908 |
* @return a CompletableFuture with this WebSocket |
37874 | 909 |
*/ |
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
38856
diff
changeset
|
910 |
CompletableFuture<WebSocket> sendClose(); |
37874 | 911 |
|
912 |
/** |
|
913 |
* Requests {@code n} more messages to be received by the {@link Listener |
|
914 |
* Listener}. |
|
915 |
* |
|
916 |
* <p> The actual number might be fewer if either of the endpoints decide to |
|
917 |
* close the connection before that or an error occurs. |
|
918 |
* |
|
919 |
* <p> A {@code WebSocket} that has just been created, hasn't requested |
|
920 |
* anything yet. Usually the initial request for messages is done in {@link |
|
921 |
* Listener#onOpen(java.net.http.WebSocket) Listener.onOpen}. |
|
922 |
* |
|
923 |
* If all requested messages have been received, and the server sends more, |
|
924 |
* then these messages are queued. |
|
925 |
* |
|
926 |
* @implNote This implementation does not distinguish between partial and |
|
927 |
* whole messages, because it's not known beforehand how a message will be |
|
928 |
* received. |
|
38856
cc3a0d1e96e0
8156650: Simplify Text message support in WebSocket API
prappo
parents:
37874
diff
changeset
|
929 |
* |
37874 | 930 |
* <p> If a server sends more messages than requested, the implementation |
931 |
* queues up these messages on the TCP connection and may eventually force |
|
932 |
* the sender to stop sending through TCP flow control. |
|
933 |
* |
|
934 |
* @param n |
|
935 |
* the number of messages |
|
936 |
* |
|
937 |
* @throws IllegalArgumentException |
|
938 |
* if {@code n < 0} |
|
939 |
* |
|
940 |
* @return resulting unfulfilled demand with this request taken into account |
|
941 |
*/ |
|
942 |
// TODO return void as it's breaking encapsulation (leaking info when exactly something deemed delivered) |
|
943 |
// or demand behaves after LONG.MAX_VALUE |
|
944 |
long request(long n); |
|
945 |
||
946 |
/** |
|
947 |
* Returns a {@linkplain Builder#subprotocols(String, String...) subprotocol} |
|
948 |
* in use. |
|
949 |
* |
|
950 |
* @return a subprotocol, or {@code null} if there is none |
|
951 |
*/ |
|
952 |
String getSubprotocol(); |
|
953 |
||
954 |
/** |
|
955 |
* Tells whether the {@code WebSocket} is closed. |
|
956 |
* |
|
957 |
* <p> A {@code WebSocket} deemed closed when either the underlying socket |
|
958 |
* is closed or the closing handshake is completed. |
|
959 |
* |
|
960 |
* @return {@code true} if the {@code WebSocket} is closed, |
|
961 |
* {@code false} otherwise |
|
962 |
*/ |
|
963 |
boolean isClosed(); |
|
964 |
||
965 |
/** |
|
966 |
* Closes the {@code WebSocket} abruptly. |
|
967 |
* |
|
968 |
* <p> This method closes the underlying TCP connection. If the {@code |
|
969 |
* WebSocket} is already closed then invoking this method has no effect. |
|
970 |
* |
|
971 |
* @throws IOException |
|
972 |
* if an I/O error occurs |
|
973 |
*/ |
|
974 |
void abort() throws IOException; |
|
975 |
||
976 |
/** |
|
977 |
* A {@code WebSocket} close status code. |
|
978 |
* |
|
979 |
* <p> Some codes <a href="https://tools.ietf.org/html/rfc6455#section-7.4"> |
|
980 |
* specified</a> in the WebSocket Protocol are defined as named constants |
|
981 |
* here. Others can be {@linkplain #of(int) retrieved on demand}. |
|
982 |
* |
|
983 |
* <p> This is a |
|
984 |
* <a href="../../lang/doc-files/ValueBased.html">value-based</a> class; |
|
985 |
* use of identity-sensitive operations (including reference equality |
|
986 |
* ({@code ==}), identity hash code, or synchronization) on instances of |
|
987 |
* {@code CloseCode} may have unpredictable results and should be avoided. |
|
988 |
* |
|
989 |
* @since 9 |
|
990 |
*/ |
|
991 |
final class CloseCode { |
|
992 |
||
993 |
/** |
|
994 |
* Indicates a normal close, meaning that the purpose for which the |
|
995 |
* connection was established has been fulfilled. |
|
996 |
* |
|
997 |
* <p> Numerical representation: {@code 1000} |
|
998 |
*/ |
|
999 |
public static final CloseCode NORMAL_CLOSURE |
|
1000 |
= new CloseCode(1000, "NORMAL_CLOSURE"); |
|
1001 |
||
1002 |
/** |
|
1003 |
* Indicates that an endpoint is "going away", such as a server going |
|
1004 |
* down or a browser having navigated away from a page. |
|
1005 |
* |
|
1006 |
* <p> Numerical representation: {@code 1001} |
|
1007 |
*/ |
|
1008 |
public static final CloseCode GOING_AWAY |
|
1009 |
= new CloseCode(1001, "GOING_AWAY"); |
|
1010 |
||
1011 |
/** |
|
1012 |
* Indicates that an endpoint is terminating the connection due to a |
|
1013 |
* protocol error. |
|
1014 |
* |
|
1015 |
* <p> Numerical representation: {@code 1002} |
|
1016 |
*/ |
|
1017 |
public static final CloseCode PROTOCOL_ERROR |
|
1018 |
= new CloseCode(1002, "PROTOCOL_ERROR"); |
|
1019 |
||
1020 |
/** |
|
1021 |
* Indicates that an endpoint is terminating the connection because it |
|
1022 |
* has received a type of data it cannot accept (e.g., an endpoint that |
|
1023 |
* understands only text data MAY send this if it receives a binary |
|
1024 |
* message). |
|
1025 |
* |
|
1026 |
* <p> Numerical representation: {@code 1003} |
|
1027 |
*/ |
|
1028 |
public static final CloseCode CANNOT_ACCEPT |
|
1029 |
= new CloseCode(1003, "CANNOT_ACCEPT"); |
|
1030 |
||
1031 |
/** |
|
1032 |
* Indicates that an endpoint is terminating the connection because it |
|
1033 |
* has received data within a message that was not consistent with the |
|
1034 |
* type of the message (e.g., non-UTF-8 [RFC3629] data within a text |
|
1035 |
* message). |
|
1036 |
* |
|
1037 |
* <p> Numerical representation: {@code 1007} |
|
1038 |
*/ |
|
1039 |
public static final CloseCode NOT_CONSISTENT |
|
1040 |
= new CloseCode(1007, "NOT_CONSISTENT"); |
|
1041 |
||
1042 |
/** |
|
1043 |
* Indicates that an endpoint is terminating the connection because it |
|
1044 |
* has received a message that violates its policy. This is a generic |
|
1045 |
* status code that can be returned when there is no other more suitable |
|
1046 |
* status code (e.g., {@link #CANNOT_ACCEPT} or {@link #TOO_BIG}) or if |
|
1047 |
* there is a need to hide specific details about the policy. |
|
1048 |
* |
|
1049 |
* <p> Numerical representation: {@code 1008} |
|
1050 |
*/ |
|
1051 |
public static final CloseCode VIOLATED_POLICY |
|
1052 |
= new CloseCode(1008, "VIOLATED_POLICY"); |
|
1053 |
||
1054 |
/** |
|
1055 |
* Indicates that an endpoint is terminating the connection because it |
|
1056 |
* has received a message that is too big for it to process. |
|
1057 |
* |
|
1058 |
* <p> Numerical representation: {@code 1009} |
|
1059 |
*/ |
|
1060 |
public static final CloseCode TOO_BIG |
|
1061 |
= new CloseCode(1009, "TOO_BIG"); |
|
1062 |
||
1063 |
/** |
|
1064 |
* Indicates that an endpoint is terminating the connection because it |
|
1065 |
* encountered an unexpected condition that prevented it from fulfilling |
|
1066 |
* the request. |
|
1067 |
* |
|
1068 |
* <p> Numerical representation: {@code 1011} |
|
1069 |
*/ |
|
1070 |
public static final CloseCode UNEXPECTED_CONDITION |
|
1071 |
= new CloseCode(1011, "UNEXPECTED_CONDITION"); |
|
1072 |
||
1073 |
private static final Map<Integer, CloseCode> cached = Map.ofEntries( |
|
1074 |
entry(NORMAL_CLOSURE), |
|
1075 |
entry(GOING_AWAY), |
|
1076 |
entry(PROTOCOL_ERROR), |
|
1077 |
entry(CANNOT_ACCEPT), |
|
1078 |
entry(NOT_CONSISTENT), |
|
1079 |
entry(VIOLATED_POLICY), |
|
1080 |
entry(TOO_BIG), |
|
1081 |
entry(UNEXPECTED_CONDITION) |
|
1082 |
); |
|
1083 |
||
1084 |
/** |
|
1085 |
* Returns a {@code CloseCode} from its numerical representation. |
|
1086 |
* |
|
1087 |
* <p> The given {@code code} should be in the range {@code 1000 <= code |
|
1088 |
* <= 4999}, and should not be equal to any of the following codes: |
|
1089 |
* {@code 1004}, {@code 1005}, {@code 1006} and {@code 1015}. |
|
1090 |
* |
|
1091 |
* @param code |
|
1092 |
* numerical representation |
|
1093 |
* |
|
1094 |
* @return a close code corresponding to the provided numerical value |
|
1095 |
* |
|
1096 |
* @throws IllegalArgumentException |
|
1097 |
* if {@code code} violates any of the requirements above |
|
1098 |
*/ |
|
1099 |
public static CloseCode of(int code) { |
|
1100 |
if (code < 1000 || code > 4999) { |
|
1101 |
throw new IllegalArgumentException("Out of range: " + code); |
|
1102 |
} |
|
1103 |
if (code == 1004 || code == 1005 || code == 1006 || code == 1015) { |
|
1104 |
throw new IllegalArgumentException("Reserved: " + code); |
|
1105 |
} |
|
1106 |
CloseCode closeCode = cached.get(code); |
|
1107 |
return closeCode != null ? closeCode : new CloseCode(code, ""); |
|
1108 |
} |
|
1109 |
||
1110 |
private final int code; |
|
1111 |
private final String description; |
|
1112 |
||
1113 |
private CloseCode(int code, String description) { |
|
1114 |
assert description != null; |
|
1115 |
this.code = code; |
|
1116 |
this.description = description; |
|
1117 |
} |
|
1118 |
||
1119 |
/** |
|
1120 |
* Returns a numerical representation of this close code. |
|
1121 |
* |
|
1122 |
* @return a numerical representation |
|
1123 |
*/ |
|
1124 |
public int getCode() { |
|
1125 |
return code; |
|
1126 |
} |
|
1127 |
||
1128 |
/** |
|
1129 |
* Compares this close code to the specified object. |
|
1130 |
* |
|
1131 |
* @param o |
|
1132 |
* the object to compare this {@code CloseCode} against |
|
1133 |
* |
|
1134 |
* @return {@code true} iff the argument is a close code with the same |
|
1135 |
* {@linkplain #getCode() numerical representation} as this one |
|
1136 |
*/ |
|
1137 |
@Override |
|
1138 |
public boolean equals(Object o) { |
|
1139 |
if (this == o) { |
|
1140 |
return true; |
|
1141 |
} |
|
1142 |
if (!(o instanceof CloseCode)) { |
|
1143 |
return false; |
|
1144 |
} |
|
1145 |
CloseCode that = (CloseCode) o; |
|
1146 |
return code == that.code; |
|
1147 |
} |
|
1148 |
||
1149 |
@Override |
|
1150 |
public int hashCode() { |
|
1151 |
return code; |
|
1152 |
} |
|
1153 |
||
1154 |
/** |
|
1155 |
* Returns a human-readable representation of this close code. |
|
1156 |
* |
|
1157 |
* @apiNote The representation is not designed to be parsed; the format |
|
1158 |
* may change unexpectedly. |
|
1159 |
* |
|
1160 |
* @return a string representation |
|
1161 |
*/ |
|
1162 |
@Override |
|
1163 |
public String toString() { |
|
1164 |
return code + (description.isEmpty() ? "" : (": " + description)); |
|
1165 |
} |
|
1166 |
||
1167 |
private static Map.Entry<Integer, CloseCode> entry(CloseCode cc) { |
|
1168 |
return Map.entry(cc.getCode(), cc); |
|
1169 |
} |
|
1170 |
} |
|
1171 |
} |