11package com .uid2 .admin .vertx .service ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
34import com .fasterxml .jackson .databind .ObjectWriter ;
45import com .uid2 .admin .auth .AdminAuthMiddleware ;
56import com .uid2 .admin .legacy .ILegacyClientKeyProvider ;
@@ -83,6 +84,11 @@ public void setupRoutes(Router router) {
8384 this .handleSiteDomains (ctx );
8485 }
8586 }, Role .MAINTAINER , Role .SHARING_PORTAL ));
87+ router .post ("/api/site/app_names" ).blockingHandler (auth .handle ((ctx ) -> {
88+ synchronized (writeLock ) {
89+ this .handleSiteAppNames (ctx );
90+ }
91+ }, Role .MAINTAINER , Role .SHARING_PORTAL ));
8692 router .post ("/api/site/update" ).blockingHandler (auth .handle ((ctx ) -> {
8793 synchronized (writeLock ) {
8894 this .handleSiteUpdate (ctx );
@@ -127,12 +133,16 @@ private static JsonObject createSiteJsonObject(Site site, Map<Integer, List<Lega
127133 JsonArray domainNamesJa = new JsonArray ();
128134 site .getDomainNames ().forEach (domainNamesJa ::add );
129135
136+ JsonArray appNamesJa = new JsonArray ();
137+ site .getAppNames ().forEach (appNamesJa ::add );
138+
130139 jo .put ("id" , site .getId ());
131140 jo .put ("name" , site .getName ());
132141 jo .put ("description" , site .getDescription ());
133142 jo .put ("enabled" , site .isEnabled ());
134143 jo .put ("clientTypes" , site .getClientTypes ());
135144 jo .put ("domain_names" , domainNamesJa );
145+ jo .put ("app_names" , appNamesJa );
136146 jo .put ("visible" , site .isVisible ());
137147 jo .put ("created" , site .getCreated ());
138148
@@ -197,6 +207,15 @@ private void handleSiteAdd(RoutingContext rc) {
197207 }
198208 }
199209
210+ Set <String > normalizedAppNames = new HashSet <>();
211+ if (body != null ) {
212+ JsonArray appNamesJa = body .getJsonArray ("app_names" );
213+ if (appNamesJa != null ) {
214+ normalizedAppNames = getNormalizedAppNames (rc , appNamesJa );
215+ if (normalizedAppNames == null ) return ;
216+ }
217+ }
218+
200219 boolean enabled = false ;
201220 List <String > enabledFlags = rc .queryParam ("enabled" );
202221 if (!enabledFlags .isEmpty ()) {
@@ -220,7 +239,7 @@ private void handleSiteAdd(RoutingContext rc) {
220239 .collect (Collectors .toList ());
221240 final int siteId = 1 + sites .stream ().mapToInt (Site ::getId ).max ().orElse (Const .Data .AdvertisingTokenSiteId );
222241
223- final Site newSite = new Site (siteId , name , description , enabled , types , new HashSet <>(normalizedDomainNames ), true );
242+ final Site newSite = new Site (siteId , name , description , enabled , types , new HashSet <>(normalizedDomainNames ), normalizedAppNames , true );
224243 // add site to the array
225244 sites .add (newSite );
226245
@@ -247,15 +266,9 @@ private void handleSiteTypesSet(RoutingContext rc) {
247266 return ;
248267 }
249268
250- final List <Site > sites = this .siteProvider .getAllSites ()
251- .stream ().sorted (Comparator .comparingInt (Site ::getId ))
252- .collect (Collectors .toList ());
253-
254269 existingSite .setClientTypes (types );
255270
256- storeWriter .upload (sites , null );
257-
258- rc .response ().end (jsonWriter .writeValueAsString (existingSite ));
271+ uploadSiteToStoreWriterAndWriteExistingSiteToResponse (existingSite , rc );
259272 } catch (Exception e ) {
260273 rc .fail (500 , e );
261274 }
@@ -318,15 +331,36 @@ private void handleSiteDomains(RoutingContext rc) {
318331
319332 existingSite .setDomainNames (new HashSet <>(normalizedDomainNames ));
320333
321- final List <Site > sites = this .siteProvider .getAllSites ()
322- .stream ().sorted (Comparator .comparingInt (Site ::getId ))
323- .collect (Collectors .toList ());
334+ uploadSiteToStoreWriterAndWriteExistingSiteToResponse (existingSite , rc );
335+ } catch (Exception e ) {
336+ ResponseUtil .errorInternal (rc , "set site domain_names failed" , e );
337+ }
338+ }
324339
325- storeWriter .upload (sites , null );
340+ private void handleSiteAppNames (RoutingContext rc ) {
341+ try {
342+ // refresh manually
343+ siteProvider .loadContent ();
326344
327- rc .response ().end (jsonWriter .writeValueAsString (existingSite ));
345+ final Site existingSite = RequestUtil .getSiteFromParam (rc , "id" , siteProvider );
346+ if (existingSite == null ) {
347+ return ;
348+ }
349+
350+ JsonObject body = rc .body ().asJsonObject ();
351+ JsonArray appNamesJa = body .getJsonArray ("app_names" );
352+ if (appNamesJa == null ) {
353+ ResponseUtil .error (rc , 400 , "required parameters: app_names" );
354+ return ;
355+ }
356+ Set <String > normalizedAppNames = getNormalizedAppNames (rc , appNamesJa );
357+ if (normalizedAppNames == null ) return ;
358+
359+ existingSite .setAppNames (normalizedAppNames );
360+
361+ uploadSiteToStoreWriterAndWriteExistingSiteToResponse (existingSite , rc );
328362 } catch (Exception e ) {
329- ResponseUtil .errorInternal (rc , "set site domain_names failed" , e );
363+ ResponseUtil .errorInternal (rc , "set site app_names failed" , e );
330364 }
331365 }
332366
@@ -355,13 +389,7 @@ private void handleSiteUpdate(RoutingContext rc) {
355389 }
356390 }
357391
358- final List <Site > sites = this .siteProvider .getAllSites ()
359- .stream ().sorted (Comparator .comparingInt (Site ::getId ))
360- .collect (Collectors .toList ());
361-
362- storeWriter .upload (sites , null );
363-
364- rc .response ().end (jsonWriter .writeValueAsString (existingSite ));
392+ uploadSiteToStoreWriterAndWriteExistingSiteToResponse (existingSite , rc );
365393 } catch (Exception e ) {
366394 rc .fail (500 , e );
367395 }
@@ -389,6 +417,17 @@ private static List<String> getNormalizedDomainNames(RoutingContext rc, JsonArra
389417 return normalizedDomainNames ;
390418 }
391419
420+ private static Set <String > getNormalizedAppNames (RoutingContext rc , JsonArray appNamesJa ) {
421+ List <String > appNames = appNamesJa .stream ().map (String ::valueOf ).collect (Collectors .toList ());
422+
423+ boolean containsDuplicates = appNames .stream ().distinct ().count () < appNames .size ();
424+ if (containsDuplicates ) {
425+ ResponseUtil .error (rc , 400 , "duplicate app_names not permitted" );
426+ return null ;
427+ }
428+ return new HashSet <>(appNames );
429+ }
430+
392431 public static String getTopLevelDomainName (String origin ) throws MalformedURLException {
393432 String host ;
394433 try {
@@ -409,4 +448,13 @@ public static String getTopLevelDomainName(String origin) throws MalformedURLExc
409448 }
410449 throw new MalformedURLException ();
411450 }
451+
452+ private void uploadSiteToStoreWriterAndWriteExistingSiteToResponse (Site existingSite , RoutingContext rc ) throws Exception {
453+ final List <Site > sites = this .siteProvider .getAllSites ()
454+ .stream ().sorted (Comparator .comparingInt (Site ::getId ))
455+ .collect (Collectors .toList ());
456+
457+ storeWriter .upload (sites , null );
458+ rc .response ().end (jsonWriter .writeValueAsString (existingSite ));
459+ }
412460}
0 commit comments