forked from storytellersoftware/java-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.java
More file actions
116 lines (87 loc) · 3.16 KB
/
Route.java
File metadata and controls
116 lines (87 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package httpserver;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public abstract class Route {
private List<String> routePath = new ArrayList<>();
private boolean usesVarargs = false;
public Route(String path) {
String[] pathSegments = cleanPath(path).split("/");
for (int i = 0; i < pathSegments.length; i++) {
if (pathSegments[i].isEmpty()) {
continue;
}
routePath.add(pathSegments[i]);
if (pathSegments[i].equals("{*}")) {
if (i != pathSegments.length - 1) {
while (++i < pathSegments.length && pathSegments[i].isEmpty());
if (i != pathSegments.length - 1) {
throw new RuntimeException("\"{*}\" must be the final segment in your path.");
}
usesVarargs = true;
}
}
}
}
public void invoke(HttpRequest request, HttpResponse response) {
Map<String, String> urlParams = new HashMap<>();
List<String> varargs = new ArrayList<>();
List<String> calledPath = request.getSplitPath();
for (int i = 0; i < routePath.size(); i++) {
if (isDynamic(routePath.get(i))) {
urlParams.put(stripDynamic(routePath.get(i)), calledPath.get(i));
}
if (routePath.get(i).equals("{*}")) {
while (i < calledPath.size()) {
varargs.add(calledPath.get(i));
i++;
}
}
}
request.mergeParams(urlParams);
request.mergeVarargs(varargs);
handle(request, response);
}
public int howCorrect(List<String> calledPath) {
// If the paths aren't the same length and it is not an array,
// this is the wrong method.
if (calledPath.size() != routePath.size()) {
if (!usesVarargs) {
return 0;
}
}
// Start count at 1 because of the length matching.
int count = 1;
for (int i = 0; i < routePath.size(); i++) {
// If the paths are equal, give it priority over other methods.
if (routePath.get(i).equals(calledPath.get(i))) {
count += 2;
}
else if (isDynamic(routePath.get(i))) {
count += 1;
}
}
return count;
}
private boolean isDynamic(String path) {
return path.matches("\\{([A-Za-z0-9]{1,}|\\*)\\}");
}
public boolean matchesPerfectly(List<String> path) {
return routePath.equals(path);
}
public static String cleanPath(String path) {
path = path.trim();
if (path.startsWith("/")) {
path = path.substring(1);
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
public static String stripDynamic(String dynamicPath) {
return dynamicPath.substring(1, dynamicPath.length() - 1);
}
public abstract void handle(HttpRequest request, HttpResponse response);
}