All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.mt.subscription.UiUrlCreator Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
/*******************************************************************************
 *   © 2019-2024 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 org.apache.commons.lang3.StringUtils;

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 (!StringUtils.isBlank(baseUiUrl)) isBaseUiUrl = true;
		if (!StringUtils.isBlank(urlSeparator)) 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();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy