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
35public final class WebSocketPatterns {
36
37 private WebSocketPatterns() {
38 }
39
40 public static Optional<Map<String, String>> match(final String pattern, final String path) {
41 if (pattern == null || pattern.isBlank() || path == null || path.isBlank()) {
42 return Optional.empty();
43 }
44 if ("/**".equals(pattern)) {
45 return Optional.of(Map.of());
46 }
47
48 final var patternSegments = split(pattern);
49 final var pathSegments = split(path);
50 if (patternSegments.size() != pathSegments.size()) {
51 return Optional.empty();
52 }
53
54 final var params = new LinkedHashMap<String, String>();
55 for (int i = 0; i < patternSegments.size(); i++) {
56 final var expected = patternSegments.get(i);
57 final var actual = pathSegments.get(i);
58 if (expected.startsWith("{") && expected.endsWith("}")) {
59 params.put(expected.substring(1, expected.length() - 1), actual);
60 continue;
61 }
62 if (!expected.equals(actual)) {
63 return Optional.empty();
64 }
65 }
66
67 return Optional.of(params);
68 }
69
70 private static List<String> split(final String path) {
71 final var cleaned = path.startsWith("/") ? path.substring(1) : path;
72 if (cleaned.isEmpty()) {
73 return List.of();
74 }
75 return Arrays.asList(cleaned.split("/"));
76 }
77}
static Optional< Map< String, String > > match(final String pattern, final String path)