Skip to content

Commit

Permalink
Added named routes
Browse files Browse the repository at this point in the history
  • Loading branch information
JanHolger committed Sep 4, 2023
1 parent 631c9f3 commit 2dfd8f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
18 changes: 17 additions & 1 deletion src/main/java/org/javawebstack/http/router/HTTPRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ public HTTPRouter afterDelete(String pattern, AfterRequestHandler... handlers) {
}

public HTTPRouter route(HTTPMethod method, String pattern, RequestHandler... handlers) {
routes.add(new Route(this, method, pattern, routingOptions, Arrays.asList(handlers)));
return route(null, method, pattern, handlers);
}

public HTTPRouter route(String name, HTTPMethod method, String pattern, RequestHandler... handlers) {
routes.add(new Route(this, method, pattern, routingOptions, Arrays.asList(handlers)).setName(name));
return this;
}

Expand Down Expand Up @@ -187,6 +191,18 @@ public HTTPRouter afterRoute(HTTPMethod[] methods, String pattern, AfterRequestH
return this;
}

public List<Route> getBeforeRoutes() {
return beforeRoutes;
}

public List<Route> getRoutes() {
return routes;
}

public List<Route> getAfterRoutes() {
return afterRoutes;
}

public HTTPRouter any(String pattern, RequestHandler... handlers) {
return route(HTTPMethod.values(), pattern, handlers);
}
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/org/javawebstack/http/router/router/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import java.util.regex.Pattern;

public class Route {

private String name;
private final RouteParamTransformerProvider routeParamTransformerProvider;
private final HTTPMethod method;
private final Pattern pattern;
private final String pattern;
private final Pattern compiledPattern;
private final Map<String, String> variables = new HashMap<>();
private List<RequestHandler> handlers;
private List<AfterRequestHandler> afterHandlers;
Expand All @@ -30,6 +33,7 @@ public Route(RouteParamTransformerProvider routeParamTransformerProvider, HTTPMe
this.handlers = handlers;
this.method = method;
this.routeParamTransformerProvider = routeParamTransformerProvider;
this.pattern = pattern;
pattern = pattern.toLowerCase(Locale.ENGLISH);
if(options.isIgnoreTrailingSlash()) {
if (pattern.endsWith("/"))
Expand Down Expand Up @@ -90,22 +94,39 @@ public Route(RouteParamTransformerProvider routeParamTransformerProvider, HTTPMe
if(options.isIgnoreTrailingSlash()) {
sb.append("/?");
}
this.pattern = Pattern.compile(sb.toString());
this.compiledPattern = Pattern.compile(sb.toString());
}

public Route setName(String name) {
this.name = name;
return this;
}

public String getName() {
return name;
}

public Route setAfterHandlers(List<AfterRequestHandler> afterHandlers) {
this.afterHandlers = afterHandlers;
return this;
}

public String getPattern() {
return pattern;
}

public Map<String, String> getVariables() {
return variables;
}

public Map<String, Object> match(Exchange exchange) {
return match(exchange, exchange.getMethod(), exchange.getPath());
}

public Map<String, Object> match(Exchange exchange, HTTPMethod method, String path) {
if (this.method != method)
return null;
Matcher matcher = pattern.matcher(path);
Matcher matcher = compiledPattern.matcher(path);
if (matcher.matches()) {
Map<String, Object> params = new HashMap<>();
for (String name : variables.keySet()) {
Expand Down

0 comments on commit 2dfd8f9

Please sign in to comment.