aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/CsvResponse.java
blob: cf45bfb67f0aaf19f29ec26a7e6177ab9fcc5f22 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.billing;

import com.yahoo.container.jdisc.HttpResponse;
import org.apache.commons.csv.CSVFormat;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * @author ogronnesby
 */
class CsvResponse extends HttpResponse {

    private final String[] header;
    private final List<Object[]> rows;

    CsvResponse(String[] header, List<Object[]> rows) {
        super(200);
        this.header = header;
        this.rows = rows;
    }

    @Override
    public void render(OutputStream outputStream) throws IOException {
        var writer = new OutputStreamWriter(outputStream);
        var printer = CSVFormat.DEFAULT.withRecordSeparator('\n').withHeader(this.header).print(writer);
        for (var row : this.rows) printer.printRecord(row);
        printer.flush();
    }

    @Override
    public String getContentType() {
        return "text/csv; encoding=utf-8";
    }

}