summaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java
diff options
context:
space:
mode:
Diffstat (limited to 'vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java')
-rw-r--r--vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java b/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java
new file mode 100644
index 00000000000..9c846e9ce38
--- /dev/null
+++ b/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java
@@ -0,0 +1,48 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.document.restapi;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.yahoo.container.jdisc.HttpResponse;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+
+public class Response extends HttpResponse {
+
+ private final static ObjectMapper objectMapper = new ObjectMapper();
+ private final String jsonMessage;
+
+ public Response(int code, Optional<ObjectNode> element, Optional<RestUri> restPath) {
+ super(code);
+ ObjectNode objectNode = element.orElse(objectMapper.createObjectNode());
+ if (restPath.isPresent()) {
+ objectNode.put("id", restPath.get().generateFullId());
+ objectNode.put("pathId", restPath.get().getRawPath());
+ }
+ jsonMessage = objectNode.toString();
+ }
+
+ public static Response createErrorResponse(int code, String errorMessage) {
+ ObjectNode objectNode = objectMapper.createObjectNode();
+ objectNode.putArray("errors").add(errorMessage);
+ return new Response(code, Optional.of(objectNode), Optional.<RestUri>empty());
+ }
+
+ public static Response createErrorResponse(int code, String errorMessage, RestUri restUri) {
+ ObjectNode objectNode = objectMapper.createObjectNode();
+ objectNode.putArray("errors").add(errorMessage);
+ return new Response(code, Optional.of(objectNode), Optional.of(restUri));
+ }
+
+ @Override
+ public void render(OutputStream stream) throws IOException {
+ stream.write(jsonMessage.getBytes(StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public String getContentType() { return "application/json"; }
+
+} \ No newline at end of file