Skip to content

Commit

Permalink
Merge pull request #173 from IABTechLab/ajy-UID2-2073-Allow-updating-…
Browse files Browse the repository at this point in the history
…new-site-fields-in-UI

Allow updating new site fields in UI
  • Loading branch information
alex-yau-ttd authored Nov 9, 2023
2 parents 48d6682 + d71bc75 commit 634fd3d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- check micrometer.version vertx-micrometer-metrics consumes before bumping up -->
<micrometer.version>1.1.0</micrometer.version>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
<uid2-shared.version>5.15.0-5e9fa2fc04</uid2-shared.version>
<uid2-shared.version>5.18.0-d6c76f6aab</uid2-shared.version>
<image.version>${project.version}</image.version>
</properties>
<repositories>
Expand Down
72 changes: 58 additions & 14 deletions src/main/java/com/uid2/admin/vertx/service/SiteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -178,17 +185,18 @@ private void handleSiteAdd(RoutingContext rc) {
}

Set<ClientType> 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<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));

final Site newSite = new Site(siteId, name, description, enabled, types, new HashSet<>(normalizedDomainNames), true);
// add site to the array
sites.add(newSite);

Expand All @@ -210,7 +218,7 @@ private void handleSiteTypesSet(RoutingContext rc) {
}

Set<ClientType> types = getTypes(rc.queryParam("types").get(0));
if(types == null) {
if (types == null) {
ResponseUtil.error(rc, 400, "Invalid Types");
return;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<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) {
rc.fail(500, 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) {
for (String domain : domainNames) {
try {
String tld = getTopLevelDomainName(domain);
normalizedDomainNames.add(tld);
Expand Down Expand Up @@ -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;
}
}
Expand Down
32 changes: 21 additions & 11 deletions src/main/resources/localstack/s3/sites/sites.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
]
]
34 changes: 33 additions & 1 deletion webroot/adm/site.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ <h3>Inputs</h3>
<input type="text" id="siteName" name="siteName">
<label for="siteId">Site Id:</label>
<input type="text" id="siteId" name="siteId">
<label for="description">Description:</label>
<input type="text" id="description" name="description">
<label for="types">Client Types:</label>
<input type="text" id="types" name="types">
</div>
Expand All @@ -37,8 +39,11 @@ <h3>Operations</h3>
<li class="ro-cki" style="display: none"><a href="#" id="doAdd">Add Site</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doEnable">Enable Site</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doDisable">Disable Site</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doSetDescription">Set Description</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doSetTypes">Set Types</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doDomainNames">Set Site Domain Names</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doSetVisible">Set Visible</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doSetNotVisible">Set Not Visible</a></li>
</ul>

<br>
Expand All @@ -59,7 +64,8 @@ <h3>Output</h3>
$('#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}));
Expand Down Expand Up @@ -102,6 +108,32 @@ <h3>Output</h3>

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');
});
});
</script>

Expand Down

0 comments on commit 634fd3d

Please sign in to comment.