diff options
author | Jon Marius Venstad <jonmv@gmail.com> | 2022-04-07 16:29:59 +0200 |
---|---|---|
committer | Jon Marius Venstad <jonmv@gmail.com> | 2022-04-07 16:29:59 +0200 |
commit | 75114c6c4b96f507071a2caf145b24a22ef0c5ff (patch) | |
tree | e4cca74c128bfdad380bc74a9f501d981b9c78ef /container-core | |
parent | 190dde5de267462b02c43a18ad605da469715949 (diff) |
Use HttpURL for base url and query in StateRequestHandler
Diffstat (limited to 'container-core')
4 files changed, 21 insertions, 11 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 f11c1b3189d..40f281a948e 100644 --- a/container-core/src/main/java/com/yahoo/restapi/Path.java +++ b/container-core/src/main/java/com/yahoo/restapi/Path.java @@ -105,6 +105,13 @@ public class Path { return rest; } + /** + * The path this holds. + */ + public HttpURL.Path getPath() { + return path; + } + @Override public String toString() { return path.toString(); diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApi.java b/container-core/src/main/java/com/yahoo/restapi/RestApi.java index 05528bc79e2..5dfb19029cc 100644 --- a/container-core/src/main/java/com/yahoo/restapi/RestApi.java +++ b/container-core/src/main/java/com/yahoo/restapi/RestApi.java @@ -129,12 +129,9 @@ public interface RestApi { Optional<RequestContent> requestContent(); RequestContent requestContentOrThrow(); ObjectMapper jacksonJsonMapper(); - /** - * Creates a URI builder pre-initialized with scheme, host and port. - * Intended for response generation (e.g for interactive REST APIs). - * DO NOT USE FOR CUSTOM ROUTING. - */ - UriBuilder uriBuilder(); + /** Scheme, domain and port, for the original request. <em>Use this only for generating resources links, not for custom routing!</em> */ + // TODO: this needs to include path and query as well, to be useful for generating resource links that need not be rewritten. + HttpURL baseRequestURL(); AclMapping.Action aclAction(); Optional<Principal> userPrincipal(); Principal userPrincipalOrThrow(); @@ -155,9 +152,11 @@ public interface RestApi { } interface PathParameters extends Parameters { + HttpURL.Path getFullPath(); Optional<HttpURL.Path> getRest(); } interface QueryParameters extends Parameters { + HttpURL.Query getFullQuery(); List<String> getStringList(String name); } interface Headers extends Parameters {} diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java b/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java index ccc3dd49795..1bde8d635a5 100644 --- a/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java +++ b/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java @@ -2,6 +2,7 @@ package com.yahoo.restapi; import ai.vespa.http.HttpURL; +import ai.vespa.http.HttpURL.Query; import com.fasterxml.jackson.databind.ObjectMapper; import com.yahoo.container.jdisc.AclMapping; import com.yahoo.container.jdisc.HttpRequest; @@ -399,7 +400,7 @@ class RestApiImpl implements RestApi { return requestContent().orElseThrow(() -> new RestApiException.BadRequest("Request content missing")); } @Override public ObjectMapper jacksonJsonMapper() { return jacksonJsonMapper; } - @Override public UriBuilder uriBuilder() { + @Override public HttpURL baseRequestURL() { URI uri = request.getUri(); // Reconstruct the URI used by the client to access the API. // This is needed for producing URIs in the response that links to other parts of the Rest API. @@ -409,7 +410,7 @@ class RestApiImpl implements RestApi { if (hostHeader == null || hostHeader.isBlank()) { hostHeader = request.getHeader("Host"); } - if (hostHeader != null && !hostHeader.isBlank()) { + if (hostHeader != null && ! hostHeader.isBlank()) { sb.append(hostHeader); } else { sb.append(uri.getHost()); @@ -417,7 +418,7 @@ class RestApiImpl implements RestApi { sb.append(":").append(uri.getPort()); } } - return new UriBuilder(sb.toString()); + return HttpURL.from(URI.create(sb.toString())); } @Override public AclMapping.Action aclAction() { return aclAction; } @Override public Optional<Principal> userPrincipal() { @@ -436,10 +437,12 @@ class RestApiImpl implements RestApi { return getString(name) .orElseThrow(() -> new RestApiException.BadRequest("Path parameter '" + name + "' is missing")); } + @Override public HttpURL.Path getFullPath() { + return pathMatcher.getPath(); + } @Override public Optional<HttpURL.Path> getRest() { return Optional.ofNullable(pathMatcher.getRest()); } - } private class QueryParametersImpl implements RestApi.RequestContext.QueryParameters { @@ -453,6 +456,7 @@ class RestApiImpl implements RestApi { if (result == null) return List.of(); return List.copyOf(result); } + @Override public HttpURL.Query getFullQuery() { return Query.empty().add(request.getJDiscRequest().parameters()); } } private class HeadersImpl implements RestApi.RequestContext.Headers { diff --git a/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java b/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java index 1b03d87f405..d4a53fb5d85 100644 --- a/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java +++ b/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java @@ -111,7 +111,7 @@ class RestApiImplTest { @Test void uri_builder_creates_valid_uri_prefix() { RestApi restApi = RestApi.builder() - .addRoute(route("/test").get(ctx -> new MessageResponse(ctx.uriBuilder().toString()))) + .addRoute(route("/test").get(ctx -> new MessageResponse(ctx.baseRequestURL().toString()))) .build(); verifyJsonResponse(restApi, Method.GET, "/test", null, 200, "{\"message\":\"http://localhost\"}"); verifyJsonResponse(restApi, Method.GET, "/test", null, 200, "{\"message\":\"http://mydomain:81\"}", Map.of("Host", "mydomain:81")); |