summaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java
blob: cbc816b4e095243666ab0277e0e6b151f8d710fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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, RestUri.apiErrorCodes errorID) {
        return createErrorResponse(code, errorMessage, null, errorID);
    }

    public static Response createErrorResponse(int code, String errorMessage, RestUri restUri, RestUri.apiErrorCodes errorID) {
        ObjectNode errorNode = objectMapper.createObjectNode();
        errorNode.put("description", errorID.name() + " " + errorMessage);
        errorNode.put("id", errorID.value);

        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.putArray("errors").add(errorNode);
        return new Response(code, Optional.of(objectNode), Optional.ofNullable(restUri));
    }

    @Override
    public void render(OutputStream stream) throws IOException {
        stream.write(jsonMessage.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String getContentType() { return "application/json"; }

}