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

com.mnubo.java.sdk.client.services.MnuboSDKFactory Maven / Gradle / Ivy

package com.mnubo.java.sdk.client.services;

import static com.mnubo.java.sdk.client.Constants.*;
import static com.mnubo.java.sdk.client.utils.ValidationUtils.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.mnubo.java.sdk.client.config.ExponentialBackoffConfig;
import com.mnubo.java.sdk.client.utils.ValidationUtils;
import org.springframework.web.client.RestTemplate;

import com.mnubo.java.sdk.client.config.MnuboSDKConfig;
import com.mnubo.java.sdk.client.spi.MnuboSDKClient;

/**
 * MnuboSDKFactory. This build a MnuboSDKClient instance.
 */
public abstract class MnuboSDKFactory {

    //@formatter:off
    /**
     * returns a mnubo sdk client to send request to mnubo servers from a basic
     * configuration. This uses default parameters
     *
     * This is the list of default parameters set:
     * client.config.platform-port: 443.
     * client.config.autentication-port: 443.
     * client.http.client.disable-redirect-handling: false.
     * client.http.client.disable-automatic-retries: false.
     * client.http.client.disable-content-compression: false.
     * client.http.client.max-connections-per-route: 200.
     * client.http.client.http.client.default-timeout: 15000 ms.
     * client.http.client.http.client.connect-timeout: 15000 ms.
     * client.http.client.connection-request-timeout: 15000 ms.
     * client.http.client.http.client.socket-timeout: 15000 ms.
     * client.http.client.max-total-connection: 200.
     * client.http.client.base-path: /api/v3.
     * client.config.http-protocol: https.
     *
     * @param hostName: mnubo's servers.
     * @param securityConsumerKey: unique identity key provided by mnubo.
     * @param securityConsumerSecret: secret key provided by mnubo.
     * @return MnuboSDKClient: mnubo sdk client instance.
     *
     */
    //@formatter:on
    public static MnuboSDKClient getClient(String hostName, String securityConsumerKey, String securityConsumerSecret) {
        return getClient(hostName, securityConsumerKey, securityConsumerSecret, null);
    }


    public static MnuboSDKClient getClientWithToken(String hostName, String token) {
        return getClientWithToken(hostName, token, null);
    }

    public static MnuboSDKClient getClientWithToken(String hostName, String token, ExponentialBackoffConfig exponentialBackoffConfig) {
        MnuboSDKConfig.Builder configBuilder = MnuboSDKConfig.builder();

        notBlank(hostName, "hostname property cannot be empty or null.");
        notBlank(token, "token property cannot be empty or null.");

        configBuilder.withHostName(hostName);
        configBuilder.withToken(token);
        configBuilder.withExponentionalBackoffConfig(exponentialBackoffConfig);

        return generateClients(configBuilder.build());
    }


    public static MnuboSDKClient getClient(String hostName, String securityConsumerKey, String securityConsumerSecret, ExponentialBackoffConfig exponentialBackoffConfig) {
        MnuboSDKConfig.Builder configBuilder = MnuboSDKConfig.builder();

        notBlank(hostName, "hostname property cannot be empty or null.");
        notBlank(securityConsumerKey, "securityConsumerKey property cannot be empty or null.");
        notBlank(securityConsumerSecret, "securityConsumerSecret property cannot be empty or null.");

        configBuilder.withHostName(hostName);
        configBuilder.withSecurityConsumerKey(securityConsumerKey);
        configBuilder.withSecurityConsumerSecret(securityConsumerSecret);
        configBuilder.withExponentionalBackoffConfig(exponentialBackoffConfig);

        return generateClients(configBuilder.build());
    }

    //@formatter:off
    /**
     * returns a mnubo sdk client to send request to mnubo servers from a property
     * instance.
     *
     * This is the list of parameters to set:
     * client.config.hostname: mnubo's server name. For example: "rest.sandbox.mnubo.com".
     * client.security.consumer-key: The unique client identity key.
     * client.security.consumer-secret: The secret key which is used in conjunction with the consumer key to access the mnubo server.
     * client.config.platform-port: mnubo's server port.
     * client.config.autentication-port: Authentication server port.
     * client.http.client.disable-redirect-handling: Disable automatic redirect handling
     * client.http.client.disable-automatic-retries: Disables automatic request recovery and re-execution.
     * client.http.client.disable-content-compression: Disables (gzip) compression of the request content.
     * client.http.client.max-connections-per-route: The maximum number of connections per route.
     * client.http.client.http.client.default-timeout: The number of seconds a session can be idle before it is abandoned.
     * client.http.client.http.client.connect-timeout: This is timeout in seconds until  a connection is established.
     * client.http.client.connection-request-timeout: This is timeout in seconds used when requesting a connection from the connection manager.
     * client.http.client.http.client.socket-timeout: After a connection has been established this is the maximum period inactivity between two consecutive data packets.
     * client.http.client.max-total-connection: This is the maximum number of connections allowed across all routes.
     * client.http.client.base-path: This is the base common path in all request.
     * client.config.http-protocol: Use "http" for unsecure connection and "https" for secure connections.
     *
     * @param properties: property instance.
     * @return MnuboSDKClient: mnubo sdk client instance.
     *
     */
    //@formatter:on
    @Deprecated
    public static MnuboSDKClient getAdvanceClient(Properties properties) {
        MnuboSDKConfig.Builder configBuilder = MnuboSDKConfig.builder();

        validNotNull(properties, "Configuration properties instance");
        if (properties.containsKey(HOST_NAME)) {
            configBuilder.withHostName(properties.getProperty(HOST_NAME));
        }
        if (properties.containsKey(INGESTION_PORT)) {
            configBuilder.withIngestionPort(properties.getProperty(INGESTION_PORT));
        }
        if (properties.containsKey(RESTITUTION_PORT)) {
            configBuilder.withRestitutionPort(properties.getProperty(RESTITUTION_PORT));
        }
        if (properties.containsKey(AUTHENTICATION_PORT)) {
            configBuilder.withAuthenticationPort(properties.getProperty(AUTHENTICATION_PORT));
        }
        if (properties.containsKey(SECURITY_CONSUMER_KEY)) {
            configBuilder.withSecurityConsumerKey(properties.getProperty(SECURITY_CONSUMER_KEY));
        }
        if (properties.containsKey(SECURITY_CONSUMER_SECRET)) {
            configBuilder.withSecurityConsumerSecret(properties.getProperty(SECURITY_CONSUMER_SECRET));
        }
        if (properties.containsKey(HTTP_PROTOCOL)) {
            configBuilder.withHttpProtocol(properties.getProperty(HTTP_PROTOCOL));
        }
        if (properties.containsKey(CLIENT_DISABLE_REDIRECT_HANDLING)) {
            configBuilder.withHttpDisableRedirectHandling(properties.getProperty(CLIENT_DISABLE_REDIRECT_HANDLING));
        }
        if (properties.containsKey(CLIENT_DISABLE_AUTOMATIC_RETRIES)) {
            configBuilder.withHttpDisableAutomaticRetries(properties.getProperty(CLIENT_DISABLE_AUTOMATIC_RETRIES));
        }
        if (properties.containsKey(CLIENT_DISABLE_CONTENT_COMPRESSION)) {
            configBuilder.withHttpDisableContentCompression(properties.getProperty(CLIENT_DISABLE_CONTENT_COMPRESSION));
        }
        if (properties.containsKey(CLIENT_MAX_CONNECTIONS_PER_ROUTE)) {
            configBuilder.withHttpMaxConnectionPerRoute(properties.getProperty(CLIENT_MAX_CONNECTIONS_PER_ROUTE));
        }
        if (properties.containsKey(CLIENT_DEFAULT_TIMEOUT)) {
            configBuilder.withHttpDefaultTimeout(properties.getProperty(CLIENT_DEFAULT_TIMEOUT));
        }
        if (properties.containsKey(CLIENT_CONNECT_TIMEOUT)) {
            configBuilder.withHttpConnectionTimeout(properties.getProperty(CLIENT_CONNECT_TIMEOUT));
        }
        if (properties.containsKey(CLIENT_CONNECTION_REQUEST_TIMEOUT)) {
            configBuilder.withHttpConnectionRequestTimeout(properties.getProperty(CLIENT_CONNECTION_REQUEST_TIMEOUT));
        }
        if (properties.containsKey(CLIENT_SOCKET_TIMEOUT)) {
            configBuilder.withHttpSocketTimeout(properties.getProperty(CLIENT_SOCKET_TIMEOUT));
        }
        if (properties.containsKey(CLIENT_MAX_TOTAL_CONNECTION)) {
            configBuilder.withHttpMaxTotalConnection(properties.getProperty(CLIENT_MAX_TOTAL_CONNECTION));
        }
        if (properties.containsKey(CLIENT_BASE_PATH)) {
            configBuilder.withHttpBasePath(properties.getProperty(CLIENT_BASE_PATH));
        }

        return generateClients(configBuilder.build());
    }

    //@formatter:off
    /**
     * returns a mnubo sdk client to send request to mnubo servers from a InputStream
     * instance.
     *
     * This is the list of parameters to set:
     * client.config.hostname: mnubo's server name. For example: "rest.sandbox.mnubo.com".
     * client.security.consumer-key: The unique client identity key.
     * client.security.consumer-secret: The secret key which is used in conjunction with the consumer key to access the mnubo server.
     * client.config.platform-port: mnubo's server port.
     * client.config.autentication-port: Authentication server port.
     * client.http.client.disable-redirect-handling: Disable automatic redirect handling
     * client.http.client.disable-automatic-retries: Disables automatic request recovery and re-execution.
     * client.http.client.disable-content-compression: Disables (gzip) compression of the request content.
     * client.http.client.max-connections-per-route: The maximum number of connections per route.
     * client.http.client.http.client.default-timeout: The number of seconds a session can be idle before it is abandoned.
     * client.http.client.http.client.connect-timeout: This is timeout in seconds until  a connection is established.
     * client.http.client.connection-request-timeout: This is timeout in seconds used when requesting a connection from the connection manager.
     * client.http.client.http.client.socket-timeout: After a connection has been established this is the maximum period inactivity between two consecutive data packets.
     * client.http.client.max-total-connection: This is the maximum number of connections allowed across all routes.
     * client.http.client.base-path: This is the base common path in all request.
     * client.config.http-protocol: Use "http" for unsecure connection and "https" for secure connections.
     *
     * @param config: InputStream instance.
     * @return MnuboSDKClient: mnubo sdk client instance.
     * @throws IOException if any exception occurs.
     *
     */
    //@formatter:on
    @Deprecated
    public static MnuboSDKClient getAdvanceClient(InputStream config) throws IOException {
        validNotNull(config, "configuration streaming instance");
        Properties properties = new Properties();
        properties.load(config);
        return getAdvanceClient(properties);

    }

    //@formatter:off
    /**
     * returns a mnubo sdk client to send request to mnubo servers from a File instance.
     *
     * This is the list of parameters to set:
     * client.config.hostname: mnubo's server name. For example: "rest.sandbox.mnubo.com".
     * client.security.consumer-key: The unique client identity key.
     * client.security.consumer-secret: The secret key which is used in conjunction with the consumer key to access the mnubo server.
     * client.config.platform-port: mnubo's server port.
     * client.config.autentication-port: Authentication server port.
     * client.http.client.disable-redirect-handling: Disable automatic redirect handling
     * client.http.client.disable-automatic-retries: Disables automatic request recovery and re-execution.
     * client.http.client.disable-content-compression: Disables (gzip) compression of the request content.
     * client.http.client.max-connections-per-route: The maximum number of connections per route.
     * client.http.client.http.client.default-timeout: The number of seconds a session can be idle before it is abandoned.
     * client.http.client.http.client.connect-timeout: This is timeout in seconds until  a connection is established.
     * client.http.client.connection-request-timeout: This is timeout in seconds used when requesting a connection from the connection manager.
     * client.http.client.http.client.socket-timeout: After a connection has been established this is the maximum period inactivity between two consecutive data packets.
     * client.http.client.max-total-connection: This is the maximum number of connections allowed across all routes.
     * client.http.client.base-path: This is the base common path in all request.
     * client.config.http-protocol: Use "http" for unsecure connection and "https" for secure connections.
     *
     * @param configFile: File instance.
     * @return MnuboSDKClient: mnubo sdk client instance.
     * @throws IOException if any exception occurs.
     *
     */
    //@formatter:on
    @Deprecated
    public static MnuboSDKClient getAdvanceClient(File configFile) throws IOException {
        validIsFile(configFile);
        FileInputStream config = new FileInputStream(configFile);
        return getAdvanceClient(config);
    }

    private static MnuboSDKClient generateClients(MnuboSDKConfig config) {

        // configurating custom restTemplate
        RestTemplate restTemplate = new HttpRestTemplate(config).getRestTemplate();

        // credential handler
        CredentialHandler credentials;
        if (!isBlank(config.getToken())) {
            credentials = new StaticCredentialHandler(config.getToken());
        } else if (!isBlank(config.getSecurityConsumerKey()) && !isBlank(config.getSecurityConsumerSecret())) {
            credentials = new ClientSecretCredentialHandler(config, restTemplate);
        } else {
            throw new IllegalArgumentException("You either need a consumer key and consumer secret or a token to initialize");
        }

        return new MnuboSDKClientImpl(config, restTemplate, credentials);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy