-
Notifications
You must be signed in to change notification settings - Fork 188
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
Showing
35 changed files
with
1,633 additions
and
3 deletions.
There are no files selected for viewing
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
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,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.prebid.server.hooks.modules</groupId> | ||
<artifactId>all-modules</artifactId> | ||
<version>3.15.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>pb-request-correction</artifactId> | ||
|
||
<name>pb-request-correction</name> | ||
<description>Request correction module</description> | ||
</project> |
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 @@ | ||
lombok.anyConstructor.addConstructorProperties = true |
25 changes: 25 additions & 0 deletions
25
...org/prebid/server/hooks/modules/pb/request/correction/core/RequestCorrectionProvider.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,25 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core; | ||
|
||
import com.iab.openrtb.request.BidRequest; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.config.model.Config; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.Correction; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.CorrectionProducer; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class RequestCorrectionProvider { | ||
|
||
private final List<CorrectionProducer> correctionProducers; | ||
|
||
public RequestCorrectionProvider(List<CorrectionProducer> correctionProducers) { | ||
this.correctionProducers = Objects.requireNonNull(correctionProducers); | ||
} | ||
|
||
public List<Correction> corrections(Config config, BidRequest bidRequest) { | ||
return correctionProducers.stream() | ||
.filter(correctionProducer -> correctionProducer.shouldProduce(config, bidRequest)) | ||
.map(correctionProducer -> correctionProducer.produce(config)) | ||
.toList(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../java/org/prebid/server/hooks/modules/pb/request/correction/core/config/model/Config.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,21 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.config.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAlias; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
@Value | ||
@Builder | ||
public class Config { | ||
|
||
boolean enabled; | ||
|
||
@JsonAlias("pbsdkAndroidInstlRemove") | ||
@JsonProperty("pbsdk-android-instl-remove") | ||
boolean interstitialCorrectionEnabled; | ||
|
||
@JsonAlias("pbsdkUaCleanup") | ||
@JsonProperty("pbsdk-ua-cleanup") | ||
boolean userAgentCorrectionEnabled; | ||
} |
9 changes: 9 additions & 0 deletions
9
...ava/org/prebid/server/hooks/modules/pb/request/correction/core/correction/Correction.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,9 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.correction; | ||
|
||
import com.iab.openrtb.request.BidRequest; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.config.model.Config; | ||
|
||
public interface Correction { | ||
|
||
BidRequest apply(BidRequest bidRequest); | ||
} |
11 changes: 11 additions & 0 deletions
11
...prebid/server/hooks/modules/pb/request/correction/core/correction/CorrectionProducer.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,11 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.correction; | ||
|
||
import com.iab.openrtb.request.BidRequest; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.config.model.Config; | ||
|
||
public interface CorrectionProducer { | ||
|
||
boolean shouldProduce(Config config, BidRequest bidRequest); | ||
|
||
Correction produce(Config config); | ||
} |
24 changes: 24 additions & 0 deletions
24
...ks/modules/pb/request/correction/core/correction/interstitial/InterstitialCorrection.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,24 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.correction.interstitial; | ||
|
||
import com.iab.openrtb.request.BidRequest; | ||
import com.iab.openrtb.request.Imp; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.Correction; | ||
|
||
public class InterstitialCorrection implements Correction { | ||
|
||
@Override | ||
public BidRequest apply(BidRequest bidRequest) { | ||
return bidRequest.toBuilder() | ||
.imp(bidRequest.getImp().stream() | ||
.map(InterstitialCorrection::removeInterstitial) | ||
.toList()) | ||
.build(); | ||
} | ||
|
||
private static Imp removeInterstitial(Imp imp) { | ||
final Integer interstitial = imp.getInstl(); | ||
return interstitial != null && interstitial == 1 | ||
? imp.toBuilder().instl(null).build() | ||
: imp; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...es/pb/request/correction/core/correction/interstitial/InterstitialCorrectionProducer.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,80 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.correction.interstitial; | ||
|
||
import com.iab.openrtb.request.App; | ||
import com.iab.openrtb.request.BidRequest; | ||
import com.iab.openrtb.request.Imp; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.config.model.Config; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.Correction; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.CorrectionProducer; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.util.VersionUtil; | ||
import org.prebid.server.proto.openrtb.ext.request.ExtApp; | ||
import org.prebid.server.proto.openrtb.ext.request.ExtAppPrebid; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class InterstitialCorrectionProducer implements CorrectionProducer { | ||
|
||
private static final InterstitialCorrection CORRECTION_INSTANCE = new InterstitialCorrection(); | ||
|
||
private static final String PREBID_MOBILE = "prebid-mobile"; | ||
private static final String ANDROID = "android"; | ||
|
||
private static final int MAX_VERSION_MAJOR = 2; | ||
private static final int MAX_VERSION_MINOR = 2; | ||
private static final int MAX_VERSION_PATCH = 3; | ||
|
||
@Override | ||
public boolean shouldProduce(Config config, BidRequest bidRequest) { | ||
final App app = bidRequest.getApp(); | ||
return config.isInterstitialCorrectionEnabled() | ||
&& hasInterstitialToRemove(bidRequest.getImp()) | ||
&& isPrebidMobile(app) | ||
&& isAndroid(app) | ||
&& isApplicableVersion(app); | ||
} | ||
|
||
private static boolean hasInterstitialToRemove(List<Imp> imps) { | ||
for (Imp imp : imps) { | ||
final Integer interstitial = imp.getInstl(); | ||
if (interstitial != null && interstitial == 1) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static boolean isPrebidMobile(App app) { | ||
final String source = Optional.ofNullable(app) | ||
.map(App::getExt) | ||
.map(ExtApp::getPrebid) | ||
.map(ExtAppPrebid::getSource) | ||
.orElse(null); | ||
|
||
return StringUtils.equalsIgnoreCase(source, PREBID_MOBILE); | ||
} | ||
|
||
private static boolean isAndroid(App app) { | ||
return StringUtils.containsIgnoreCase(app.getBundle(), ANDROID); | ||
} | ||
|
||
private static boolean isApplicableVersion(App app) { | ||
return Optional.ofNullable(app) | ||
.map(App::getExt) | ||
.map(ExtApp::getPrebid) | ||
.map(ExtAppPrebid::getVersion) | ||
.map(InterstitialCorrectionProducer::checkVersion) | ||
.orElse(false); | ||
} | ||
|
||
private static boolean checkVersion(String version) { | ||
return VersionUtil.isVersionLessThan(version, MAX_VERSION_MAJOR, MAX_VERSION_MINOR, MAX_VERSION_PATCH); | ||
} | ||
|
||
@Override | ||
public Correction produce(Config config) { | ||
return CORRECTION_INSTANCE; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...er/hooks/modules/pb/request/correction/core/correction/useragent/UserAgentCorrection.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,25 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.correction.useragent; | ||
|
||
import com.iab.openrtb.request.BidRequest; | ||
import com.iab.openrtb.request.Device; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.Correction; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class UserAgentCorrection implements Correction { | ||
|
||
private static final Pattern USER_AGENT_PATTERN = Pattern.compile("PrebidMobile/[0-9][^ ]*"); | ||
|
||
@Override | ||
public BidRequest apply(BidRequest bidRequest) { | ||
return bidRequest.toBuilder() | ||
.device(correctDevice(bidRequest.getDevice())) | ||
.build(); | ||
} | ||
|
||
private static Device correctDevice(Device device) { | ||
return device.toBuilder() | ||
.ua(USER_AGENT_PATTERN.matcher(device.getUa()).replaceAll("")) | ||
.build(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
.../modules/pb/request/correction/core/correction/useragent/UserAgentCorrectionProducer.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,76 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.correction.useragent; | ||
|
||
import com.iab.openrtb.request.App; | ||
import com.iab.openrtb.request.BidRequest; | ||
import com.iab.openrtb.request.Device; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.config.model.Config; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.Correction; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.CorrectionProducer; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.util.VersionUtil; | ||
import org.prebid.server.proto.openrtb.ext.request.ExtApp; | ||
import org.prebid.server.proto.openrtb.ext.request.ExtAppPrebid; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class UserAgentCorrectionProducer implements CorrectionProducer { | ||
|
||
private static final UserAgentCorrection CORRECTION_INSTANCE = new UserAgentCorrection(); | ||
|
||
private static final String PREBID_MOBILE = "prebid-mobile"; | ||
private static final Pattern USER_AGENT_PATTERN = Pattern.compile(".*PrebidMobile/[0-9]+[^ ]*.*"); | ||
|
||
|
||
private static final int MAX_VERSION_MAJOR = 2; | ||
private static final int MAX_VERSION_MINOR = 1; | ||
private static final int MAX_VERSION_PATCH = 6; | ||
|
||
@Override | ||
public boolean shouldProduce(Config config, BidRequest bidRequest) { | ||
final App app = bidRequest.getApp(); | ||
return config.isUserAgentCorrectionEnabled() | ||
&& isPrebidMobile(app) | ||
&& isApplicableVersion(app) | ||
&& isApplicableDevice(bidRequest.getDevice()); | ||
} | ||
|
||
private static boolean isPrebidMobile(App app) { | ||
final String source = Optional.ofNullable(app) | ||
.map(App::getExt) | ||
.map(ExtApp::getPrebid) | ||
.map(ExtAppPrebid::getSource) | ||
.orElse(null); | ||
|
||
return StringUtils.equalsIgnoreCase(source, PREBID_MOBILE); | ||
} | ||
|
||
private static boolean isApplicableVersion(App app) { | ||
return Optional.ofNullable(app) | ||
.map(App::getExt) | ||
.map(ExtApp::getPrebid) | ||
.map(ExtAppPrebid::getVersion) | ||
.map(UserAgentCorrectionProducer::checkVersion) | ||
.orElse(false); | ||
} | ||
|
||
private static boolean checkVersion(String version) { | ||
return VersionUtil.isVersionLessThan(version, MAX_VERSION_MAJOR, MAX_VERSION_MINOR, MAX_VERSION_PATCH); | ||
} | ||
|
||
private static boolean isApplicableDevice(Device device) { | ||
return Optional.ofNullable(device) | ||
.map(Device::getUa) | ||
.filter(StringUtils::isNotEmpty) | ||
.map(USER_AGENT_PATTERN::matcher) | ||
.map(Matcher::matches) | ||
.orElse(false); | ||
} | ||
|
||
|
||
@Override | ||
public Correction produce(Config config) { | ||
return CORRECTION_INSTANCE; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ain/java/org/prebid/server/hooks/modules/pb/request/correction/core/util/VersionUtil.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,35 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.core.util; | ||
|
||
public class VersionUtil { | ||
|
||
public static boolean isVersionLessThan(String versionAsString, int major, int minor, int patch) { | ||
return compareVersion(versionAsString, major, minor, patch) < 0; | ||
} | ||
|
||
private static int compareVersion(String versionAsString, int major, int minor, int patch) { | ||
final String[] version = versionAsString.split("\\."); | ||
|
||
final int parsedMajor = getAtAsIntOrDefault(version, 0, -1); | ||
final int parsedMinor = getAtAsIntOrDefault(version, 1, 0); | ||
final int parsedPatch = getAtAsIntOrDefault(version, 2, 0); | ||
|
||
int diff = parsedMajor >= 0 ? parsedMajor - major : 1; | ||
diff = diff == 0 ? parsedMinor - minor : diff; | ||
diff = diff == 0 ? parsedPatch - patch : diff; | ||
|
||
return diff; | ||
} | ||
|
||
private static int getAtAsIntOrDefault(String[] array, int index, int defaultValue) { | ||
return array.length > index ? intOrDefault(array[index], defaultValue) : defaultValue; | ||
} | ||
|
||
private static int intOrDefault(String intAsString, int defaultValue) { | ||
try { | ||
final int parsed = Integer.parseInt(intAsString); | ||
return parsed >= 0 ? parsed : defaultValue; | ||
} catch (NumberFormatException e) { | ||
return defaultValue; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...oks/modules/pb/request/correction/spring/config/RequestCorrectionModuleConfiguration.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,38 @@ | ||
package org.prebid.server.hooks.modules.pb.request.correction.spring.config; | ||
|
||
import org.prebid.server.hooks.modules.pb.request.correction.core.RequestCorrectionProvider; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.CorrectionProducer; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.interstitial.InterstitialCorrectionProducer; | ||
import org.prebid.server.hooks.modules.pb.request.correction.core.correction.useragent.UserAgentCorrectionProducer; | ||
import org.prebid.server.hooks.modules.pb.request.correction.v1.RequestCorrectionModule; | ||
import org.prebid.server.json.ObjectMapperProvider; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@ConditionalOnProperty(prefix = "hooks." + RequestCorrectionModule.CODE, name = "enabled", havingValue = "true") | ||
public class RequestCorrectionModuleConfiguration { | ||
|
||
@Bean | ||
InterstitialCorrectionProducer interstitialCorrectionProducer() { | ||
return new InterstitialCorrectionProducer(); | ||
} | ||
|
||
@Bean | ||
UserAgentCorrectionProducer userAgentCorrectionProducer() { | ||
return new UserAgentCorrectionProducer(); | ||
} | ||
|
||
@Bean | ||
RequestCorrectionProvider requestCorrectionProvider(List<CorrectionProducer> correctionProducers) { | ||
return new RequestCorrectionProvider(correctionProducers); | ||
} | ||
|
||
@Bean | ||
RequestCorrectionModule requestCorrectionModule(RequestCorrectionProvider requestCorrectionProvider) { | ||
return new RequestCorrectionModule(requestCorrectionProvider, ObjectMapperProvider.mapper()); | ||
} | ||
} |
Oops, something went wrong.