forked from prebid/prebid-server-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThirtyThreeAcrossBidder.java
More file actions
218 lines (186 loc) · 9.29 KB
/
Copy pathThirtyThreeAcrossBidder.java
File metadata and controls
218 lines (186 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package org.prebid.server.bidder.thirtythreeacross;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Video;
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.collections4.ListUtils;
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.thirtythreeacross.proto.ThirtyThreeAcrossExtTtx;
import org.prebid.server.bidder.thirtythreeacross.proto.ThirtyThreeAcrossExtTtxCaller;
import org.prebid.server.bidder.thirtythreeacross.proto.ThirtyThreeAcrossImpExt;
import org.prebid.server.bidder.thirtythreeacross.proto.ThirtyThreeAcrossImpExtTtx;
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.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.thirtythreeacross.ExtImpThirtyThreeAcross;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;
import org.prebid.server.util.ObjectUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class ThirtyThreeAcrossBidder implements Bidder<BidRequest> {
private static final TypeReference<ExtPrebid<?, ExtImpThirtyThreeAcross>> THIRTY_THREE_ACROSS_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};
private static final ThirtyThreeAcrossExtTtxCaller PREBID_CALLER =
ThirtyThreeAcrossExtTtxCaller.of("Prebid-Server-Java", "n/a");
private static final JsonPointer BID_MEDIA_TYPE_POINTER = JsonPointer.valueOf("/ttx/mediaType");
private final String endpointUrl;
private final JacksonMapper mapper;
public ThirtyThreeAcrossBidder(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 Map<String, List<Imp>> impsMap = new HashMap<>();
final List<HttpRequest<BidRequest>> requests = new ArrayList<>();
final BidRequest modifiedRequest = modifyRequest(request, errors);
for (Imp imp : modifiedRequest.getImp()) {
try {
validateImp(imp);
final ExtImpThirtyThreeAcross extImpTtx = parseImpExt(imp);
final String productId = extImpTtx.getProductId();
final ThirtyThreeAcrossImpExt modifiedImpExt = createImpExt(productId, extImpTtx);
final Imp updatedImp = updateImp(imp, productId, modifiedImpExt);
impsMap.computeIfAbsent(getImpGroupName(modifiedImpExt), ignored -> new ArrayList<>()).add(updatedImp);
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
}
}
for (List<Imp> imps : impsMap.values()) {
requests.add(createRequest(modifiedRequest, imps));
}
return Result.of(requests, errors);
}
private BidRequest modifyRequest(BidRequest request, List<BidderError> errors) {
try {
return request.toBuilder().ext(modifyRequestExt(request.getExt())).build();
} catch (PreBidException e) {
errors.add(BidderError.badInput(e.getMessage()));
return request;
}
}
private ExtRequest modifyRequestExt(ExtRequest ext) throws PreBidException {
final Map<String, JsonNode> extProperties = ObjectUtil.getIfNotNull(ext, ExtRequest::getProperties);
final JsonNode extTtxNode = ObjectUtil.getIfNotNull(extProperties, properties -> properties.get("ttx"));
final ThirtyThreeAcrossExtTtx extTtx;
try {
extTtx = mapper.mapper().convertValue(extTtxNode, ThirtyThreeAcrossExtTtx.class);
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage());
}
final ExtRequest modifiedExt = ExtRequest.empty();
if (extProperties != null) {
modifiedExt.addProperties(extProperties);
}
modifiedExt.addProperty("ttx", mapper.mapper().valueToTree(modifyRequestExtTtx(extTtx)));
return modifiedExt;
}
private static ThirtyThreeAcrossExtTtx modifyRequestExtTtx(ThirtyThreeAcrossExtTtx extTtx) {
return ThirtyThreeAcrossExtTtx.of(extTtx != null && CollectionUtils.isNotEmpty(extTtx.getCaller())
? ListUtils.union(extTtx.getCaller(), Collections.singletonList(PREBID_CALLER))
: Collections.singletonList(PREBID_CALLER));
}
private static void validateImp(Imp imp) {
if (imp.getBanner() == null && imp.getVideo() == null) {
throw new PreBidException(
"Imp ID %s must have at least one of [Banner, Video] defined".formatted(imp.getId()));
}
}
private ExtImpThirtyThreeAcross parseImpExt(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), THIRTY_THREE_ACROSS_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage());
}
}
private static ThirtyThreeAcrossImpExt createImpExt(String productId, ExtImpThirtyThreeAcross extImpTtx) {
final String zoneId = extImpTtx.getZoneId();
return ThirtyThreeAcrossImpExt.of(ThirtyThreeAcrossImpExtTtx.of(
productId, StringUtils.isNotEmpty(zoneId) ? zoneId : extImpTtx.getSiteId()));
}
private Imp updateImp(Imp imp, String productId, ThirtyThreeAcrossImpExt impExt) {
return imp.toBuilder()
.video(updatedVideo(imp.getVideo(), productId))
.ext(mapper.mapper().valueToTree(impExt))
.build();
}
private static Video updatedVideo(Video video, String productId) {
if (video == null) {
return null;
}
if (BidderUtil.isNullOrZero(video.getW())
|| BidderUtil.isNullOrZero(video.getH())
|| CollectionUtils.isEmpty(video.getProtocols())
|| CollectionUtils.isEmpty(video.getMimes())
|| CollectionUtils.isEmpty(video.getPlaybackmethod())) {
throw new PreBidException("One or more invalid or missing video field(s) "
+ "w, h, protocols, mimes, playbackmethod");
}
return video.toBuilder()
.startdelay(resolveStartDelay(video.getStartdelay(), productId))
.build();
}
private static Integer resolveStartDelay(Integer startDelay, String productId) {
return Objects.equals(productId, "instream") ? Integer.valueOf(0) : startDelay;
}
private static String getImpGroupName(ThirtyThreeAcrossImpExt impExt) {
final ThirtyThreeAcrossImpExtTtx impExtTtx = impExt.getTtx();
return impExtTtx.getProd() + impExtTtx.getZoneid();
}
private HttpRequest<BidRequest> createRequest(BidRequest request, List<Imp> requestImps) {
final BidRequest modifiedRequest = request.toBuilder()
.imp(requestImps)
.build();
return BidderUtil.defaultRequest(modifiedRequest, 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 String mediaType = ObjectUtil.getIfNotNull(bid.getExt(),
ext -> ext.at(BID_MEDIA_TYPE_POINTER).asText(null));
return "video".equals(mediaType) ? BidType.video : BidType.banner;
}
}