From a82802d5cc9b9fc1ba967216b7db576457405b40 Mon Sep 17 00:00:00 2001 From: markvdouw Date: Mon, 6 Nov 2023 13:31:18 -0300 Subject: [PATCH 01/19] Change on access utils function --- .../androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt index dae8615ca..fe50ec848 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt @@ -532,7 +532,7 @@ class MParticleOptionsTest : BaseAbstractTest() { Assert.assertTrue( com.mparticle.networking.AccessUtils.equals( options.networkOptions, - com.mparticle.networking.AccessUtils.defaultNetworkOptions + com.mparticle.networking.AccessUtils.getDefaultNetworkOptions() ) ) } From 84fc41c3c74ac3c9327cd79be321c6764d8ba6a2 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Fri, 12 Jul 2024 14:23:19 -0400 Subject: [PATCH 02/19] feat: SQDSDKS-5914 - Pod redirection -2 --- .../com.mparticle/networking/AccessUtils.kt | 9 ++- .../networking/MParticleBaseClientImplTest.kt | 69 ++++++++++++++++++- .../networking/NetworkOptionsTest.kt | 41 ++++++----- .../com/mparticle/internal/ConfigManager.java | 24 +++++++ .../networking/MParticleBaseClientImpl.java | 7 +- .../mparticle/networking/NetworkOptions.java | 11 +++ .../networking/NetworkOptionsManager.java | 7 +- .../kotlin/com.mparticle/NetworkUtilities.kt | 26 +++++++ .../com/mparticle/networking/MockServer.java | 2 +- 9 files changed, 166 insertions(+), 30 deletions(-) create mode 100644 android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt index 17191bddf..4a3bd50a2 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt @@ -1,8 +1,13 @@ package com.mparticle.networking object AccessUtils { - val defaultNetworkOptions: NetworkOptions - get() = NetworkOptionsManager.defaultNetworkOptions() + private var defaultNetworkOptions: NetworkOptions? = null + + fun getDefaultNetworkOptions(): NetworkOptions { + defaultNetworkOptions = + NetworkOptionsManager.defaultNetworkOptions() + return defaultNetworkOptions!! + } fun equals(networkOptions1: NetworkOptions, networkOptions2: NetworkOptions): Boolean { if (networkOptions1 === networkOptions2) { diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 668f8d722..312e7413c 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -2,10 +2,12 @@ package com.mparticle.networking import com.mparticle.MParticle import com.mparticle.MParticleOptions +import com.mparticle.NetworkUtilities import com.mparticle.internal.AccessUtils import com.mparticle.testutils.BaseCleanInstallEachTest import junit.framework.TestCase import org.junit.Assert +import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test import java.net.MalformedURLException @@ -20,11 +22,72 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { startMParticle(MParticleOptions.builder(mContext).credentials(apiKey, "secret")) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl for (endpoint in MParticleBaseClientImpl.Endpoint.values()) { - defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name) + defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name, false) } MParticle.setInstance(null) } + @Test + fun testUrlPrefixWithPodRedirection() { + val prefix = "eu1" + val url = + NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) + assertEquals("${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", url) + } + + @Test + fun testUrlPrefixWithoutPodRedirection() { + val prefix = "eu1" + val url = + NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, false) + assertEquals("${NetworkOptionsManager.MP_URL_PREFIX}.mparticle.com", url) + } + + @Test + fun testAllPrefixes() { + val map = mapOf( + Pair("us1-1vc4gbp24cdtx6e31s58icnymzy83f1uf", "us1"), + Pair("us2-v2p8lr3w2g90vtpaumbq21zy05cl50qm3", "us2"), + Pair("eu1-bkabfno0b8zpv5bwi2zm2mfa1kfml19al", "eu1"), + Pair("au1-iermuj83dbeoshm0n32f10feotclq6i4a", "au1"), + Pair("st1-k77ivhkbbqf4ce0s3y12zpcthyn1ixfyu", "st1"), + Pair("us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", "us3") + ) + map.forEach { key, value -> + val prefix = NetworkUtilities.getPodPrefix(key) ?: "" + assertEquals(value, prefix) + assertEquals( + "${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", + NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) + ) + assertEquals( + "${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.$prefix.mparticle.com", + NetworkUtilities.getUrlWithPrefix( + NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, + prefix, + true + ) + ) + + assertEquals( + "${NetworkOptionsManager.MP_URL_PREFIX}.mparticle.com", + NetworkUtilities.getUrlWithPrefix( + NetworkOptionsManager.MP_URL_PREFIX, + prefix, + false + ) + ) + assertEquals( + "${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.mparticle.com", + NetworkUtilities.getUrlWithPrefix( + NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, + prefix, + false + ) + ) + } + } + @Test @Throws(MalformedURLException::class) fun testGetUrlForceDefaultOption() { @@ -56,14 +119,14 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl for (endpoint in MParticleBaseClientImpl.Endpoint.values()) { val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, true) - Assert.assertEquals(defaultUrls[endpoint].toString(), generatedUrl.toString()) + assertEquals(defaultUrls[endpoint].toString(), generatedUrl.toString()) TestCase.assertTrue(generatedUrl === generatedUrl.defaultUrl) } for (endpoint in MParticleBaseClientImpl.Endpoint.values()) { val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, false) Assert.assertNotEquals(defaultUrls[endpoint].toString(), generatedUrl.toString()) Assert.assertFalse(generatedUrl === generatedUrl.defaultUrl) - Assert.assertEquals( + assertEquals( defaultUrls[endpoint].toString(), generatedUrl.defaultUrl.toString() ) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index 70beceb25..cb4317722 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -2,6 +2,7 @@ package com.mparticle.networking import com.mparticle.MParticle import com.mparticle.MParticleOptions +import com.mparticle.NetworkUtilities import com.mparticle.internal.AccessUtils import com.mparticle.testutils.BaseCleanInstallEachTest import org.junit.After @@ -38,7 +39,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(MParticleOptions.builder(mContext).credentials(apiKey, "s").build()) setClients() Assert.assertEquals( - NetworkOptionsManager.MP_URL, + NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_URL_PREFIX, "", false), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -46,11 +47,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - NetworkOptionsManager.MP_URL, + NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_URL_PREFIX, "", false), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - NetworkOptionsManager.MP_IDENTITY_URL, + NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, "", false), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) var randIdentityPath = mRandomUtils.getAlphaString(10) @@ -71,7 +72,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { identityClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - NetworkOptionsManager.MP_IDENTITY_URL, + NetworkOptionsManager.MP_IDENTITY_URL_PREFIX.addSuffix(), identityClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) randIdentityPath = mRandomUtils.getAlphaString(10) @@ -84,10 +85,12 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } + private fun String.addSuffix(suffix: String = ".mparticle.com") = "$this$suffix" + @Test @Throws(MalformedURLException::class) fun testRandomEndpoint() { - val identityUrl = mRandomUtils.getAlphaString(20) + val identityUrl = "${mRandomUtils.getAlphaString(20)}" val configUrl = mRandomUtils.getAlphaString(20) val audienceUrl = mRandomUtils.getAlphaString(20) val eventsUrl = mRandomUtils.getAlphaString(20) @@ -114,7 +117,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - audienceUrl, + audienceUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -122,11 +125,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - identityUrl, + identityUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) var randIdentityPath = mRandomUtils.getAlphaString(10) @@ -135,7 +138,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path ) Assert.assertEquals( - audienceUrl, + audienceUrl.addSuffix(), identityClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -143,11 +146,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { identityClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), identityClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - identityUrl, + identityUrl.addSuffix(), identityClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) randIdentityPath = mRandomUtils.getAlphaString(10) @@ -211,7 +214,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - audienceUrl, + audienceUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -219,11 +222,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - identityUrl, + identityUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) val randIdentityPath = mRandomUtils.getAlphaString(10) @@ -285,11 +288,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.ALIAS).authority ) } @@ -311,11 +314,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - aliasUrl, + aliasUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.ALIAS).authority ) } @@ -335,7 +338,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - eventsUrl, + eventsUrl.addSuffix(), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index cf225f843..dc82e20e6 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -51,6 +51,7 @@ public class ConfigManager { static final String KEY_OPT_OUT = "oo"; public static final String KEY_UNHANDLED_EXCEPTIONS = "cue"; public static final String KEY_PUSH_MESSAGES = "pmk"; + public static final String KEY_DIRECT_URL_ROUTING = "dur"; public static final String KEY_EMBEDDED_KITS = "eks"; static final String KEY_UPLOAD_INTERVAL = "uitl"; static final String KEY_SESSION_TIMEOUT = "stl"; @@ -83,6 +84,7 @@ public class ConfigManager { static SharedPreferences sPreferences; private static JSONArray sPushKeys; + private boolean directUrlRouting = true; private UserStorage mUserStorage; private String mLogUnhandledExceptions = VALUE_APP_DEFINED; @@ -406,6 +408,11 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC editor.putString(KEY_PUSH_MESSAGES, sPushKeys.toString()); } + if (responseJSON.has(KEY_DIRECT_URL_ROUTING)) { + // directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING, false); + editor.putBoolean(KEY_DIRECT_URL_ROUTING, directUrlRouting); + } + mRampValue = responseJSON.optInt(KEY_RAMP, -1); if (responseJSON.has(KEY_OPT_OUT)) { @@ -1276,6 +1283,23 @@ public ConsentState getConsentState(long mpid) { return ConsentState.withConsentState(serializedConsent).build(); } + public boolean isDirectUrlRoutingEnabled() { + return directUrlRouting; + } + + public String getPodPrefix() { + String prefix = "us1"; + String[] prefixFromApi = getApiKey().split("-"); + try { + if (prefixFromApi.length > 1) { + prefix = getApiKey().split("-")[0]; + } + } catch (Exception e) { + prefix = "us1"; + } + return prefix; + } + public void setConsentState(ConsentState state, long mpid) { String serializedConsent = null; if (state != null) { diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index 5615fe927..a59606551 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -7,6 +7,7 @@ import androidx.annotation.Nullable; import com.mparticle.BuildConfig; +import com.mparticle.NetworkUtilities; import com.mparticle.internal.ConfigManager; import com.mparticle.internal.Constants; import com.mparticle.internal.Logger; @@ -110,6 +111,9 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean url = domainMappingUrl; } Uri uri; + if (endpoint != Endpoint.CONFIG) { + url = NetworkUtilities.INSTANCE.getUrlWithPrefix(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); + } MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null; String subdirectory; boolean overridesSubdirectory = domainMapping.isOverridesSubdirectory() && !forceDefaultUrl; @@ -133,8 +137,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean } } } - uri = builder.build(); - return MPUrl.getUrl(uri.toString(), defaultUrl); + return MPUrl.getUrl(builder.build().toString(), defaultUrl); case EVENTS: subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_2 + "/"; uri = new Uri.Builder() diff --git a/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java b/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java index b0ff76b8f..c0acffd1b 100644 --- a/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java +++ b/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java @@ -48,6 +48,10 @@ public static Builder builder() { return new Builder(); } + public static Builder builder(String podPrefix, boolean enable) { + return new Builder(podPrefix, enable); + } + @Nullable public static NetworkOptions withNetworkOptions(@Nullable String jsonString) { if (MPUtility.isEmpty(jsonString)) { @@ -134,10 +138,17 @@ public static class Builder { private Map domainMappings = new HashMap(); private Boolean pinningDisabledInDevelopment; private Boolean pinningDisabled; + private String podPrefix = "us1"; + private boolean enablePodRedirection = true; private Builder() { } + private Builder(String podPrefix, boolean enable){ + this.podPrefix = podPrefix; + this.enablePodRedirection = enable; + } + @NonNull public Builder addDomainMapping(@Nullable DomainMapping domain) { if (domainMappings == null) { diff --git a/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java b/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java index 994aca1bf..7f5a8bbbb 100644 --- a/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java +++ b/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java @@ -11,8 +11,9 @@ public class NetworkOptionsManager { public static String MP_CONFIG_URL = "config2.mparticle.com"; - public static String MP_IDENTITY_URL = "identity.mparticle.com"; public static String MP_URL = "nativesdks.mparticle.com"; + public static String MP_IDENTITY_URL_PREFIX = "identity"; + public static String MP_URL_PREFIX = "nativesdks"; public static NetworkOptions validateAndResolve(NetworkOptions networkOptions) { if (networkOptions == null) { @@ -73,11 +74,11 @@ static String getDefaultUrl(Endpoint type) { case CONFIG: return MPUtility.isEmpty(BuildConfig.MP_CONFIG_URL) ? MP_CONFIG_URL : BuildConfig.MP_CONFIG_URL; case IDENTITY: - return MPUtility.isEmpty(BuildConfig.MP_IDENTITY_URL) ? MP_IDENTITY_URL : BuildConfig.MP_IDENTITY_URL; + return MPUtility.isEmpty(BuildConfig.MP_IDENTITY_URL) ? MP_IDENTITY_URL_PREFIX : BuildConfig.MP_IDENTITY_URL; case EVENTS: case ALIAS: case AUDIENCE: - return MPUtility.isEmpty(BuildConfig.MP_URL) ? MP_URL : BuildConfig.MP_URL; + return MPUtility.isEmpty(BuildConfig.MP_URL) ? MP_URL_PREFIX : BuildConfig.MP_URL; default: throw new IllegalArgumentException("Missing a Url for type " + type.name()); } diff --git a/android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt b/android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt new file mode 100644 index 000000000..41ba2581c --- /dev/null +++ b/android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt @@ -0,0 +1,26 @@ +package com.mparticle + +object NetworkUtilities { + fun getUrlWithPrefix( + url: String?, + podPrefix: String, + enablePodRedirection: Boolean + ): String? { + return url?.let { + val newUrl = if (enablePodRedirection) { + "$it.$podPrefix" + } else { + "$it" + } + "$newUrl.mparticle.com" + } + } + + fun getPodPrefix(apiKey: String): String? { + return try { + apiKey.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().get(0) + } catch (e: Exception) { + "us1" + } + } +} \ No newline at end of file diff --git a/testutils/src/main/java/com/mparticle/networking/MockServer.java b/testutils/src/main/java/com/mparticle/networking/MockServer.java index d83435e28..68ef5673e 100644 --- a/testutils/src/main/java/com/mparticle/networking/MockServer.java +++ b/testutils/src/main/java/com/mparticle/networking/MockServer.java @@ -503,7 +503,7 @@ public int compare(Map.Entry o1, Map.Entry o2) entry.getKey().url = getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).getFile(); Logger.error("New Url: " + url + " -> " + entry.getKey().url); } - if (url.contains(NetworkOptionsManager.MP_URL) && !url.contains(apiKey)) { + if (url.contains(NetworkOptionsManager.MP_URL_PREFIX) && !url.contains(apiKey)) { entry.getKey().url = getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).getFile(); } if (url.contains("/alias") && !url.contains(apiKey)) { From 1952aee73184c61014e9cf1b2a31fe1e6a8c5a11 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Fri, 12 Jul 2024 15:32:13 -0400 Subject: [PATCH 03/19] fix unit test cases --- .../src/main/java/com/mparticle/internal/ConfigManager.java | 6 +++--- .../com/mparticle/networking/MParticleBaseClientImpl.java | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index dc82e20e6..c7fa77237 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -84,7 +84,7 @@ public class ConfigManager { static SharedPreferences sPreferences; private static JSONArray sPushKeys; - private boolean directUrlRouting = true; + private boolean directUrlRouting = false; private UserStorage mUserStorage; private String mLogUnhandledExceptions = VALUE_APP_DEFINED; @@ -409,7 +409,7 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC } if (responseJSON.has(KEY_DIRECT_URL_ROUTING)) { - // directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING, false); + directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING, false); editor.putBoolean(KEY_DIRECT_URL_ROUTING, directUrlRouting); } @@ -1289,8 +1289,8 @@ public boolean isDirectUrlRoutingEnabled() { public String getPodPrefix() { String prefix = "us1"; - String[] prefixFromApi = getApiKey().split("-"); try { + String[] prefixFromApi = getApiKey().split("-"); if (prefixFromApi.length > 1) { prefix = getApiKey().split("-")[0]; } diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index a59606551..a702f8e4a 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -111,8 +111,10 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean url = domainMappingUrl; } Uri uri; + Logger.debug("before Mansi -->"+url); if (endpoint != Endpoint.CONFIG) { url = NetworkUtilities.INSTANCE.getUrlWithPrefix(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); + Logger.debug("Mansi -->"+url); } MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null; String subdirectory; From da0c85609d6f5e8e8c7651315368bec99c8ff0fb Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Mon, 15 Jul 2024 12:03:41 -0400 Subject: [PATCH 04/19] remove test log --- .../java/com/mparticle/networking/MParticleBaseClientImpl.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index a702f8e4a..a59606551 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -111,10 +111,8 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean url = domainMappingUrl; } Uri uri; - Logger.debug("before Mansi -->"+url); if (endpoint != Endpoint.CONFIG) { url = NetworkUtilities.INSTANCE.getUrlWithPrefix(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); - Logger.debug("Mansi -->"+url); } MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null; String subdirectory; From a9992152f2008fedbb008d9a09da9ed36990448a Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Tue, 16 Jul 2024 12:02:15 -0400 Subject: [PATCH 05/19] Address review comments --- .../networking/MParticleBaseClientImplTest.kt | 101 ++++++++++++++++-- .../networking/NetworkOptionsTest.kt | 15 ++- .../com/mparticle/internal/ConfigManager.java | 7 +- .../networking/MParticleBaseClientImpl.java | 15 ++- .../kotlin/com.mparticle/NetworkUtilities.kt | 26 ----- 5 files changed, 122 insertions(+), 42 deletions(-) delete mode 100644 android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 312e7413c..8bd0101b3 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -2,7 +2,6 @@ package com.mparticle.networking import com.mparticle.MParticle import com.mparticle.MParticleOptions -import com.mparticle.NetworkUtilities import com.mparticle.internal.AccessUtils import com.mparticle.testutils.BaseCleanInstallEachTest import junit.framework.TestCase @@ -30,16 +29,68 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { @Test fun testUrlPrefixWithPodRedirection() { val prefix = "eu1" + val identityUrl = mRandomUtils.getAlphaString(20) + val configUrl = mRandomUtils.getAlphaString(20) + val audienceUrl = mRandomUtils.getAlphaString(20) + val eventsUrl = mRandomUtils.getAlphaString(20) + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .networkOptions( + NetworkOptions.builder() + .addDomainMapping( + DomainMapping.audienceMapping(audienceUrl) + .build() + ) + .addDomainMapping( + DomainMapping.configMapping(configUrl) + .build() + ) + .addDomainMapping( + DomainMapping.identityMapping(identityUrl) + .build() + ) + .addDomainMapping(DomainMapping.eventsMapping(eventsUrl).build()) + .build() + ) + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl val url = - NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) + baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) assertEquals("${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", url) } @Test fun testUrlPrefixWithoutPodRedirection() { val prefix = "eu1" + val identityUrl = mRandomUtils.getAlphaString(20) + val configUrl = mRandomUtils.getAlphaString(20) + val audienceUrl = mRandomUtils.getAlphaString(20) + val eventsUrl = mRandomUtils.getAlphaString(20) + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .networkOptions( + NetworkOptions.builder() + .addDomainMapping( + DomainMapping.audienceMapping(audienceUrl) + .build() + ) + .addDomainMapping( + DomainMapping.configMapping(configUrl) + .build() + ) + .addDomainMapping( + DomainMapping.identityMapping(identityUrl) + .build() + ) + .addDomainMapping(DomainMapping.eventsMapping(eventsUrl).build()) + .build() + ) + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl val url = - NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, false) + baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, prefix, false) assertEquals("${NetworkOptionsManager.MP_URL_PREFIX}.mparticle.com", url) } @@ -53,16 +104,42 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { Pair("st1-k77ivhkbbqf4ce0s3y12zpcthyn1ixfyu", "st1"), Pair("us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", "us3") ) + val identityUrl = mRandomUtils.getAlphaString(20) + val configUrl = mRandomUtils.getAlphaString(20) + val audienceUrl = mRandomUtils.getAlphaString(20) + val eventsUrl = mRandomUtils.getAlphaString(20) + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .networkOptions( + NetworkOptions.builder() + .addDomainMapping( + DomainMapping.audienceMapping(audienceUrl) + .build() + ) + .addDomainMapping( + DomainMapping.configMapping(configUrl) + .build() + ) + .addDomainMapping( + DomainMapping.identityMapping(identityUrl) + .build() + ) + .addDomainMapping(DomainMapping.eventsMapping(eventsUrl).build()) + .build() + ) + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl map.forEach { key, value -> - val prefix = NetworkUtilities.getPodPrefix(key) ?: "" + val prefix = getPodPrefix(key) ?: "" assertEquals(value, prefix) assertEquals( "${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", - NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) + baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) ) assertEquals( "${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.$prefix.mparticle.com", - NetworkUtilities.getUrlWithPrefix( + baseClientImpl.getPodUrl( NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, prefix, true @@ -71,7 +148,7 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { assertEquals( "${NetworkOptionsManager.MP_URL_PREFIX}.mparticle.com", - NetworkUtilities.getUrlWithPrefix( + baseClientImpl.getPodUrl( NetworkOptionsManager.MP_URL_PREFIX, prefix, false @@ -79,7 +156,7 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { ) assertEquals( "${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.mparticle.com", - NetworkUtilities.getUrlWithPrefix( + baseClientImpl.getPodUrl( NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, prefix, false @@ -132,4 +209,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { ) } } + + private fun getPodPrefix(apiKey: String): String? { + return try { + apiKey.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().get(0) + } catch (e: Exception) { + "us1" + } + } } diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index cb4317722..a6e783062 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -2,7 +2,6 @@ package com.mparticle.networking import com.mparticle.MParticle import com.mparticle.MParticleOptions -import com.mparticle.NetworkUtilities import com.mparticle.internal.AccessUtils import com.mparticle.testutils.BaseCleanInstallEachTest import org.junit.After @@ -37,9 +36,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { @Throws(MalformedURLException::class, InterruptedException::class) fun testDefaultEndpoints() { MParticle.start(MParticleOptions.builder(mContext).credentials(apiKey, "s").build()) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + setClients() Assert.assertEquals( - NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_URL_PREFIX, "", false), + baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, "", false), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -47,11 +48,15 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_URL_PREFIX, "", false), + baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, "", false), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, "", false), + baseClientImpl.getPodUrl( + NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, + "", + false + ), mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) var randIdentityPath = mRandomUtils.getAlphaString(10) @@ -90,7 +95,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { @Test @Throws(MalformedURLException::class) fun testRandomEndpoint() { - val identityUrl = "${mRandomUtils.getAlphaString(20)}" + val identityUrl = mRandomUtils.getAlphaString(20) val configUrl = mRandomUtils.getAlphaString(20) val audienceUrl = mRandomUtils.getAlphaString(20) val eventsUrl = mRandomUtils.getAlphaString(20) diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index c7fa77237..d4254c838 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -1287,15 +1287,20 @@ public boolean isDirectUrlRoutingEnabled() { return directUrlRouting; } + /* When Direct URL Routing is true, we create a new set of baseUrls that include the silo in the urls. mParticle API keys are prefixed with the + silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys + were us1. As such, if we split on a '-' and the resulting array length is 1, then it is an older APIkey that should route to us1. + When splitKey.length is greater than 1, then splitKey[0] will be us1, us2, eu1, au1, or st1, etc as new silos are added*/ public String getPodPrefix() { String prefix = "us1"; try { String[] prefixFromApi = getApiKey().split("-"); if (prefixFromApi.length > 1) { - prefix = getApiKey().split("-")[0]; + prefix = prefixFromApi[0]; } } catch (Exception e) { prefix = "us1"; + Logger.error("Error while setting direct URL routing : " + e); } return prefix; } diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index a59606551..7a491adef 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -7,7 +7,6 @@ import androidx.annotation.Nullable; import com.mparticle.BuildConfig; -import com.mparticle.NetworkUtilities; import com.mparticle.internal.ConfigManager; import com.mparticle.internal.Constants; import com.mparticle.internal.Logger; @@ -112,7 +111,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean } Uri uri; if (endpoint != Endpoint.CONFIG) { - url = NetworkUtilities.INSTANCE.getUrlWithPrefix(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); + url = getPodUrl(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); } MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null; String subdirectory; @@ -174,6 +173,18 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean } } + String getPodUrl( + String URLPrefix, + String pod, + boolean enablePodRedirection + ) { + if (URLPrefix != null) { + String newUrl = enablePodRedirection ? URLPrefix + "." + pod : URLPrefix; + return newUrl + ".mparticle.com"; + } + return null; + } + public enum Endpoint { CONFIG(1), IDENTITY(2), diff --git a/android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt b/android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt deleted file mode 100644 index 41ba2581c..000000000 --- a/android-core/src/main/kotlin/com.mparticle/NetworkUtilities.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.mparticle - -object NetworkUtilities { - fun getUrlWithPrefix( - url: String?, - podPrefix: String, - enablePodRedirection: Boolean - ): String? { - return url?.let { - val newUrl = if (enablePodRedirection) { - "$it.$podPrefix" - } else { - "$it" - } - "$newUrl.mparticle.com" - } - } - - fun getPodPrefix(apiKey: String): String? { - return try { - apiKey.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().get(0) - } catch (e: Exception) { - "us1" - } - } -} \ No newline at end of file From 2b0d73801524a8d75c73f26e3c19b7dd6e1d59fe Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Wed, 24 Jul 2024 14:31:31 -0400 Subject: [PATCH 06/19] handle domain mapping --- .../networking/NetworkOptionsTest.kt | 184 ++++++++++++++++-- .../networking/MParticleBaseClientImpl.java | 2 +- 2 files changed, 170 insertions(+), 16 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index a6e783062..27531a77b 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -37,7 +37,6 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { fun testDefaultEndpoints() { MParticle.start(MParticleOptions.builder(mContext).credentials(apiKey, "s").build()) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl - setClients() Assert.assertEquals( baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, "", false), @@ -122,7 +121,104 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - audienceUrl.addSuffix(), + audienceUrl, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority + ) + Assert.assertEquals( + configUrl, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority + ) + Assert.assertEquals( + eventsUrl, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority + ) + Assert.assertEquals( + identityUrl, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority + ) + var randIdentityPath = mRandomUtils.getAlphaString(10) + Assert.assertEquals( + "/v1/$randIdentityPath", + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path + ) + Assert.assertEquals( + audienceUrl, + identityClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority + ) + Assert.assertEquals( + configUrl, + identityClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority + ) + Assert.assertEquals( + eventsUrl, + identityClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority + ) + Assert.assertEquals( + identityUrl, + identityClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority + ) + randIdentityPath = mRandomUtils.getAlphaString(10) + Assert.assertEquals( + "/v1/$randIdentityPath", + identityClient.getUrl( + MParticleBaseClientImpl.Endpoint.IDENTITY, + randIdentityPath + ).path + ) + + // test the that the Path is still the default one (make sure the overrideSubdirectory is not kicking in when it shouldn't) + Assert.assertEquals( + defaultUrls[MParticleBaseClientImpl.Endpoint.AUDIENCE]?.path, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).path + ) + Assert.assertEquals( + defaultUrls[MParticleBaseClientImpl.Endpoint.CONFIG]?.path, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).path + ) + Assert.assertEquals( + defaultUrls[MParticleBaseClientImpl.Endpoint.EVENTS]?.path, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).path + ) + Assert.assertEquals( + defaultUrls[MParticleBaseClientImpl.Endpoint.IDENTITY]?.path, + mpClient.getUrl( + MParticleBaseClientImpl.Endpoint.IDENTITY, + MParticleBaseClientImpl.Endpoint.IDENTITY.name + ).path + ) + } + + @Test + @Throws(MalformedURLException::class) + fun testRandomEndpoint_With_DomainMapping_And_DirectURL_Routing_true() { + val identityUrl = mRandomUtils.getAlphaString(20) + val configUrl = mRandomUtils.getAlphaString(20) + val audienceUrl = mRandomUtils.getAlphaString(20) + val eventsUrl = mRandomUtils.getAlphaString(20) + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .networkOptions( + NetworkOptions.builder() + .addDomainMapping( + DomainMapping.audienceMapping(audienceUrl) + .build() + ) + .addDomainMapping( + DomainMapping.configMapping(configUrl) + .build() + ) + .addDomainMapping( + DomainMapping.identityMapping(identityUrl) + .build() + ) + .addDomainMapping(DomainMapping.eventsMapping(eventsUrl).build()) + .build() + ) + .build() + MParticle.start(options) + setClients() + Assert.assertEquals( + audienceUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -130,11 +226,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - identityUrl.addSuffix(), + identityUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) var randIdentityPath = mRandomUtils.getAlphaString(10) @@ -143,7 +239,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path ) Assert.assertEquals( - audienceUrl.addSuffix(), + audienceUrl, identityClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -151,11 +247,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { identityClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, identityClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - identityUrl.addSuffix(), + identityUrl, identityClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) randIdentityPath = mRandomUtils.getAlphaString(10) @@ -189,6 +285,64 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } + @Test + @Throws(MalformedURLException::class) + fun testRandomEndpoint_when_No_Domain_Mapping_And_DirectURL_Routing_false() { + val options = MParticleOptions.builder(mContext) + .credentials("us1-14a65s4d65a4ds", "jasjgasgauysdkabmnx") + .build() + MParticle.start(options) + setClients() + Assert.assertEquals( + NetworkOptionsManager.MP_URL_PREFIX.addSuffix(), + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority + ) + Assert.assertEquals( + NetworkOptionsManager.MP_CONFIG_URL, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority + ) + Assert.assertEquals( + NetworkOptionsManager.MP_URL_PREFIX.addSuffix(), + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority + ) + Assert.assertEquals( + NetworkOptionsManager.MP_IDENTITY_URL_PREFIX.addSuffix(), + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority + ) + } + + /* + /*This test case only runs when ConfigManager's 'directUrlRouting' is set to true. + Couldn't simulate the config response, so disabling the following test case. + */ + @Test + @Throws(MalformedURLException::class) + fun testRandomEndpoint_when_No_Domain_Mapping_And_DirectURL_Routing_true() { + val silo="us1" + val options = MParticleOptions.builder(mContext) + .credentials("$silo-14a65s4d65a4ds", "jasjgasgauysdkabmnx") + .build() + MParticle.start(options) + setClients() + Assert.assertEquals( + NetworkOptionsManager.MP_URL_PREFIX+".$silo".addSuffix(), + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority + ) + Assert.assertEquals( + NetworkOptionsManager.MP_CONFIG_URL, + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority + ) + Assert.assertEquals( + NetworkOptionsManager.MP_URL_PREFIX+".$silo".addSuffix(), + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority + ) + Assert.assertEquals( + NetworkOptionsManager.MP_IDENTITY_URL_PREFIX+".$silo".addSuffix(), + mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority + ) + + }*/ + @Test @Throws(MalformedURLException::class) fun testOverrideSubdirectory() { @@ -219,7 +373,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - audienceUrl.addSuffix(), + audienceUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority ) Assert.assertEquals( @@ -227,11 +381,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority ) Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - identityUrl.addSuffix(), + identityUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority ) val randIdentityPath = mRandomUtils.getAlphaString(10) @@ -293,11 +447,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.ALIAS).authority ) } @@ -319,11 +473,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( - aliasUrl.addSuffix(), + aliasUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.ALIAS).authority ) } @@ -343,7 +497,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { MParticle.start(options) setClients() Assert.assertEquals( - eventsUrl.addSuffix(), + eventsUrl, mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority ) Assert.assertEquals( diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index 7a491adef..c714aee5f 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -110,7 +110,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean url = domainMappingUrl; } Uri uri; - if (endpoint != Endpoint.CONFIG) { + if (endpoint != Endpoint.CONFIG & isDefaultUrl) { url = getPodUrl(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); } MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null; From 5d7451a2c290e5971882d5363b18367b33be5b0c Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Wed, 24 Jul 2024 16:16:44 -0400 Subject: [PATCH 07/19] address review comments --- .../networking/MParticleBaseClientImplTest.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 8bd0101b3..a7e73bf62 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -96,13 +96,15 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { @Test fun testAllPrefixes() { + //Following are the fake APIs for testing purposes. val map = mapOf( Pair("us1-1vc4gbp24cdtx6e31s58icnymzy83f1uf", "us1"), Pair("us2-v2p8lr3w2g90vtpaumbq21zy05cl50qm3", "us2"), Pair("eu1-bkabfno0b8zpv5bwi2zm2mfa1kfml19al", "eu1"), Pair("au1-iermuj83dbeoshm0n32f10feotclq6i4a", "au1"), Pair("st1-k77ivhkbbqf4ce0s3y12zpcthyn1ixfyu", "st1"), - Pair("us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", "us3") + Pair("us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", "us3"), + Pair("kajsdhasdiuyaiudiashhadjhdasjk", "us1") ) val identityUrl = mRandomUtils.getAlphaString(20) val configUrl = mRandomUtils.getAlphaString(20) @@ -212,7 +214,13 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { private fun getPodPrefix(apiKey: String): String? { return try { - apiKey.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().get(0) + val apiKeyParts = + apiKey.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + if (apiKeyParts.size > 1) { + apiKeyParts.firstOrNull() + } else { + "us1" + } } catch (e: Exception) { "us1" } From 434bd2f4ae62e59214cc4736500a977f99013edd Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Wed, 24 Jul 2024 16:49:30 -0400 Subject: [PATCH 08/19] remove test method,use method from the config class --- .../networking/MParticleBaseClientImplTest.kt | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index a7e73bf62..6e77389a8 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -3,6 +3,7 @@ package com.mparticle.networking import com.mparticle.MParticle import com.mparticle.MParticleOptions import com.mparticle.internal.AccessUtils +import com.mparticle.internal.ConfigManager import com.mparticle.testutils.BaseCleanInstallEachTest import junit.framework.TestCase import org.junit.Assert @@ -96,7 +97,20 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { @Test fun testAllPrefixes() { - //Following are the fake APIs for testing purposes. + val mConfigManager = ConfigManager( + mContext, + MParticle.Environment.Production, + "some api key", + "some api secret", + null, + null, + null, + null, + null, + null + ) + + // Following are the fake APIs for testing purposes. val map = mapOf( Pair("us1-1vc4gbp24cdtx6e31s58icnymzy83f1uf", "us1"), Pair("us2-v2p8lr3w2g90vtpaumbq21zy05cl50qm3", "us2"), @@ -133,7 +147,8 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { MParticle.start(options) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl map.forEach { key, value -> - val prefix = getPodPrefix(key) ?: "" + mConfigManager?.setCredentials(key, value) + val prefix = mConfigManager?.podPrefix assertEquals(value, prefix) assertEquals( "${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", @@ -211,18 +226,4 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { ) } } - - private fun getPodPrefix(apiKey: String): String? { - return try { - val apiKeyParts = - apiKey.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - if (apiKeyParts.size > 1) { - apiKeyParts.firstOrNull() - } else { - "us1" - } - } catch (e: Exception) { - "us1" - } - } } From 85b5f78443ffee8814fca8b9fd439f68ea7d6622 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Fri, 2 Aug 2024 19:06:27 -0400 Subject: [PATCH 09/19] address review comment --- .../kotlin/com.mparticle/MParticleOptionsTest.kt | 2 +- .../kotlin/com.mparticle/networking/AccessUtils.kt | 9 ++------- .../java/com/mparticle/internal/ConfigManager.java | 6 +++--- .../java/com/mparticle/networking/NetworkOptions.java | 11 ----------- 4 files changed, 6 insertions(+), 22 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt index fe50ec848..dae8615ca 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/MParticleOptionsTest.kt @@ -532,7 +532,7 @@ class MParticleOptionsTest : BaseAbstractTest() { Assert.assertTrue( com.mparticle.networking.AccessUtils.equals( options.networkOptions, - com.mparticle.networking.AccessUtils.getDefaultNetworkOptions() + com.mparticle.networking.AccessUtils.defaultNetworkOptions ) ) } diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt index 4a3bd50a2..17191bddf 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/AccessUtils.kt @@ -1,13 +1,8 @@ package com.mparticle.networking object AccessUtils { - private var defaultNetworkOptions: NetworkOptions? = null - - fun getDefaultNetworkOptions(): NetworkOptions { - defaultNetworkOptions = - NetworkOptionsManager.defaultNetworkOptions() - return defaultNetworkOptions!! - } + val defaultNetworkOptions: NetworkOptions + get() = NetworkOptionsManager.defaultNetworkOptions() fun equals(networkOptions1: NetworkOptions, networkOptions2: NetworkOptions): Boolean { if (networkOptions1 === networkOptions2) { diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index d4254c838..7d4f53234 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -409,7 +409,7 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC } if (responseJSON.has(KEY_DIRECT_URL_ROUTING)) { - directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING, false); + directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING); editor.putBoolean(KEY_DIRECT_URL_ROUTING, directUrlRouting); } @@ -1288,7 +1288,7 @@ public boolean isDirectUrlRoutingEnabled() { } /* When Direct URL Routing is true, we create a new set of baseUrls that include the silo in the urls. mParticle API keys are prefixed with the - silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys + silo and a hyphen (ex. "us1-", "us2-", "eu1-", etc). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys were us1. As such, if we split on a '-' and the resulting array length is 1, then it is an older APIkey that should route to us1. When splitKey.length is greater than 1, then splitKey[0] will be us1, us2, eu1, au1, or st1, etc as new silos are added*/ public String getPodPrefix() { @@ -1300,7 +1300,7 @@ public String getPodPrefix() { } } catch (Exception e) { prefix = "us1"; - Logger.error("Error while setting direct URL routing : " + e); + Logger.error("Error while getting pod prefix for direct URL routing : " + e); } return prefix; } diff --git a/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java b/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java index c0acffd1b..b0ff76b8f 100644 --- a/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java +++ b/android-core/src/main/java/com/mparticle/networking/NetworkOptions.java @@ -48,10 +48,6 @@ public static Builder builder() { return new Builder(); } - public static Builder builder(String podPrefix, boolean enable) { - return new Builder(podPrefix, enable); - } - @Nullable public static NetworkOptions withNetworkOptions(@Nullable String jsonString) { if (MPUtility.isEmpty(jsonString)) { @@ -138,17 +134,10 @@ public static class Builder { private Map domainMappings = new HashMap(); private Boolean pinningDisabledInDevelopment; private Boolean pinningDisabled; - private String podPrefix = "us1"; - private boolean enablePodRedirection = true; private Builder() { } - private Builder(String podPrefix, boolean enable){ - this.podPrefix = podPrefix; - this.enablePodRedirection = enable; - } - @NonNull public Builder addDomainMapping(@Nullable DomainMapping domain) { if (domainMappings == null) { From 84ce09fa8d3fbb0201c0d175762345b4474cb710 Mon Sep 17 00:00:00 2001 From: Mansi-mParticle <159845845+Mansi-mParticle@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:33:10 -0400 Subject: [PATCH 10/19] fix: Remove recursion from the method and add default URL generation. (#501) --- .../networking/MParticleBaseClientImplTest.kt | 102 +++++++++++++++++- .../networking/MParticleBaseClientImpl.java | 80 ++++++++++---- 2 files changed, 160 insertions(+), 22 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 6e77389a8..91805fef0 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -1,11 +1,12 @@ package com.mparticle.networking +import android.net.Uri +import com.mparticle.BuildConfig import com.mparticle.MParticle import com.mparticle.MParticleOptions import com.mparticle.internal.AccessUtils import com.mparticle.internal.ConfigManager import com.mparticle.testutils.BaseCleanInstallEachTest -import junit.framework.TestCase import org.junit.Assert import org.junit.Assert.assertEquals import org.junit.Before @@ -213,8 +214,7 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl for (endpoint in MParticleBaseClientImpl.Endpoint.values()) { val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, true) - assertEquals(defaultUrls[endpoint].toString(), generatedUrl.toString()) - TestCase.assertTrue(generatedUrl === generatedUrl.defaultUrl) + assertEquals(defaultUrls[endpoint].toString(), generatedUrl.defaultUrl.toString()) } for (endpoint in MParticleBaseClientImpl.Endpoint.values()) { val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, false) @@ -226,4 +226,100 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { ) } } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val subdirectory = "/v2/" + val uri = Uri.Builder() + .scheme(BuildConfig.SCHEME) + .encodedAuthority("nativesdks.us1.mparticle.com") + .path("$subdirectory us1-foo/events") + .build() + val result = baseClientImpl.generateDefaultURL(uri, "nativesdks.mparticle.com", "v2/us1-akshd324uajbhg123OIASI/events") + assertEquals("https://nativesdks.mparticle.com/v2/us1-akshd324uajbhg123OIASI/events", result.toString()) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_defaultDomain_IS_Empty() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val subdirectory = "/v2/" + val uri = Uri.Builder() + .scheme(BuildConfig.SCHEME) + .encodedAuthority("nativesdks.us1.mparticle.com") + .path("$subdirectory eu1-fooapi/events") + .build() + val result = baseClientImpl.generateDefaultURL(uri, "", "v2/us1-asjdjasdgjhasgdjhas/events") + assertEquals("https://nativesdks.us1.mparticle.com/v2/us1-asjdjasdgjhasgdjhas/events", result.toString()) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_defaultDomain_IS_NULL() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val subdirectory = "/v2/" + val uri = Uri.Builder() + .scheme(BuildConfig.SCHEME) + .encodedAuthority("nativesdks.us1.mparticle.com") + .path("$subdirectory us1-foo/events") + .build() + val result = baseClientImpl.generateDefaultURL(uri, null, "v2/us1-asjdjasdgjhasgdjhas/events") + assertEquals("https://nativesdks.us1.mparticle.com/v2/us1-asjdjasdgjhasgdjhas/events", result.toString()) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_URL_IS_NULL() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val result = baseClientImpl.generateDefaultURL(null, "nativesdks.mparticle.com", "v2/us1-bee5781b649a7a40a592c2000bc892d0/events") + assertEquals(null, result) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_PATH_IS_NULL() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val subdirectory = "/v2/" + val uri = Uri.Builder() + .scheme(BuildConfig.SCHEME) + .encodedAuthority("nativesdks.us1.mparticle.com") + .path("$subdirectory us1-foo/events") + .build() + val result = baseClientImpl.generateDefaultURL(uri, "nativesdks.mparticle.com", null) + assertEquals("https://nativesdks.mparticle.com/v2/%20us1-foo/events", result.toString()) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_defaultDomain_AND_URL_AND_PATH_ARE_NULL() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val result = baseClientImpl.generateDefaultURL(null, null, null) + assertEquals(null, result) + } } diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index c714aee5f..b59c36397 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -103,26 +103,37 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean NetworkOptions networkOptions = mConfigManager.getNetworkOptions(); DomainMapping domainMapping = networkOptions.getDomain(endpoint); String url = NetworkOptionsManager.getDefaultUrl(endpoint); + // Default domain to use for URL generation when domain mapping is specified + String defaultDomain = url; boolean isDefaultUrl = true; - if (domainMapping != null && !MPUtility.isEmpty(domainMapping.getUrl()) && !forceDefaultUrl) { - String domainMappingUrl = domainMapping.getUrl(); + String domainMappingUrl = domainMapping != null ? domainMapping.getUrl() : null; + if (!MPUtility.isEmpty(domainMappingUrl)) { isDefaultUrl = url.equals(domainMappingUrl); url = domainMappingUrl; } Uri uri; - if (endpoint != Endpoint.CONFIG & isDefaultUrl) { - url = getPodUrl(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); + // If domain mapping is not specified, need to set the prefix when it is enabled in the configuration. If EndPoint is config, there's no need to set the prefix. + if (endpoint != Endpoint.CONFIG) { + if (isDefaultUrl) { + url = getPodUrl(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); + } else { + // When domain mapping is specified, generate the default domain. Whether podRedirection is enabled or not, always use the original URL. + defaultDomain = getPodUrl(defaultDomain, null, false); + } } - MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null; String subdirectory; - boolean overridesSubdirectory = domainMapping.isOverridesSubdirectory() && !forceDefaultUrl; + String pathPrefix; + String pathPostfix; + boolean overridesSubdirectory = domainMapping.isOverridesSubdirectory(); switch (endpoint) { case CONFIG: - subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_4 + "/"; + pathPrefix = SERVICE_VERSION_4 + "/"; + subdirectory = overridesSubdirectory ? "" : pathPrefix; + pathPostfix = mApiKey + "/config"; Uri.Builder builder = new Uri.Builder() .scheme(BuildConfig.SCHEME) .encodedAuthority(url) - .path(subdirectory + mApiKey + "/config") + .path(subdirectory + pathPostfix) .appendQueryParameter("av", MPUtility.getAppVersionName(mContext)) .appendQueryParameter("sv", Constants.MPARTICLE_VERSION); if (mConfigManager.getDataplanId() != null) { @@ -136,43 +147,74 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean } } } - return MPUrl.getUrl(builder.build().toString(), defaultUrl); + return MPUrl.getUrl(builder.build().toString(), !isDefaultUrl ? generateDefaultURL(builder.build(), defaultDomain, (pathPrefix + pathPostfix)) : null); case EVENTS: - subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_2 + "/"; + pathPrefix = SERVICE_VERSION_2 + "/"; + subdirectory = overridesSubdirectory ? "" : pathPrefix; + pathPostfix = mApiKey + "/events"; uri = new Uri.Builder() .scheme(BuildConfig.SCHEME) .encodedAuthority(url) - .path(subdirectory + mApiKey + "/events") + .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), defaultUrl); + + return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); case ALIAS: - subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_1 + "/identity/"; + pathPrefix = SERVICE_VERSION_1 + "/identity/"; + subdirectory = overridesSubdirectory ? "" : pathPrefix; + pathPostfix = mApiKey + "/alias"; uri = new Uri.Builder() .scheme(BuildConfig.SCHEME) .encodedAuthority(url) - .path(subdirectory + mApiKey + "/alias") + .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), defaultUrl); + return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); case IDENTITY: + pathPrefix = SERVICE_VERSION_1 + "/"; subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_1 + "/"; + pathPostfix = identityPath; uri = new Uri.Builder() .scheme(BuildConfig.SCHEME) .encodedAuthority(url) - .path(subdirectory + identityPath) + .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), defaultUrl); + return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); case AUDIENCE: + pathPostfix = SERVICE_VERSION_2 + "/" + mApiKey + "/audience?mpID=" + mConfigManager.getMpid(); uri = new Uri.Builder() .scheme(BuildConfig.SCHEME) .encodedAuthority(url) - .path(SERVICE_VERSION_2 + "/" + mApiKey + "/audience?mpID=" + mConfigManager.getMpid()) + .path(pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), defaultUrl); + return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, pathPostfix) : null); default: return null; } } + /** + * Generates a new URL using the default domain when domain mapping is specified. + * This method creates a new URI based on the existing URI, but replaces the domain with the default domain. + * + * @param uri The existing URI which includes the domain mapping. + * @param defaultDomain The default domain name to be used in the new URI. + * @param path path to be used in the new URI. + * @return A new URI with the same path and scheme as the original URI, but with the default domain. + */ + protected MPUrl generateDefaultURL(Uri uri, String defaultDomain, String path) throws MalformedURLException { + if (uri != null) { + Uri.Builder uriBuilder = Uri.parse(uri.toString()).buildUpon(); + if (defaultDomain != null && !defaultDomain.isEmpty()) { + uriBuilder.encodedAuthority(defaultDomain); + } + if (path != null && !path.isEmpty()) { + uriBuilder.path(path); + } + return MPUrl.getUrl(uriBuilder.build().toString(), null); + } + return null; + } + String getPodUrl( String URLPrefix, String pod, From acb223c6837135eb142962226e0ecc98d2eefdc7 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Wed, 7 Aug 2024 12:37:04 -0400 Subject: [PATCH 11/19] Address review comments, remove commented test case --- .../networking/MParticleBaseClientImplTest.kt | 6 +++- .../networking/NetworkOptionsTest.kt | 30 ------------------- .../com/mparticle/internal/ConfigManager.java | 9 ++++++ .../networking/MParticleBaseClientImpl.java | 6 +--- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 91805fef0..9202b2d42 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -153,7 +153,11 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { assertEquals(value, prefix) assertEquals( "${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", - baseClientImpl.getPodUrl(NetworkOptionsManager.MP_URL_PREFIX, prefix, true) + baseClientImpl.getPodUrl( + NetworkOptionsManager.MP_URL_PREFIX, + prefix, + true + ) ) assertEquals( "${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.$prefix.mparticle.com", diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index 27531a77b..2ce3abd27 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -311,37 +311,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } - /* - /*This test case only runs when ConfigManager's 'directUrlRouting' is set to true. - Couldn't simulate the config response, so disabling the following test case. - */ - @Test - @Throws(MalformedURLException::class) - fun testRandomEndpoint_when_No_Domain_Mapping_And_DirectURL_Routing_true() { - val silo="us1" - val options = MParticleOptions.builder(mContext) - .credentials("$silo-14a65s4d65a4ds", "jasjgasgauysdkabmnx") - .build() - MParticle.start(options) - setClients() - Assert.assertEquals( - NetworkOptionsManager.MP_URL_PREFIX+".$silo".addSuffix(), - mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority - ) - Assert.assertEquals( - NetworkOptionsManager.MP_CONFIG_URL, - mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority - ) - Assert.assertEquals( - NetworkOptionsManager.MP_URL_PREFIX+".$silo".addSuffix(), - mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority - ) - Assert.assertEquals( - NetworkOptionsManager.MP_IDENTITY_URL_PREFIX+".$silo".addSuffix(), - mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority - ) - }*/ @Test @Throws(MalformedURLException::class) diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index 7d4f53234..3fa32f05b 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -409,7 +409,11 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC } if (responseJSON.has(KEY_DIRECT_URL_ROUTING)) { +<<<<<<< Updated upstream directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING); +======= + directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING, false); +>>>>>>> Stashed changes editor.putBoolean(KEY_DIRECT_URL_ROUTING, directUrlRouting); } @@ -1287,8 +1291,13 @@ public boolean isDirectUrlRoutingEnabled() { return directUrlRouting; } +<<<<<<< Updated upstream /* When Direct URL Routing is true, we create a new set of baseUrls that include the silo in the urls. mParticle API keys are prefixed with the silo and a hyphen (ex. "us1-", "us2-", "eu1-", etc). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys +======= + /* This function is called to get the specific pod/silo prefix when the `directUrlRouting` is `true`. mParticle API keys are prefixed with the + silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys +>>>>>>> Stashed changes were us1. As such, if we split on a '-' and the resulting array length is 1, then it is an older APIkey that should route to us1. When splitKey.length is greater than 1, then splitKey[0] will be us1, us2, eu1, au1, or st1, etc as new silos are added*/ public String getPodPrefix() { diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index b59c36397..abdd0037c 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -215,11 +215,7 @@ protected MPUrl generateDefaultURL(Uri uri, String defaultDomain, String path) t return null; } - String getPodUrl( - String URLPrefix, - String pod, - boolean enablePodRedirection - ) { + String getPodUrl(String URLPrefix, String pod, boolean enablePodRedirection) { if (URLPrefix != null) { String newUrl = enablePodRedirection ? URLPrefix + "." + pod : URLPrefix; return newUrl + ".mparticle.com"; From addeb2b44b8069e9d5a7d9289378499a7f3abbdf Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Wed, 7 Aug 2024 12:39:55 -0400 Subject: [PATCH 12/19] Resolve the merge conflict --- .../java/com/mparticle/internal/ConfigManager.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index 3fa32f05b..01d4fb628 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -409,11 +409,7 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC } if (responseJSON.has(KEY_DIRECT_URL_ROUTING)) { -<<<<<<< Updated upstream - directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING); -======= - directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING, false); ->>>>>>> Stashed changes + directUrlRouting = responseJSON.optBoolean(KEY_DIRECT_URL_ROUTING); editor.putBoolean(KEY_DIRECT_URL_ROUTING, directUrlRouting); } @@ -1291,15 +1287,11 @@ public boolean isDirectUrlRoutingEnabled() { return directUrlRouting; } -<<<<<<< Updated upstream - /* When Direct URL Routing is true, we create a new set of baseUrls that include the silo in the urls. mParticle API keys are prefixed with the - silo and a hyphen (ex. "us1-", "us2-", "eu1-", etc). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys -======= + /* This function is called to get the specific pod/silo prefix when the `directUrlRouting` is `true`. mParticle API keys are prefixed with the silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo,and before other silos existed, there were no prefixes and all apiKeys ->>>>>>> Stashed changes were us1. As such, if we split on a '-' and the resulting array length is 1, then it is an older APIkey that should route to us1. - When splitKey.length is greater than 1, then splitKey[0] will be us1, us2, eu1, au1, or st1, etc as new silos are added*/ + When splitKey.length is greater than 1, then splitKey[0] will be us1, us2, eu1, au1, or st1, etc as new silos are added */ public String getPodPrefix() { String prefix = "us1"; try { From 2c410ebec93054b9d50d8e46de3a8fbaf5219974 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Wed, 7 Aug 2024 12:58:27 -0400 Subject: [PATCH 13/19] fix lint error --- .../kotlin/com.mparticle/networking/NetworkOptionsTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index 2ce3abd27..ad3653804 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -311,8 +311,6 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } - - @Test @Throws(MalformedURLException::class) fun testOverrideSubdirectory() { From 7be9e190fa653cc119435ef179054aacdb8f73c0 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Thu, 8 Aug 2024 11:12:34 -0400 Subject: [PATCH 14/19] Link to Jira ticket for detailed information. --- .../kotlin/com.mparticle/networking/NetworkOptionsTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index ad3653804..7eb420407 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -188,6 +188,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } + + // For more details, refer to Jira ticket: https://mparticle-eng.atlassian.net/browse/SQDSDKS-6550 @Test @Throws(MalformedURLException::class) fun testRandomEndpoint_With_DomainMapping_And_DirectURL_Routing_true() { From 355d2d7551d71cdfac5684e0aac7446827afadb7 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Thu, 8 Aug 2024 11:16:04 -0400 Subject: [PATCH 15/19] Remove extra space --- .../kotlin/com.mparticle/networking/NetworkOptionsTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index 7eb420407..7254795dd 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -188,7 +188,6 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } - // For more details, refer to Jira ticket: https://mparticle-eng.atlassian.net/browse/SQDSDKS-6550 @Test @Throws(MalformedURLException::class) From 7cd4886391fe165e439c6464214d2ec9d233aa2f Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Thu, 8 Aug 2024 12:21:19 -0400 Subject: [PATCH 16/19] Correct reference link --- .../kotlin/com.mparticle/networking/NetworkOptionsTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index 7254795dd..4b1c38621 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -188,7 +188,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } - // For more details, refer to Jira ticket: https://mparticle-eng.atlassian.net/browse/SQDSDKS-6550 + // TODO : https://go.mparticle.com/work/SQDSDKS-6550 @Test @Throws(MalformedURLException::class) fun testRandomEndpoint_With_DomainMapping_And_DirectURL_Routing_true() { From 6cf1bef8044c6f8a77ef35563f7512cdc8b93d79 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Thu, 8 Aug 2024 12:32:49 -0400 Subject: [PATCH 17/19] address review comment --- .../kotlin/com.mparticle/networking/NetworkOptionsTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index 4b1c38621..bd6fd3e6f 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -188,7 +188,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } - // TODO : https://go.mparticle.com/work/SQDSDKS-6550 + // https://go.mparticle.com/work/SQDSDKS-6550 @Test @Throws(MalformedURLException::class) fun testRandomEndpoint_With_DomainMapping_And_DirectURL_Routing_true() { From c1ad77bdaea7c400ec0d27deaee38123d37d473e Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Fri, 9 Aug 2024 16:32:52 -0400 Subject: [PATCH 18/19] Refactor code for improved readability --- .../networking/MParticleBaseClientImplTest.kt | 60 +++++++++++++++++-- .../networking/MParticleBaseClientImpl.java | 45 ++++++++------ 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 9202b2d42..61e8bd59b 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -245,7 +245,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory us1-foo/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, "nativesdks.mparticle.com", "v2/us1-akshd324uajbhg123OIASI/events") + val result = baseClientImpl.generateDefaultURL( + false, + uri, + "nativesdks.mparticle.com", + "v2/us1-akshd324uajbhg123OIASI/events" + ) assertEquals("https://nativesdks.mparticle.com/v2/us1-akshd324uajbhg123OIASI/events", result.toString()) } @@ -263,7 +268,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory eu1-fooapi/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, "", "v2/us1-asjdjasdgjhasgdjhas/events") + val result = baseClientImpl.generateDefaultURL( + false, + uri, + "", + "v2/us1-asjdjasdgjhasgdjhas/events" + ) assertEquals("https://nativesdks.us1.mparticle.com/v2/us1-asjdjasdgjhasgdjhas/events", result.toString()) } @@ -281,7 +291,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory us1-foo/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, null, "v2/us1-asjdjasdgjhasgdjhas/events") + val result = baseClientImpl.generateDefaultURL( + false, + uri, + null, + "v2/us1-asjdjasdgjhasgdjhas/events" + ) assertEquals("https://nativesdks.us1.mparticle.com/v2/us1-asjdjasdgjhasgdjhas/events", result.toString()) } @@ -293,7 +308,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .build() MParticle.start(options) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl - val result = baseClientImpl.generateDefaultURL(null, "nativesdks.mparticle.com", "v2/us1-bee5781b649a7a40a592c2000bc892d0/events") + val result = baseClientImpl.generateDefaultURL( + false, + null, + "nativesdks.mparticle.com", + "v2/us1-bee5781b649a7a40a592c2000bc892d0/events" + ) assertEquals(null, result) } @@ -311,7 +331,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory us1-foo/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, "nativesdks.mparticle.com", null) + val result = baseClientImpl.generateDefaultURL( + false, + uri, + "nativesdks.mparticle.com", + null + ) assertEquals("https://nativesdks.mparticle.com/v2/%20us1-foo/events", result.toString()) } @@ -323,7 +348,30 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .build() MParticle.start(options) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl - val result = baseClientImpl.generateDefaultURL(null, null, null) + val result = baseClientImpl.generateDefaultURL(false, null, null, null) + assertEquals(null, result) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_defaultDomain_FLAG_IS_TRUE() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val subdirectory = "/v2/" + val uri = Uri.Builder() + .scheme(BuildConfig.SCHEME) + .encodedAuthority("nativesdks.us1.mparticle.com") + .path("$subdirectory us1-foo/events") + .build() + val result = baseClientImpl.generateDefaultURL( + true, + uri, + "nativesdks.mparticle.com", + "v2/us1-akshd324uajbhg123OIASI/events" + ) assertEquals(null, result) } } diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index abdd0037c..bd935d45a 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -23,10 +23,10 @@ public class MParticleBaseClientImpl implements MParticleBaseClient { - private Context mContext; - private ConfigManager mConfigManager; + private final Context mContext; + private final ConfigManager mConfigManager; private BaseNetworkConnection mRequestHandler; - private SharedPreferences mPreferences; + private final SharedPreferences mPreferences; String mApiKey; private static final String SERVICE_VERSION_1 = "/v1"; @@ -103,24 +103,29 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean NetworkOptions networkOptions = mConfigManager.getNetworkOptions(); DomainMapping domainMapping = networkOptions.getDomain(endpoint); String url = NetworkOptionsManager.getDefaultUrl(endpoint); - // Default domain to use for URL generation when domain mapping is specified + + // `defaultDomain` variable is for URL generation when domain mapping is specified. String defaultDomain = url; - boolean isDefaultUrl = true; + boolean isDefaultDomain = true; + + //Check if domain mapping is specified and update the URL based on domain mapping String domainMappingUrl = domainMapping != null ? domainMapping.getUrl() : null; if (!MPUtility.isEmpty(domainMappingUrl)) { - isDefaultUrl = url.equals(domainMappingUrl); + isDefaultDomain = url.equals(domainMappingUrl); url = domainMappingUrl; } - Uri uri; - // If domain mapping is not specified, need to set the prefix when it is enabled in the configuration. If EndPoint is config, there's no need to set the prefix. + if (endpoint != Endpoint.CONFIG) { - if (isDefaultUrl) { + // Set URL with pod prefix if it’s the default domain and endpoint is not CONFIG + if (isDefaultDomain) { url = getPodUrl(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); } else { // When domain mapping is specified, generate the default domain. Whether podRedirection is enabled or not, always use the original URL. defaultDomain = getPodUrl(defaultDomain, null, false); } } + + Uri uri; String subdirectory; String pathPrefix; String pathPostfix; @@ -147,7 +152,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean } } } - return MPUrl.getUrl(builder.build().toString(), !isDefaultUrl ? generateDefaultURL(builder.build(), defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(builder.build().toString(), generateDefaultURL(isDefaultDomain, builder.build(), defaultDomain, (pathPrefix + pathPostfix))); case EVENTS: pathPrefix = SERVICE_VERSION_2 + "/"; subdirectory = overridesSubdirectory ? "" : pathPrefix; @@ -158,7 +163,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, (pathPrefix + pathPostfix))); case ALIAS: pathPrefix = SERVICE_VERSION_1 + "/identity/"; subdirectory = overridesSubdirectory ? "" : pathPrefix; @@ -168,7 +173,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .encodedAuthority(url) .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, (pathPrefix + pathPostfix))); case IDENTITY: pathPrefix = SERVICE_VERSION_1 + "/"; subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_1 + "/"; @@ -178,7 +183,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .encodedAuthority(url) .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, (pathPrefix + pathPostfix))); case AUDIENCE: pathPostfix = SERVICE_VERSION_2 + "/" + mApiKey + "/audience?mpID=" + mConfigManager.getMpid(); uri = new Uri.Builder() @@ -186,7 +191,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .encodedAuthority(url) .path(pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, pathPostfix) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, pathPostfix)); default: return null; } @@ -196,12 +201,16 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean * Generates a new URL using the default domain when domain mapping is specified. * This method creates a new URI based on the existing URI, but replaces the domain with the default domain. * - * @param uri The existing URI which includes the domain mapping. - * @param defaultDomain The default domain name to be used in the new URI. - * @param path path to be used in the new URI. + * @param isDefaultDomain If the default domain is true, return null without generating a new URL. If the default domain is false, generate and return a new URL. + * @param uri The existing URI which includes the domain mapping. + * @param defaultDomain The default domain name to be used in the new URI. + * @param path path to be used in the new URI. * @return A new URI with the same path and scheme as the original URI, but with the default domain. */ - protected MPUrl generateDefaultURL(Uri uri, String defaultDomain, String path) throws MalformedURLException { + protected MPUrl generateDefaultURL(boolean isDefaultDomain, Uri uri, String defaultDomain, String path) throws MalformedURLException { + if (isDefaultDomain) { + return null; + } if (uri != null) { Uri.Builder uriBuilder = Uri.parse(uri.toString()).buildUpon(); if (defaultDomain != null && !defaultDomain.isEmpty()) { From d1a9dce5668edc0a4a8b283bfd94086de1b1afc0 Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Mon, 12 Aug 2024 11:11:59 -0400 Subject: [PATCH 19/19] Add a reference link for additional context --- .../kotlin/com.mparticle/networking/NetworkOptionsTest.kt | 2 +- .../java/com/mparticle/networking/MParticleBaseClientImpl.java | 2 +- .../java/com/mparticle/networking/NetworkOptionsManager.java | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt index bd6fd3e6f..4c35066df 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt @@ -188,7 +188,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() { ) } - // https://go.mparticle.com/work/SQDSDKS-6550 + // https://go.mparticle.com/work/SQDSDKS-6613 @Test @Throws(MalformedURLException::class) fun testRandomEndpoint_With_DomainMapping_And_DirectURL_Routing_true() { diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index bd935d45a..eb7a0cce4 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -108,7 +108,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean String defaultDomain = url; boolean isDefaultDomain = true; - //Check if domain mapping is specified and update the URL based on domain mapping + // Check if domain mapping is specified and update the URL based on domain mapping String domainMappingUrl = domainMapping != null ? domainMapping.getUrl() : null; if (!MPUtility.isEmpty(domainMappingUrl)) { isDefaultDomain = url.equals(domainMappingUrl); diff --git a/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java b/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java index 7f5a8bbbb..b6ddb5c3d 100644 --- a/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java +++ b/android-core/src/main/java/com/mparticle/networking/NetworkOptionsManager.java @@ -69,6 +69,7 @@ static List getDefaultCertificates() { } }; + // https://go.mparticle.com/work/SQDSDKS-6621 static String getDefaultUrl(Endpoint type) { switch (type) { case CONFIG: