summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOla Aunrønning <olaa@verizonmedia.com>2021-06-11 14:45:35 +0200
committerOla Aunrønning <olaa@verizonmedia.com>2021-06-11 14:45:46 +0200
commitbec0a4df37e1e65fe915eadecef5b6cd7f4ea8c9 (patch)
tree3a58e2cc1bc88f59f9b4b8b28a547bd77591c753
parent7e3caa43004b15230393f958d220f63292c8a15d (diff)
HorizonApiHandler closes stream
-rw-r--r--controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java1
-rw-r--r--controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java22
2 files changed, 16 insertions, 7 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java
index bd6c2607fc2..c9f6f67df6c 100644
--- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java
+++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java
@@ -1,7 +1,6 @@
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.horizon;
-import com.yahoo.slime.Slime;
import java.io.InputStream;
/**
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 422b8f22000..be8613f5eff 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
@@ -16,7 +16,9 @@ import com.yahoo.yolean.Exceptions;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.UncheckedIOException;
import java.util.Optional;
+import java.util.function.Supplier;
import java.util.logging.Level;
/**
@@ -57,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 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")));
+ 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")));
return ErrorResponse.notFoundError("Nothing at " + path);
}
@@ -73,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 new JsonInputStreamResponse(client.getUser());
+ if (path.matches("/horizon/v1/config/user")) return jsonInputStreamResponse(client::getUser);
return ErrorResponse.notFoundError("Nothing at " + path);
}
@@ -81,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 new JsonInputStreamResponse(client.getMetrics(data));
+ return jsonInputStreamResponse(() -> client.getMetrics(data));
} catch (TsdbQueryRewriter.UnauthorizedException e) {
return ErrorResponse.forbidden("Access denied");
} catch (IOException e) {
@@ -96,6 +98,14 @@ 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;