Skip to content

Commit

Permalink
Merge branch 'prebid:master' into tradplus
Browse files Browse the repository at this point in the history
  • Loading branch information
tradplus authored Oct 18, 2024
2 parents 77816f0 + 87e36d9 commit 5056de3
Show file tree
Hide file tree
Showing 36 changed files with 1,147 additions and 186 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: [ 'java' ]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 21

- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}

- name: Build with Maven
run: mvn -B package --file extra/pom.xml

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
with:
category: "/language:${{ matrix.language }}"
2 changes: 1 addition & 1 deletion extra/bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.prebid</groupId>
<artifactId>prebid-server-aggregator</artifactId>
<version>3.14.0-SNAPSHOT</version>
<version>3.15.0-SNAPSHOT</version>
<relativePath>../../extra/pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion extra/modules/confiant-ad-quality/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.prebid.server.hooks.modules</groupId>
<artifactId>all-modules</artifactId>
<version>3.14.0-SNAPSHOT</version>
<version>3.15.0-SNAPSHOT</version>
</parent>

<artifactId>confiant-ad-quality</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extra/modules/fiftyone-devicedetection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.prebid.server.hooks.modules</groupId>
<artifactId>all-modules</artifactId>
<version>3.14.0-SNAPSHOT</version>
<version>3.15.0-SNAPSHOT</version>
</parent>

<artifactId>fiftyone-devicedetection</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extra/modules/ortb2-blocking/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.prebid.server.hooks.modules</groupId>
<artifactId>all-modules</artifactId>
<version>3.14.0-SNAPSHOT</version>
<version>3.15.0-SNAPSHOT</version>
</parent>

<artifactId>ortb2-blocking</artifactId>
Expand Down
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;
}
}
Loading

0 comments on commit 5056de3

Please sign in to comment.