forked from prebid/prebid-server-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e542c7a
commit cc46412
Showing
16 changed files
with
779 additions
and
3 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/main/java/org/prebid/server/bidder/pubrise/PubriseBidder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package org.prebid.server.bidder.pubrise; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.iab.openrtb.request.BidRequest; | ||
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.bidder.pubrise.proto.PubriseImpExtBidder; | ||
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.pubrise.ExtImpPubrise; | ||
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; | ||
|
||
public class PubriseBidder implements Bidder<BidRequest> { | ||
|
||
private static final TypeReference<ExtPrebid<?, ExtImpPubrise>> TYPE_REFERENCE = new TypeReference<>() { | ||
}; | ||
|
||
private final String endpointUrl; | ||
private final JacksonMapper mapper; | ||
|
||
public PubriseBidder(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<HttpRequest<BidRequest>> outgoingRequests = new ArrayList<>(); | ||
final List<BidderError> errors = new ArrayList<>(); | ||
|
||
for (Imp imp : request.getImp()) { | ||
final ExtImpPubrise extImp; | ||
try { | ||
extImp = parseImpExt(imp); | ||
outgoingRequests.add(makeRequest(modifyImp(imp, extImp), request)); | ||
} catch (PreBidException e) { | ||
errors.add(BidderError.badInput(e.getMessage())); | ||
} | ||
} | ||
|
||
return CollectionUtils.isEmpty(outgoingRequests) | ||
? Result.withError(BidderError.badInput("found no valid impressions")) | ||
: Result.of(outgoingRequests, errors); | ||
} | ||
|
||
private ExtImpPubrise parseImpExt(Imp imp) { | ||
try { | ||
return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder(); | ||
} catch (IllegalArgumentException e) { | ||
throw new PreBidException(e.getMessage()); | ||
} | ||
} | ||
|
||
private Imp modifyImp(Imp imp, ExtImpPubrise extImp) { | ||
final PubriseImpExtBidder impExtBidder = getImpExtWithType(extImp); | ||
final ObjectNode modifiedImpExtBidder = mapper.mapper().createObjectNode(); | ||
|
||
modifiedImpExtBidder.set("bidder", mapper.mapper().valueToTree(impExtBidder)); | ||
|
||
return imp.toBuilder().ext(modifiedImpExtBidder).build(); | ||
} | ||
|
||
private PubriseImpExtBidder getImpExtWithType(ExtImpPubrise extImpQt) { | ||
final boolean hasPlacementId = StringUtils.isNotBlank(extImpQt.getPlacementId()); | ||
final boolean hasEndpointId = StringUtils.isNotBlank(extImpQt.getEndpointId()); | ||
|
||
return PubriseImpExtBidder.builder() | ||
.type(hasPlacementId ? "publisher" : hasEndpointId ? "network" : null) | ||
.placementId(hasPlacementId ? extImpQt.getPlacementId() : null) | ||
.endpointId(hasEndpointId ? extImpQt.getEndpointId() : null) | ||
.build(); | ||
} | ||
|
||
private HttpRequest<BidRequest> makeRequest(Imp imp, BidRequest request) { | ||
final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(imp)).build(); | ||
return BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper); | ||
} | ||
|
||
@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 4 -> BidType.xNative; | ||
default -> throw new PreBidException("Unable to fetch mediaType in multi-format: %s" | ||
.formatted(bid.getImpid())); | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/prebid/server/bidder/pubrise/proto/PubriseImpExtBidder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.prebid.server.bidder.pubrise.proto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
@Builder | ||
@Value | ||
public class PubriseImpExtBidder { | ||
|
||
String type; | ||
|
||
@JsonProperty("placementId") | ||
String placementId; | ||
|
||
@JsonProperty("endpointId") | ||
String endpointId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/org/prebid/server/proto/openrtb/ext/request/pubrise/ExtImpPubrise.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.prebid.server.proto.openrtb.ext.request.pubrise; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Value; | ||
|
||
@Value(staticConstructor = "of") | ||
public class ExtImpPubrise { | ||
|
||
@JsonProperty("placementId") | ||
String placementId; | ||
|
||
@JsonProperty("endpointId") | ||
String endpointId; | ||
} |
2 changes: 1 addition & 1 deletion
2
...r/proto/openrtb/ext/request/ExtImpQt.java → ...roto/openrtb/ext/request/qt/ExtImpQt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/prebid/server/spring/config/bidder/PubriseConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.prebid.server.spring.config.bidder; | ||
|
||
import org.prebid.server.bidder.BidderDeps; | ||
import org.prebid.server.bidder.pubrise.PubriseBidder; | ||
import org.prebid.server.json.JacksonMapper; | ||
import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties; | ||
import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler; | ||
import org.prebid.server.spring.config.bidder.util.UsersyncerCreator; | ||
import org.prebid.server.spring.env.YamlPropertySourceFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
@Configuration | ||
@PropertySource(value = "classpath:/bidder-config/pubrise.yaml", factory = YamlPropertySourceFactory.class) | ||
public class PubriseConfiguration { | ||
|
||
private static final String BIDDER_NAME = "pubrise"; | ||
|
||
@Bean("pubriseConfigurationProperties") | ||
@ConfigurationProperties("adapters.pubrise") | ||
BidderConfigurationProperties configurationProperties() { | ||
return new BidderConfigurationProperties(); | ||
} | ||
|
||
@Bean | ||
BidderDeps pubriseBidderDeps(BidderConfigurationProperties pubriseConfigurationProperties, | ||
@NotBlank @Value("${external-url}") String externalUrl, | ||
JacksonMapper mapper) { | ||
|
||
return BidderDepsAssembler.forBidder(BIDDER_NAME) | ||
.withConfig(pubriseConfigurationProperties) | ||
.usersyncerCreator(UsersyncerCreator.create(externalUrl)) | ||
.bidderCreator(config -> new PubriseBidder(config.getEndpoint(), mapper)) | ||
.assemble(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
adapters: | ||
pubrise: | ||
endpoint: https://backend.pubrise.ai/ | ||
meta-info: | ||
maintainer-email: [email protected] | ||
app-media-types: | ||
- banner | ||
- video | ||
- native | ||
site-media-types: | ||
- banner | ||
- video | ||
- native | ||
supported-vendors: | ||
vendor-id: 0 | ||
usersync: | ||
cookie-family-name: pubrise | ||
redirect: | ||
support-cors: false | ||
url: https://sync.pubrise.ai/pbserver?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ccpa={{us_privacy}}&gpp={{gpp}}&gpp_sid={{gpp_sid}}&redir={{redirect_url}} | ||
uid-macro: '[UID]' | ||
iframe: | ||
support-cors: false | ||
url: https://sync.pubrise.ai/pbserverIframe?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ccpa={{us_privacy}}&gpp={{gpp}}&gpp_sid={{gpp_sid}}&pbserverUrl={{redirect_url}} | ||
uid-macro: '[UID]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "Pubrise Adapter Params", | ||
"description": "A schema which validates params accepted by the Pubrise adapter", | ||
"type": "object", | ||
"properties": { | ||
"placementId": { | ||
"type": "string", | ||
"minLength": 1, | ||
"description": "Placement ID" | ||
}, | ||
"endpointId": { | ||
"type": "string", | ||
"minLength": 1, | ||
"description": "Endpoint ID" | ||
} | ||
}, | ||
"oneOf": [ | ||
{ | ||
"required": [ | ||
"placementId" | ||
] | ||
}, | ||
{ | ||
"required": [ | ||
"endpointId" | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.