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

com.sap.cloud.spring.boot.autoconfig.services.SaasRegistryConfig Maven / Gradle / Ivy

The newest version!
/******************************************************************************
 * © 2020 SAP SE or an SAP affiliate company. All rights reserved.            *
 ******************************************************************************/

package com.sap.cloud.spring.boot.autoconfig.services;

import com.sap.cds.mtx.impl.Authenticator;
import com.sap.cds.mtx.impl.ClientCredentialJwtAccess;
import com.sap.cds.mtx.impl.ClientCredentialJwtReader;
import com.sap.cds.mtx.impl.XsuaaParams;
import com.sap.cloud.mt.subscription.SaasRegistry;
import com.sap.cloud.mt.subscription.ServiceSpecification;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultDestinationLoader;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import org.apache.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration("ComSapMtSaasRegistryConfig")
@ConfigurationProperties(SaasRegistryConfig.COM_SAP_CLOUD_SAAS_REGISTRY)
@ConditionalOnMissingBean(SaasRegistry.class)
@ConditionalOnProperty(name = "com.sap.cloud.saas-registry.url")
public class SaasRegistryConfig {
    static final String COM_SAP_CLOUD_SAAS_REGISTRY = "com.sap.cloud.saas-registry";
    private static final Logger logger = LoggerFactory.getLogger(SaasRegistryConfig.class);
    private String xsuaaUrl;
    private String clientId;
    private String clientSecret;
    private String url;
    private Map credentials;

    public XsuaaParams xsuaaParamsForSaasReg() {
        if (credentials == null || credentials.isEmpty()) {
            credentials = new HashMap<>();
            credentials.put(XsuaaParams.URL, xsuaaUrl);
            credentials.put(XsuaaParams.CLIENTID, clientId);
            credentials.put(XsuaaParams.CLIENT_SECRET, clientSecret);
        }
        return new XsuaaParams(credentials);
    }

    private ClientCredentialJwtReader clientCredentialJwtReaderForSaasReg() {
        return new ClientCredentialJwtReader(xsuaaParamsForSaasReg());
    }

    private ClientCredentialJwtAccess clientCredentialJwtAccessForSaasReg() {
        return new ClientCredentialJwtAccess(clientCredentialJwtReaderForSaasReg());
    }

    @Bean("ComSapMtSaasRegistry")
    public SaasRegistry saasRegistry() {
        logger.debug("Create saas registry");
        addDestination();
        Authenticator authenticator = clientCredentialJwtAccessForSaasReg();
        ServiceSpecification serviceSpecification = ServiceSpecification.Builder.create()
                .requestEnhancer(request -> {
                    if (authenticator.getAuthorization().isPresent()) {
                        request.addHeader(HttpHeaders.AUTHORIZATION, authenticator.getAuthorization().get());
                    }
                }).build();
        return new SaasRegistry(serviceSpecification);
    }

    public String getXsuaaUrl() {
        return xsuaaUrl;
    }

    public void setXsuaaUrl(String xsuaaUrl) {
        this.xsuaaUrl = xsuaaUrl;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setCredentials(Map credentials) {
        this.credentials = credentials;
    }

    private void addDestination() {
        DefaultDestinationLoader loader = new DefaultDestinationLoader();
        loader.registerDestination(DefaultHttpDestination.builder(url).name(SaasRegistry.SAAS_REGISTRY_DESTINATION).build());
        DestinationAccessor.prependDestinationLoader(loader);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy