summaryrefslogtreecommitdiffstats
path: root/sample-apps/http-api-using-request-handlers-and-processors/src/main/java/com/yahoo/demo/DemoRenderer.java
blob: c30adbcf7aa4c6d616c8f489b42d7709c95d53c9 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.demo;

import com.yahoo.processing.Response;
import com.yahoo.processing.rendering.AsynchronousSectionedRenderer;
import com.yahoo.processing.response.Data;
import com.yahoo.processing.response.DataList;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

/**
 * Render a response as plain text. First line is whether an error occurred,
 * second rendering initialization time stamp, then each line is response data
 * (indented according to its place in the hierarchic response), and the last
 * line is time stamp for when the renderer was finished.
 */
public class DemoRenderer extends AsynchronousSectionedRenderer<Response> {

    /**
     * Indent size for rendering hierarchic response data.
     */
    public static final int INDENT_SIZE = 4;

    // Response heading
    private String heading;

    // Just a utility to write strings to output stream
    private Writer writer;

    // current indent in the rendered tree
    String indent;

    /**
     * No global, shared state to set.
     */
    public DemoRenderer() {
    }

    @Override
    public void beginResponse(OutputStream stream) throws IOException {
        writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8.newEncoder());
        if (getResponse().data().request().errors().size() == 0) {
            writer.write("OK\n");
        } else {
            writer.write("Oops!\n");
        }
        writer.write(heading);
        writer.write('\n');
    }

    /**
     * Indent {@link #INDENT_SIZE} spaces for each level in the tree.
     */
    @Override
    public void beginList(DataList<?> list) throws IOException {
        indent = spaces((getRecursionLevel() - 1) * INDENT_SIZE);
    }

    @Override
    public void data(Data data) throws IOException {
        if (!(data instanceof DataProcessor.DemoData)) {
            return;
        }
        writer.write(indent);
        writer.write(((DataProcessor.DemoData) data).content());
        writer.write('\n');
    }

    private static String spaces(int len) {
        StringBuilder s = new StringBuilder(len);
        for (int i = 0; i < len; ++i) {
            s.append(' ');
        }
        return s.toString();
    }

    /**
     * Out-dent one level if not at outermost level.
     */
    @Override
    public void endList(DataList<?> list) throws IOException {
        if (indent.length() == 0) {
            return;
        }
        indent = spaces(indent.length() - INDENT_SIZE);
    }

    @Override
    public void endResponse() throws IOException {
        writer.write("Rendering finished work: " + System.currentTimeMillis());
        writer.write('\n');
        writer.close();
    }

    @Override
    public String getEncoding() {
        return StandardCharsets.UTF_8.name();
    }

    @Override
    public String getMimeType() {
        return "text/plain";
    }

    /**
     * Initialize mutable, per-result set state here.
     */
    @Override
    public void init() {
        long time = System.currentTimeMillis();

        super.init(); // Important! The base class needs to initialize itself.
        heading = "Renderer initialized: " + time;
    }
}