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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.flags;
import com.google.inject.Inject;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.LoggingRequestHandler;
import com.yahoo.restapi.Path;
import com.yahoo.vespa.config.server.http.HttpHandler;
import com.yahoo.vespa.config.server.http.NotFoundException;
import com.yahoo.vespa.configserver.flags.FlagsDb;
import com.yahoo.vespa.flags.FlagId;
import com.yahoo.vespa.flags.Flags;
import com.yahoo.vespa.flags.json.FlagData;
import java.net.URI;
import java.util.Objects;
/**
* Handles /flags/v1 requests
*
* @author hakonhall
*/
public class FlagsHandler extends HttpHandler {
private final FlagsDb flagsDb;
@Inject
public FlagsHandler(LoggingRequestHandler.Context context, FlagsDb flagsDb) {
super(context);
this.flagsDb = flagsDb;
}
@Override
protected HttpResponse handleGET(HttpRequest request) {
Path path = new Path(request.getUri().getPath());
if (path.matches("/flags/v1")) return new V1Response(flagsV1Uri(request), "data", "defined");
if (path.matches("/flags/v1/data")) return getFlagDataList(request);
if (path.matches("/flags/v1/data/{flagId}")) return getFlagData(findFlagId(request, path));
if (path.matches("/flags/v1/defined")) return getDefinedFlagList(request);
throw new NotFoundException("Nothing at path '" + path + "'");
}
@Override
protected HttpResponse handlePUT(HttpRequest request) {
Path path = new Path(request.getUri().getPath());
if (path.matches("/flags/v1/data/{flagId}")) return putFlagData(request, findFlagId(request, path));
throw new NotFoundException("Nothing at path '" + path + "'");
}
@Override
protected HttpResponse handleDELETE(HttpRequest request) {
Path path = new Path(request.getUri().getPath());
if (path.matches("/flags/v1/data/{flagId}")) return deleteFlagData(findFlagId(request, path));
throw new NotFoundException("Nothing at path '" + path + "'");
}
private HttpResponse getDefinedFlagList(HttpRequest request) {
return new DefinedFlags(Flags.getAllFlags());
}
private String flagsV1Uri(HttpRequest request) {
URI uri = request.getUri();
return uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/flags/v1";
}
private HttpResponse getFlagDataList(HttpRequest request) {
return new FlagDataListResponse(flagsV1Uri(request), flagsDb.getAllFlags(),
Objects.equals(request.getProperty("recursive"), "true"));
}
private HttpResponse getFlagData(FlagId flagId) {
FlagData data = flagsDb.getValue(flagId).orElseThrow(() -> new NotFoundException("Flag " + flagId + " not set"));
return new FlagDataResponse(data);
}
private HttpResponse putFlagData(HttpRequest request, FlagId flagId) {
flagsDb.setValue(flagId, FlagData.deserialize(request.getData()));
return new OKResponse();
}
private HttpResponse deleteFlagData(FlagId flagId) {
flagsDb.removeValue(flagId);
return new OKResponse();
}
private FlagId findFlagId(HttpRequest request, Path path) {
FlagId flagId = new FlagId(path.get("flagId"));
if (!Objects.equals(request.getProperty("force"), "true")) {
if (Flags.getAllFlags().stream().noneMatch(definition -> flagId.equals(definition.getUnboundFlag().id()))) {
throw new NotFoundException("There is no flag '" + flagId + "' (use ?force=true to override)");
}
}
return flagId;
}
}
|