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

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

There is a newer version: 3.3.1
Show newest version
/******************************************************************************
 * © 2020 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 com.sap.cloud.mt.subscription.json.SaasCallbackPayload;
import com.sap.cloud.mt.tools.api.RequestEnhancer;
import com.sap.cloud.mt.tools.api.ResilienceConfig;
import com.sap.cloud.mt.tools.api.ServiceEndpoint;
import com.sap.cloud.mt.tools.exception.InternalException;
import com.sap.cloud.mt.tools.exception.ServiceException;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

import static org.apache.http.HttpStatus.SC_BAD_GATEWAY;
import static org.apache.http.HttpStatus.SC_GATEWAY_TIMEOUT;
import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR;
import static org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE;

public class SaasRegistry {
    private static final String APPLICATION_JSON = "application/json";
    public static final String SUCCEEDED = "SUCCEEDED";
    public static final String FAILED = "FAILED";
    private final String saasRegistryUrl;
    private static final String AUTHENTIFICATION_SCHEME = "Bearer";
    private static final Logger logger = LoggerFactory.getLogger(SaasRegistry.class);
    private static final String SAAS_REGISTRY_DESTINATION = "com.sap.cds.saasRegistry";
    private final ResilienceConfig resilienceConfig;
    private final RequestEnhancer requestEnhancer;

    public SaasRegistry(RequestEnhancer requestEnhancer, String saasRegistryUrl, ResilienceConfig resilienceConfig) {
        this.requestEnhancer = requestEnhancer;
        this.saasRegistryUrl = saasRegistryUrl;
        this.resilienceConfig = resilienceConfig;
    }

    public SaasRegistry(RequestEnhancer requestEnhancer, String saasRegistryUrl) {
        this(requestEnhancer, saasRegistryUrl, ResilienceConfig.NONE);
    }

    public void callBackSaasRegistry(boolean success, String message, String subscriptionUrl, String callbackUrl) throws InternalError {
        ServiceEndpoint saasRegistry = null;
        try {
            saasRegistry = ServiceEndpoint.create().at(saasRegistryUrl).destinationName(SAAS_REGISTRY_DESTINATION).path(callbackUrl)
                    .returnCodeChecker(c -> {
                        if (c != HttpStatus.SC_OK) {
                            return new InternalError("Saas registry returned http status " + c + " . Called saas registry url is " + saasRegistryUrl + callbackUrl);
                        } else {
                            return null;
                        }
                    })
                    .retry()
                    .forReturnCodes(SC_BAD_GATEWAY, SC_GATEWAY_TIMEOUT, SC_INTERNAL_SERVER_ERROR, SC_SERVICE_UNAVAILABLE)
                    .config(resilienceConfig)
                    .end();
        } catch (InternalException e) {
            throw new InternalError(e);
        }
        SaasCallbackPayload payload = getSaasCallbackPayload(success, message, subscriptionUrl);
        Map headerFields = new HashMap<>();
        headerFields.put(HttpHeaders.ACCEPT, APPLICATION_JSON);
        logger.debug("Call saas registry at url {}", saasRegistryUrl + callbackUrl);
        try {
            saasRegistry.createServiceCall().http().put().payload(payload).noPathParameter().noQuery()
                    .enhancer(requestEnhancer)
                    .insertHeaderFields(headerFields).end().execute();
        } catch (InternalException e) {
            throw new InternalError(e.getCause());
        } catch (ServiceException e) {
            if (e.getCause() instanceof InternalError) {
                throw (InternalError) e.getCause();
            }
            throw new InternalError(e.getCause());
        }
    }

    private SaasCallbackPayload getSaasCallbackPayload(boolean success, String message, String subscriptionUrl) {
        SaasCallbackPayload payload = new SaasCallbackPayload();
        payload.message = message;
        if (success) {
            payload.status = SUCCEEDED;
        } else {
            payload.status = FAILED;
        }
        payload.subscriptionUrl = subscriptionUrl;
        return payload;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy