-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSiteService.java
More file actions
381 lines (325 loc) · 14.2 KB
/
SiteService.java
File metadata and controls
381 lines (325 loc) · 14.2 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.uid2.admin.vertx.service;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.uid2.shared.model.ClientType;
import com.google.common.net.InternetDomainName;
import com.uid2.admin.store.writer.StoreWriter;
import com.uid2.admin.vertx.JsonUtil;
import com.uid2.admin.vertx.RequestUtil;
import com.uid2.admin.vertx.ResponseUtil;
import com.uid2.admin.vertx.WriteLock;
import com.uid2.shared.Const;
import com.uid2.shared.auth.ClientKey;
import com.uid2.shared.auth.Role;
import com.uid2.shared.middleware.AuthMiddleware;
import com.uid2.shared.model.Site;
import com.uid2.shared.store.IClientKeyProvider;
import com.uid2.shared.store.reader.RotatingSiteStore;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.ext.web.Router;
import static com.uid2.admin.vertx.RequestUtil.getTypes;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
public class SiteService implements IService {
private final AuthMiddleware auth;
private final WriteLock writeLock;
private final StoreWriter<Collection<Site>> storeWriter;
private final RotatingSiteStore siteProvider;
private final IClientKeyProvider clientKeyProvider;
private final ObjectWriter jsonWriter = JsonUtil.createJsonWriter();
private static final Logger LOGGER = LoggerFactory.getLogger(SiteService.class);
public SiteService(AuthMiddleware auth,
WriteLock writeLock,
StoreWriter<Collection<Site>> storeWriter,
RotatingSiteStore siteProvider,
IClientKeyProvider clientKeyProvider) {
this.auth = auth;
this.writeLock = writeLock;
this.storeWriter = storeWriter;
this.siteProvider = siteProvider;
this.clientKeyProvider = clientKeyProvider;
}
@Override
public void setupRoutes(Router router) {
router.post("/api/site/rewrite_metadata").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleRewriteMetadata(ctx);
}
}, Role.CLIENTKEY_ISSUER));
router.get("/api/site/list").handler(
auth.handle(this::handleSiteList, Role.CLIENTKEY_ISSUER, Role.SHARING_PORTAL));
router.post("/api/site/add").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteAdd(ctx);
}
}, Role.CLIENTKEY_ISSUER));
router.post("/api/site/enable").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteEnable(ctx);
}
}, Role.CLIENTKEY_ISSUER));
router.post("/api/site/set-types").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteTypesSet(ctx);
}
}, Role.CLIENTKEY_ISSUER));
router.post("/api/site/domain_names").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteDomains(ctx);
}
}, Role.CLIENTKEY_ISSUER));
router.post("/api/site/created").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteCreated(ctx);
}
}, Role.CLIENTKEY_ISSUER));
}
private void handleRewriteMetadata(RoutingContext rc) {
try {
storeWriter.rewriteMeta();
rc.response().end("OK");
} catch (Exception e) {
LOGGER.error("Could not rewrite metadata", e);
rc.fail(500, e);
}
}
private void handleSiteList(RoutingContext rc) {
try {
JsonArray ja = new JsonArray();
final Collection<Site> sites = this.siteProvider.getAllSites().stream()
.sorted(Comparator.comparing(Site::getName))
.collect(Collectors.toList());
final Map<Integer, List<ClientKey>> clientKeys = this.clientKeyProvider.getAll().stream()
.collect(Collectors.groupingBy(ClientKey::getSiteId));
final List<ClientKey> emptySiteKeys = new ArrayList<>();
for (Site site : sites) {
JsonObject jo = new JsonObject();
ja.add(jo);
JsonArray domainNamesJa = new JsonArray();
site.getDomainNames().forEach(domainNamesJa::add);
jo.put("id", site.getId());
jo.put("name", site.getName());
jo.put("enabled", site.isEnabled());
jo.put("clientTypes", site.getClientTypes());
jo.put("domain_names", domainNamesJa);
jo.put("created", site.getCreated());
List<ClientKey> clients = clientKeys.getOrDefault(site.getId(), emptySiteKeys);
JsonArray jr = new JsonArray();
clients.stream()
.map(c -> c.getRoles()).flatMap(Set::stream).collect(Collectors.toSet())
.forEach(r -> jr.add(r));
jo.put("roles", jr);
jo.put("client_count", clients.size());
}
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(ja.encode());
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleSiteAdd(RoutingContext rc) {
try {
// refresh manually
siteProvider.loadContent();
final String name = rc.queryParam("name").isEmpty() ? "" : rc.queryParam("name").get(0).trim();
if (name == null || name.isEmpty()) {
ResponseUtil.error(rc, 400, "must specify a valid site name");
return;
}
Optional<Site> existingSite = this.siteProvider.getAllSites()
.stream().filter(c -> c.getName().equals(name))
.findFirst();
if (existingSite.isPresent()) {
ResponseUtil.error(rc, 400, "site existed");
return;
}
List<String> normalizedDomainNames = new ArrayList<>();
JsonObject body = rc.body().asJsonObject();
if (body != null) {
JsonArray domainNamesJa = body.getJsonArray("domain_names");
if (domainNamesJa != null) {
normalizedDomainNames = getNormalizedDomainNames(rc, domainNamesJa);
if(normalizedDomainNames == null) return;
}
}
boolean enabled = false;
List<String> enabledFlags = rc.queryParam("enabled");
if (!enabledFlags.isEmpty()) {
try {
enabled = Boolean.valueOf(enabledFlags.get(0));
} catch (Exception ex) {
ResponseUtil.error(rc, 400, "unable to parse enabled " + ex.getMessage());
return;
}
}
Set<ClientType> types = new HashSet<>();
if(!rc.queryParam("types").isEmpty())
{
types = getTypes(rc.queryParam("types").get(0));
}
final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());
final int siteId = 1 + sites.stream().mapToInt(Site::getId).max().orElse(Const.Data.AdvertisingTokenSiteId);
final Site newSite = new Site(siteId, name, enabled, types, new HashSet<>(normalizedDomainNames));
// add site to the array
sites.add(newSite);
// upload to storage
storeWriter.upload(sites, null);
// respond with new client created
rc.response().end(jsonWriter.writeValueAsString(newSite));
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleSiteTypesSet(RoutingContext rc) {
try {
final Site existingSite = RequestUtil.getSite(rc, "id", siteProvider);
if (existingSite == null) {
return;
}
Set<ClientType> types = getTypes(rc.queryParam("types").get(0));
if(types == null) {
ResponseUtil.error(rc, 400, "Invalid Types");
return;
}
final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());
existingSite.setClientTypes(types);
storeWriter.upload(sites, null);
rc.response().end(jsonWriter.writeValueAsString(existingSite));
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleSiteEnable(RoutingContext rc) {
try {
// refresh manually
siteProvider.loadContent();
final Site existingSite = RequestUtil.getSite(rc, "id", siteProvider);
if (existingSite == null) {
return;
}
Boolean enabled = false;
List<String> enabledFlags = rc.queryParam("enabled");
if (!enabledFlags.isEmpty()) {
try {
enabled = Boolean.valueOf(enabledFlags.get(0));
} catch (Exception ex) {
ResponseUtil.error(rc, 400, "unable to parse enabled " + ex.getMessage());
return;
}
}
final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());
if (existingSite.isEnabled() != enabled) {
existingSite.setEnabled(enabled);
storeWriter.upload(sites, null);
}
rc.response().end(jsonWriter.writeValueAsString(existingSite));
} catch (Exception e) {
rc.fail(500, e);
}
}
private void handleSiteDomains(RoutingContext rc) {
try {
// refresh manually
siteProvider.loadContent();
final Site existingSite = RequestUtil.getSite(rc, "id", siteProvider);
if (existingSite == null) {
return;
}
JsonObject body = rc.body().asJsonObject();
JsonArray domainNamesJa = body.getJsonArray("domain_names");
if(domainNamesJa == null) {
ResponseUtil.error(rc, 400, "required parameters: domain_names");
return;
}
List<String> normalizedDomainNames = getNormalizedDomainNames(rc, domainNamesJa);
if (normalizedDomainNames == null) return;
existingSite.setDomainNames(new HashSet<>(normalizedDomainNames));
final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());
storeWriter.upload(sites, null);
rc.response().end(jsonWriter.writeValueAsString(existingSite));
} catch (Exception e) {
ResponseUtil.errorInternal(rc, "set site domain_names failed", e);
}
}
private void handleSiteCreated(RoutingContext rc) {
try {
// refresh manually
siteProvider.loadContent();
final Site existingSite = RequestUtil.getSite(rc, "id", siteProvider);
if (existingSite == null) {
return;
}
JsonObject body = rc.body().asJsonObject();
Long created = body.getLong("created");
if(created == null) {
ResponseUtil.error(rc, 400, "required parameters: created");
return;
}
Site updatedSite = new Site(existingSite.getId(), existingSite.getName(), existingSite.isEnabled(), existingSite.getClientTypes(), existingSite.getDomainNames(), created);
final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());
sites.remove(existingSite);
sites.add(updatedSite);
storeWriter.upload(sites, null);
rc.response().end(jsonWriter.writeValueAsString(updatedSite));
} catch (Exception e) {
ResponseUtil.errorInternal(rc, "set site created failed", e);
}
}
private static List<String> getNormalizedDomainNames(RoutingContext rc, JsonArray domainNamesJa) {
List<String> domainNames = domainNamesJa.stream().map(String::valueOf).collect(Collectors.toList());
List<String> normalizedDomainNames = new ArrayList<>();
for(String domain : domainNames) {
try {
String tld = getTopLevelDomainName(domain);
normalizedDomainNames.add(tld);
} catch (Exception e) {
ResponseUtil.error(rc, 400, "invalid domain name: " + domain);
return null;
}
}
boolean containsDuplicates = normalizedDomainNames.stream().distinct().count() < normalizedDomainNames.size();
if (containsDuplicates) {
ResponseUtil.error(rc, 400, "duplicate domain_names not permitted");
return null;
}
return normalizedDomainNames;
}
public static String getTopLevelDomainName(String origin) throws MalformedURLException {
String host;
try {
URL url = new URL(origin);
host = url.getHost();
} catch (Exception e) {
host = origin;
}
//InternetDomainName will normalise the domain name to lower case already
InternetDomainName name = InternetDomainName.from(host);
//if the domain name has a proper TLD suffix
if(name.isUnderPublicSuffix()) {
try {
return name.topPrivateDomain().toString();
}
catch(Exception e) {
throw e;
}
}
throw new MalformedURLException();
}
}