aboutsummaryrefslogtreecommitdiffstats
path: root/configserver-flags/src/main/java/com/yahoo/vespa/configserver/flags/http/DefinedFlags.java
blob: e32177cf2e1428867584de787f2524741c9d46da (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.configserver.flags.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.jdisc.Response;
import com.yahoo.vespa.flags.FlagDefinition;

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

/**
 * @author hakonhall
 */
public class DefinedFlags extends HttpResponse {
    private static ObjectMapper mapper = new ObjectMapper();
    private static final Comparator<FlagDefinition> sortByFlagId = Comparator.comparing(flagDefinition -> flagDefinition.getUnboundFlag().id());

    private final List<FlagDefinition> flags;

    public DefinedFlags(List<FlagDefinition> flags) {
        super(Response.Status.OK);
        this.flags = flags;
    }

    @Override
    public void render(OutputStream outputStream) throws IOException {
        ObjectNode rootNode = mapper.createObjectNode();
        flags.stream().sorted(sortByFlagId).forEach(flagDefinition -> {
            ObjectNode definitionNode = rootNode.putObject(flagDefinition.getUnboundFlag().id().toString());
            DefinedFlag.renderFlagDefinition(flagDefinition, definitionNode);
        });
        mapper.writeValue(outputStream, rootNode);
    }

    @Override
    public String getContentType() {
        return "application/json";
    }
}