diff --git a/pom.xml b/pom.xml index 1daf5419..cce18234 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ 1.1.0 5.7.0 - 5.15.0-5e9fa2fc04 + 5.18.0-d6c76f6aab ${project.version} diff --git a/src/main/java/com/uid2/admin/vertx/service/SiteService.java b/src/main/java/com/uid2/admin/vertx/service/SiteService.java index 347951da..114635b2 100644 --- a/src/main/java/com/uid2/admin/vertx/service/SiteService.java +++ b/src/main/java/com/uid2/admin/vertx/service/SiteService.java @@ -72,15 +72,20 @@ public void setupRoutes(Router router) { } }, Role.CLIENTKEY_ISSUER)); router.post("/api/site/set-types").blockingHandler(auth.handle((ctx) -> { - synchronized (writeLock) { - this.handleSiteTypesSet(ctx); - } - }, Role.CLIENTKEY_ISSUER)); + 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/update").blockingHandler(auth.handle((ctx) -> { + synchronized (writeLock) { + this.handleSiteUpdate(ctx); + } + }, Role.CLIENTKEY_ISSUER)); } private void handleRewriteMetadata(RoutingContext rc) { @@ -111,9 +116,11 @@ private void handleSiteList(RoutingContext rc) { jo.put("id", site.getId()); jo.put("name", site.getName()); + jo.put("description", site.getDescription()); jo.put("enabled", site.isEnabled()); jo.put("clientTypes", site.getClientTypes()); jo.put("domain_names", domainNamesJa); + jo.put("visible", site.isVisible()); jo.put("created", site.getCreated()); JsonArray jr = new JsonArray(); @@ -162,7 +169,7 @@ private void handleSiteAdd(RoutingContext rc) { JsonArray domainNamesJa = body.getJsonArray("domain_names"); if (domainNamesJa != null) { normalizedDomainNames = getNormalizedDomainNames(rc, domainNamesJa); - if(normalizedDomainNames == null) return; + if (normalizedDomainNames == null) return; } } @@ -178,17 +185,18 @@ private void handleSiteAdd(RoutingContext rc) { } Set types = new HashSet<>(); - if(!rc.queryParam("types").isEmpty()) - { + if (!rc.queryParam("types").isEmpty()) { types = getTypes(rc.queryParam("types").get(0)); } + String description = rc.queryParam("description").stream().findFirst().orElse(null); + final List 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)); + final Site newSite = new Site(siteId, name, description, enabled, types, new HashSet<>(normalizedDomainNames), true); // add site to the array sites.add(newSite); @@ -210,7 +218,7 @@ private void handleSiteTypesSet(RoutingContext rc) { } Set types = getTypes(rc.queryParam("types").get(0)); - if(types == null) { + if (types == null) { ResponseUtil.error(rc, 400, "Invalid Types"); return; } @@ -277,7 +285,7 @@ private void handleSiteDomains(RoutingContext rc) { JsonObject body = rc.body().asJsonObject(); JsonArray domainNamesJa = body.getJsonArray("domain_names"); - if(domainNamesJa == null) { + if (domainNamesJa == null) { ResponseUtil.error(rc, 400, "required parameters: domain_names"); return; } @@ -298,11 +306,48 @@ private void handleSiteDomains(RoutingContext rc) { } } + private void handleSiteUpdate(RoutingContext rc) { + try { + // refresh manually + siteProvider.loadContent(); + + final Site existingSite = RequestUtil.getSite(rc, "id", siteProvider); + if (existingSite == null) { + return; + } + String description = rc.queryParam("description").stream().findFirst().orElse(null); + String visibleParam = rc.queryParam("visible").stream().findFirst().orElse(null); + + if (description != null) { + existingSite.setDescription(description); + } + if (visibleParam != null) { + if ("true".equalsIgnoreCase(visibleParam)) { + existingSite.setVisible(true); + } else if ("false".equalsIgnoreCase(visibleParam)) { + existingSite.setVisible(false); + } else { + ResponseUtil.error(rc, 400, "Invalid parameter for visible: " + visibleParam); + } + } + + final List 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) { + rc.fail(500, e); + } + } + private static List getNormalizedDomainNames(RoutingContext rc, JsonArray domainNamesJa) { List domainNames = domainNamesJa.stream().map(String::valueOf).collect(Collectors.toList()); List normalizedDomainNames = new ArrayList<>(); - for(String domain : domainNames) { + for (String domain : domainNames) { try { String tld = getTopLevelDomainName(domain); normalizedDomainNames.add(tld); @@ -331,11 +376,10 @@ public static String getTopLevelDomainName(String origin) throws MalformedURLExc //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()) { + if (name.isUnderPublicSuffix()) { try { return name.topPrivateDomain().toString(); - } - catch(Exception e) { + } catch (Exception e) { throw e; } } diff --git a/src/main/resources/localstack/s3/sites/sites.json b/src/main/resources/localstack/s3/sites/sites.json index 62d7a584..e8d35546 100644 --- a/src/main/resources/localstack/s3/sites/sites.json +++ b/src/main/resources/localstack/s3/sites/sites.json @@ -2,32 +2,38 @@ { "id": 999, "name": "Special", + "description": "A special description", "enabled": true, - "clientTypes": ["DSP", "PUBLISHER", "DATA_PROVIDER", "ADVERTISER"] + "clientTypes": ["DSP", "PUBLISHER", "DATA_PROVIDER", "ADVERTISER"], + "visible": true }, { "id": 123, "name": "DSP", "enabled": true, - "clientTypes": ["DSP"] + "clientTypes": ["DSP"], + "visible": true }, { "id": 124, "name": "Publisher", "enabled": true, - "clientTypes": ["PUBLISHER"] + "clientTypes": ["PUBLISHER"], + "visible": true }, { "id": 125, "name": "Data Provider", "enabled": true, - "clientTypes": ["DATA_PROVIDER"] + "clientTypes": ["DATA_PROVIDER"], + "visible": true }, { "id": 126, "name": "Advertiser", "enabled": true, - "clientTypes": ["ADVERTISER"] + "clientTypes": ["ADVERTISER"], + "visible": true }, { "id": 127, @@ -37,21 +43,25 @@ { "id": 128, "name": "Legacy DSP", - "enabled": true + "enabled": true, + "visible": false }, { "id": 129, "name": "Legacy Publisher", - "enabled": true + "enabled": true, + "visible": false }, { "id": 130, "name": "Legacy Data Provider", - "enabled": true + "enabled": true, + "visible": false }, { - "id": 130, + "id": 131, "name": "Legacy Advertiser", - "enabled": true + "enabled": true, + "visible": false } -] \ No newline at end of file +] diff --git a/webroot/adm/site.html b/webroot/adm/site.html index dc2a5e51..cf93d4df 100644 --- a/webroot/adm/site.html +++ b/webroot/adm/site.html @@ -18,6 +18,8 @@

Inputs

+ + @@ -37,8 +39,11 @@

Operations

+ + +
@@ -59,7 +64,8 @@

Output

$('#doAdd').on('click', function () { var siteName = encodeURIComponent($('#siteName').val()); var types = encodeURIComponent($('#types').val()); - var url = '/api/site/add?name=' + siteName + '&types=' + types; + var description = encodeURIComponent($('#description').val()); + var url = '/api/site/add?name=' + siteName + '&types=' + types + '&description=' + description; let domainNames = ($('#domainNames').val()).replace(/\s+/g, '').split(',').filter( (value, _, __) => value !== ""); doApiCall('POST', url, '#standardOutput', '#errorOutput', JSON.stringify({domain_names : domainNames})); @@ -102,6 +108,32 @@

Output

doApiCall('POST', url, '#standardOutput', '#errorOutput', JSON.stringify(payload)); }); + + $('#doSetDescription').on('click', function () { + var siteId = encodeURIComponent($('#siteId').val()); + + var description = encodeURIComponent($('#description').val()); + + var url = '/api/site/update?id=' + siteId + '&description=' + description; + + doApiCall('POST', url, '#standardOutput', '#errorOutput'); + }); + + $('#doSetVisible').on('click', function () { + var siteId = encodeURIComponent($('#siteId').val()); + + var url = '/api/site/update?id=' + siteId + '&visible=true'; + + doApiCall('POST', url, '#standardOutput', '#errorOutput'); + }); + + $('#doSetNotVisible').on('click', function () { + var siteId = encodeURIComponent($('#siteId').val()); + + var url = '/api/site/update?id=' + siteId + '&visible=false'; + + doApiCall('POST', url, '#standardOutput', '#errorOutput'); + }); });