Skip to content

Commit

Permalink
Merge branch 'master' into melozen-bidder
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/test/resources/org/prebid/server/it/test-application.properties
  • Loading branch information
Compile-Ninja committed Aug 21, 2024
2 parents 355d1b1 + 4f0d564 commit acd4335
Show file tree
Hide file tree
Showing 37 changed files with 1,286 additions and 118 deletions.
11 changes: 5 additions & 6 deletions src/main/java/org/prebid/server/auction/ImpAdjuster.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ public Imp adjust(Imp originalImp, String bidder, BidderAliases bidderAliases, L
final JsonNode bidderNode = getBidderNode(bidder, bidderAliases, impExtPrebidImp);

if (bidderNode == null || bidderNode.isEmpty()) {
removeImpExtPrebidImp(originalImp.getExt());
return originalImp;
}

// remove circular references according to the requirements
removeExtPrebidBidder(bidderNode);

try {
final JsonNode originalImpNode = jacksonMapper.mapper().valueToTree(originalImp);
final JsonNode mergedImpNode = jsonMerger.merge(bidderNode, originalImpNode);

// clean up merged imp.ext.prebid.imp
removeImpExtPrebidImp(mergedImpNode);
removeImpExtPrebidImp(mergedImpNode.get(IMP_EXT));

final Imp resultImp = jacksonMapper.mapper().convertValue(mergedImpNode, Imp.class);

Expand All @@ -61,6 +60,7 @@ public Imp adjust(Imp originalImp, String bidder, BidderAliases bidderAliases, L
} catch (Exception e) {
debugMessages.add("imp.ext.prebid.imp.%s can not be merged into original imp [id=%s], reason: %s"
.formatted(bidder, originalImp.getId(), e.getMessage()));
removeImpExtPrebidImp(originalImp.getExt());
return originalImp;
}
}
Expand Down Expand Up @@ -90,9 +90,8 @@ private static void removeExtPrebidBidder(JsonNode bidderNode) {
.ifPresent(ext -> ext.remove(EXT_PREBID_BIDDER));
}

private static void removeImpExtPrebidImp(JsonNode mergedImpNode) {
Optional.ofNullable(mergedImpNode.get(IMP_EXT))
.map(extNode -> extNode.get(EXT_PREBID))
private static void removeImpExtPrebidImp(JsonNode impExt) {
Optional.ofNullable(impExt.get(EXT_PREBID))
.map(ObjectNode.class::cast)
.ifPresent(prebid -> prebid.remove(EXT_PREBID_IMP));
}
Expand Down

This file was deleted.

158 changes: 158 additions & 0 deletions src/main/java/org/prebid/server/bidder/metax/MetaxBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.prebid.server.bidder.metax;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Format;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.bidder.model.BidderCall;
import org.prebid.server.bidder.model.BidderError;
import org.prebid.server.bidder.model.HttpRequest;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.metax.ExtImpMetax;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class MetaxBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpMetax>> METAX_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};
private static final String PUBLISHER_ID_MACRO = "{{publisherId}}";
private static final String AD_UNIT_MACRO = "{{adUnit}}";

private final String endpointUrl;
private final JacksonMapper mapper;

public MetaxBidder(String endpointUrl, JacksonMapper mapper) {
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {
final List<BidderError> errors = new ArrayList<>();
final List<HttpRequest<BidRequest>> httpRequests = new ArrayList<>();

for (Imp imp : request.getImp()) {
try {
final ExtImpMetax extImpMetax = parseImpExt(imp);
httpRequests.add(BidderUtil.defaultRequest(prepareBidRequest(request, imp),
resolveEndpoint(extImpMetax),
mapper));
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}

return Result.of(httpRequests, errors);
}

private ExtImpMetax parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), METAX_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage());
}
}

private static BidRequest prepareBidRequest(BidRequest bidRequest, Imp imp) {
return bidRequest.toBuilder()
.imp(Collections.singletonList(modifyImp(imp)))
.build();
}

private static Imp modifyImp(Imp imp) {
final Banner banner = imp.getBanner();
final Integer width = banner != null ? banner.getW() : null;
final Integer height = banner != null ? banner.getH() : null;
if (width != null && height != null) {
return imp;
}

final List<Format> formats = banner != null ? banner.getFormat() : null;
if (CollectionUtils.isEmpty(formats)) {
return imp;
}

final Format firstFormat = formats.getFirst();
return imp.toBuilder()
.banner(banner.toBuilder()
.w(Optional.ofNullable(firstFormat).map(Format::getW).orElse(0))
.h(Optional.ofNullable(firstFormat).map(Format::getH).orElse(0))
.build())
.build();
}

private String resolveEndpoint(ExtImpMetax extImpMetax) {
final String publisherIdAsString = Optional.ofNullable(extImpMetax.getPublisherId())
.map(Object::toString)
.orElse(StringUtils.EMPTY);
final String adUnitAsString = Optional.ofNullable(extImpMetax.getAdUnit())
.map(Object::toString)
.orElse(StringUtils.EMPTY);

return endpointUrl
.replace(PUBLISHER_ID_MACRO, publisherIdAsString)
.replace(AD_UNIT_MACRO, adUnitAsString);
}

@Override
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(bidResponse));
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}

return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid).filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur()))
.toList();
}

private static BidType getBidType(Bid bid) {
final Integer markupType = bid.getMtype();
if (markupType == null) {
throw new PreBidException("Missing MType for bid: " + bid.getId());
}

return switch (markupType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 3 -> BidType.audio;
case 4 -> BidType.xNative;
default -> throw new PreBidException("Unsupported MType: %s"
.formatted(bid.getImpid()));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ private BigDecimal convertToXAPICurrency(BigDecimal value,

private static BigDecimal resolveBidFloorPrice(Imp imp) {
final BigDecimal bidFloor = imp.getBidfloor();
return BidderUtil.isValidPrice(bidFloor) ? bidFloor : null;
return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) >= 0 ? bidFloor : null;
}

private static String resolveBidFloorCurrency(Imp imp, BidRequest bidRequest, List<BidderError> errors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.prebid.server.bidder.liftoff;
package org.prebid.server.bidder.vungle;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.App;
Expand All @@ -13,18 +13,18 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.liftoff.model.LiftoffImpressionExt;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.bidder.model.BidderCall;
import org.prebid.server.bidder.model.BidderError;
import org.prebid.server.bidder.model.HttpRequest;
import org.prebid.server.bidder.model.Price;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.bidder.vungle.model.VungleImpressionExt;
import org.prebid.server.currency.CurrencyConversionService;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.request.liftoff.ExtImpLiftoff;
import org.prebid.server.proto.openrtb.ext.request.vungle.ExtImpVungle;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;
Expand All @@ -37,7 +37,7 @@
import java.util.List;
import java.util.Objects;

public class LiftoffBidder implements Bidder<BidRequest> {
public class VungleBidder implements Bidder<BidRequest> {

private static final String BIDDER_CURRENCY = "USD";
private static final String X_OPENRTB_VERSION = "2.5";
Expand All @@ -46,9 +46,9 @@ public class LiftoffBidder implements Bidder<BidRequest> {
private final CurrencyConversionService currencyConversionService;
private final JacksonMapper mapper;

public LiftoffBidder(String endpointUrl,
CurrencyConversionService currencyConversionService,
JacksonMapper mapper) {
public VungleBidder(String endpointUrl,
CurrencyConversionService currencyConversionService,
JacksonMapper mapper) {

this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.currencyConversionService = Objects.requireNonNull(currencyConversionService);
Expand All @@ -63,8 +63,8 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
for (Imp imp : bidRequest.getImp()) {
try {
final Price price = resolveBidFloor(imp, bidRequest);
final LiftoffImpressionExt impExt = parseImpExt(imp);
final LiftoffImpressionExt modifiedImpExt = modifyImpExt(impExt, bidRequest);
final VungleImpressionExt impExt = parseImpExt(imp);
final VungleImpressionExt modifiedImpExt = modifyImpExt(impExt, bidRequest);
final Imp modifiedImp = modifyImp(imp, modifiedImpExt, price);
final BidRequest modifiedRequest = modifyBidRequest(
bidRequest,
Expand Down Expand Up @@ -92,22 +92,22 @@ private Price resolveBidFloor(Imp imp, BidRequest bidRequest) {
return Price.of(BIDDER_CURRENCY, bigDecimal);
}

private LiftoffImpressionExt parseImpExt(Imp imp) {
return mapper.mapper().convertValue(imp.getExt(), LiftoffImpressionExt.class);
private VungleImpressionExt parseImpExt(Imp imp) {
return mapper.mapper().convertValue(imp.getExt(), VungleImpressionExt.class);
}

private static LiftoffImpressionExt modifyImpExt(LiftoffImpressionExt impExt, BidRequest bidRequest) {
final ExtImpLiftoff bidder = impExt.getBidder();
private static VungleImpressionExt modifyImpExt(VungleImpressionExt impExt, BidRequest bidRequest) {
final ExtImpVungle bidder = impExt.getBidder();
final String buyerId = ObjectUtil.getIfNotNull(bidRequest.getUser(), User::getBuyeruid);
final ExtImpLiftoff vungle = ExtImpLiftoff.of(
final ExtImpVungle vungle = ExtImpVungle.of(
buyerId,
bidder.getAppStoreId(),
bidder.getPlacementReferenceId());

return impExt.toBuilder().vungle(vungle).build();
}

private Imp modifyImp(Imp imp, LiftoffImpressionExt modifiedImpExt, Price price) {
private Imp modifyImp(Imp imp, VungleImpressionExt modifiedImpExt, Price price) {
return imp.toBuilder()
.tagid(modifiedImpExt.getBidder().getPlacementReferenceId())
.ext(mapper.mapper().convertValue(modifiedImpExt, ObjectNode.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.prebid.server.bidder.vungle.model;

import lombok.Builder;
import lombok.Getter;
import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid;
import org.prebid.server.proto.openrtb.ext.request.vungle.ExtImpVungle;

@Builder(toBuilder = true)
@Getter
public class VungleImpressionExt {

ExtImpPrebid prebid;

ExtImpVungle bidder;

ExtImpVungle vungle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.metax;

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

@Value(staticConstructor = "of")
public class ExtImpMetax {

@JsonProperty("publisherId")
Integer publisherId;

@JsonProperty("adunit")
Integer adUnit;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.prebid.server.proto.openrtb.ext.request.liftoff;
package org.prebid.server.proto.openrtb.ext.request.vungle;

import lombok.Value;

@Value(staticConstructor = "of")
public class ExtImpLiftoff {
public class ExtImpVungle {

String bidToken;

Expand Down
Loading

0 comments on commit acd4335

Please sign in to comment.