Skip to content

Commit 799bcf8

Browse files
committed
Added Context.matches
1 parent 26bf415 commit 799bcf8

File tree

11 files changed

+50
-2
lines changed

11 files changed

+50
-2
lines changed

jooby/src/main/java/io/jooby/Context.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public interface Context extends Registry {
178178
*/
179179
@Nonnull Route getRoute();
180180

181+
boolean matches(@Nonnull String pattern);
182+
181183
/**
182184
* Set matching route. This is part of public API, but shouldn't be use by application code.
183185
*

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public interface DefaultContext extends Context {
5959
return this;
6060
}
6161

62+
@Override default boolean matches(String pattern) {
63+
return getRouter().match(pattern, pathString());
64+
}
65+
6266
/**
6367
* Get an attribute by his key. This is just an utility method around {@link #getAttributes()}.
6468
* This method look first in current context and fallback to application attributes.

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public ForwardingContext(@Nonnull Context context) {
4949
return this;
5050
}
5151

52+
@Override public boolean matches(@Nonnull String pattern) {
53+
return ctx.matches(pattern);
54+
}
55+
5256
@Override public boolean isSecure() {
5357
return ctx.isSecure();
5458
}

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ public Route route(@Nonnull String method, @Nonnull String pattern,
391391
return router.match(ctx);
392392
}
393393

394+
@Override public boolean match(@Nonnull String pattern, @Nonnull String path) {
395+
return router.match(pattern, path);
396+
}
397+
394398
@Nonnull @Override
395399
public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
396400
@Nonnull StatusCode statusCode) {

jooby/src/main/java/io/jooby/Router.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ interface Match {
536536
*/
537537
@Nonnull Match match(@Nonnull Context ctx);
538538

539+
boolean match(@Nonnull String pattern, @Nonnull String path);
540+
539541
/* Error handler: */
540542

541543
/**

jooby/src/main/java/io/jooby/WebVariables.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
16
package io.jooby;
27

38
import javax.annotation.Nonnull;

jooby/src/main/java/io/jooby/internal/$Chi.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,9 @@ void setEndpoint(String method, String pattern, Route route) {
327327

328328
// Recursive edge traversal by checking all nodeTyp groups along the way.
329329
// It's like searching through a multi-dimensional radix trie.
330-
Route findRoute(RouterMatch rctx, String method, String path) {
330+
Route findRoute(RouterMatch rctx, String method, String search) {
331331
Node n = this;
332332
Node nn = n;
333-
String search = path;
334333

335334
for (int ntyp = 0; ntyp < nn.children.length; ntyp++) {
336335
Node[] nds = nn.children[ntyp];
@@ -643,6 +642,14 @@ public RouterMatch find(Context context, MessageEncoder encoder,
643642
return find(context, context.pathString(), encoder, more);
644643
}
645644

645+
public boolean find(String method, String path) {
646+
Route route = staticPaths.get(method + path);
647+
if (route == null) {
648+
return root.findRoute(new RouterMatch(), method, path) != null;
649+
}
650+
return true;
651+
}
652+
646653
public RouterMatch find(Context context, String path, MessageEncoder encoder,
647654
List<RadixTree> more) {
648655
String method = context.getMethod();

jooby/src/main/java/io/jooby/internal/RadixTree.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
interface RadixTree {
1717
void insert(String method, String pattern, Route route);
1818

19+
boolean find(String method, String path);
20+
1921
RouterMatch find(Context context, String path, MessageEncoder encoder, List<RadixTree> more);
2022

2123
default RadixTree with(Predicate<Context> predicate) {
@@ -24,6 +26,10 @@ default RadixTree with(Predicate<Context> predicate) {
2426
RadixTree.this.insert(method, pattern, route);
2527
}
2628

29+
@Override public boolean find(String method, String path) {
30+
return RadixTree.this.find(method, path);
31+
}
32+
2733
@Override
2834
public RouterMatch find(Context context, String path, MessageEncoder encoder,
2935
List<RadixTree> more) {
@@ -42,10 +48,16 @@ public RouterMatch find(Context context, String path, MessageEncoder encoder,
4248

4349
default RadixTree options(boolean ignoreCase, boolean ignoreTrailingSlash) {
4450
return new RadixTree() {
51+
4552
@Override public void insert(String method, String pattern, Route route) {
4653
RadixTree.this.insert(method, pattern, route);
4754
}
4855

56+
@Override public boolean find(String method, String path) {
57+
return RadixTree.this
58+
.find(method, Router.normalizePath(path, ignoreCase, ignoreTrailingSlash));
59+
}
60+
4961
@Override public RouterMatch find(Context context, String path, MessageEncoder encoder,
5062
List<RadixTree> more) {
5163
return RadixTree.this

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public Stack executor(Executor executor) {
105105
}
106106
}
107107

108+
private static final Route ROUTE_MARK = new Route(Router.GET, "/", null);
109+
108110
private ErrorHandler err;
109111

110112
private Map<String, StatusCode> errorCodes;
@@ -530,6 +532,12 @@ public void destroy() {
530532
return chi.find(ctx, ctx.pathString(), encoder, trees);
531533
}
532534

535+
@Override public boolean match(@Nonnull String pattern, @Nonnull String path) {
536+
$Chi chi = new $Chi();
537+
chi.insert(Router.GET, pattern, ROUTE_MARK);
538+
return chi.find(Router.GET, path);
539+
}
540+
533541
@Nonnull @Override public Router errorCode(@Nonnull Class<? extends Throwable> type,
534542
@Nonnull StatusCode statusCode) {
535543
if (errorCodes == null) {

0 commit comments

Comments
 (0)