summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/status/statuspage/StatusPageResponse.java
blob: 84850eb344cad87997c70db3d57d74b33ae71ef8 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.status.statuspage;

import org.apache.commons.lang.StringEscapeUtils;

import java.io.*;

public class StatusPageResponse {

    private ByteArrayOutputStream output = new ByteArrayOutputStream();
    private String contentType;
    private ResponseCode responseCode = ResponseCode.OK;
    private boolean clientCachingEnabled = false;

    public enum ResponseCode {
        OK(200, "OK"),
        NOT_MODIFIED(304, "Not Modified"),
        BAD_REQUEST(400, "Bad Request"),
        NOT_FOUND(404, "Not Found"),
        INTERNAL_SERVER_ERROR(500, "Internal Server Error");

        private final int code;
        private final String message;
        ResponseCode(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }

    public String getContentType() { return contentType; }
    public ResponseCode getResponseCode() { return responseCode; }
    public ByteArrayOutputStream getOutputStream() { return output; }

    public BufferedWriter createBufferedWriter() {
        return new BufferedWriter(new OutputStreamWriter(output));
    }

    public void writeContent(String content) {
        try {
            BufferedWriter writer = createBufferedWriter();
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setContentType(String type) { contentType = type; }
    public void setResponseCode(ResponseCode responseCode) {
        this.responseCode = responseCode;
    }

    public boolean isClientCachingEnabled() {
        return clientCachingEnabled;
    }

    public void setClientCachingEnabled(boolean clientCachingEnabled) {
        this.clientCachingEnabled = clientCachingEnabled;
    }

    public void writeHtmlHeader(StringBuilder content, String title) {
        String escaped_title = StringEscapeUtils.escapeHtml(title);
        content.append("<html>\n")
               .append("<head><title>").append(escaped_title).append("</title></head>")
               .append("<body>\n")
               .append("<h1>").append(escaped_title).append("</h1>\n");
    }

    public void writeHtmlFooter(StringBuilder content, String hiddenMessage) {
        if (hiddenMessage != null && !hiddenMessage.isEmpty()) {
            content.append("\n<!-- " + StringEscapeUtils.escapeHtml(hiddenMessage) + " -->\n");
        }
        content.append("</body>\n")
               .append("</html>\n");
    }

}