diff options
Diffstat (limited to 'container-core')
-rw-r--r-- | container-core/abi-spec.json | 1 | ||||
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/Path.java | 47 | ||||
-rw-r--r-- | container-core/src/test/java/com/yahoo/restapi/PathTest.java | 15 |
3 files changed, 47 insertions, 16 deletions
diff --git a/container-core/abi-spec.json b/container-core/abi-spec.json index d20d5643647..b21a496cd01 100644 --- a/container-core/abi-spec.json +++ b/container-core/abi-spec.json @@ -862,6 +862,7 @@ ], "methods": [ "public void <init>(java.lang.String)", + "public void <init>(java.lang.String, java.lang.String)", "public boolean matches(java.lang.String)", "public java.lang.String get(java.lang.String)", "public java.lang.String getRest()", diff --git a/container-core/src/main/java/com/yahoo/restapi/Path.java b/container-core/src/main/java/com/yahoo/restapi/Path.java index 7f78572f2d7..3aa23fbc916 100644 --- a/container-core/src/main/java/com/yahoo/restapi/Path.java +++ b/container-core/src/main/java/com/yahoo/restapi/Path.java @@ -21,37 +21,36 @@ import java.util.stream.Collectors; * * Note that for convenience in common use this has state which changes as a side effect of each matches * invocation. It is therefore for single thread use. - * + * + * A optional prefix can be used to match the path spec against an alternative path. This + * is used when you have alternative paths mapped to the same resource. + * * @author bratseth */ public class Path { // This path private final String pathString; + private final String optionalPrefix; private final String[] elements; // Info about the last match private final Map<String, String> values = new HashMap<>(); private String rest = ""; - + public Path(String path) { + this.optionalPrefix = ""; this.pathString = path; this.elements = path.split("/"); } - /** - * Parses the path according to pathSpec - must be called prior to {@link #get} - * - * Returns whether this path matches the given template string. - * If the given template has placeholders, their values (accessible by get) are reset by calling this, - * whether or not the path matches the given template. - * - * This will NOT match empty path elements. - * - * @param pathSpec the path string to match to this - * @return true if the string matches, false otherwise - */ - public boolean matches(String pathSpec) { + public Path(String path, String optionalPrefix) { + this.optionalPrefix = optionalPrefix; + this.pathString = path; + this.elements = path.split("/"); + } + + private boolean matches_inner(String pathSpec) { values.clear(); String[] specElements = pathSpec.split("/"); boolean matchPrefix = false; @@ -87,6 +86,24 @@ public class Path { } /** + * Parses the path according to pathSpec - must be called prior to {@link #get} + * + * Returns whether this path matches the given template string. + * If the given template has placeholders, their values (accessible by get) are reset by calling this, + * whether or not the path matches the given template. + * + * This will NOT match empty path elements. + * + * @param pathSpec the path string to match to this + * @return true if the string matches, false otherwise + */ + public boolean matches(String pathSpec) { + if (matches_inner(pathSpec)) return true; + if (optionalPrefix.isEmpty()) return false; + return matches_inner(optionalPrefix + pathSpec); + } + + /** * Returns the value of the given template variable in the last path matched, or null * if the previous matches call returned false or if this has not matched anything yet. */ diff --git a/container-core/src/test/java/com/yahoo/restapi/PathTest.java b/container-core/src/test/java/com/yahoo/restapi/PathTest.java index 9e77dc7b3f0..886b3ba9c87 100644 --- a/container-core/src/test/java/com/yahoo/restapi/PathTest.java +++ b/container-core/src/test/java/com/yahoo/restapi/PathTest.java @@ -11,7 +11,20 @@ import static org.junit.Assert.assertEquals; * @author bratseth */ public class PathTest { - + + @Test + public void testWithPrefix() { + // Test that a path with a prefix matches spec without the prefix + Path path = new Path("/ball/a/1/bar/fuz", "/ball"); + assertTrue(path.matches("/a/{foo}/bar/{b}")); + assertEquals("1", path.get("foo")); + assertEquals("fuz", path.get("b")); + + // Also test that prefix does not cause false matches + assertFalse(path.matches("/ball/a/{foo}/zoo/{b}")); + } + + @Test public void testPath() { assertFalse(new Path("").matches("/a/{foo}/bar/{b}")); |