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

com.sap.cloud.sdk.cloudplatform.connectivity.GetOnPremiseProxyHeadersCommand Maven / Gradle / Ivy

Go to download

Implementation of the Cloud platform abstraction for general-purpose connectivity on the SAP Cloud Platform (Cloud Foundry).

The newest version!
/*
 * Copyright (c) 2020 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.security.exception.TokenRequestDeniedException;
import com.sap.cloud.sdk.cloudplatform.security.exception.TokenRequestFailedException;
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> 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;
    private final boolean propagateUser;

    GetOnPremiseProxyHeadersCommand(
        @Nonnull final XsuaaService xsuaaService,
        @Nonnull final ConnectivityService connectivityService,
        final boolean useProviderTenant,
        final boolean propagateUser )
    {
        super(setterBuilder.build());

        this.xsuaaService = xsuaaService;
        this.connectivityService = connectivityService;
        this.useProviderTenant = useProviderTenant;
        this.propagateUser = propagateUser;
    }

    private AccessToken getAccessToken()
    {
        try {
            return xsuaaService.getServiceToken(ConnectivityService.SERVICE_NAME, propagateUser, 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(), propagateUser); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy