Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
Route.java
Go to the documentation of this file.
1package dev.rafex.ether.http.core;
2
3/*-
4 * #%L
5 * ether-http-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.LinkedHashSet;
32import java.util.List;
33import java.util.Map;
34import java.util.Optional;
35import java.util.Set;
36
37public record Route(String pattern, Set<String> allowedMethods) {
38
39 public Route {
40 allowedMethods = normalizeMethods(allowedMethods);
41 }
42
43 public static Route of(final String pattern, final Set<String> methods) {
44 return new Route(pattern, methods);
45 }
46
47 public boolean allows(final String method) {
48 return allowedMethods.contains(method.toUpperCase());
49 }
50
51 public Optional<Map<String, String>> match(final String relPath) {
52 if ("/**".equals(pattern)) {
53 return Optional.of(Map.of());
54 }
55
56 final var patternSegments = split(pattern);
57 final var pathSegments = split(relPath);
58 if (patternSegments.size() != pathSegments.size()) {
59 return Optional.empty();
60 }
61
62 final var params = new LinkedHashMap<String, String>();
63 for (int i = 0; i < patternSegments.size(); i++) {
64 final var expected = patternSegments.get(i);
65 final var actual = pathSegments.get(i);
66
67 if (expected.startsWith("{") && expected.endsWith("}")) {
68 final var key = expected.substring(1, expected.length() - 1);
69 params.put(key, actual);
70 continue;
71 }
72 if (!expected.equals(actual)) {
73 return Optional.empty();
74 }
75 }
76
77 return Optional.of(params);
78 }
79
80 private static List<String> split(final String path) {
81 final var cleaned = path.startsWith("/") ? path.substring(1) : path;
82 if (cleaned.isEmpty()) {
83 return List.of();
84 }
85 return Arrays.asList(cleaned.split("/"));
86 }
87
88 private static Set<String> normalizeMethods(final Set<String> methods) {
89 final var out = new LinkedHashSet<String>();
90 if (methods != null) {
91 for (final var method : methods) {
92 if (method != null && !method.isBlank()) {
93 out.add(method.trim().toUpperCase());
94 }
95 }
96 }
97 return Set.copyOf(out);
98 }
99}
record Route(String pattern, Set< String > allowedMethods)
Definition Route.java:37