diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2021-06-11 17:40:15 +0200 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2021-06-11 17:40:15 +0200 |
commit | 0a37561a0890524b7454c42550588f96fae254f0 (patch) | |
tree | 21a2c34dff271cf3d7e57037a89c456121ffb4c5 /controller-server | |
parent | f163dd2b355819e490fd8f2e0327a3a6950bb94a (diff) |
Don't close stream before rendering body
Diffstat (limited to 'controller-server')
-rw-r--r-- | controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java index be8613f5eff..035d32e1837 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java @@ -59,10 +59,10 @@ public class HorizonApiHandler extends LoggingRequestHandler { private HttpResponse get(HttpRequest request) { Path path = new Path(request.getUri()); - if (path.matches("/horizon/v1/config/dashboard/topFolders")) return jsonInputStreamResponse(client::getTopFolders); - if (path.matches("/horizon/v1/config/dashboard/file/{id}")) return jsonInputStreamResponse(() -> client.getDashboard(path.get("id"))); - if (path.matches("/horizon/v1/config/dashboard/favorite")) return jsonInputStreamResponse(() -> client.getFavorite(request.getProperty("user"))); - if (path.matches("/horizon/v1/config/dashboard/recent")) return jsonInputStreamResponse(() -> client.getRecent(request.getProperty("user"))); + if (path.matches("/horizon/v1/config/dashboard/topFolders")) return new JsonInputStreamResponse(client::getTopFolders); + if (path.matches("/horizon/v1/config/dashboard/file/{id}")) return new JsonInputStreamResponse(() -> client.getDashboard(path.get("id"))); + if (path.matches("/horizon/v1/config/dashboard/favorite")) return new JsonInputStreamResponse(() -> client.getFavorite(request.getProperty("user"))); + if (path.matches("/horizon/v1/config/dashboard/recent")) return new JsonInputStreamResponse(() -> client.getRecent(request.getProperty("user"))); return ErrorResponse.notFoundError("Nothing at " + path); } @@ -75,7 +75,7 @@ public class HorizonApiHandler extends LoggingRequestHandler { private HttpResponse put(HttpRequest request) { Path path = new Path(request.getUri()); - if (path.matches("/horizon/v1/config/user")) return jsonInputStreamResponse(client::getUser); + if (path.matches("/horizon/v1/config/user")) return new JsonInputStreamResponse(client::getUser); return ErrorResponse.notFoundError("Nothing at " + path); } @@ -83,7 +83,7 @@ public class HorizonApiHandler extends LoggingRequestHandler { SecurityContext securityContext = getAttribute(request, SecurityContext.ATTRIBUTE_NAME, SecurityContext.class); try { byte[] data = TsdbQueryRewriter.rewrite(request.getData().readAllBytes(), securityContext.roles(), systemName); - return jsonInputStreamResponse(() -> client.getMetrics(data)); + return new JsonInputStreamResponse(() -> client.getMetrics(data)); } catch (TsdbQueryRewriter.UnauthorizedException e) { return ErrorResponse.forbidden("Access denied"); } catch (IOException e) { @@ -98,21 +98,13 @@ public class HorizonApiHandler extends LoggingRequestHandler { .orElseThrow(() -> new IllegalArgumentException("Attribute '" + attributeName + "' was not set on request")); } - private JsonInputStreamResponse jsonInputStreamResponse(Supplier<InputStream> supplier) { - try (InputStream inputStream = supplier.get()) { - return new JsonInputStreamResponse(inputStream); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - private static class JsonInputStreamResponse extends HttpResponse { - private final InputStream jsonInputStream; + private final Supplier<InputStream> inputStreamSupplier; - public JsonInputStreamResponse(InputStream jsonInputStream) { + public JsonInputStreamResponse(Supplier<InputStream> inputStreamSupplier) { super(200); - this.jsonInputStream = jsonInputStream; + this.inputStreamSupplier = inputStreamSupplier; } @Override @@ -122,7 +114,9 @@ public class HorizonApiHandler extends LoggingRequestHandler { @Override public void render(OutputStream outputStream) throws IOException { - jsonInputStream.transferTo(outputStream); + try (InputStream inputStream = inputStreamSupplier.get()) { + inputStream.transferTo(outputStream); + } } } } |