Thu, 15 Oct 2020 18:36:05 +0200
adds the possibility to specify path parameters to RequestMapping
130
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | package de.uapcore.lightpit; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | import java.util.ArrayList; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | import java.util.List; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | public final class PathPattern { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | private final List<String> nodePatterns; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | private final boolean collection; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | /** |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * Constructs a new path pattern. |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * The special directories . and .. are disallowed in the pattern. |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * @param pattern |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | */ |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | public PathPattern(String pattern) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | nodePatterns = parse(pattern); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | collection = pattern.endsWith("/"); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | private List<String> parse(String pattern) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | var nodes = new ArrayList<String>(); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | var parts = pattern.split("/"); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | for (var part : parts) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | if (part.isBlank()) continue; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | if (part.equals(".") || part.equals("..")) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | throw new IllegalArgumentException("Path must not contain '.' or '..' nodes."); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | nodes.add(part); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | return nodes; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | /** |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | * Matches a path against this pattern. |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | * The path must be canonical in the sense that no . or .. parts occur. |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | * |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | * @param path the path to match |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | * @return true if the path matches the pattern, false otherwise |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | */ |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | public boolean matches(String path) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | if (collection ^ path.endsWith("/")) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | return false; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | var nodes = parse(path); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | if (nodePatterns.size() != nodes.size()) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | return false; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | for (int i = 0 ; i < nodePatterns.size() ; i++) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | var pattern = nodePatterns.get(i); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | var node = nodes.get(i); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | if (pattern.startsWith("$")) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | continue; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | if (!pattern.equals(node)) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | return false; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | return true; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | /** |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | * Returns the path parameters found in the specified path using this pattern. |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | * The return value of this method is undefined, if the patter does not match. |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | * |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | * @param path the path |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | * @return the path parameters, if any, or an empty map |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | * @see #matches(String) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | */ |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | public PathParameters obtainPathParameters(String path) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | var params = new PathParameters(); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | var nodes = parse(path); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | for (int i = 0 ; i < Math.min(nodes.size(), nodePatterns.size()) ; i++) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | var pattern = nodePatterns.get(i); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | var node = nodes.get(i); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | if (pattern.startsWith("$")) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | params.put(pattern.substring(1), node); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | return params; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | @Override |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | public int hashCode() { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | var str = new StringBuilder(); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | for (var node : nodePatterns) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | if (node.startsWith("$")) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | str.append("/$"); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | } else { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | str.append('/'); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | str.append(node); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | if (collection) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | str.append('/'); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | return str.toString().hashCode(); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | @Override |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | public boolean equals(Object obj) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | if (!obj.getClass().equals(PathPattern.class)) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | return false; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | var other = (PathPattern) obj; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | if (collection ^ other.collection || nodePatterns.size() != other.nodePatterns.size()) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | return false; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | for (int i = 0 ; i < nodePatterns.size() ; i++) { |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | var left = nodePatterns.get(i); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | var right = other.nodePatterns.get(i); |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | if (left.startsWith("$") && right.startsWith("$")) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | continue; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | if (!left.equals(right)) |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | return false; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | return true; |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | } |
7ef369744fd1
adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | } |