Skip to content

Commit

Permalink
Support wildcard in path template. (helidon-io#3559)
Browse files Browse the repository at this point in the history
* 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.
3. Update copyright details and documentation for wildcard support in path template.
4. Perform wildcard matching even if it is an optionalSequence
  • Loading branch information
klustria authored and dalexandrov committed Oct 21, 2021
1 parent d47c635 commit 80dc928
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/se/webserver/03_routing.adoc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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', ...)`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -54,6 +54,8 @@
* <td headers="description">A convenience shortcut for {@code /foo/{:.+}}.</td></tr>
* <tr><td headers="construct">{@code /foo[/bar]}</td>
* <td headers="description">A optional section. Translated to regexp: {@code /foo(/bar)?}</td></tr>
* <tr><td headers="construct">{@code /* or /foo*}</td>
* <td headers="description">Wildcard character can be matched with any number of characters.</td></tr>
* </table>
*/
public interface PathMatcher {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -126,6 +126,10 @@ static PathMatcher compile(CharSequence pattern) {
paramCounter++;
}
break;
case '*':
isRegexp = true;
regexp.append(".*?");
break;
default:
shouldContinue = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -32,7 +31,6 @@
/**
* The PathTemplateTest.
*/
@Disabled
public class PathPatternTest {

private String normalize(String path) {
Expand Down Expand Up @@ -229,6 +227,22 @@ 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", "/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]");
Expand Down

0 comments on commit 80dc928

Please sign in to comment.