summaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/Response.java
blob: 9c846e9ce381555c022a31cbe1dadc098ef5d82c (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
// 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"; }

}