Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
WebSocketSession.java
Go to the documentation of this file.
1package dev.rafex.ether.websocket.core;
2
3/*-
4 * #%L
5 * ether-websocket-core
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;
32import java.util.concurrent.CompletionStage;
33
34/**
35 * Representa una sesión WebSocket activa. Proporciona acceso a los metadatos de la conexión
36 * (identificador, path, headers, parámetros) y métodos para enviar mensajes y cerrar la sesión.
37 */
38public interface WebSocketSession {
39
40 /**
41 * Devuelve el identificador único de esta sesión.
42 *
43 * @return cadena que identifica unívocamente la sesión
44 */
45 String id();
46
47 /**
48 * Devuelve el path de la petición WebSocket que originó esta sesión.
49 *
50 * @return path de la URI de la conexión
51 */
52 String path();
53
54 /**
55 * Devuelve el subprotocolo WebSocket negociado durante el handshake.
56 *
57 * @return nombre del subprotocolo seleccionado, o cadena vacía si no se negoció
58 */
59 String subprotocol();
60
61 /**
62 * Indica si la conexión WebSocket sigue abierta.
63 *
64 * @return {@code true} si la sesión está activa; {@code false} si ya se cerró
65 */
66 boolean isOpen();
67
68 /**
69 * Devuelve el valor del parámetro de path con el nombre especificado.
70 *
71 * @param name nombre del parámetro de path
72 * @return valor del parámetro, o {@code null} si no existe
73 */
74 String pathParam(String name);
75
76 /**
77 * Devuelve el primer valor del parámetro de query con el nombre especificado.
78 *
79 * @param name nombre del parámetro de query
80 * @return primer valor del parámetro, o {@code null} si no existe
81 */
82 String queryFirst(String name);
83
84 /**
85 * Devuelve todos los valores del parámetro de query con el nombre especificado.
86 *
87 * @param name nombre del parámetro de query
88 * @return lista de valores asociados; vacía si el parámetro no existe
89 */
90 List<String> queryAll(String name);
91
92 /**
93 * Devuelve el primer valor del header HTTP con el nombre especificado.
94 *
95 * @param name nombre del header (case-insensitive)
96 * @return primer valor del header, o {@code null} si no existe
97 */
98 String headerFirst(String name);
99
100 /**
101 * Devuelve un atributo almacenado en esta sesión bajo el nombre dado.
102 *
103 * @param name clave del atributo
104 * @return valor del atributo, o {@code null} si no existe
105 */
106 Object attribute(String name);
107
108 /**
109 * Almacena un atributo en esta sesión bajo el nombre dado.
110 *
111 * @param name clave del atributo
112 * @param value valor a almacenar
113 */
114 void attribute(String name, Object value);
115
116 /**
117 * Devuelve todos los parámetros de path extraídos durante el matching de ruta.
118 *
119 * @return mapa inmutable de nombres de parámetro a sus valores
120 */
121 Map<String, String> pathParams();
122
123 /**
124 * Devuelve todos los parámetros de query de la URI de la conexión.
125 *
126 * @return mapa de nombres de parámetro a listas de valores
127 */
128 Map<String, List<String>> queryParams();
129
130 /**
131 * Devuelve todos los headers HTTP de la petición de handshake.
132 *
133 * @return mapa de nombres de header a listas de valores
134 */
135 Map<String, List<String>> headers();
136
137 /**
138 * Envía un mensaje de texto al cliente conectado.
139 *
140 * @param text contenido de texto a enviar
141 * @return {@link CompletionStage} que se completa cuando el mensaje se envía
142 */
143 CompletionStage<Void> sendText(String text);
144
145 /**
146 * Envía un mensaje binario al cliente conectado.
147 *
148 * @param data contenido binario a enviar
149 * @return {@link CompletionStage} que se completa cuando el mensaje se envía
150 */
151 CompletionStage<Void> sendBinary(ByteBuffer data);
152
153 /**
154 * Cierra la conexión WebSocket con el estado de cierre especificado.
155 *
156 * @param status estado de cierre a enviar al cliente
157 * @return {@link CompletionStage} que se completa cuando la conexión se cierra
158 */
159 CompletionStage<Void> close(WebSocketCloseStatus status);
160}
Representa una sesión WebSocket activa.
boolean isOpen()
Indica si la conexión WebSocket sigue abierta.
void attribute(String name, Object value)
Almacena un atributo en esta sesión bajo el nombre dado.
Object attribute(String name)
Devuelve un atributo almacenado en esta sesión bajo el nombre dado.
CompletionStage< Void > close(WebSocketCloseStatus status)
Cierra la conexión WebSocket con el estado de cierre especificado.
List< String > queryAll(String name)
Devuelve todos los valores del parámetro de query con el nombre especificado.
CompletionStage< Void > sendBinary(ByteBuffer data)
Envía un mensaje binario al cliente conectado.
String pathParam(String name)
Devuelve el valor del parámetro de path con el nombre especificado.
Map< String, List< String > > queryParams()
Devuelve todos los parámetros de query de la URI de la conexión.
Map< String, String > pathParams()
Devuelve todos los parámetros de path extraídos durante el matching de ruta.
CompletionStage< Void > sendText(String text)
Envía un mensaje de texto al cliente conectado.
String path()
Devuelve el path de la petición WebSocket que originó esta sesión.
String id()
Devuelve el identificador único de esta sesión.
String queryFirst(String name)
Devuelve el primer valor del parámetro de query con el nombre especificado.
String headerFirst(String name)
Devuelve el primer valor del header HTTP con el nombre especificado.
String subprotocol()
Devuelve el subprotocolo WebSocket negociado durante el handshake.
Map< String, List< String > > headers()
Devuelve todos los headers HTTP de la petición de handshake.
record WebSocketCloseStatus(int code, String reason)
Representa un estado de cierre de una conexión WebSocket conforme a la especificación RFC 6455.