@@ -114,50 +114,18 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, HashMap
114114 protected MPUrl getUrl (Endpoint endpoint , @ Nullable String identityPath ,HashMap <String , String > audienceQueryParams , @ Nullable UploadSettings uploadSettings ) throws MalformedURLException {
115115 NetworkOptions networkOptions = uploadSettings == null ? mConfigManager .getNetworkOptions () : uploadSettings .getNetworkOptions ();
116116 DomainMapping domainMapping = networkOptions .getDomain (endpoint );
117- String url = NetworkOptionsManager .getDefaultUrl (endpoint );
118117 String apiKey = uploadSettings == null ? mApiKey : uploadSettings .getApiKey ();
119- final String customBaseURL = networkOptions .getCustomBaseURL ();
118+ final boolean usingCustomBaseURL = ! MPUtility . isEmpty ( networkOptions .getCustomBaseURL () );
120119
121- // `defaultDomain` variable is for URL generation when domain mapping is specified.
122- String defaultDomain = url ;
123- boolean isDefaultDomain = true ;
124-
125- if (!MPUtility .isEmpty (customBaseURL )) {
126- // customBaseURL takes priority over all individual domain mappings.
127- if (domainMapping != null && !MPUtility .isEmpty (domainMapping .getUrl ())) {
128- Logger .warning ("NetworkOptions: customBaseURL is set; domain mapping for " + endpoint .name () + " is ignored." );
129- }
130- url = customBaseURL ;
131- isDefaultDomain = false ;
132- if (endpoint != Endpoint .CONFIG ) {
133- // When custom CNAME is used, the default-domain URL still needs the pod prefix
134- // so MPConnectionTest matching and pinning fallbacks continue to work.
135- defaultDomain = getPodUrl (defaultDomain , null , false );
136- }
137- } else {
138- // Check if domain mapping is specified and update the URL based on domain mapping
139- String domainMappingUrl = domainMapping != null ? domainMapping .getUrl () : null ;
140- if (!MPUtility .isEmpty (domainMappingUrl )) {
141- isDefaultDomain = url .equals (domainMappingUrl );
142- url = domainMappingUrl ;
143- }
144-
145- if (endpoint != Endpoint .CONFIG ) {
146- // Set URL with pod prefix if it’s the default domain and endpoint is not CONFIG
147- if (isDefaultDomain ) {
148- url = getPodUrl (url , mConfigManager .getPodPrefix (apiKey ), mConfigManager .isDirectUrlRoutingEnabled ());
149- } else {
150- // When domain mapping is specified, generate the default domain. Whether podRedirection is enabled or not, always use the original URL.
151- defaultDomain = getPodUrl (defaultDomain , null , false );
152- }
153- }
154- }
120+ ResolvedHost host = resolveHost (endpoint , networkOptions , domainMapping , apiKey );
121+ String url = host .url ;
122+ String defaultDomain = host .defaultDomain ;
123+ boolean isDefaultDomain = host .isDefaultDomain ;
155124
156125 Uri uri ;
157126 String subdirectory ;
158127 String pathPrefix ;
159128 String pathPostfix ;
160- final boolean usingCustomBaseURL = !MPUtility .isEmpty (customBaseURL );
161129 boolean overridesSubdirectory = domainMapping != null && domainMapping .isOverridesSubdirectory ();
162130 if (usingCustomBaseURL && overridesSubdirectory ) {
163131 Logger .warning ("NetworkOptions: customBaseURL with overridesSubdirectory is unsupported for CDN routing; overridesSubdirectory will be ignored for " + endpoint .name () + "." );
@@ -268,6 +236,57 @@ String getPodUrl(String URLPrefix, String pod, boolean enablePodRedirection) {
268236 return null ;
269237 }
270238
239+ /**
240+ * Resolves the host(s) used to build the endpoint URL. Returns three values:
241+ * {@code url} — host used for the request, {@code defaultDomain} — host used as the
242+ * fallback for {@link #generateDefaultURL}, and {@code isDefaultDomain} — true when
243+ * {@code url} is the unmodified mParticle default.
244+ *
245+ * <p>Priority: {@code customBaseURL} → per-endpoint {@code DomainMapping} → default.
246+ */
247+ private ResolvedHost resolveHost (Endpoint endpoint , NetworkOptions networkOptions , DomainMapping domainMapping , String apiKey ) {
248+ String defaultUrl = NetworkOptionsManager .getDefaultUrl (endpoint );
249+ String customBaseURL = networkOptions .getCustomBaseURL ();
250+
251+ if (!MPUtility .isEmpty (customBaseURL )) {
252+ if (domainMapping != null && !MPUtility .isEmpty (domainMapping .getUrl ())) {
253+ Logger .warning ("NetworkOptions: customBaseURL is set; domain mapping for " + endpoint .name () + " is ignored." );
254+ }
255+ // When custom CNAME is used, the default-domain URL still needs the pod prefix
256+ // so MPConnectionTest matching and pinning fallbacks continue to work.
257+ String defaultDomain = endpoint == Endpoint .CONFIG ? defaultUrl : getPodUrl (defaultUrl , null , false );
258+ return new ResolvedHost (customBaseURL , defaultDomain , false );
259+ }
260+
261+ String domainMappingUrl = domainMapping != null ? domainMapping .getUrl () : null ;
262+ boolean isDefaultDomain = MPUtility .isEmpty (domainMappingUrl ) || defaultUrl .equals (domainMappingUrl );
263+ String url = isDefaultDomain ? defaultUrl : domainMappingUrl ;
264+ String defaultDomain = defaultUrl ;
265+
266+ if (endpoint != Endpoint .CONFIG ) {
267+ if (isDefaultDomain ) {
268+ // Default domain gets the pod prefix.
269+ url = getPodUrl (url , mConfigManager .getPodPrefix (apiKey ), mConfigManager .isDirectUrlRoutingEnabled ());
270+ } else {
271+ // Domain-mapped: always generate the default with the original (un-pod-prefixed) host.
272+ defaultDomain = getPodUrl (defaultDomain , null , false );
273+ }
274+ }
275+ return new ResolvedHost (url , defaultDomain , isDefaultDomain );
276+ }
277+
278+ private static final class ResolvedHost {
279+ final String url ;
280+ final String defaultDomain ;
281+ final boolean isDefaultDomain ;
282+
283+ ResolvedHost (String url , String defaultDomain , boolean isDefaultDomain ) {
284+ this .url = url ;
285+ this .defaultDomain = defaultDomain ;
286+ this .isDefaultDomain = isDefaultDomain ;
287+ }
288+ }
289+
271290 public enum Endpoint {
272291 CONFIG (1 ),
273292 IDENTITY (2 ),
0 commit comments