Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added usersyncers cookie-family-name property validation #2790

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/main/java/org/prebid/server/handler/SetuidHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -103,13 +105,22 @@ public SetuidHandler(long defaultTimeout,
this.analyticsDelegator = Objects.requireNonNull(analyticsDelegator);
this.metrics = Objects.requireNonNull(metrics);
this.timeoutFactory = Objects.requireNonNull(timeoutFactory);
this.cookieNameToSyncType = collectMap(bidderCatalog);
}

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

cookieNameToSyncType = bidderCatalog.names().stream()
final Supplier<Stream<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();

validateUsersyncers(usersyncers.get());

return usersyncers.get()
VeryExtraordinaryUsername marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toMap(Usersyncer::getCookieFamilyName, SetuidHandler::preferredUserSyncType));
}

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

private static void validateUsersyncers(Stream<Usersyncer> usersyncers) {
final Stream<String> cookieFamilyNameDuplicates = usersyncers.map(Usersyncer::getCookieFamilyName)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(name -> name.getValue() > 1)
.map(Map.Entry::getKey)
.distinct();
if (cookieFamilyNameDuplicates.findAny().isPresent()) {
throw new IllegalArgumentException(
"Duplicated \"cookie-family-name\" found, values: "
+ String.join(", ", cookieFamilyNameDuplicates.toList()));
}
}

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