Skip to content

Commit

Permalink
ORTB 2.6: Add cattax field support for blocking module (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
CTMBNara authored Jul 27, 2022
1 parent 1c23171 commit d83789a
Show file tree
Hide file tree
Showing 34 changed files with 1,021 additions and 530 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Value;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.auction.versionconverter.OrtbVersion;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.hooks.modules.ortb2.blocking.core.exception.InvalidAccountConfigurationException;
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.AnalyticsResult;
Expand Down Expand Up @@ -36,43 +38,49 @@ public class BidsBlocker {
private static final String BUNDLE_FIELD = "bundle";
private static final String ATTR_FIELD = "attr";

private static final Integer DEFAULT_BLOCKED_CATTAX_COMPLEMENT = 1;

private final List<BidderBid> bids;
private final String bidder;
private final OrtbVersion ortbVersion;
private final ObjectNode accountConfig;
private final BlockedAttributes blockedAttributes;
private final boolean debugEnabled;

private BidsBlocker(
List<BidderBid> bids,
String bidder,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {
private BidsBlocker(List<BidderBid> bids,
String bidder,
OrtbVersion ortbVersion,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {

this.bids = bids;
this.bidder = bidder;
this.ortbVersion = ortbVersion;
this.accountConfig = accountConfig;
this.blockedAttributes = blockedAttributes;
this.debugEnabled = debugEnabled;
}

public static BidsBlocker create(
List<BidderBid> bids,
String bidder,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {
public static BidsBlocker create(List<BidderBid> bids,
String bidder,
OrtbVersion ortbVersion,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {

return new BidsBlocker(
Objects.requireNonNull(bids),
Objects.requireNonNull(bidder),
Objects.requireNonNull(ortbVersion),
accountConfig,
blockedAttributes,
debugEnabled);
}

public ExecutionResult<BlockedBids> block() {
final AccountConfigReader accountConfigReader = AccountConfigReader.create(accountConfig, bidder, debugEnabled);
final AccountConfigReader accountConfigReader = AccountConfigReader.create(
accountConfig, bidder, ortbVersion, debugEnabled);

try {
final List<Result<BlockingResult>> blockedBidResults = bids.stream()
Expand Down Expand Up @@ -100,14 +108,15 @@ public ExecutionResult<BlockedBids> block() {
}

private Result<BlockingResult> isBlocked(BidderBid bidderBid, AccountConfigReader accountConfigReader) {
final Result<ResponseBlockingConfig> blockingConfigResult =
accountConfigReader.responseBlockingConfigFor(bidderBid);
final Result<ResponseBlockingConfig> blockingConfigResult = accountConfigReader
.responseBlockingConfigFor(bidderBid);
final ResponseBlockingConfig blockingConfig = blockingConfigResult.getValue();

final BlockingResult blockingResult = BlockingResult.of(
bidderBid.getBid().getImpid(),
checkBadv(bidderBid, blockingConfig),
checkBcat(bidderBid, blockingConfig),
checkCattax(bidderBid, blockingConfig),
checkBapp(bidderBid, blockingConfig),
checkBattr(bidderBid, blockingConfig));

Expand All @@ -128,24 +137,38 @@ private AttributeCheckResult<String> checkBcat(BidderBid bidderBid, ResponseBloc
blockedAttributeValues(BlockedAttributes::getBcat));
}

private AttributeCheckResult<Integer> checkCattax(BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {
final Integer cattax = bidderBid.getBid().getCattax();
if (cattax == null || ortbVersion.ordinal() < OrtbVersion.ORTB_2_6.ordinal()) {
return AttributeCheckResult.succeeded();
}

return checkAttributeComplement(
cattax,
blockingConfig.getCattax(),
ObjectUtils.defaultIfNull(
blockedAttributeValues(BlockedAttributes::getCattaxComplement),
DEFAULT_BLOCKED_CATTAX_COMPLEMENT));
}

private AttributeCheckResult<String> checkBapp(BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {
return checkAttribute(
bidderBid.getBid().getBundle(),
blockingConfig.getBapp(),
blockedAttributeValues(BlockedAttributes::getBapp));
}

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

return checkAttribute(
bidderBid.getBid().getAttr(),
blockingConfig.getBattr(),
blockedAttributeValues(BlockedAttributes::getBattr, bidderBid.getBid().getImpid()));
}

private <T> AttributeCheckResult<T> checkAttribute(
List<T> attribute, BidAttributeBlockingConfig<T> blockingConfig, List<T> blockedAttributeValues) {
private <T> AttributeCheckResult<T> checkAttribute(List<T> attribute,
BidAttributeBlockingConfig<T> blockingConfig,
List<T> blockedAttributeValues) {

if (blockingConfig == null || !blockingConfig.isEnforceBlocks()) {
return AttributeCheckResult.succeeded();
Expand All @@ -171,8 +194,9 @@ private <T> AttributeCheckResult<T> checkAttribute(
return AttributeCheckResult.succeeded();
}

private AttributeCheckResult<String> checkAttribute(
String attribute, BidAttributeBlockingConfig<String> blockingConfig, List<String> blockedAttributeValues) {
private AttributeCheckResult<String> checkAttribute(String attribute,
BidAttributeBlockingConfig<String> blockingConfig,
List<String> blockedAttributeValues) {

if (blockingConfig == null
|| !blockingConfig.isEnforceBlocks()
Expand All @@ -182,8 +206,23 @@ private AttributeCheckResult<String> checkAttribute(
return AttributeCheckResult.succeeded();
}

final boolean blocked =
blockedAttributeValues.contains(attribute) && !blockingConfig.getAllowedValues().contains(attribute);
final boolean blocked = blockedAttributeValues.contains(attribute)
&& !blockingConfig.getAllowedValues().contains(attribute);

return blocked
? AttributeCheckResult.failed(Collections.singletonList(attribute))
: AttributeCheckResult.succeeded();
}

private AttributeCheckResult<Integer> checkAttributeComplement(Integer attribute,
BidAttributeBlockingConfig<Integer> blockingConfig,
Integer blockedAttributeComplementValue) {

if (blockingConfig == null || !blockingConfig.isEnforceBlocks()) {
return AttributeCheckResult.succeeded();
}

final boolean blocked = !blockedAttributeComplementValue.equals(attribute);

return blocked
? AttributeCheckResult.failed(Collections.singletonList(attribute))
Expand All @@ -200,10 +239,7 @@ private <T> T blockedAttributeValues(Function<BlockedAttributes, Map<String, T>>
return blockedAttributeValues != null ? blockedAttributeValues.get(impId) : null;
}

private List<String> debugMessages(
Set<Integer> blockedBidIndexes,
List<Result<BlockingResult>> blockedBidResults) {

private List<String> debugMessages(Set<Integer> blockedBidIndexes, List<Result<BlockingResult>> blockedBidResults) {
if (!debugEnabled) {
return null;
}
Expand Down Expand Up @@ -244,6 +280,10 @@ private Map<String, Object> toAnalyticsResultValues(BlockingResult blockingResul
if (bcatResult.isFailed()) {
values.put(BCAT_FIELD, bcatResult.getFailedValues());
}
final AttributeCheckResult<Integer> cattaxResult = blockingResult.getCattaxCheckResult();
if (cattaxResult.isFailed()) {
values.put(BCAT_FIELD, cattaxResult.getFailedValues());
}
final AttributeCheckResult<String> bappResult = blockingResult.getBappCheckResult();
if (bappResult.isFailed()) {
values.put(BUNDLE_FIELD, bappResult.getFailedValues().get(0));
Expand All @@ -261,6 +301,7 @@ private static class BlockingResult {

private static final String BADV_ATTRIBUTE = "badv";
private static final String BCAT_ATTRIBUTE = "bcat";
private static final String CATTAX_ATTRIBUTE = "cattax";
private static final String BAPP_ATTRIBUTE = "bapp";
private static final String BATTR_ATTRIBUTE = "battr";

Expand All @@ -272,28 +313,31 @@ private static class BlockingResult {

AttributeCheckResult<String> bcatCheckResult;

AttributeCheckResult<Integer> cattaxCheckResult;

AttributeCheckResult<String> bappCheckResult;

AttributeCheckResult<Integer> battrCheckResult;

public static BlockingResult of(
String impId,
AttributeCheckResult<String> badvCheckResult,
AttributeCheckResult<String> bcatCheckResult,
AttributeCheckResult<String> bappCheckResult,
AttributeCheckResult<Integer> battrCheckResult) {
public static BlockingResult of(String impId,
AttributeCheckResult<String> badvCheckResult,
AttributeCheckResult<String> bcatCheckResult,
AttributeCheckResult<Integer> cattaxCheckResult,
AttributeCheckResult<String> bappCheckResult,
AttributeCheckResult<Integer> battrCheckResult) {

final boolean blocked =
badvCheckResult.isFailed()
|| bcatCheckResult.isFailed()
|| bappCheckResult.isFailed()
|| battrCheckResult.isFailed();
final boolean blocked = badvCheckResult.isFailed()
|| bcatCheckResult.isFailed()
|| cattaxCheckResult.isFailed()
|| bappCheckResult.isFailed()
|| battrCheckResult.isFailed();

return of(
impId,
blocked,
badvCheckResult,
bcatCheckResult,
cattaxCheckResult,
bappCheckResult,
battrCheckResult);
}
Expand All @@ -310,6 +354,9 @@ public List<String> getFailedChecks() {
if (bcatCheckResult.isFailed()) {
failedChecks.add(BCAT_ATTRIBUTE);
}
if (cattaxCheckResult.isFailed()) {
failedChecks.add(CATTAX_ATTRIBUTE);
}
if (bappCheckResult.isFailed()) {
failedChecks.add(BAPP_ATTRIBUTE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.BidRequest;
import org.prebid.server.auction.versionconverter.OrtbVersion;
import org.prebid.server.hooks.modules.ortb2.blocking.core.exception.InvalidAccountConfigurationException;
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.BlockedAttributes;
import org.prebid.server.hooks.modules.ortb2.blocking.core.model.ExecutionResult;
Expand All @@ -13,40 +14,44 @@ public class BlockedAttributesResolver {

private final BidRequest bidRequest;
private final String bidder;
private final OrtbVersion ortbVersion;
private final ObjectNode accountConfig;
private final boolean debugEnabled;

private BlockedAttributesResolver(
BidRequest bidRequest,
String bidder,
ObjectNode accountConfig,
boolean debugEnabled) {
private BlockedAttributesResolver(BidRequest bidRequest,
String bidder,
OrtbVersion ortbVersion,
ObjectNode accountConfig,
boolean debugEnabled) {

this.bidRequest = bidRequest;
this.bidder = bidder;
this.ortbVersion = ortbVersion;
this.accountConfig = accountConfig;
this.debugEnabled = debugEnabled;
}

public static BlockedAttributesResolver create(
BidRequest bidRequest,
String bidder,
ObjectNode accountConfig,
boolean debugEnabled) {
public static BlockedAttributesResolver create(BidRequest bidRequest,
String bidder,
OrtbVersion ortbVersion,
ObjectNode accountConfig,
boolean debugEnabled) {

return new BlockedAttributesResolver(
Objects.requireNonNull(bidRequest),
Objects.requireNonNull(bidder),
Objects.requireNonNull(ortbVersion),
accountConfig,
debugEnabled);
}

public ExecutionResult<BlockedAttributes> resolve() {
final AccountConfigReader accountConfigReader = AccountConfigReader.create(accountConfig, bidder, debugEnabled);
final AccountConfigReader accountConfigReader = AccountConfigReader.create(
accountConfig, bidder, ortbVersion, debugEnabled);

try {
final Result<BlockedAttributes> blockedAttributesResult =
accountConfigReader.blockedAttributesFor(bidRequest);
final Result<BlockedAttributes> blockedAttributesResult = accountConfigReader
.blockedAttributesFor(bidRequest);

return ExecutionResult.<BlockedAttributes>builder()
.value(blockedAttributesResult.getValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class RequestUpdater {

Expand All @@ -27,14 +26,16 @@ public static RequestUpdater create(BlockedAttributes blockedAttributes) {
public BidRequest update(BidRequest bidRequest) {
final List<String> blockedAdomain = blockedAttributes.getBadv();
final List<String> blockedAdvCat = blockedAttributes.getBcat();
final Integer cattax = bidRequest.getCattax();
final List<String> blockedApp = blockedAttributes.getBapp();

return bidRequest.toBuilder()
.badv(CollectionUtils.isNotEmpty(blockedAdomain) ? blockedAdomain : bidRequest.getBadv())
.bcat(CollectionUtils.isNotEmpty(blockedAdvCat) ? blockedAdvCat : bidRequest.getBcat())
.bapp(CollectionUtils.isNotEmpty(blockedApp) ? blockedApp : bidRequest.getBapp())
.imp(updateImps(bidRequest.getImp()))
.build();
.badv(CollectionUtils.isNotEmpty(blockedAdomain) ? blockedAdomain : bidRequest.getBadv())
.bcat(CollectionUtils.isNotEmpty(blockedAdvCat) ? blockedAdvCat : bidRequest.getBcat())
.cattax(cattax != null ? cattax : blockedAttributes.getCattaxComplement())
.bapp(CollectionUtils.isNotEmpty(blockedApp) ? blockedApp : bidRequest.getBapp())
.imp(updateImps(bidRequest.getImp()))
.build();
}

private List<Imp> updateImps(List<Imp> imps) {
Expand All @@ -46,14 +47,13 @@ private List<Imp> updateImps(List<Imp> imps) {
}

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

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

final String impId = imp.getId();
final List<Integer> btypeForImp = blockedBannerType != null ? blockedBannerType.get(impId) : null;
Expand All @@ -69,10 +69,10 @@ private Imp updateImp(
final Banner.BannerBuilder bannerBuilder = banner != null ? banner.toBuilder() : Banner.builder();

return imp.toBuilder()
.banner(bannerBuilder
.btype(CollectionUtils.isNotEmpty(btypeForImp) ? btypeForImp : existingBtype)
.battr(CollectionUtils.isNotEmpty(battrForImp) ? battrForImp : existingBattr)
.build())
.build();
.banner(bannerBuilder
.btype(CollectionUtils.isNotEmpty(btypeForImp) ? btypeForImp : existingBtype)
.battr(CollectionUtils.isNotEmpty(battrForImp) ? battrForImp : existingBattr)
.build())
.build();
}
}
Loading

0 comments on commit d83789a

Please sign in to comment.