com.sap.cloud.yaas.servicesdk.apiconsole.utils.ExternalUrlHeaderUtil Maven / Gradle / Ivy
/*
* © 2016 SAP SE or an SAP affiliate company.
* All rights reserved.
* Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
* notices.
*/
package com.sap.cloud.yaas.servicesdk.apiconsole.utils;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Used to calculate the external base path based on the values of headers.
* The external base path is built using the values of the hyrbis-external-url
* headers. If any of these values are null/empty the external base path cannot be calculated and the result will be
* null.
*
* @deprecated Has been moved to {@link com.sap.cloud.yaas.servicesdk.ramlrewriter.filter.ExternalUrlHeaderUtil}
*/
@Deprecated
public final class ExternalUrlHeaderUtil
{
/** Header name use for passing the external url. */
public static final String HYBRIS_EXTERNAL_URL = "hybris-external-url";
private static final Logger LOG = LoggerFactory.getLogger(ExternalUrlHeaderUtil.class);
private ExternalUrlHeaderUtil()
{
//
}
private static boolean isBlank(final String text)
{
return text == null || "".equals(text.trim());
}
/**
* Flag informing that the provided header parameters are correct through the proxy external call. Which means :
*
* - contains the {@value ExternalUrlHeaderUtil#HYBRIS_EXTERNAL_URL}
*
* .
*
* @param externalUrlHeader the given headers
* @return a flag
*/
public static boolean isExternal(final String externalUrlHeader)
{
if (isBlank(externalUrlHeader))
{
return false;
}
return assureIsInternal(externalUrlHeader);
}
private static boolean assureIsInternal(final String externalUrlHeader)
{
try
{
new URL(externalUrlHeader).toURI();
return true;
}
catch (final MalformedURLException | URISyntaxException e)
{
LOG.warn(HYBRIS_EXTERNAL_URL + " header contains invalid url '" + externalUrlHeader + "'", e);
return false;
}
}
/**
* Returns the external base uri calculated from given header if possible. If any external header value
* is null, or corrupted, the method returns null (external path cannot be calculated).
*
* See more {@link #isExternal(String)}.
*
* @param externalUrlHeader the given header value
* @return an external base uri for headers if possible
*/
public static URI asExternalUri(final String externalUrlHeader)
{
if (isExternal(externalUrlHeader))
{
try
{
return new URL(externalUrlHeader).toURI();
}
catch (final MalformedURLException | URISyntaxException e)
{
LOG.warn(HYBRIS_EXTERNAL_URL + " header contains invalid url '" + externalUrlHeader + "'", e);
}
}
return null;
}
}