diff options
author | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2019-04-02 14:18:06 +0200 |
---|---|---|
committer | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2019-04-02 14:18:06 +0200 |
commit | 803282854a3b1459b5f547d8b1500450e51611f8 (patch) | |
tree | 18516463f75123d314402443cb52e0beaea82660 /container-core/src/main | |
parent | 67f18277a9d68bb35bca00e25b38378495bcdd6a (diff) |
Create Path from URI with correct decoding and deprecate from String
Diffstat (limited to 'container-core/src/main')
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/Path.java | 39 |
1 files changed, 31 insertions, 8 deletions
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 764fa64f954..fe65245fd15 100644 --- a/container-core/src/main/java/com/yahoo/restapi/Path.java +++ b/container-core/src/main/java/com/yahoo/restapi/Path.java @@ -1,17 +1,22 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.restapi; +import java.net.URI; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.stream.Stream; /** * A path which is able to match strings containing bracketed placeholders and return the - * values given at the placeholders. + * values given at the placeholders. The path is split on '/', and each part is then URL decoded. * - * E.g a path /a/1/bar/fuz - * will match /a/{foo}/bar/{b} - * and return foo=1 and b=fuz + * E.g a path /a/1/bar/fuz/baz/%62%2f + * will match /a/{foo}/bar/{b}/baz/{c} + * and return foo=1, b=fuz, and c=c/ * * Only full path elements may be placeholders, i.e /a{bar} is not interpreted as one. * @@ -21,7 +26,7 @@ import java.util.Map; * 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 + * An 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 @@ -37,18 +42,36 @@ public class Path { private final Map<String, String> values = new HashMap<>(); private String rest = ""; + /** + * @deprecated use {@link #Path(URI)} for correct handling of URL encoded paths. + */ + @Deprecated public Path(String path) { - this.optionalPrefix = ""; - this.pathString = path; - this.elements = path.split("/"); + this(path, ""); } + /** + * @deprecated use {@link #Path(URI, String)} for correct handling of URL encoded paths. + */ + @Deprecated public Path(String path, String optionalPrefix) { this.optionalPrefix = optionalPrefix; this.pathString = path; this.elements = path.split("/"); } + public Path(URI uri) { + this(uri, ""); + } + + public Path(URI uri, String optionalPrefix) { + this.optionalPrefix = optionalPrefix; + this.pathString = uri.getRawPath(); + this.elements = Stream.of(this.pathString.split("/")) + .map(part -> URLDecoder.decode(part, StandardCharsets.UTF_8)) + .toArray(String[]::new); + } + private boolean matchesInner(String pathSpec) { values.clear(); String[] specElements = pathSpec.split("/"); |