Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HttpRequest to support body as array of bytes #1535

Merged
merged 6 commits into from
Nov 9, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public <T> Future<Void> processEvent(T event) {
logEvent = new LogEvent<>("unknown", null);
}

logger.debug(mapper.encode(logEvent));
logger.debug(mapper.encodeToString(logEvent));

return Future.succeededFuture();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void updateConfig(boolean enabled, String endpoint, String scopeId) {
private <T> void buffer(T event) {
final ObjectNode eventNode = jacksonMapper.mapper().valueToTree(event);
eventNode.put(SCOPE_FIELD_NAME, scopeId);
final String jsonEvent = jacksonMapper.encode(eventNode);
final String jsonEvent = jacksonMapper.encodeToString(eventNode);
events.get().add(jsonEvent);
byteSize.getAndAdd(jsonEvent.getBytes().length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ private String createNativeMarkup(String bidAdm, Imp correspondingImp) {
}

responseAssets.forEach(asset -> setAssetTypes(asset, nativeRequest.getAssets()));
return mapper.encode(nativeMarkup);
return mapper.encodeToString(nativeMarkup);
}

return bidAdm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void setHeadersLog(CaseInsensitiveMultiMap headers) {

public void setExtBidResponse(ExtBidResponse response) {
try {
this.extBidResponse = response != null ? jacksonMapper.encode(response) : StringUtils.EMPTY;
this.extBidResponse = response != null ? jacksonMapper.encodeToString(response) : StringUtils.EMPTY;
} catch (EncodeException ex) {
final String errorMessage = String.format("Unable to marshal response ext for debugging with a reason: %s",
ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private Void handleTimeoutNotificationSuccess(HttpClientResponse response, HttpR
String.format(
"Notified bidder about timeout. Status code: %s. Request body: %s",
response.getStatusCode(),
timeoutNotification.getBody()),
new String(timeoutNotification.getBody())),
rpanchyk marked this conversation as resolved.
Show resolved Hide resolved
logTimeoutNotificationSamplingRate);
}

Expand All @@ -84,7 +84,7 @@ private Void handleTimeoutNotificationFailure(Throwable exception, HttpRequest<V
String.format(
"Error occurred while notifying bidder about timeout. Error message: %s. Request body: %s",
exception.getMessage(),
timeoutNotification.getBody()),
new String(timeoutNotification.getBody())),
logTimeoutNotificationSamplingRate);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/prebid/server/bidder/GenericBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest b
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.body(mapper.encode(bidRequest))
.body(mapper.encodeToBytes(bidRequest))
.payload(bidRequest)
.build());
}
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/org/prebid/server/bidder/HttpBidderRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.prebid.server.bidder.model.HttpResponse;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.execution.Timeout;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.model.CaseInsensitiveMultiMap;
import org.prebid.server.proto.openrtb.ext.response.ExtHttpCall;
import org.prebid.server.util.HttpUtil;
Expand Down Expand Up @@ -52,16 +53,19 @@ public class HttpBidderRequester {
private final BidderRequestCompletionTrackerFactory completionTrackerFactory;
private final BidderErrorNotifier bidderErrorNotifier;
private final HttpBidderRequestEnricher requestEnricher;
private final JacksonMapper mapper;

public HttpBidderRequester(HttpClient httpClient,
BidderRequestCompletionTrackerFactory completionTrackerFactory,
BidderErrorNotifier bidderErrorNotifier,
HttpBidderRequestEnricher requestEnricher) {
HttpBidderRequestEnricher requestEnricher,
JacksonMapper mapper) {

this.httpClient = Objects.requireNonNull(httpClient);
this.completionTrackerFactory = completionTrackerFactoryOrFallback(completionTrackerFactory);
this.bidderErrorNotifier = Objects.requireNonNull(bidderErrorNotifier);
this.requestEnricher = Objects.requireNonNull(requestEnricher);
this.mapper = Objects.requireNonNull(mapper);
}

/**
Expand Down Expand Up @@ -93,7 +97,8 @@ public <T> Future<BidderSeatBid> requestBids(Bidder<T> bidder,
: httpRequests.stream().map(httpRequest -> doRequest(httpRequest, timeout));

final BidderRequestCompletionTracker completionTracker = completionTrackerFactory.create(bidRequest);
final ResultBuilder<T> resultBuilder = new ResultBuilder<>(httpRequests, bidderErrors, completionTracker);
final ResultBuilder<T> resultBuilder =
new ResultBuilder<>(httpRequests, bidderErrors, completionTracker, mapper);

final List<Future<Void>> httpRequestFutures = httpCalls
.map(httpCallFuture -> httpCallFuture
Expand Down Expand Up @@ -266,13 +271,16 @@ private static class ResultBuilder<T> {
final Map<HttpRequest<T>, HttpCall<T>> httpCallsRecorded = new HashMap<>();
final List<BidderBid> bidsRecorded = new ArrayList<>();
final List<BidderError> errorsRecorded = new ArrayList<>();
private final JacksonMapper mapper;

ResultBuilder(List<HttpRequest<T>> httpRequests,
List<BidderError> previousErrors,
BidderRequestCompletionTracker completionTracker) {
BidderRequestCompletionTracker completionTracker,
JacksonMapper mapper) {
this.httpRequests = httpRequests;
this.previousErrors = previousErrors;
this.completionTracker = completionTracker;
this.mapper = mapper;
}

void addHttpCall(HttpCall<T> httpCall, Result<List<BidderBid>> bidsResult) {
Expand All @@ -299,7 +307,9 @@ BidderSeatBid toBidderSeatBid(boolean debugEnabled) {

// Capture debugging info from the requests
final List<ExtHttpCall> extHttpCalls = debugEnabled
? httpCalls.stream().map(ResultBuilder::toExt).collect(Collectors.toList())
? httpCalls.stream()
.map(this::toExt)
.collect(Collectors.toList())
: Collections.emptyList();

final List<BidderError> errors = errors(previousErrors, httpCalls, errorsRecorded);
Expand All @@ -310,11 +320,11 @@ BidderSeatBid toBidderSeatBid(boolean debugEnabled) {
/**
* Constructs {@link ExtHttpCall} filled with HTTP call information.
*/
private static <T> ExtHttpCall toExt(HttpCall<T> httpCall) {
private <T> ExtHttpCall toExt(HttpCall<T> httpCall) {
final HttpRequest<T> request = httpCall.getRequest();
final ExtHttpCall.ExtHttpCallBuilder builder = ExtHttpCall.builder()
.uri(request.getUri())
.requestbody(request.getBody())
.requestbody(mapper.encodeToString(request.getPayload()))
.requestheaders(HttpUtil.toDebugHeaders(request.getHeaders()));

final HttpResponse response = httpCall.getResponse();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/prebid/server/bidder/OpenrtbBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ private HttpRequest<BidRequest> makeRequest(BidRequest bidRequest, List<ImpWithE
modifyRequest(bidRequest, requestBuilder, impsWithExts);

final BidRequest outgoingRequest = requestBuilder.build();
final String body = mapper.encode(outgoingRequest);

return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.headers(headers())
.payload(outgoingRequest)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
.method(HttpMethod.POST)
.uri(resolveEndpoint(extImpAceex.getAccountId()))
.headers(constructHeaders(request))
.body(mapper.encode(request))
.body(mapper.encodeToBytes(request))
.payload(request)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
.uri(url)
.headers(resolveHeaders(request.getDevice()))
.payload(outgoingRequest)
.body(mapper.encode(outgoingRequest))
.body(mapper.encodeToBytes(outgoingRequest))
.build()),
Collections.emptyList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
.uri(endpointUrl)
.headers(resolveHeaders(request.getDevice()))
.payload(request)
.body(mapper.encode(request))
.body(mapper.encodeToBytes(request))
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ String getEids(ExtUser extUser, JacksonMapper mapper) {
}
}

final String encodedEids = mapper.encode(eidsMap);
final String encodedEids = mapper.encodeToString(eidsMap);

return ObjectUtils
.defaultIfNull(Base64.getUrlEncoder().withoutPadding().encodeToString(encodedEids.getBytes()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Result<List<HttpRequest<Void>>> makeHttpRequests(BidRequest request) {
HttpRequest.<Void>builder()
.method(HttpMethod.POST)
.uri(uri)
.body(mapper.encode(buildBody(request, extImpAdhese)))
.body(mapper.encodeToBytes(buildBody(request, extImpAdhese)))
.headers(replaceHeaders(request.getDevice()))
.build()),
Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,12 @@ private HttpRequest<BidRequest> createHttpRequest(Map.Entry<ExtImpAdkernel, List
.add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.5");

final BidRequest outgoingRequest = createBidRequest(extAndImp.getValue(), requestBuilder, site, app);
final String body = mapper.encode(outgoingRequest);

return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(uri)
.headers(headers)
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.payload(outgoingRequest)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,10 @@ private List<HttpRequest<BidRequest>> buildAdapterRequests(BidRequest preBidRequ

private HttpRequest<BidRequest> createRequest(ExtImpAdkernelAdn extImp, List<Imp> imps, BidRequest preBidRequest) {
final BidRequest outgoingRequest = createBidRequest(preBidRequest, imps);
final String body = mapper.encode(outgoingRequest);
return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(buildEndpoint(extImp))
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.headers(headers())
.payload(outgoingRequest)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
}
}
final BidRequest outgoingRequest = request.toBuilder().imp(validImps).build();
final String body = mapper.encode(outgoingRequest);

return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.payload(outgoingRequest)
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.build()),
errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ private String resolveUrl(ExtImpAdoppler extImp) {

private HttpRequest<BidRequest> createSingleRequest(Imp imp, BidRequest request, String url) {
final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(imp)).build();
final String body = mapper.encode(outgoingRequest);
final MultiMap headers = HttpUtil.headers().add(HttpUtil.X_OPENRTB_VERSION_HEADER, "2.5");
return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(url)
.headers(headers)
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.payload(outgoingRequest)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
.uri(endpointUrl)
.headers(HttpUtil.headers())
.payload(bidRequest)
.body(mapper.encode(bidRequest))
.body(mapper.encodeToBytes(bidRequest))
.build()),
errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
.uri(endpointUrl)
.headers(HttpUtil.headers()
.add(HttpUtil.X_OPENRTB_VERSION_HEADER, OPENRTB_VERSION))
.body(mapper.encode(bidRequest))
.body(mapper.encodeToBytes(bidRequest))
.payload(bidRequest)
.build()),
Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
httpRequests.add(HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(url)
.body(mapper.encode(bidRequest))
.body(mapper.encodeToBytes(bidRequest))
.headers(HttpUtil.headers())
.payload(bidRequest)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ private Result<List<HttpRequest<BidRequest>>> createHttpRequests(Map<Integer, Li
for (Map.Entry<Integer, List<Imp>> sourceIdToImps : sourceToImps.entrySet()) {
final String url = String.format("%s?aid=%d", endpointUrl, sourceIdToImps.getKey());
final BidRequest bidRequest = request.toBuilder().imp(sourceIdToImps.getValue()).build();
final String bidRequestBody;
final byte[] bidRequestBody;
try {
bidRequestBody = mapper.encode(bidRequest);
bidRequestBody = mapper.encodeToBytes(bidRequest);
} catch (EncodeException e) {
errors.add(BidderError.badInput(
String.format("error while encoding bidRequest, err: %s", e.getMessage())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private List<HttpRequest<BidRequest>> buildAdapterRequests(BidRequest bidRequest
final HttpRequest<BidRequest> createdBidRequest = HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(createdEndpoint)
.body(mapper.encode(updatedBidRequest))
.body(mapper.encodeToBytes(updatedBidRequest))
.headers(headers)
.payload(bidRequest)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
.method(HttpMethod.POST)
.uri(resolveEndpoint(extImpAdview.getAccountId()))
.headers(HttpUtil.headers())
.body(mapper.encode(modifiedRequest))
.body(mapper.encodeToBytes(modifiedRequest))
.payload(modifiedRequest)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.body(mapper.encode(bidRequest))
.body(mapper.encodeToBytes(bidRequest))
.payload(bidRequest)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
.uri(endpointUrl)
.headers(resolveHeaders())
.payload(outgoingRequest)
.body(mapper.encode(outgoingRequest))
.body(mapper.encodeToBytes(outgoingRequest))
.build());
}

Expand Down
12 changes: 1 addition & 11 deletions src/main/java/org/prebid/server/bidder/aja/AjaBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.prebid.server.bidder.model.Result;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.EncodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.response.BidType;
Expand Down Expand Up @@ -100,20 +99,11 @@ private HttpRequest<BidRequest> createSingleRequest(Imp imp, BidRequest request,
.imp(Collections.singletonList(imp))
.build();

final String body;
try {
body = mapper.encode(outgoingRequest);
} catch (EncodeException e) {
errors.add(BidderError.badInput(
String.format("Failed to unmarshal bidrequest ID: %s err: %s", request.getId(), e.getMessage())));
return null;
}

return HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(url)
.headers(HttpUtil.headers())
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.payload(outgoingRequest)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
}

final BidRequest outgoingRequest = request.toBuilder().imp(updatedImps).build();
final String body = mapper.encode(outgoingRequest);
return Result.of(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(resolveUrl(endpointUrl, extImpAlgorix))
.headers(resolveHeaders())
.payload(outgoingRequest)
.body(body)
.body(mapper.encodeToBytes(outgoingRequest))
.build()),
errors);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/prebid/server/bidder/amx/AmxBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
.uri(endpointUrl)
.headers(HttpUtil.headers())
.payload(outgoingRequest)
.body(mapper.encode(outgoingRequest))
.body(mapper.encodeToBytes(outgoingRequest))
.build()), errors);
}

Expand Down
Loading