Skip to content

Commit

Permalink
Added usersyncers cookie-family-name property validation
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryExtraordinaryUsername committed Nov 23, 2023
1 parent 98fd4b3 commit 337b584
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/main/java/org/prebid/server/handler/SetuidHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.prebid.server.util.HttpUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -104,12 +105,22 @@ public SetuidHandler(long defaultTimeout,
this.metrics = Objects.requireNonNull(metrics);
this.timeoutFactory = Objects.requireNonNull(timeoutFactory);

cookieNameToSyncType = bidderCatalog.names().stream()
cookieNameToSyncType = collectMap(bidderCatalog);
}

private static Map<String, UsersyncMethodType> collectMap(BidderCatalog bidderCatalog) {

List<Usersyncer> usersyncers = bidderCatalog.names().stream()
.filter(bidderCatalog::isActive)
.map(bidderCatalog::usersyncerByName)
.filter(Optional::isPresent)
.map(Optional::get)
.distinct() // built-in aliases looks like bidders with the same usersyncers
.distinct()
.toList();

validateUsersyncers(usersyncers);

return usersyncers.stream()
.collect(Collectors.toMap(Usersyncer::getCookieFamilyName, SetuidHandler::preferredUserSyncType));
}

Expand All @@ -121,6 +132,18 @@ private static UsersyncMethodType preferredUserSyncType(Usersyncer usersyncer) {
.get(); // when usersyncer is present, it will contain at least one method
}

private static void validateUsersyncers(List<Usersyncer> usersyncers) {
List<String> cookieFamilyNames = usersyncers.stream()
.map(Usersyncer::getCookieFamilyName)
.toList();
cookieFamilyNames.stream()
.filter(name -> Collections.frequency(cookieFamilyNames, name) > 1)
.findAny()
.ifPresent(name -> {
throw new IllegalArgumentException("Duplicated \"cookie-family-name\" found, value: " + name);
});
}

@Override
public void handle(RoutingContext routingContext) {
toSetuidContext(routingContext)
Expand Down

0 comments on commit 337b584

Please sign in to comment.