com.sap.cloud.mt.subscription.UiUrlCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multi-tenant-subscription Show documentation
Show all versions of multi-tenant-subscription Show documentation
Spring Boot Enablement Parent
/******************************************************************************
* © 2020 SAP SE or an SAP affiliate company. All rights reserved. *
******************************************************************************/
package com.sap.cloud.mt.subscription;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
import java.net.MalformedURLException;
import java.net.URL;
public class UiUrlCreator {
private UiUrlCreator() {
}
public static String createUrl(String subDomain, String baseUiUrl, String urlSeparator) throws InternalError {
if (subDomain == null || subDomain.trim().isEmpty()) {
throw new InternalError("No subdomain specified");
}
boolean isBaseUiUrl = false;
boolean isUrlSeparator = false;
if (!(baseUiUrl == null) && !baseUiUrl.trim().isEmpty()) isBaseUiUrl = true;
if (!(urlSeparator == null) && !urlSeparator.trim().isEmpty()) isUrlSeparator = true;
// at least one set, but not all three
if (!(isBaseUiUrl && isUrlSeparator)
&& (isBaseUiUrl || isUrlSeparator)) {
throw new InternalError("You have specified one of the URL parameters baseUiUrl and urlSeparator, but not all of them.");
}
// all not set
if (!(isBaseUiUrl || isUrlSeparator)) return "";
// all set
try {
return new URL("https://" + subDomain + urlSeparator + strippedBaseUiUrl(baseUiUrl)).toExternalForm();
} catch (MalformedURLException e) {
throw new InternalError(e);
}
}
//if the base url starts with a protocol (for example https://) extract the domain name
private static String strippedBaseUiUrl(String urlStr) {
URL url = null;
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
return urlStr;
}
return url.getHost();
}
}