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
39/**
40 * Adapta una instancia de {@link WebSocketEndpoint} al Listener de Jetty 12.
41 *
42 * <p>Esta clase traduce los eventos del ciclo de vida de Jetty a las llamadas
43 * definidas en la interfaz {@link WebSocketEndpoint}.</p>
44 */
45final class JettyWebSocketEndpointAdapter implements Session.Listener.AutoDemanding {
46
47 private final WebSocketEndpoint endpoint;
48 private final String path;
49 private final Map<String, String> pathParams;
50 private final Map<String, List<String>> queryParams;
51 private final Map<String, List<String>> headers;
52 private volatile JettyWebSocketSession session;
53
54 /**
55 * Crea un adaptador para el endpoint dado.
56 *
57 * @param endpoint el endpoint personalizado que manejará los eventos
58 * @param path la ruta asociada a la conexión
59 * @param pathParams parámetros extraídos de la ruta
60 * @param queryParams parámetros de la consulta HTTP
61 * @param headers cabeceras HTTP
62 */
63 JettyWebSocketEndpointAdapter(final WebSocketEndpoint endpoint, final String path,
64 final Map<String, String> pathParams, final Map<String, List<String>> queryParams,
65 final Map<String, List<String>> headers) {
66 this.endpoint = endpoint;
67 this.path = path;
68 this.pathParams = pathParams;
69 this.queryParams = queryParams;
70 this.headers = headers;
71 }
72
73 @Override
74 public void onWebSocketOpen(final Session session) {
75 this.session = new JettyWebSocketSession(session, path, pathParams, queryParams, headers);
76 try {
77 endpoint.onOpen(this.session);
78 } catch (final Exception e) {
79 handleFailure(e);
80 }
81 }
82
83 @Override
84 public void onWebSocketText(final String message) {
85 try {
86 endpoint.onText(session, message);
87 } catch (final Exception e) {
88 handleFailure(e);
89 }
90 }
91
92 @Override
93 public void onWebSocketBinary(final ByteBuffer payload, final Callback callback) {
94 try {
95 endpoint.onBinary(session, payload == null ? ByteBuffer.allocate(0) : payload.slice());
96 callback.succeed();
97 } catch (final Exception e) {
98 callback.fail(e);
99 handleFailure(e);
100 }
101 }
102
103 @Override
104 public void onWebSocketPartialText(final String payload, final boolean fin) {
105 if (fin) {
106 onWebSocketText(payload);
107 }
108 }
109
110 @Override
111 public void onWebSocketPartialBinary(final ByteBuffer payload, final boolean fin, final Callback callback) {
112 if (fin) {
113 onWebSocketBinary(payload, callback);
114 return;
115 }
116 callback.succeed();
117 }
118
119 @Override
120 public void onWebSocketClose(final int statusCode, final String reason, final Callback callback) {
121 if (session == null) {
122 callback.succeed();
123 return;
124 }
125 try {
126 endpoint.onClose(session, WebSocketCloseStatus.of(statusCode, reason));
127 callback.succeed();
128 } catch (final Exception e) {
129 endpoint.onError(session, e);
130 callback.fail(e);
131 }
132 }
133
134 private void handleFailure(final Exception error) {
135 endpoint.onError(session, error);
136 if (session != null && session.isOpen()) {
137 session.close(WebSocketCloseStatus.SERVER_ERROR);
138 }
139 }
140
141 @Override
142 public void onWebSocketError(final Throwable cause) {
143 if (session == null) {
144 return;
145 }
146 endpoint.onError(session, cause);
147 }
148}
boolean isOpen()
Returns whether the underlying session is still open.
CompletionStage< Void > close(final WebSocketCloseStatus status)
Closes the session with the given status code and reason.
Define el contrato de un punto final WebSocket.
default void onText(final WebSocketSession session, final String message)
Invocado cuando se recibe un mensaje de texto del cliente.
default void onOpen(final WebSocketSession session)
Invocado cuando la conexión WebSocket se abre exitosamente.
default void onBinary(final WebSocketSession session, final ByteBuffer message)
Invocado cuando se recibe un mensaje binario del cliente.
default void onClose(final WebSocketSession session, final WebSocketCloseStatus closeStatus)
Invocado cuando el cliente solicita cerrar la conexión.
default void onError(final WebSocketSession session, final Throwable error)
Invocado cuando ocurre un error en la conexión WebSocket.