Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyWebSocketEndpointAdapter.java
Go to the documentation of this file.
1package dev.rafex.ether.websocket.jetty12;
2
3/*-
4 * #%L
5 * ether-websocket-jetty12
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.nio.ByteBuffer;
30import java.util.List;
31import java.util.Map;
32
33import org.eclipse.jetty.websocket.api.Callback;
34import org.eclipse.jetty.websocket.api.Session;
35
36import dev.rafex.ether.websocket.core.WebSocketCloseStatus;
37import dev.rafex.ether.websocket.core.WebSocketEndpoint;
38
39final class JettyWebSocketEndpointAdapter implements Session.Listener.AutoDemanding {
40
41 private final WebSocketEndpoint endpoint;
42 private final String path;
43 private final Map<String, String> pathParams;
44 private final Map<String, List<String>> queryParams;
45 private final Map<String, List<String>> headers;
46 private volatile JettyWebSocketSession session;
47
48 JettyWebSocketEndpointAdapter(final WebSocketEndpoint endpoint, final String path, final Map<String, String> pathParams,
49 final Map<String, List<String>> queryParams, final Map<String, List<String>> headers) {
50 this.endpoint = endpoint;
51 this.path = path;
52 this.pathParams = pathParams;
53 this.queryParams = queryParams;
54 this.headers = headers;
55 }
56
57 @Override
58 public void onWebSocketOpen(final Session session) {
59 this.session = new JettyWebSocketSession(session, path, pathParams, queryParams, headers);
60 try {
61 endpoint.onOpen(this.session);
62 } catch (final Exception e) {
63 handleFailure(e);
64 }
65 }
66
67 @Override
68 public void onWebSocketText(final String message) {
69 try {
70 endpoint.onText(session, message);
71 } catch (final Exception e) {
72 handleFailure(e);
73 }
74 }
75
76 @Override
77 public void onWebSocketBinary(final ByteBuffer payload, final Callback callback) {
78 try {
79 endpoint.onBinary(session, payload == null ? ByteBuffer.allocate(0) : payload.slice());
80 callback.succeed();
81 } catch (final Exception e) {
82 callback.fail(e);
83 handleFailure(e);
84 }
85 }
86
87 @Override
88 public void onWebSocketPartialText(final String payload, final boolean fin) {
89 if (fin) {
90 onWebSocketText(payload);
91 }
92 }
93
94 @Override
95 public void onWebSocketPartialBinary(final ByteBuffer payload, final boolean fin, final Callback callback) {
96 if (fin) {
97 onWebSocketBinary(payload, callback);
98 return;
99 }
100 callback.succeed();
101 }
102
103 @Override
104 public void onWebSocketClose(final int statusCode, final String reason, final Callback callback) {
105 if (session == null) {
106 callback.succeed();
107 return;
108 }
109 try {
110 endpoint.onClose(session, WebSocketCloseStatus.of(statusCode, reason));
111 callback.succeed();
112 } catch (final Exception e) {
113 endpoint.onError(session, e);
114 callback.fail(e);
115 }
116 }
117
118 private void handleFailure(final Exception error) {
119 endpoint.onError(session, error);
120 if (session != null && session.isOpen()) {
121 session.close(WebSocketCloseStatus.SERVER_ERROR);
122 }
123 }
124
125 @Override
126 public void onWebSocketError(final Throwable cause) {
127 if (session == null) {
128 return;
129 }
130 endpoint.onError(session, cause);
131 }
132}
CompletionStage< Void > close(final WebSocketCloseStatus status)
default void onText(final WebSocketSession session, final String message)
default void onOpen(final WebSocketSession session)
default void onBinary(final WebSocketSession session, final ByteBuffer message)
default void onClose(final WebSocketSession session, final WebSocketCloseStatus closeStatus)
default void onError(final WebSocketSession session, final Throwable error)