diff options
Diffstat (limited to 'controller-server')
10 files changed, 8 insertions, 181 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/Path.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/Path.java deleted file mode 100644 index c6781657b8a..00000000000 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/Path.java +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.controller.restapi; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * A path which is able to match strings containing bracketed placeholders and return the - * values given at the placeholders. - * - * E.g a path /a/1/bar/fuz - * will match /a/{foo}/bar/{b} - * and return foo=1 and b=fuz - * - * Only full path elements may be placeholders, i.e /a{bar} is not interpreted as one. - * - * If the path spec ends with /{*}, it will match urls with any rest path. - * The rest path (not including the trailing slash) will be available as getRest(). - * - * Note that for convenience in common use this has state which is changes as a side effect of each matches - * invocation. It is therefore for single thread use. - * - * @author bratseth - */ -public class Path { - - // This path - private final String pathString; - 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.pathString = path; - this.elements = path.split("/"); - } - - /** - * 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) { - values.clear(); - String[] specElements = pathSpec.split("/"); - boolean matchPrefix = false; - if (specElements[specElements.length-1].equals("{*}")) { - matchPrefix = true; - specElements = Arrays.copyOf(specElements, specElements.length-1); - } - - if (matchPrefix) { - if (this.elements.length < specElements.length) return false; - } - else { // match exact - if (this.elements.length != specElements.length) return false; - } - - for (int i = 0; i < specElements.length; i++) { - if (specElements[i].startsWith("{") && specElements[i].endsWith("}")) // placeholder - values.put(specElements[i].substring(1, specElements[i].length()-1), elements[i]); - else if ( ! specElements[i].equals(this.elements[i])) - return false; - } - - if (matchPrefix) { - StringBuilder rest = new StringBuilder(); - for (int i = specElements.length; i < this.elements.length; i++) - rest.append(elements[i]).append("/"); - if ( ! pathString.endsWith("/") && rest.length() > 0) - rest.setLength(rest.length() - 1); - this.rest = rest.toString(); - } - - return true; - } - - /** - * 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. - */ - public String get(String placeholder) { - return values.get(placeholder); - } - - /** - * Returns the rest of the last matched path. - * This is always the empty string (never null) unless the path spec ends with {*} - */ - public String getRest() { return rest; } - - public String asString() { - return pathString; - } - - @Override - public String toString() { - return "path '" + Arrays.stream(elements).collect(Collectors.joining("/")) + "'"; - } - -} diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java index 2652b49f86f..b8f2ccad879 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java @@ -65,7 +65,7 @@ import com.yahoo.vespa.hosted.controller.application.JobStatus; import com.yahoo.vespa.hosted.controller.application.SourceRevision; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; import com.yahoo.vespa.hosted.controller.restapi.MessageResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.restapi.ResourceResponse; import com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse; import com.yahoo.vespa.hosted.controller.restapi.StringResponse; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/controller/ControllerApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/controller/ControllerApiHandler.java index 6fefb7099f1..19ec6316841 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/controller/ControllerApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/controller/ControllerApiHandler.java @@ -12,7 +12,7 @@ import com.yahoo.vespa.hosted.controller.maintenance.ControllerMaintenance; import com.yahoo.vespa.hosted.controller.maintenance.Upgrader; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; import com.yahoo.vespa.hosted.controller.restapi.MessageResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.restapi.ResourceResponse; import com.yahoo.vespa.hosted.controller.versions.VespaVersion.Confidence; import com.yahoo.yolean.Exceptions; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java index 2f2e72120ea..8a5013a9c16 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java @@ -19,7 +19,7 @@ import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; import com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse; import com.yahoo.vespa.hosted.controller.restapi.Uri; import com.yahoo.vespa.hosted.controller.restapi.application.EmptyJsonResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.yolean.Exceptions; import java.util.Optional; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/ControllerAuthorizationFilter.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/ControllerAuthorizationFilter.java index a05d30a0232..803138e8b3b 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/ControllerAuthorizationFilter.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/ControllerAuthorizationFilter.java @@ -18,7 +18,7 @@ import com.yahoo.vespa.hosted.controller.TenantController; import com.yahoo.vespa.hosted.controller.api.integration.athenz.ApplicationAction; import com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzClientFactory; import com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsException; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.tenant.AthenzTenant; import com.yahoo.vespa.hosted.controller.tenant.Tenant; import com.yahoo.vespa.hosted.controller.tenant.UserTenant; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/screwdriver/ScrewdriverApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/screwdriver/ScrewdriverApiHandler.java index 26d0b1868ae..74caa4dcb47 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/screwdriver/ScrewdriverApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/screwdriver/ScrewdriverApiHandler.java @@ -12,7 +12,7 @@ import com.yahoo.vespa.hosted.controller.Controller; import com.yahoo.vespa.hosted.controller.api.integration.BuildService; import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse; import com.yahoo.vespa.hosted.controller.versions.VespaVersion; import com.yahoo.yolean.Exceptions; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/statuspage/StatusPageProxyHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/statuspage/StatusPageProxyHandler.java index ad69681fe7e..9021de366cb 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/statuspage/StatusPageProxyHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/statuspage/StatusPageProxyHandler.java @@ -8,7 +8,7 @@ import com.yahoo.container.jdisc.LoggingRequestHandler; import com.yahoo.container.jdisc.secretstore.SecretStore; import com.yahoo.slime.Slime; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse; import com.yahoo.vespa.hosted.controller.statuspage.config.StatuspageConfig; import com.yahoo.yolean.Exceptions; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v1/ZoneApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v1/ZoneApiHandler.java index 28c17770f6a..11c1e5ec6df 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v1/ZoneApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v1/ZoneApiHandler.java @@ -11,7 +11,7 @@ import com.yahoo.slime.Cursor; import com.yahoo.slime.Slime; import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse; import com.yahoo.yolean.Exceptions; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v2/ZoneApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v2/ZoneApiHandler.java index 6eec5c4965d..3d86d5da262 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v2/ZoneApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v2/ZoneApiHandler.java @@ -13,7 +13,7 @@ import com.yahoo.vespa.hosted.controller.proxy.ConfigServerRestExecutor; import com.yahoo.vespa.hosted.controller.proxy.ProxyException; import com.yahoo.vespa.hosted.controller.proxy.ProxyRequest; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse; -import com.yahoo.vespa.hosted.controller.restapi.Path; +import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse; import com.yahoo.yolean.Exceptions; diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/PathTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/PathTest.java deleted file mode 100644 index 0bce930175d..00000000000 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/PathTest.java +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.controller.restapi.application; - -import com.yahoo.vespa.hosted.controller.restapi.Path; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; - -/** - * @author bratseth - */ -public class PathTest { - - @Test - public void testPath() { - assertFalse(new Path("").matches("/a/{foo}/bar/{b}")); - assertFalse(new Path("///").matches("/a/{foo}/bar/{b}")); - assertFalse(new Path("///foo").matches("/a/{foo}/bar/{b}")); - assertFalse(new Path("///bar/").matches("/a/{foo}/bar/{b}")); - Path path = new Path("/a/1/bar/fuz"); - assertTrue(path.matches("/a/{foo}/bar/{b}")); - assertEquals("1", path.get("foo")); - assertEquals("fuz", path.get("b")); - } - - @Test - public void testPathWithRest() { - { - Path path = new Path("/a/1/bar/fuz/"); - assertTrue(path.matches("/a/{foo}/bar/{b}/{*}")); - assertEquals("1", path.get("foo")); - assertEquals("fuz", path.get("b")); - assertEquals("", path.getRest()); - } - - { - Path path = new Path("/a/1/bar/fuz/kanoo"); - assertTrue(path.matches("/a/{foo}/bar/{b}/{*}")); - assertEquals("1", path.get("foo")); - assertEquals("fuz", path.get("b")); - assertEquals("kanoo", path.getRest()); - } - - { - Path path = new Path("/a/1/bar/fuz/kanoo/trips"); - assertTrue(path.matches("/a/{foo}/bar/{b}/{*}")); - assertEquals("1", path.get("foo")); - assertEquals("fuz", path.get("b")); - assertEquals("kanoo/trips", path.getRest()); - } - - { - Path path = new Path("/a/1/bar/fuz/kanoo/trips/"); - assertTrue(path.matches("/a/{foo}/bar/{b}/{*}")); - assertEquals("1", path.get("foo")); - assertEquals("fuz", path.get("b")); - assertEquals("kanoo/trips/", path.getRest()); - } - } - -} |