Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
WebSocketPatterns.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.util.Arrays;
30import java.util.LinkedHashMap;
31import java.util.List;
32import java.util.Map;
33import java.util.Optional;
34
35/**
36 * Utilidad para el matching de patrones de rutas WebSocket.
37 * Soporta segmentos con variables de captura entre llaves (por ejemplo, {@code /chat/{room}})
38 * y el comodín global {@code /**} que coincide con cualquier path.
39 */
40public final class WebSocketPatterns {
41
42 private WebSocketPatterns() {
43 }
44
45 /**
46 * Comprueba si el path dado coincide con el patrón de ruta especificado.
47 * Si coinciden, devuelve los parámetros de path extraídos como un mapa.
48 *
49 * @param pattern patrón de ruta con segmentos literales o variables {@code {nombre}}
50 * @param path path entrante a comparar contra el patrón
51 * @return un {@link Optional} con el mapa de parámetros extraídos si hay coincidencia;
52 * {@link Optional#empty()} si no coinciden o si los argumentos son inválidos
53 */
54 public static Optional<Map<String, String>> match(final String pattern, final String path) {
55 if (pattern == null || pattern.isBlank() || path == null || path.isBlank()) {
56 return Optional.empty();
57 }
58 if ("/**".equals(pattern)) {
59 return Optional.of(Map.of());
60 }
61
62 final var patternSegments = split(pattern);
63 final var pathSegments = split(path);
64 if (patternSegments.size() != pathSegments.size()) {
65 return Optional.empty();
66 }
67
68 final var params = new LinkedHashMap<String, String>();
69 for (int i = 0; i < patternSegments.size(); i++) {
70 final var expected = patternSegments.get(i);
71 final var actual = pathSegments.get(i);
72 if (expected.startsWith("{") && expected.endsWith("}")) {
73 params.put(expected.substring(1, expected.length() - 1), actual);
74 continue;
75 }
76 if (!expected.equals(actual)) {
77 return Optional.empty();
78 }
79 }
80
81 return Optional.of(params);
82 }
83
84 /**
85 * Divide un path en sus segmentos, eliminando la barra inicial si existe.
86 *
87 * @param path path a dividir (por ejemplo, {@code "/chat/room1"})
88 * @return lista de segmentos no vacíos; lista vacía si el path es raíz o vacío
89 */
90 private static List<String> split(final String path) {
91 final var cleaned = path.startsWith("/") ? path.substring(1) : path;
92 if (cleaned.isEmpty()) {
93 return List.of();
94 }
95 return Arrays.asList(cleaned.split("/"));
96 }
97}
static Optional< Map< String, String > > match(final String pattern, final String path)
Comprueba si el path dado coincide con el patrón de ruta especificado.