Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
166 changes: 166 additions & 0 deletions src/main/java/org/prebid/server/bidder/mobkoi/MobkoiBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package org.prebid.server.bidder.mobkoi;

import com.fasterxml.jackson.core.type.TypeReference;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.User;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import org.apache.commons.collections4.CollectionUtils;
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.ExtUser;
import org.prebid.server.proto.openrtb.ext.request.mobkoi.ExtImpMobkoi;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Vector;
import java.util.stream.Collectors;

public class MobkoiBidder implements Bidder<BidRequest> {

private static final TypeReference<ExtPrebid<?, ExtImpMobkoi>> MOBKOI_EXT_TYPE_REFERENCE =
new TypeReference<>() {
};

private final String endpointUrl;
private final JacksonMapper mapper;

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

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {

Comment thread
CTMBNara marked this conversation as resolved.
Outdated
final Imp firstImp = bidRequest.getImp().stream().findFirst()
.orElseThrow(() -> new PreBidException("No impression found"));
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated

final ExtImpMobkoi extImpMobkoi;
try {
extImpMobkoi = parseExtImp(firstImp);
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}
Comment thread
AntoxaAntoxic marked this conversation as resolved.

final Imp validImp;
if (firstImp.getTagid() == null) {
if (extImpMobkoi.getPlacementId() != null) {
validImp = modifyImp(firstImp, extImpMobkoi.getPlacementId());
} else {
return Result.withError(
BidderError.badInput(
"invalid because it comes with neither request.imp[0].tagId nor "
+ "req.imp[0].ext.Bidder.placementId"));
}
} else {
validImp = firstImp;
}

List<Imp> modifiedImps = bidRequest.getImp();
if (validImp != firstImp) {
modifiedImps = updateFirstImpWith(bidRequest.getImp(), validImp);
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated
}

final String selectedEndpointUrl = Optional.ofNullable(extImpMobkoi.getAdServerBaseUrl())
.flatMap(this::validateAndReplaceUri)
.orElse(endpointUrl);

final User user = modifyUser(bidRequest.getUser());
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated

return Result.withValue(BidderUtil.defaultRequest(
modifyBidRequest(bidRequest, user, modifiedImps),
selectedEndpointUrl,
mapper));
}

private ExtImpMobkoi parseExtImp(Imp imp) {
try {
return mapper.mapper().convertValue(imp.getExt(), MOBKOI_EXT_TYPE_REFERENCE).getBidder();
} catch (IllegalArgumentException e) {
throw new PreBidException(
"Invalid imp.ext for impression id %s. Error Information: %s"
.formatted(imp.getId(), e.getMessage()));
}
}

private Optional<String> validateAndReplaceUri(String customUri) {
try {
HttpUtil.validateUrl(customUri);
final URI uri = new URI(customUri);
return Optional.of(uri.resolve("/bid").toString());
} catch (IllegalArgumentException | URISyntaxException e) {
return Optional.empty();
}
}
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated
Comment on lines +96 to +105

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/prebid/prebid-server-java/blob/master/docs/developers/bid-adapter-porting-guide.md#specific-rules-and-tips-for-porting

Point 5.2: Fully dynamic hostnames in URLs aren't allowed in bid adapters.

Please, add a comment for that method:

  // url is already validated with `bidder-params` json schema

Also, you can remove HttpUtil.validateUrl(customUri); step

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CTMBNara, so we take the assumption that all custom URIs are valid?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbonnafon this check will be done by new Uri().toString()


private Imp modifyImp(Imp imp, String tagId) {
return imp.toBuilder().tagid(tagId).build();
}
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated

private List<Imp> updateFirstImpWith(List<Imp> imp, Imp validImp) {
final List<Imp> imps = new Vector<>(imp);
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated
imps.set(0, validImp);
return Collections.unmodifiableList(imps);
}

private User modifyUser(User user) {
if (user == null || user.getConsent() == null) {
return user;
}

final String consent = user.getConsent();
final ExtUser userExt = ExtUser.builder().consent(consent).build();

return user.toBuilder().ext(userExt).build();
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated
}

private static BidRequest modifyBidRequest(BidRequest bidRequest, User user, List<Imp> imps) {
return bidRequest.toBuilder().user(user).imp(imps).build();
}

@Override
public final 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 bidsFromResponse(bidResponse);
}

private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
return bidResponse.getSeatbid()
.stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, BidType.banner, "mobkoi", bidResponse.getCur()))
.collect(Collectors.toList());
Comment thread
CTMBNara marked this conversation as resolved.
Outdated
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.mobkoi;

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

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

@JsonProperty("placementId")
String placementId;

@JsonProperty("adServerBaseUrl")
String adServerBaseUrl;
}
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.mobkoi.MobkoiBidder;
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 javax.validation.constraints.NotBlank;

@Configuration
@PropertySource(value = "classpath:/bidder-config/mobkoi.yaml", factory = YamlPropertySourceFactory.class)
public class MobkoiConfiguration {

private static final String BIDDER_NAME = "mobkoi";

@Bean("mobkoiConfigurationProperties")
@ConfigurationProperties("adapters.mobkoi")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps mobkoiBidderDeps(BidderConfigurationProperties mobkoiConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(mobkoiConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new MobkoiBidder(config.getEndpoint(), mapper))
.assemble();
}
}
11 changes: 11 additions & 0 deletions src/main/resources/bidder-config/mobkoi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
adapters:
mobkoi:
endpoint: "https://pbs.maximus.mobkoi.com/bid"
meta-info:
maintainer-email: platformteam@mobkoi.com
app-media-types:
site-media-types:
- banner
supported-vendors:
vendor-id: 898
Comment thread
mbonnafon marked this conversation as resolved.
Comment thread
mbonnafon marked this conversation as resolved.
ortb-version: "2.6"
Comment thread
AntoxaAntoxic marked this conversation as resolved.
Outdated
17 changes: 17 additions & 0 deletions src/main/resources/static/bidder-params/mobkoi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Mobkoi Adapter Params",
"description": "A schema which validates params accepted by the Mobkoi adapter",
"type": "object",
"properties": {
"placementId": {
"type": "string",
"description": "Placement ID"
},
"adServerBaseUrl": {
"type": "string",
"description": "Mobkoi's ad server url",
"pattern": "^https?://[^.]+\\.mobkoi\\.com$"
}
}
}
Loading