aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/writer/HttpWriter.java
blob: b656965001c75934edfe77355a75566c11e8873f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http.writer;

public class HttpWriter {

    private final StringBuilder builder = new StringBuilder();

    private String title = "Untitled page";
    enum State { HEADER, BODY, FINALIZED }

    private State state = State.HEADER;

    public HttpWriter() {
    }

    public HttpWriter addTitle(String title) {
        verifyState(State.HEADER);
        this.title = title;
        return this;
    }

    public HttpWriter write(String paragraph) {
        verifyState(State.BODY);
        builder.append("    <p>\n")
               .append("      " + paragraph + "\n")
               .append("    </p>\n");
        return this;
    }

    public HttpWriter writeLink(String name, String link) {
        verifyState(State.BODY);
        builder.append("    <a href=\"" + link + "\">" + name + "</a>\n");
        return this;
    }

    private void verifyState(State state) {
        if (this.state == state) return;
        if (state != State.FINALIZED && this.state == State.FINALIZED) {
            throw new IllegalStateException("HTTP page already finalized");
        }
        if (state == State.HEADER && this.state == State.BODY) {
            throw new IllegalStateException("Have already started to write body. Cannot alter header");
        }
        if (this.state == State.HEADER) {
            builder.append("<html>\n"
                    + "  <head>\n"
                    + "    <title>" + title + "</title>\n"
                    + "  </head>\n"
                    + "  <body>\n"
                    + "    <h1>" + title + "</h1>\n");
            this.state = State.BODY;
            if (this.state == state) return;
        }
            // If we get here we are in state body and want to get finalized
        builder.append("  </body>\n"
                     + "</html>\n");
        this.state = State.FINALIZED;
    }

    public String toString() {
        verifyState(State.FINALIZED);
        return builder.toString();
    }

}