com.sap.cloud.sdk.cloudplatform.connectivity.GetOnPremiseProxyHeadersCommand Maven / Gradle / Ivy
/*
* Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
*/
package com.sap.cloud.sdk.cloudplatform.connectivity;
import java.util.List;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.cloudplatform.tenant.Tenant;
import com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor;
import com.sap.cloud.sdk.frameworks.hystrix.Command;
import com.sap.cloud.sdk.frameworks.hystrix.HystrixUtil;
import lombok.Data;
class GetOnPremiseProxyHeadersCommand extends Command>
{
private static final Logger logger = CloudLoggerFactory.getLogger(GetOnPremiseProxyHeadersCommand.class);
@Data
private static class CommandSetterBuilder
{
private final Class extends Command>> commandClass;
private String getCommandKey()
{
// TODO test isolation
final String commandKey =
HystrixUtil.getCommandKey(
commandClass,
TenantAccessor.getCurrentTenantIfAvailable().map(Tenant::getTenantId).orElse(null),
null);
if( logger.isDebugEnabled() ) {
logger.debug("Constructed command key: \"" + commandKey + "\".");
}
return commandKey;
}
HystrixCommand.Setter build()
{
final String groupKey = HystrixUtil.getGlobalKey(commandClass);
final String commandKey = getCommandKey();
final HystrixCommandProperties.Setter commandProperties =
HystrixCommandProperties
.Setter()
.withExecutionTimeoutInMilliseconds(6000)
.withCircuitBreakerEnabled(true)
.withCircuitBreakerSleepWindowInMilliseconds(6000)
.withFallbackEnabled(false);
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andThreadPoolPropertiesDefaults(
HystrixThreadPoolProperties
.Setter()
.withCoreSize(10)
.withQueueSizeRejectionThreshold(100)
.withMaxQueueSize(100))
.andCommandPropertiesDefaults(commandProperties);
}
}
private static final CommandSetterBuilder setterBuilder =
new CommandSetterBuilder(GetOnPremiseProxyHeadersCommand.class);
private final XsuaaService xsuaaService;
private final ConnectivityService connectivityService;
private final boolean useProviderTenant;
GetOnPremiseProxyHeadersCommand(
@Nonnull final XsuaaService xsuaaService,
@Nonnull final ConnectivityService connectivityService,
final boolean useProviderTenant )
{
super(setterBuilder.build());
this.xsuaaService = xsuaaService;
this.connectivityService = connectivityService;
this.useProviderTenant = useProviderTenant;
}
private AccessToken getAccessToken()
{
try {
return xsuaaService.getServiceToken(ConnectivityService.SERVICE_NAME, false, useProviderTenant);
}
catch( final TokenRequestFailedException | TokenRequestDeniedException e ) {
throw new DestinationAccessException(
"Failed to get the access token for " + ConnectivityService.SERVICE_NAME + " service.",
e);
}
}
@Override
protected List run()
{
return connectivityService.getOnPremiseProxyHeaders(getAccessToken());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy