Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
118 changes: 75 additions & 43 deletions src/main/java/org/prebid/server/auction/InterstitialProcessor.java
Comment thread
CTMBNara marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
import com.iab.openrtb.request.Device;
import com.iab.openrtb.request.Format;
import com.iab.openrtb.request.Imp;
import lombok.Value;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.proto.openrtb.ext.request.ExtDevice;
import org.prebid.server.proto.openrtb.ext.request.ExtDeviceInt;
import org.prebid.server.proto.openrtb.ext.request.ExtDevicePrebid;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidSdk;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

public class InterstitialProcessor {

Expand All @@ -36,15 +43,28 @@ private BidRequest processBidRequest(BidRequest bidRequest) {
if (extDeviceInt != null) {
final int minWidthPerc = extDeviceInt.getMinWidthPerc();
final int minHeightPerc = extDeviceInt.getMinHeightPerc();
final boolean usePxRatio = usePxRatio(bidRequest);
final List<Imp> updatedImps = bidRequest.getImp().stream()
.map(imp -> processInterstitialImp(imp, device, minWidthPerc, minHeightPerc))
.map(imp -> processInterstitialImp(imp, device, minWidthPerc, minHeightPerc, usePxRatio))
.toList();
bidRequest = bidRequest.toBuilder().imp(updatedImps).build();
}
return bidRequest;
}

private Imp processInterstitialImp(Imp imp, Device device, int minWidthPerc, int minHeightPerc) {
private static boolean usePxRatio(BidRequest bidRequest) {
final ExtRequest extRequest = bidRequest.getExt();
final ExtRequestPrebid prebid = extRequest != null ? extRequest.getPrebid() : null;
final ExtRequestPrebidSdk sdk = prebid != null ? prebid.getSdk() : null;
return sdk != null && BooleanUtils.isTrue(sdk.getUsePxRatio());
}

private Imp processInterstitialImp(Imp imp,
Device device,
int minWidthPerc,
int minHeightPerc,
boolean usePxRatio) {

if (!isInterstitial(imp)) {
return imp;
}
Expand All @@ -54,36 +74,58 @@ private Imp processInterstitialImp(Imp imp, Device device, int minWidthPerc, int
return imp;
}

final InterstitialSize maxSize = getMaxSize(banner, device, imp.getId(), usePxRatio);
final double minWidth = getMinSize(maxSize.getW(), minWidthPerc);
final double minHeight = getMinSize(maxSize.getH(), minHeightPerc);

final List<Format> interstitialFormats = InterstitialSize.getNestedSizes(minWidth, minHeight, maxSize)
.map(size -> Format.builder().w(size.w).h(size.h).build())
.toList();

return CollectionUtils.isNotEmpty(interstitialFormats)
? imp.toBuilder().banner(banner.toBuilder().format(interstitialFormats).build()).build()
: imp;
}

private static InterstitialSize getMaxSize(Banner banner, Device device, String impId, boolean usePxRatio) {
final List<Format> formats = banner.getFormat();
final Format firstFormat = CollectionUtils.isEmpty(formats) ? null : formats.getFirst();
Integer maxHeight = firstFormat != null ? firstFormat.getH() : null;
Integer maxWidth = firstFormat != null ? firstFormat.getW() : null;
final Integer firstFormatWidth = firstFormat != null ? firstFormat.getW() : null;
final Integer firstFormatHeight = firstFormat != null ? firstFormat.getH() : null;

if (firstFormatWidth != null
&& firstFormatHeight != null
&& (firstFormatWidth != 1 || firstFormatHeight != 1)) {

if (maxHeight == null || maxWidth == null || (maxHeight == 1 && maxWidth == 1)) {
maxHeight = device.getH();
maxWidth = device.getW();
return InterstitialSize.interstitialSize(firstFormatWidth, firstFormatHeight);
}

if (maxHeight == null || maxWidth == null) {
final Integer deviceWidth = device.getW();
final Integer deviceHeight = device.getH();
if (deviceWidth == null || deviceHeight == null) {
throw new InvalidRequestException(
"Unable to read max interstitial size for Imp id=%s (No Device sizes and no Format objects)"
.formatted(imp.getId()));
.formatted(impId));
}

final double minHeight = (double) maxHeight / 100 * minHeightPerc;
final double minWidth = (double) maxWidth / 100 * minWidthPerc;
if (usePxRatio) {
final BigDecimal pxRatio = device.getPxratio();
return InterstitialSize.interstitialSize(
deviceSizeToDips(deviceWidth, pxRatio),
deviceSizeToDips(deviceHeight, pxRatio));
}

final List<Format> interstitialFormats =
InterstitialSize.getNestedSizes(minWidth, minHeight, maxWidth, maxHeight, MAX_SIZES_COUNT)
.stream()
.map(interstitialSize -> Format.builder().w(interstitialSize.w).h(interstitialSize.h).build())
.toList();
return InterstitialSize.interstitialSize(deviceWidth, deviceHeight);
}

if (CollectionUtils.isEmpty(interstitialFormats)) {
return imp;
}
private static int deviceSizeToDips(int size, BigDecimal pxRatio) {
return pxRatio != null && pxRatio.signum() > 0
? Math.max(1, (int) Math.round(size / pxRatio.doubleValue()))
: size;
}

return imp.toBuilder().banner(banner.toBuilder().format(interstitialFormats).build()).build();
private static double getMinSize(int maxSize, int minSizePerc) {
return maxSize / 100.0 * minSizePerc;
}

private ExtDeviceInt getExtDeviceInt(Device device) {
Expand All @@ -92,6 +134,7 @@ private ExtDeviceInt getExtDeviceInt(Device device) {
return extDevicePrebid != null ? extDevicePrebid.getInterstitial() : null;
}

@Value(staticConstructor = "interstitialSize")
private static class InterstitialSize {

private static final List<InterstitialSize> INTERSTITIAL_SIZES = new ArrayList<>();
Expand Down Expand Up @@ -349,33 +392,22 @@ private static class InterstitialSize {
INTERSTITIAL_SIZES.add(interstitialSize(120, 20));
}

private final Integer w;
private final Integer h;

private InterstitialSize(Integer w, Integer h) {
this.w = w;
this.h = h;
}

private static InterstitialSize interstitialSize(Integer w, Integer h) {
return new InterstitialSize(w, h);
}

private static List<InterstitialSize> getNestedSizes(double minWidth,
double minHeight,
double maxWidth,
double maxHeight,
int count) {
int w;
int h;

private static Stream<InterstitialSize> getNestedSizes(double minWidth,
double minHeight,
InterstitialSize max) {
return INTERSTITIAL_SIZES.stream()
.filter(size -> isNested(size, minWidth, minHeight, maxWidth, maxHeight))
.limit(count)
.toList();
.filter(size -> isNested(size, minWidth, minHeight, max))
.limit(MAX_SIZES_COUNT);
}

private static boolean isNested(InterstitialSize size, double minWidth, double minHeight, double maxWidth,
double maxHeight) {
return size.w >= minWidth && size.w <= maxWidth && size.h >= minHeight && size.h <= maxHeight;
private static boolean isNested(InterstitialSize size,
double minWidth,
double minHeight,
InterstitialSize max) {
return size.w >= minWidth && size.w <= max.w && size.h >= minHeight && size.h <= max.h;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.prebid.server.proto.openrtb.ext.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Value;

import java.util.List;
Expand All @@ -14,4 +15,11 @@ public class ExtRequestPrebidSdk {
* Defines the contract for bidrequest.ext.prebid.sdk.renderers
*/
List<ExtRequestPrebidSdkRenderer> renderers;

/**
* Defines the contract for bidrequest.ext.prebid.sdk.usepxratio
*/
@JsonProperty("usepxratio")
Boolean usePxRatio;

}
Loading