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

com.sap.cloud.alert.notification.client.builder.AlertNotificationClientBuilder Maven / Gradle / Ivy

Go to download

Means for out-of-the-box events management in SAP Alert Notification service for SAP BTP service.

There is a newer version: 1.11.0
Show newest version
package com.sap.cloud.alert.notification.client.builder;

import com.sap.cloud.alert.notification.client.IRetryPolicy;
import com.sap.cloud.alert.notification.client.Platform;
import com.sap.cloud.alert.notification.client.ServiceRegion;
import com.sap.cloud.alert.notification.client.exceptions.ClientRequestException;
import com.sap.cloud.alert.notification.client.internal.*;
import com.sap.cloud.alert.notification.client.model.AlertNotificationServiceBinding;
import com.sap.cloud.alert.notification.client.model.DestinationServiceBinding;
import org.apache.http.client.HttpClient;

import java.net.URI;

import static com.sap.cloud.alert.notification.client.Platform.CF;
import static java.util.Objects.*;
import static org.apache.http.impl.client.HttpClients.createDefault;

public class AlertNotificationClientBuilder {

    private String clientId;
    private String clientSecret;
    private String certificate;
    private String privateKey;
    private URI oAuthServiceUri;
    private HttpClient httpClient;
    private IRetryPolicy retryPolicy;
    private ServiceRegion serviceRegion;
    private IAuthorizationHeader authorizationHeader;
    private DestinationCredentialsProvider destinationCredentialsProvider;
    private boolean isCertificateAuthentication = false;
    private KeyStoreDetails keyStoreDetails;
    private Long invalidationTime;

    public AlertNotificationClientBuilder() {
        this(createDefault());
    }

    public AlertNotificationClientBuilder(HttpClient httpClient) {
        this.serviceRegion = null;
        this.authorizationHeader = null;
        this.httpClient = requireNonNull(httpClient);
        this.retryPolicy = new SimpleRetryPolicy();
    }

    public AlertNotificationClientBuilder withHttpClient(HttpClient httpClient) {
        this.httpClient = httpClient;
        return this;
    }

    public AlertNotificationClientBuilder withRetryPolicy(IRetryPolicy retryPolicy) {
        this.retryPolicy = retryPolicy;

        return this;
    }

    public AlertNotificationClientBuilder withServiceRegion(ServiceRegion serviceRegion) {
        this.serviceRegion = serviceRegion;

        return this;
    }

    public AlertNotificationClientBuilder withAuthentication(String clientId, String clientSecret) {
        this.clientId = clientId;
        this.clientSecret = clientSecret;
        return this;
    }

    public AlertNotificationClientBuilder withAuthentication(String clientId, String clientSecret, URI oAuthServiceURI) {
        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.oAuthServiceUri = oAuthServiceURI;
        return this;
    }

    public AlertNotificationClientBuilder withAuthentication(String certificate, String privateKey, URI oAuthServiceURI, String clientId) {
        this.clientId = clientId;
        this.certificate = certificate;
        this.privateKey = privateKey;
        this.oAuthServiceUri = oAuthServiceURI;
        return this;
    }

    public AlertNotificationClientBuilder withCertificate(String certificate, String privateKey) {
        this.certificate = certificate;
        this.privateKey = privateKey;
        this.isCertificateAuthentication = Boolean.TRUE;
        return this;
    }

    public AlertNotificationClient buildFromServiceBinding() {
        return buildFromServiceBinding(new AlertNotificationServiceBinding());
    }

    public AlertNotificationClient buildFromServiceBinding(AlertNotificationServiceBinding serviceBinding) {
        AlertNotificationClientBuilder alertNotificationClientBuilder = withServiceRegion(new ServiceRegion(CF, serviceBinding.getServiceUri().toString()));

        if (isNull(serviceBinding.getOauthUri()) && nonNull(serviceBinding.getCertificate()) && nonNull(serviceBinding.getPrivateKey())) {
            return withCertificate(serviceBinding.getCertificate(), serviceBinding.getPrivateKey()) //
                    .build();
        }

        return isNull(serviceBinding.getOauthUri()) //
                ? alertNotificationClientBuilder.withAuthentication(serviceBinding.getClientId(), serviceBinding.getClientSecret()).build() //
                : nonNull(serviceBinding.getClientSecret()) ? alertNotificationClientBuilder.withAuthentication(serviceBinding.getClientId(), serviceBinding.getClientSecret(), serviceBinding.getOauthUri()).build()
                : alertNotificationClientBuilder.withAuthentication(serviceBinding.getCertificate(), serviceBinding.getPrivateKey(), serviceBinding.getOauthUri(), serviceBinding.getClientId()).build();
    }

    public AlertNotificationClient buildFromDestinationBinding(DestinationServiceBinding destinationServiceBinding, String destinationName) {
        this.destinationCredentialsProvider = buildDestinationCredentialsProvider(destinationName, httpClient, destinationServiceBinding);

        DestinationContext destinationContext = destinationCredentialsProvider.getDestinationContext();

        this.serviceRegion = new ServiceRegion(CF, destinationContext.getServiceUri());
        this.isCertificateAuthentication = destinationContext.isCertificateAuthentication();

        if(isCertificateAuthentication){
            this.keyStoreDetails = destinationCredentialsProvider.getKeyStoreDetails();
        }

        return build();
    }

    public AlertNotificationClient buildFromDestinationBinding(DestinationServiceBinding destinationServiceBinding, String destinationName, Long invalidationTime) {
        this.invalidationTime = invalidationTime;

        assertValidInvalidationTime();
        return buildFromDestinationBinding(destinationServiceBinding, destinationName);
    }

    private DestinationCredentialsProvider buildDestinationCredentialsProvider(String destinationName, HttpClient httpClient, DestinationServiceBinding destinationServiceBinding) {
        return new DestinationCredentialsProvider( //
                destinationName, //
                httpClient, //
                new OAuthAuthorizationHeader( //
                        requireNonNull(destinationServiceBinding.getClientId()), //
                        requireNonNull(destinationServiceBinding.getClientSecret()), //
                        requireNonNull(destinationServiceBinding.getOauthUri()), //
                        httpClient), //
                destinationServiceBinding //
        );
    }

    public AlertNotificationClient build() {
        if (isNull(destinationCredentialsProvider) && isCertificateAuthentication) {
            return new AlertNotificationClient(
                    httpClient,
                    requireNonNull(retryPolicy),
                    requireNonNull(serviceRegion),
                    requireNonNull(certificate),
                    requireNonNull(privateKey),
                    new HttpClientFactory(),
                    isCertificateAuthentication
            );
        }

        return new AlertNotificationClient(
                requireNonNull(httpClient),
                requireNonNull(retryPolicy),
                requireNonNull(serviceRegion),
                buildAuthorizationHeader(),
                invalidationTime,
                keyStoreDetails,
                destinationCredentialsProvider,
                new HttpClientFactory(),
                isCertificateAuthentication
        );
    }

    private IAuthorizationHeader buildAuthorizationHeader() {
        if(nonNull(destinationCredentialsProvider) && !isCertificateAuthentication) {
            return destinationCredentialsProvider.getAuthorizationHeader();
        }
        
        if (nonNull(certificate) && nonNull(privateKey) && nonNull(oAuthServiceUri)) {
            return new OAuthAuthorizationHeader(certificate, privateKey, oAuthServiceUri, clientId, new HttpClientFactory());
        }
        
        if (isNull(clientId) && isNull(clientSecret)) {
            return null; // Rely on HttpClient configuration only
        }

        return isNull(oAuthServiceUri) //
                ? new BasicAuthorizationHeader(clientId, clientSecret) //
                : new OAuthAuthorizationHeader(clientId, clientSecret, oAuthServiceUri, httpClient);
    }

    private void assertValidInvalidationTime() {
        if(nonNull(invalidationTime) && invalidationTime < 0) {
            throw new ClientRequestException("InvalidationTime cannot be a negative number");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy