From a26899a23b674763706709b18a9cfd8ebb468e36 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Fri, 15 Oct 2021 11:08:16 -0700 Subject: [PATCH 1/4] Support wildcard in path template. 1. Allow wildcard to be used in path template such as /* or /foo* 2. Enabled PathPatternTest unit test that has not been used for a long and added test scenarios relevant to this change. --- .../java/io/helidon/webserver/PathPattern.java | 6 ++++++ .../io/helidon/webserver/PathPatternTest.java | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java b/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java index 363b9fc70bf..0c834d578cf 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java @@ -126,6 +126,12 @@ static PathMatcher compile(CharSequence pattern) { paramCounter++; } break; + case '*': + if (!optionalSequence) { + isRegexp = true; + regexp.append(".*?"); + } + break; default: shouldContinue = false; } diff --git a/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java b/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java index cc66549c8d2..6468c9c3fc9 100644 --- a/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java +++ b/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java @@ -21,7 +21,6 @@ import java.util.Map; import java.util.stream.Collectors; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; @@ -32,7 +31,6 @@ /** * The PathTemplateTest. */ -@Disabled public class PathPatternTest { private String normalize(String path) { @@ -229,6 +227,20 @@ public void testGreedyParams() throws Exception { assertMatchWithParams("/foo/bar/baz/xxx", "/foo/{+}/xxx"); } + @Test + public void testWildCard() throws Exception { + assertMatch("/foo/bar", "/foo*"); + assertMatch("/foo/bar", "/foo/*"); + assertMatch("/foo/bar", "/foo/ba*"); + assertMatch("/foo/bar/baz", "/foo/*"); + assertMatch("/foo/bar/baz", "/foo/ba*"); + assertMatch("/foo/bar/baz", "/foo/*/*"); + assertMatch("/foo/bar/baz", "/foo/*/b*"); + assertNotMatch("/foobar", "/foo/*"); + assertMatchWithParams("/foo/bar/baz", "/foo[/{var}]/*", "var", "bar"); + assertMatchWithParams("/foo/bar/baz", "/foo/*[/{var}]", "var", "baz"); + } + @Test public void testOptionals() throws Exception { assertMatch("/foo/bar", "/foo[/bar]"); From bb861f9acbc3e62cea532eac7491d4818f9f338a Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Fri, 15 Oct 2021 12:19:39 -0700 Subject: [PATCH 2/4] Update copyright details and documentation for wildcard support in path template. --- docs/se/webserver/03_routing.adoc | 1 + .../src/main/java/io/helidon/webserver/PathMatcher.java | 4 +++- .../src/main/java/io/helidon/webserver/PathPattern.java | 2 +- .../src/test/java/io/helidon/webserver/PathPatternTest.java | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/se/webserver/03_routing.adoc b/docs/se/webserver/03_routing.adoc index 3252b5d665f..51e1b01f1ae 100644 --- a/docs/se/webserver/03_routing.adoc +++ b/docs/se/webserver/03_routing.adoc @@ -85,6 +85,7 @@ You can use *path pattern* instead of _path_ with the following syntax: * `/foo/{+var}` - Convenience shortcut for {var:.+}. A matcher is not a true URI template (as defined by RFC) but this convenience is in sync with the Apiary templates * `/foo/{+}` - Convenience shortcut for unnamed segment with regular expression {:.+} * `/foo[/bar]` - An optional block, which translates to the `/foo(/bar)?` regular expression +* `/*` or `/foo*` - `*` Wildcard character can be matched with any number of characters. IMPORTANT: Path (matcher) routing is *exact*. For example, a `/foo/bar` request is *not* routed to `.post('/foo', ...)`. diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/PathMatcher.java b/webserver/webserver/src/main/java/io/helidon/webserver/PathMatcher.java index 0ad40688ce0..84eaadbac80 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/PathMatcher.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/PathMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,6 +54,8 @@ * A convenience shortcut for {@code /foo/{:.+}}. * {@code /foo[/bar]} * A optional section. Translated to regexp: {@code /foo(/bar)?} + * {@code /* or /foo*} + * Wildcard character can be matched with any number of characters. * */ public interface PathMatcher { diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java b/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java index 0c834d578cf..64e0d6766f3 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020 Oracle and/or its affiliates. + * Copyright (c) 2018, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java b/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java index 6468c9c3fc9..c7800008aa0 100644 --- a/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java +++ b/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 70f24c6d00e466ff59cdc05511a3f7a7305fc928 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Fri, 15 Oct 2021 12:28:32 -0700 Subject: [PATCH 3/4] Update copyright. --- docs/se/webserver/03_routing.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/se/webserver/03_routing.adoc b/docs/se/webserver/03_routing.adoc index 51e1b01f1ae..69b5893f932 100644 --- a/docs/se/webserver/03_routing.adoc +++ b/docs/se/webserver/03_routing.adoc @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// - Copyright (c) 2018, 2020 Oracle and/or its affiliates. + Copyright (c) 2018, 2021 Oracle and/or its affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 145de78069f3fa259478da3a22de7c3c3e89ae24 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Mon, 18 Oct 2021 12:33:42 -0700 Subject: [PATCH 4/4] Perform wildcard matching even if it is an optionalSequence --- .../src/main/java/io/helidon/webserver/PathPattern.java | 6 ++---- .../src/test/java/io/helidon/webserver/PathPatternTest.java | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java b/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java index 64e0d6766f3..4778e168056 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/PathPattern.java @@ -127,10 +127,8 @@ static PathMatcher compile(CharSequence pattern) { } break; case '*': - if (!optionalSequence) { - isRegexp = true; - regexp.append(".*?"); - } + isRegexp = true; + regexp.append(".*?"); break; default: shouldContinue = false; diff --git a/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java b/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java index c7800008aa0..45655a15e13 100644 --- a/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java +++ b/webserver/webserver/src/test/java/io/helidon/webserver/PathPatternTest.java @@ -232,6 +232,8 @@ public void testWildCard() throws Exception { assertMatch("/foo/bar", "/foo*"); assertMatch("/foo/bar", "/foo/*"); assertMatch("/foo/bar", "/foo/ba*"); + assertMatch("/foo/bar", "/foo[/*]"); + assertMatch("/foo/bar", "/foo[/ba*]"); assertMatch("/foo/bar/baz", "/foo/*"); assertMatch("/foo/bar/baz", "/foo/ba*"); assertMatch("/foo/bar/baz", "/foo/*/*");