Skip to content

Commit 2dfd8f9

Browse files
committed
Added named routes
1 parent 631c9f3 commit 2dfd8f9

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/main/java/org/javawebstack/http/router/HTTPRouter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ public HTTPRouter afterDelete(String pattern, AfterRequestHandler... handlers) {
155155
}
156156

157157
public HTTPRouter route(HTTPMethod method, String pattern, RequestHandler... handlers) {
158-
routes.add(new Route(this, method, pattern, routingOptions, Arrays.asList(handlers)));
158+
return route(null, method, pattern, handlers);
159+
}
160+
161+
public HTTPRouter route(String name, HTTPMethod method, String pattern, RequestHandler... handlers) {
162+
routes.add(new Route(this, method, pattern, routingOptions, Arrays.asList(handlers)).setName(name));
159163
return this;
160164
}
161165

@@ -187,6 +191,18 @@ public HTTPRouter afterRoute(HTTPMethod[] methods, String pattern, AfterRequestH
187191
return this;
188192
}
189193

194+
public List<Route> getBeforeRoutes() {
195+
return beforeRoutes;
196+
}
197+
198+
public List<Route> getRoutes() {
199+
return routes;
200+
}
201+
202+
public List<Route> getAfterRoutes() {
203+
return afterRoutes;
204+
}
205+
190206
public HTTPRouter any(String pattern, RequestHandler... handlers) {
191207
return route(HTTPMethod.values(), pattern, handlers);
192208
}

src/main/java/org/javawebstack/http/router/router/Route.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import java.util.regex.Pattern;
1616

1717
public class Route {
18+
19+
private String name;
1820
private final RouteParamTransformerProvider routeParamTransformerProvider;
1921
private final HTTPMethod method;
20-
private final Pattern pattern;
22+
private final String pattern;
23+
private final Pattern compiledPattern;
2124
private final Map<String, String> variables = new HashMap<>();
2225
private List<RequestHandler> handlers;
2326
private List<AfterRequestHandler> afterHandlers;
@@ -30,6 +33,7 @@ public Route(RouteParamTransformerProvider routeParamTransformerProvider, HTTPMe
3033
this.handlers = handlers;
3134
this.method = method;
3235
this.routeParamTransformerProvider = routeParamTransformerProvider;
36+
this.pattern = pattern;
3337
pattern = pattern.toLowerCase(Locale.ENGLISH);
3438
if(options.isIgnoreTrailingSlash()) {
3539
if (pattern.endsWith("/"))
@@ -90,22 +94,39 @@ public Route(RouteParamTransformerProvider routeParamTransformerProvider, HTTPMe
9094
if(options.isIgnoreTrailingSlash()) {
9195
sb.append("/?");
9296
}
93-
this.pattern = Pattern.compile(sb.toString());
97+
this.compiledPattern = Pattern.compile(sb.toString());
98+
}
99+
100+
public Route setName(String name) {
101+
this.name = name;
102+
return this;
103+
}
104+
105+
public String getName() {
106+
return name;
94107
}
95108

96109
public Route setAfterHandlers(List<AfterRequestHandler> afterHandlers) {
97110
this.afterHandlers = afterHandlers;
98111
return this;
99112
}
100113

114+
public String getPattern() {
115+
return pattern;
116+
}
117+
118+
public Map<String, String> getVariables() {
119+
return variables;
120+
}
121+
101122
public Map<String, Object> match(Exchange exchange) {
102123
return match(exchange, exchange.getMethod(), exchange.getPath());
103124
}
104125

105126
public Map<String, Object> match(Exchange exchange, HTTPMethod method, String path) {
106127
if (this.method != method)
107128
return null;
108-
Matcher matcher = pattern.matcher(path);
129+
Matcher matcher = compiledPattern.matcher(path);
109130
if (matcher.matches()) {
110131
Map<String, Object> params = new HashMap<>();
111132
for (String name : variables.keySet()) {

0 commit comments

Comments
 (0)