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
120
121
122
123
124
125
126
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;
import com.yahoo.component.annotation.Inject;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.JsonFormat;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.server.RequestHandler;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.config.provision.ApplicationId;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import static com.yahoo.jdisc.http.HttpResponse.Status.*;
/**
* Handler for a list configs operation. Lists all configs in model.
*
* @author vegardh
* @since 5.1.11
*/
public class HttpListConfigsHandler extends HttpHandler {
static final String RECURSIVE_QUERY_PROPERTY = "recursive";
private final RequestHandler requestHandler;
@Inject
public HttpListConfigsHandler(HttpHandler.Context ctx, TenantRepository tenantRepository) {
this(ctx, tenantRepository.defaultTenant().getRequestHandler());
}
public HttpListConfigsHandler(HttpHandler.Context ctx, RequestHandler requestHandler) {
super(ctx);
this.requestHandler = requestHandler;
}
@Override
public HttpResponse handleGET(HttpRequest req) {
boolean recursive = req.getBooleanProperty(RECURSIVE_QUERY_PROPERTY);
Set<ConfigKey<?>> configs = requestHandler.listConfigs(ApplicationId.defaultId(), Optional.empty(), recursive);
String urlBase = Utils.getUrlBase(req, "/config/v1/");
Set<ConfigKey<?>> allConfigs = requestHandler.allConfigsProduced(ApplicationId.defaultId(), Optional.empty());
return new ListConfigsResponse(configs, allConfigs, urlBase, recursive);
}
static class ListConfigsResponse extends HttpResponse {
private final List<ConfigKey<?>> configs;
private final Set<ConfigKey<?>> allConfigs;
private final String urlBase;
private final boolean recursive;
/**
* New list response
*
* @param configs the configs to include in the list
* @param urlBase for example "http://foo.com:19071/config/v1/ (configs are appended to the listed URLs based on configs list)
* @param recursive list recursively
*/
public ListConfigsResponse(Set<ConfigKey<?>> configs, Set<ConfigKey<?>> allConfigs, String urlBase, boolean recursive) {
super(OK);
this.configs = new ArrayList<>(configs);
Collections.sort(this.configs);
this.allConfigs = allConfigs;
this.urlBase = urlBase;
this.recursive = recursive;
}
/**
* The listing URL for this config in this service
*
* @param key config key
* @param rec recursive
* @return url
*/
String toUrl(ConfigKey<?> key, boolean rec) {
return urlBase + key.getNamespace() + "." + key.getName() + "/" + key.getConfigId() + (rec ? "" : "/");
}
@Override
public void render(OutputStream outputStream) throws IOException {
Slime slime = new Slime();
Cursor root = slime.setObject();
Cursor array;
if (!recursive) {
array = root.setArray("children");
for (ConfigKey<?> key : keysThatHaveAChildWithSameName(configs, allConfigs)) {
array.addString(toUrl(key, false));
}
}
array = root.setArray("configs");
for (ConfigKey<?> key : configs) {
array.addString(toUrl(key, true));
}
new JsonFormat(true).encode(outputStream, slime);
}
static Set<ConfigKey<?>> keysThatHaveAChildWithSameName(Collection<ConfigKey<?>> keys, Set<ConfigKey<?>> allConfigs) {
Set<ConfigKey<?>> ret = new LinkedHashSet<>();
for (ConfigKey<?> k : keys) {
if (ListConfigsResponse.hasAChild(k, allConfigs)) ret.add(k);
}
return ret;
}
static boolean hasAChild(ConfigKey<?> key, Set<ConfigKey<?>> keys) {
if ("".equals(key.getConfigId())) return false;
for (ConfigKey<?> k : keys) {
if (!k.getName().equals(key.getName())) continue;
if ("".equals(k.getConfigId())) continue;
if (k.getConfigId().equals(key.getConfigId())) continue;
if (k.getConfigId().startsWith(key.getConfigId())) return true;
}
return false;
}
@Override
public String getContentType() {
return HttpConfigResponse.JSON_CONTENT_TYPE;
}
}
}
|