Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 0fdec8c

Browse files
committed
add a request.matches(pattern) method fix jooby-project#219
1 parent 17a4451 commit 0fdec8c

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class RequestPathMatcher extends ServerFeature {
7+
8+
{
9+
get("/static/", req -> req.matches("/static"));
10+
11+
get("/static/f.js", req -> req.matches("/static/**"));
12+
13+
get("/admin/**", req -> req.matches("/admin/**"));
14+
}
15+
16+
@Test
17+
public void pathMatcher() throws Exception {
18+
request()
19+
.get("/static")
20+
.expect("true");
21+
22+
request()
23+
.get("/static/f.js")
24+
.expect("true");
25+
26+
request()
27+
.get("/admin")
28+
.expect("true");
29+
30+
request()
31+
.get("/admin/people")
32+
.expect("true");
33+
}
34+
}

jooby/src/main/java/org/jooby/Request.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public String path() {
6666
return req.path();
6767
}
6868

69+
@Override
70+
public boolean matches(final String pattern) {
71+
return req.matches(pattern);
72+
}
73+
6974
@Override
7075
public String contextPath() {
7176
return req.contextPath();
@@ -365,6 +370,14 @@ default Optional<MediaType> accepts(final String... types) {
365370
return accepts(MediaType.valueOf(types));
366371
}
367372

373+
/**
374+
* Test if the given request path matches the pattern.
375+
*
376+
* @param pattern A pattern to test for.
377+
* @return True, if the request path matches the pattern.
378+
*/
379+
boolean matches(String pattern);
380+
368381
/**
369382
* True, if request accept any of the given types.
370383
*

jooby/src/main/java/org/jooby/internal/RequestImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public <T> Optional<T> get(final String name) {
126126
return Optional.ofNullable((T) locals.get(name));
127127
}
128128

129+
@Override
130+
public boolean matches(final String pattern) {
131+
RoutePattern p = new RoutePattern("*", pattern);
132+
return p.matcher(route.path()).matches();
133+
}
134+
129135
@Override
130136
public Map<String, Object> attributes() {
131137
return Collections.unmodifiableMap(locals);

jooby/src/test/java/org/jooby/RequestTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public MediaType type() {
2525
throw new UnsupportedOperationException();
2626
}
2727

28+
@Override
29+
public boolean matches(final String pattern) {
30+
throw new UnsupportedOperationException();
31+
}
32+
2833
@Override
2934
public List<MediaType> accept() {
3035
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)