Skip to content

Commit

Permalink
ORTB-Blocking Module: Add new configuration options (#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
CTMBNara authored Oct 16, 2024
1 parent 2dfd97a commit dedd923
Show file tree
Hide file tree
Showing 26 changed files with 1,090 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.ResponseBlockingConfig;
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.Result;
import org.prebid.server.hooks.modules.ortb2.blocking.core.util.MergeUtils;
import org.prebid.server.spring.config.bidder.model.MediaType;
import org.prebid.server.util.ObjectUtil;
import org.prebid.server.util.StreamUtil;

Expand Down Expand Up @@ -51,7 +52,11 @@ public class AccountConfigReader {
private static final String ALLOWED_APP_FOR_DEALS_FIELD = "allowed-app-for-deals";
private static final String BLOCKED_BANNER_TYPE_FIELD = "blocked-banner-type";
private static final String BLOCKED_BANNER_ATTR_FIELD = "blocked-banner-attr";
private static final String BLOCKED_VIDEO_ATTR_FIELD = "blocked-video-attr";
private static final String BLOCKED_AUDIO_ATTR_FIELD = "blocked-audio-attr";
private static final String ALLOWED_BANNER_ATTR_FOR_DEALS = "allowed-banner-attr-for-deals";
private static final String ALLOWED_VIDEO_ATTR_FOR_DEALS = "allowed-video-attr-for-deals";
private static final String ALLOWED_AUDIO_ATTR_FOR_DEALS = "allowed-audio-attr-for-deals";
private static final String ACTION_OVERRIDES_FIELD = "action-overrides";
private static final String OVERRIDE_FIELD = "override";
private static final String CONDITIONS_FIELD = "conditions";
Expand Down Expand Up @@ -100,8 +105,14 @@ public Result<BlockedAttributes> blockedAttributesFor(BidRequest bidRequest) {
blockedAttribute(BAPP_FIELD, String.class, BLOCKED_APP_FIELD, requestMediaTypes);
final Result<Map<String, List<Integer>>> btype =
blockedAttributesForImps(BTYPE_FIELD, Integer.class, BLOCKED_BANNER_TYPE_FIELD, bidRequest);
final Result<Map<String, List<Integer>>> battr =
final Result<Map<String, List<Integer>>> bannerBattr =
blockedAttributesForImps(BATTR_FIELD, Integer.class, BLOCKED_BANNER_ATTR_FIELD, bidRequest);
final Result<Map<String, List<Integer>>> videoBattr =
blockedAttributesForImps(BATTR_FIELD, Integer.class, BLOCKED_VIDEO_ATTR_FIELD, bidRequest);
final Result<Map<String, List<Integer>>> audioBattr =
blockedAttributesForImps(BATTR_FIELD, Integer.class, BLOCKED_AUDIO_ATTR_FIELD, bidRequest);
final Result<Map<MediaType, Map<String, List<Integer>>>> battr =
mergeBlockedAttributes(bannerBattr, videoBattr, audioBattr);

return Result.of(
toBlockedAttributes(badv, bcat, cattaxComplement, bapp, btype, battr),
Expand Down Expand Up @@ -133,22 +144,39 @@ public Result<ResponseBlockingConfig> responseBlockingConfigFor(BidderBid bidder
ALLOWED_APP_FOR_DEALS_FIELD,
bidMediaTypes,
dealid);
final Result<BidAttributeBlockingConfig<Integer>> battr = blockingConfigForAttribute(
final Result<BidAttributeBlockingConfig<Integer>> bannerBattr = blockingConfigForAttribute(
BATTR_FIELD,
Integer.class,
ALLOWED_BANNER_ATTR_FOR_DEALS,
bidMediaTypes,
dealid);
final Result<BidAttributeBlockingConfig<Integer>> videoBattr = blockingConfigForAttribute(
BATTR_FIELD,
Integer.class,
ALLOWED_VIDEO_ATTR_FOR_DEALS,
bidMediaTypes,
dealid);
final Result<BidAttributeBlockingConfig<Integer>> audioBattr = blockingConfigForAttribute(
BATTR_FIELD,
Integer.class,
ALLOWED_AUDIO_ATTR_FOR_DEALS,
bidMediaTypes,
dealid);
final Map<MediaType, BidAttributeBlockingConfig<Integer>> battr = new HashMap<>();
battr.put(MediaType.BANNER, bannerBattr.getValue());
battr.put(MediaType.VIDEO, videoBattr.getValue());
battr.put(MediaType.AUDIO, audioBattr.getValue());

final ResponseBlockingConfig response = ResponseBlockingConfig.builder()
.badv(badv.getValue())
.bcat(bcat.getValue())
.cattax(cattax.getValue())
.bapp(bapp.getValue())
.battr(battr.getValue())
.battr(battr)
.build();

final List<String> warnings = MergeUtils.mergeMessages(badv, bcat, cattax, bapp, battr);
final List<String> warnings = MergeUtils.mergeMessages(
badv, bcat, cattax, bapp, bannerBattr, videoBattr, audioBattr);

return Result.of(response, warnings);
}
Expand Down Expand Up @@ -218,6 +246,28 @@ private <T> Result<Map<String, List<T>>> blockedAttributesForImps(String attribu
MergeUtils.mergeMessages(results));
}

private static Result<Map<MediaType, Map<String, List<Integer>>>> mergeBlockedAttributes(
Result<Map<String, List<Integer>>> bannerBattr,
Result<Map<String, List<Integer>>> videoBattr,
Result<Map<String, List<Integer>>> audioBattr) {

final Map<MediaType, Map<String, List<Integer>>> battr = new HashMap<>();

if (bannerBattr.hasValue()) {
battr.put(MediaType.BANNER, bannerBattr.getValue());
}
if (videoBattr.hasValue()) {
battr.put(MediaType.VIDEO, videoBattr.getValue());
}
if (audioBattr.hasValue()) {
battr.put(MediaType.AUDIO, audioBattr.getValue());
}

return Result.of(
!battr.isEmpty() ? battr : null,
MergeUtils.mergeMessages(bannerBattr, videoBattr, audioBattr));
}

private <T> Result<BidAttributeBlockingConfig<T>> blockingConfigForAttribute(String attribute,
Class<T> attributeType,
String blockUnknownField,
Expand Down Expand Up @@ -360,8 +410,8 @@ private Result<JsonNode> toResult(List<JsonNode> specificBidderResults,
Set<String> actualMediaTypes) {

final JsonNode value = ObjectUtils.firstNonNull(
specificBidderResults.size() > 0 ? specificBidderResults.get(0) : null,
catchAllBidderResults.size() > 0 ? catchAllBidderResults.get(0) : null);
!specificBidderResults.isEmpty() ? specificBidderResults.getFirst() : null,
!catchAllBidderResults.isEmpty() ? catchAllBidderResults.getFirst() : null);
final List<String> warnings = debugEnabled && specificBidderResults.size() + catchAllBidderResults.size() > 1
? Collections.singletonList(
"More than one conditions matches request. Bidder: %s, request media types: %s"
Expand All @@ -376,7 +426,7 @@ private static BlockedAttributes toBlockedAttributes(Result<List<String>> badv,
Result<Integer> cattaxComplement,
Result<List<String>> bapp,
Result<Map<String, List<Integer>>> btype,
Result<Map<String, List<Integer>>> battr) {
Result<Map<MediaType, Map<String, List<Integer>>>> battr) {

return badv.hasValue()
|| bcat.hasValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.ResponseBlockingConfig;
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.Result;
import org.prebid.server.hooks.modules.ortb2.blocking.core.util.MergeUtils;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.spring.config.bidder.model.MediaType;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -91,7 +93,6 @@ public ExecutionResult<BlockedBids> block() {

try {
final List<Result<BlockingResult>> blockedBidResults = bids.stream()
.sequential()
.map(bid -> isBlocked(bid, accountConfigReader))
.toList();

Expand Down Expand Up @@ -170,11 +171,30 @@ private AttributeCheckResult<String> checkBapp(BidderBid bidderBid, ResponseBloc
}

private AttributeCheckResult<Integer> checkBattr(BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {

final MediaType mediaType = mapBidTypeToMediaType(bidderBid.getType());
return checkAttribute(
bidderBid.getBid().getAttr(),
blockingConfig.getBattr(),
blockedAttributeValues(BlockedAttributes::getBattr, bidderBid.getBid().getImpid()));
blockingConfig.getBattr().get(mediaType),
blockedAttributeValues(
blockedAttributes -> extractBattrForMediaType(blockedAttributes, mediaType),
bidderBid.getBid().getImpid()));
}

private static MediaType mapBidTypeToMediaType(BidType bidType) {
return switch (bidType) {
case banner -> MediaType.BANNER;
case video -> MediaType.VIDEO;
case audio -> MediaType.AUDIO;
case xNative -> MediaType.NATIVE;
case null -> null;
};
}

private static Map<String, List<Integer>> extractBattrForMediaType(BlockedAttributes blockedAttributes,
MediaType mediaType) {

final Map<MediaType, Map<String, List<Integer>>> battr = blockedAttributes.getBattr();
return battr != null ? battr.get(mediaType) : null;
}

private <T> AttributeCheckResult<T> checkAttribute(List<T> attribute,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.prebid.server.hooks.modules.ortb2.blocking.core;

import com.iab.openrtb.request.Audio;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Video;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.BlockedAttributes;
import org.prebid.server.spring.config.bidder.model.MediaType;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class RequestUpdater {

Expand Down Expand Up @@ -40,39 +44,93 @@ public BidRequest update(BidRequest bidRequest) {

private List<Imp> updateImps(List<Imp> imps) {
final Map<String, List<Integer>> blockedBannerType = blockedAttributes.getBtype();
final Map<String, List<Integer>> blockedBannerAttr = blockedAttributes.getBattr();
final Map<MediaType, Map<String, List<Integer>>> blockedAttr = blockedAttributes.getBattr();

if (MapUtils.isEmpty(blockedBannerType) && MapUtils.isEmpty(blockedBannerAttr)) {
if (MapUtils.isEmpty(blockedBannerType) && MapUtils.isEmpty(blockedAttr)) {
return imps;
}

return imps.stream()
.map(imp -> updateImp(imp, blockedBannerType, blockedBannerAttr))
.map(imp -> updateImp(imp, blockedBannerType, blockedAttr))
.toList();
}

private Imp updateImp(Imp imp,
Map<String, List<Integer>> blockedBannerType,
Map<String, List<Integer>> blockedBannerAttr) {
Map<MediaType, Map<String, List<Integer>>> blockedAttr) {

final String impId = imp.getId();
final List<Integer> btypeForImp = blockedBannerType != null ? blockedBannerType.get(impId) : null;
final List<Integer> battrForImp = blockedBannerAttr != null ? blockedBannerAttr.get(impId) : null;
final List<Integer> bannerBattrForImp = extractBattr(blockedAttr, MediaType.BANNER, impId);
final List<Integer> videoBattrForImp = extractBattr(blockedAttr, MediaType.VIDEO, impId);
final List<Integer> audioBattrForImp = extractBattr(blockedAttr, MediaType.AUDIO, impId);

if (CollectionUtils.isEmpty(btypeForImp)
&& CollectionUtils.isEmpty(bannerBattrForImp)
&& CollectionUtils.isEmpty(videoBattrForImp)
&& CollectionUtils.isEmpty(audioBattrForImp)) {

if (CollectionUtils.isEmpty(btypeForImp) && CollectionUtils.isEmpty(battrForImp)) {
return imp;
}

final Banner banner = imp.getBanner();
final List<Integer> existingBtype = banner != null ? banner.getBtype() : null;
final List<Integer> existingBattr = banner != null ? banner.getBattr() : null;
final Banner.BannerBuilder bannerBuilder = banner != null ? banner.toBuilder() : Banner.builder();
final Video video = imp.getVideo();
final Audio audio = imp.getAudio();

return imp.toBuilder()
.banner(bannerBuilder
.btype(CollectionUtils.isNotEmpty(existingBtype) ? existingBtype : btypeForImp)
.battr(CollectionUtils.isNotEmpty(existingBattr) ? existingBattr : battrForImp)
.build())
.banner(CollectionUtils.isNotEmpty(btypeForImp) || CollectionUtils.isNotEmpty(bannerBattrForImp)
? updateBanner(banner, btypeForImp, bannerBattrForImp)
: banner)
.video(CollectionUtils.isNotEmpty(videoBattrForImp)
? updateVideo(imp.getVideo(), videoBattrForImp)
: video)
.audio(CollectionUtils.isNotEmpty(audioBattrForImp)
? updateAudio(imp.getAudio(), audioBattrForImp)
: audio)
.build();
}

private static List<Integer> extractBattr(Map<MediaType, Map<String, List<Integer>>> blockedAttr,
MediaType mediaType,
String impId) {

final Map<String, List<Integer>> impIdToBattr = blockedAttr != null ? blockedAttr.get(mediaType) : null;
return impIdToBattr != null ? impIdToBattr.get(impId) : null;
}

private static Banner updateBanner(Banner banner, List<Integer> btype, List<Integer> battr) {
final List<Integer> existingBtype = banner != null ? banner.getBtype() : null;
final List<Integer> existingBattr = banner != null ? banner.getBattr() : null;

return CollectionUtils.isEmpty(existingBtype) || CollectionUtils.isEmpty(existingBattr)
? Optional.ofNullable(banner)
.map(Banner::toBuilder)
.orElseGet(Banner::builder)
.btype(CollectionUtils.isNotEmpty(existingBtype) ? existingBtype : btype)
.battr(CollectionUtils.isNotEmpty(existingBattr) ? existingBattr : battr)
.build()
: banner;
}

private static Video updateVideo(Video video, List<Integer> battr) {
final List<Integer> existingBattr = video != null ? video.getBattr() : null;
return CollectionUtils.isEmpty(existingBattr)
? Optional.ofNullable(video)
.map(Video::toBuilder)
.orElseGet(Video::builder)
.battr(battr)
.build()
: video;
}

private static Audio updateAudio(Audio audio, List<Integer> battr) {
final List<Integer> existingBattr = audio != null ? audio.getBattr() : null;
return CollectionUtils.isEmpty(existingBattr)
? Optional.ofNullable(audio)
.map(Audio::toBuilder)
.orElseGet(Audio::builder)
.battr(battr)
.build()
: audio;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Builder;
import lombok.Value;
import org.prebid.server.spring.config.bidder.model.MediaType;

import java.util.List;
import java.util.Map;
Expand All @@ -20,5 +21,5 @@ public class BlockedAttributes {

Map<String, List<Integer>> btype;

Map<String, List<Integer>> battr;
Map<MediaType, Map<String, List<Integer>>> battr;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import lombok.Builder;
import lombok.Value;
import org.prebid.server.spring.config.bidder.model.MediaType;

import java.util.Map;

@Builder
@Value
Expand All @@ -15,5 +18,5 @@ public class ResponseBlockingConfig {

BidAttributeBlockingConfig<String> bapp;

BidAttributeBlockingConfig<Integer> battr;
Map<MediaType, BidAttributeBlockingConfig<Integer>> battr;
}
Loading

0 comments on commit dedd923

Please sign in to comment.