Skip to content

Commit

Permalink
Update First Party Data workflow and introduce Bidder Config (#573)
Browse files Browse the repository at this point in the history
* Update First Party Data workflow and introduce Bidder Config

* Post-merge fix

* Fix post-merge errors

* Fix integration tests for new bidders

* Fix post-merge errors

* Add tests to validate no mask

* Handling of empty fpd json parameter

* Fix tests

* Add amp handling off targeting parameter

* Minor refactoring

* Make context field in imp.ext to be not required if null

* Revert test JSONs

* Revert test JSONs

Co-authored-by: DGarbar <dimagarbar49@gmail.com>
Co-authored-by: apavlyuk <apavlyuk@rubiconproject.com>
Co-authored-by: Sergii Chernysh <schernysh@rubiconproject.com>
Co-authored-by: rpanchyk <rpanchyk@rubiconproject.com>
  • Loading branch information
5 people authored Jun 3, 2020
1 parent fa0429c commit c9cfd47
Show file tree
Hide file tree
Showing 25 changed files with 698 additions and 236 deletions.
69 changes: 58 additions & 11 deletions src/main/java/org/prebid/server/auction/AmpRequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.privacy.ccpa.Ccpa;
import org.prebid.server.privacy.gdpr.TcfDefinerService;
Expand All @@ -32,9 +33,11 @@
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidCache;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidCacheBids;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidCacheVastxml;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidData;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestTargeting;
import org.prebid.server.proto.openrtb.ext.request.ExtSite;
import org.prebid.server.proto.openrtb.ext.request.ExtUser;
import org.prebid.server.proto.request.Targeting;
import org.prebid.server.util.HttpUtil;

import java.util.ArrayList;
Expand All @@ -48,6 +51,7 @@ public class AmpRequestFactory {
private static final Logger logger = LoggerFactory.getLogger(AmpRequestFactory.class);

private static final String TAG_ID_REQUEST_PARAM = "tag_id";
private static final String TARGETING_REQUEST_PARAM = "targeting";
private static final String DEBUG_REQUEST_PARAM = "debug";
private static final String OW_REQUEST_PARAM = "ow";
private static final String OH_REQUEST_PARAM = "oh";
Expand Down Expand Up @@ -84,7 +88,6 @@ public Future<AuctionContext> fromRequest(RoutingContext routingContext, long st
if (StringUtils.isBlank(tagId)) {
return Future.failedFuture(new InvalidRequestException("AMP requests require an AMP tag_id", true));
}

return createBidRequest(routingContext, tagId)
.compose(bidRequest ->
auctionRequestFactory.toAuctionContext(routingContext, bidRequest, startTime, timeoutResolver));
Expand Down Expand Up @@ -227,11 +230,15 @@ private BidRequest overrideParameters(BidRequest bidRequest, HttpServerRequest r
}
}

final Site updatedSite = overrideSite(bidRequest.getSite(), request);
final String requestTargeting = request.getParam(TARGETING_REQUEST_PARAM);
final Targeting targeting = parseTargeting(requestTargeting);

final Site updatedSite = overrideSite(bidRequest.getSite(), targeting, request);
final Imp updatedImp = overrideImp(bidRequest.getImp().get(0), request);
final Long updatedTimeout = overrideTimeout(bidRequest.getTmax(), request);
final User updatedUser = overrideUser(bidRequest.getUser(), gdprConsent);
final User updatedUser = overrideUser(bidRequest.getUser(), gdprConsent, targeting, request);
final Regs updatedRegs = overrideRegs(bidRequest.getRegs(), ccpaConsent);
final ObjectNode updatedExt = overrideExt(bidRequest.getExt(), targeting);

final BidRequest result;
if (updatedSite != null || updatedImp != null || updatedTimeout != null || updatedUser != null
Expand All @@ -242,20 +249,33 @@ private BidRequest overrideParameters(BidRequest bidRequest, HttpServerRequest r
.tmax(updatedTimeout != null ? updatedTimeout : bidRequest.getTmax())
.user(updatedUser != null ? updatedUser : bidRequest.getUser())
.regs(updatedRegs != null ? updatedRegs : bidRequest.getRegs())
.ext(updatedExt != null ? updatedExt : bidRequest.getExt())
.build();
} else {
result = bidRequest;
}
return result;
}

private Site overrideSite(Site site, HttpServerRequest request) {
private Targeting parseTargeting(String jsonTargeting) {
try {
final String decodedJsonTargeting = HttpUtil.decodeUrl(jsonTargeting);
return decodedJsonTargeting == null
? Targeting.empty()
: mapper.decodeValue(decodedJsonTargeting, Targeting.class);
} catch (DecodeException e) {
throw new InvalidRequestException(String.format("Error decoding targeting from url: %s", e.getMessage()));
}
}

private Site overrideSite(Site site, Targeting targeting, HttpServerRequest request) {
final String canonicalUrl = canonicalUrl(request);
final String accountId = request.getParam(ACCOUNT_REQUEST_PARAM);

final boolean hasSite = site != null;
final ObjectNode siteExt = hasSite ? site.getExt() : null;
final boolean shouldSetExtAmp = siteExt == null || siteExt.get("amp") == null;
final ObjectNode targetingSite = targeting.getSite();
final boolean shouldSetExtAmp = targetingSite != null || siteExt == null || siteExt.get("amp") == null;

if (StringUtils.isNotBlank(canonicalUrl) || StringUtils.isNotBlank(accountId) || shouldSetExtAmp) {
final Site.SiteBuilder siteBuilder = hasSite ? site.toBuilder() : Site.builder();
Expand All @@ -270,8 +290,12 @@ private Site overrideSite(Site site, HttpServerRequest request) {
siteBuilder.publisher(publisherBuilder.id(accountId).build());
}
if (shouldSetExtAmp) {
final ObjectNode data = siteExt != null ? (ObjectNode) siteExt.get("data") : null;
siteBuilder.ext(mapper.mapper().valueToTree(ExtSite.of(1, data)));
if (targetingSite != null) {
siteBuilder.ext(mapper.mapper().valueToTree(ExtSite.of(1, targetingSite)));
} else {
final ObjectNode data = siteExt != null ? (ObjectNode) siteExt.get("data") : null;
siteBuilder.ext(mapper.mapper().valueToTree(ExtSite.of(1, data)));
}
}
return siteBuilder.build();
}
Expand Down Expand Up @@ -400,8 +424,9 @@ private static Long overrideTimeout(Long tmax, HttpServerRequest request) {
return timeout > 0 && !Objects.equals(timeout, tmax) ? timeout : null;
}

private User overrideUser(User user, String gdprConsent) {
if (StringUtils.isBlank(gdprConsent)) {
private User overrideUser(User user, String gdprConsent, Targeting targeting, HttpServerRequest request) {
final ObjectNode targetingUser = targeting.getUser();
if (StringUtils.isBlank(gdprConsent) && targetingUser == null) {
return null;
}

Expand All @@ -412,12 +437,18 @@ private User overrideUser(User user, String gdprConsent) {
? extractExtUser(extUserNode).toBuilder()
: ExtUser.builder();

final ExtUser updatedExtUser = extUserBuilder.consent(gdprConsent).build();
if (StringUtils.isNotBlank(gdprConsent)) {
extUserBuilder.consent(gdprConsent);
}

if (targetingUser != null) {
extUserBuilder.data(targetingUser);
}

final User.UserBuilder userBuilder = hasUser ? user.toBuilder() : User.builder();

return userBuilder
.ext(mapper.mapper().valueToTree(updatedExtUser))
.ext(mapper.mapper().valueToTree(extUserBuilder.build()))
.build();
}

Expand Down Expand Up @@ -447,6 +478,22 @@ private Regs overrideRegs(Regs regs, String ccpaConsent) {
return Regs.of(coppa, mapper.mapper().valueToTree(ExtRegs.of(gdpr, ccpaConsent)));
}

private ObjectNode overrideExt(ObjectNode ext, Targeting targeting) {
final List<String> bidders = targeting.getBidders();
if (bidders == null) {
return null;
}
final ExtBidRequest extBidRequest = extBidRequest(ext);
final ExtRequestPrebid prebid = extBidRequest.getPrebid();
final ExtRequestPrebidData data = ExtRequestPrebidData.of(bidders);

final ExtRequestPrebid updatedPrebid = prebid == null
? ExtRequestPrebid.builder().data(data).build()
: prebid.toBuilder().data(data).build();

return mapper.mapper().valueToTree(ExtBidRequest.of(updatedPrebid));
}

/**
* Extracts {@link ExtRegs} from bidrequest.regs.ext {@link ObjectNode}.
*/
Expand Down
Loading

0 comments on commit c9cfd47

Please sign in to comment.