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

Fix OpenRTB field #805

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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 @@ -432,12 +432,12 @@ public void setGpid(@Nullable String gpid) {
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
public String getImpOrtbConfig() {
return configuration.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
configuration.setImpOrtbConfig(ortbConfig);
}

protected BidRequesterListener createBidListener(OnCompleteListener originalListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ HashSet<AdSize> getSizes() {
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
public String getImpOrtbConfig() {
return configuration.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
configuration.setImpOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public Parameters getParameters() {
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
public String getImpOrtbConfig() {
return configuration.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
configuration.setImpOrtbConfig(ortbConfig);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ public void setMinSizePercentage(
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
public String getImpOrtbConfig() {
return configuration.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
configuration.setImpOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ public NativeAdUnitConfiguration getNativeConfiguration() {
}

@Nullable
public String getOrtbConfig() {return configuration.getOrtbConfig();}
public String getImpOrtbConfig() {return configuration.getImpOrtbConfig();}

public void setOrtbConfig(@Nullable String ortbConfig) {configuration.setOrtbConfig(ortbConfig);}
public void setImpOrtbConfig(@Nullable String ortbConfig) {configuration.setImpOrtbConfig(ortbConfig);}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package org.prebid.mobile;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

/**
* The OpenRtbMerger class provides functionality to merge a given JSON request object
* with an OpenRTB string representation.
*/
public class OpenRtbMerger {

private static final String TAG = "OpenRtbMerger";

private OpenRtbMerger() {
}

/**
* It merges the SDK originalRequest and OpenRTB string.
*
* @param originalRequest original request
* @param openRtb OpenRTB string
* @return merged JSON object
*/
@NonNull
public static JSONObject globalMerge(@NonNull JSONObject originalRequest, String openRtb) {
if (openRtb == null || openRtb.isEmpty()) {
return originalRequest;
}

try {
JSONObject openRtbJson = new JSONObject(openRtb);
removeSensitiveData(openRtbJson);
return merge(originalRequest, openRtbJson);
} catch (Exception e) {
LogUtil.error(TAG, "Can't merge OpenRTB config: " + e.getMessage());
}

return originalRequest;
}

@NonNull
private static JSONObject merge(JSONObject requestJsonRoot, JSONObject openRtbJsonRoot) throws JSONException {
for (Iterator<String> it = openRtbJsonRoot.keys(); it.hasNext(); ) {
String openRtbKey = it.next();
Object openRtbValue = openRtbJsonRoot.opt(openRtbKey);

boolean newFieldForRequest = !requestJsonRoot.has(openRtbKey);
if (newFieldForRequest) {
requestJsonRoot.put(openRtbKey, openRtbValue);
continue;
}

Object requestValue = requestJsonRoot.opt(openRtbKey);
if (openRtbValue instanceof JSONObject openRtbJson && requestValue instanceof JSONObject requestJson) {
merge(requestJson, openRtbJson);
continue;
}

if (openRtbValue instanceof JSONArray openRtbJsonArray && requestValue instanceof JSONArray requestJsonArray) {
merge(requestJsonArray, openRtbJsonArray, openRtbKey, requestJsonRoot);
continue;
}

requestJsonRoot.put(openRtbKey, openRtbValue);
}
return requestJsonRoot;
}

private static void merge(JSONArray requestJsonArray, JSONArray openRtbJsonArray, String key, JSONObject parentJson) throws JSONException {
if (openRtbJsonArray.length() == 0) {
return;
}

if (requestJsonArray.length() == 0) {
for (int i = 0; i < openRtbJsonArray.length(); i++) {
requestJsonArray.put(i, openRtbJsonArray.get(i));
}
return;
}

boolean differentItemTypes = requestJsonArray.opt(0).getClass() != openRtbJsonArray.opt(0).getClass();
if (differentItemTypes) {
LogUtil.verbose(TAG, "JSON arrays of different types. Rewriting with OpenRTB values...");
parentJson.put(key, openRtbJsonArray);
return;
}

for (int i = 0; i < openRtbJsonArray.length(); i++) {
Object openRtbValue = openRtbJsonArray.get(i);
requestJsonArray.put(openRtbValue);
}
}

private static void removeSensitiveData(@NonNull JSONObject openRtbJson) {
JSONObject userJson = openRtbJson.optJSONObject("user");
JSONObject extJson = userJson != null ? userJson.optJSONObject("ext") : null;
removeFields(extJson, FIELDS_USER_EXT);

removeFields(openRtbJson.optJSONObject("regs"), FIELDS_REGS);
removeFields(openRtbJson.optJSONObject("geo"), FIELDS_GEO);
removeFields(openRtbJson.optJSONObject("device"), FIELDS_DEVICE);
}

private static void removeFields(@Nullable JSONObject json, String... fields) {
if (json == null) return;

for (String field : fields) {
json.remove(field);
}
}


private static final String[] FIELDS_USER_EXT = {
"consent"
};

private static final String[] FIELDS_REGS = {
"gdpr",
"us_privacy",
"coppa"
};

private static final String[] FIELDS_GEO = {
"lat",
"lon",
"type",
"accuracy",
"lastfix",
"country",
"region",
"regionfips104",
"metro",
"city",
"zip",
"utcoffset"
};

private static final String[] FIELDS_DEVICE = {
"ua",
"dnt",
"lmt",
"ip",
"ipv6",
"devicetype",
"make",
"model",
"os",
"osv",
"hwv",
"flashver",
"language",
"carrier",
"mccmnc",
"ifa",
"didsha1",
"didmd5",
"dpidsha1",
"dpidmd5",
"h",
"w",
"ppi",
"js",
"connectiontype",
"pxratio",
"geo",
"ext"
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class TargetingParams {
private static String omidPartnerName;
private static String omidPartnerVersion;
private static String userCustomData;
@Nullable
private static String openRtbConfig;
private static Pair<Float, Float> userLatLon;
private static Ext userExt;
private static JSONArray extendedUserIds;
Expand Down Expand Up @@ -795,6 +797,18 @@ public static Boolean getDeviceAccessConsent() {
return UserConsentUtils.tryToGetDeviceAccessConsent();
}

@Nullable
public static String getGlobalOrtbConfig() {
return openRtbConfig;
}

/**
* Sets global OpenRTB JSON string for merging with the original request.
*/
public static void setGlobalOrtbConfig(String config) {
openRtbConfig = config;
}


/* -------------------- Ext -------------------- */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void setVideoParameters(@Nullable VideoParameters parameters) {
}

@Nullable
public String getOrtbConfig() { return configuration.getOrtbConfig(); }
public String getImpOrtbConfig() { return configuration.getImpOrtbConfig(); }

public void setOrtbConfig(@Nullable String ortbConfig) { configuration.setOrtbConfig(ortbConfig);}
public void setImpOrtbConfig(@Nullable String ortbConfig) { configuration.setImpOrtbConfig(ortbConfig);}

/**
* @deprecated use `setVideoParameters()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ private void cancelRefresh() {
}

@Nullable
public String getOrtbConfig() {
return adUnitConfig.getOrtbConfig();
public String getImpOrtbConfig() {
return adUnitConfig.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setImpOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,12 @@ public BidResponse getBidResponse() {
}

@Nullable
public String getOrtbConfig() {
return adUnitConfig.getOrtbConfig();
public String getImpOrtbConfig() {
return adUnitConfig.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setImpOrtbConfig(ortbConfig);
}

//region ==================== HelperMethods for Unit Tests. Should be used only in tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ public void clearUserData() {
}

@Nullable
public String getOrtbConfig() {
return adUnitConfig.getOrtbConfig();
public String getImpOrtbConfig() {
return adUnitConfig.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setImpOrtbConfig(ortbConfig);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public void destroy() {
}

@Nullable
public String getOrtbConfig() {
return adUnitConfiguration.getOrtbConfig();
public String getImpOrtbConfig() {
return adUnitConfiguration.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfiguration.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
adUnitConfiguration.setImpOrtbConfig(ortbConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ private static BidResponse getBidResponseFromCache(String id) throws AdException
}

@Nullable
public String getOrtbConfig() {
return adUnitConfiguration.getOrtbConfig();
public String getImpOrtbConfig() {
return adUnitConfiguration.getImpOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfiguration.setOrtbConfig(ortbConfig);
public void setImpOrtbConfig(@Nullable String ortbConfig) {
adUnitConfiguration.setImpOrtbConfig(ortbConfig);
}

}
Loading