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

org.sourcelab.github.client.ConfigurationBuilder Maven / Gradle / Ivy

The newest version!

package org.sourcelab.github.client;

import org.sourcelab.github.client.exception.BuilderValidationException;
import org.sourcelab.github.client.http.ClientFactory;
import org.sourcelab.github.client.http.DefaultClientFactory;

import java.io.File;

/**
 * Configuration builder for {@see Configuration}.
 */
public final class ConfigurationBuilder {
    // Required Settings
    private String apiToken = null;
    private String apiUrl = "https://api.github.com";
    private ClientFactory clientFactory = new DefaultClientFactory();

    // Optional settings
    private int requestTimeoutInSeconds = 60;
    private int connectionTimeToLiveInSeconds = 300;

    // Optional settings to validate SSL certificate.
    private boolean ignoreInvalidSslCertificates = false;
    private File trustStoreFile = null;
    private String trustStorePassword = null;

    // Optional settings to send a client certificate with request.
    private File keyStoreFile = null;
    private String keyStorePassword = null;

    // Optional Proxy Configuration
    private String proxyHost = null;
    private int proxyPort = 0;
    private String proxyScheme = "HTTP";

    // Optional Proxy Authentication.
    private String proxyUsername = null;
    private String proxyPassword = null;

    /**
     * Constructor.
     */
    public ConfigurationBuilder() {
    }

    /**
     * Set the configured API Token.
     * @param apiToken value to set.
     * @return self.
     */
    public ConfigurationBuilder withApiToken(final String apiToken) {
        this.apiToken = apiToken;
        return this;
    }

    /**
     * Set the configured api url.  Should incldue protocol (IE http:// or https:// ) but no trailing /
     * @param apiUrl value to set.
     * @return self.
     */
    public ConfigurationBuilder withApiUrl(final String apiUrl) {
        this.apiUrl = apiUrl;
        return this;
    }

    /**
     * Skip all validation of SSL Certificates.  This is insecure and highly discouraged!
     *
     * @return Configuration instance.
     */
    public ConfigurationBuilder useInsecureSslCertificates() {
        this.ignoreInvalidSslCertificates = true;
        return this;
    }

    /**
     * Override the underlying http org.sourcelab.github.client library.
     * @param clientFactory Supply your own Client Factory implementation.
     * @return self.
     */
    public ConfigurationBuilder withClientFactory(final ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        return this;
    }

    public ConfigurationBuilder withRequestTimeoutInSeconds(final int requestTimeoutInSeconds) {
        this.requestTimeoutInSeconds = requestTimeoutInSeconds;
        return this;
    }

    public ConfigurationBuilder withConnectionTimeToLiveInSeconds(final int connectionTimeToLiveInSeconds) {
        this.connectionTimeToLiveInSeconds = connectionTimeToLiveInSeconds;
        return this;
    }

    public ConfigurationBuilder withIgnoreInvalidSslCertificates(final boolean ignoreInvalidSslCertificates) {
        this.ignoreInvalidSslCertificates = ignoreInvalidSslCertificates;
        return this;
    }

    public ConfigurationBuilder withTrustStoreFile(final File trustStoreFile) {
        this.trustStoreFile = trustStoreFile;
        return this;
    }

    public ConfigurationBuilder withTrustStorePassword(final String trustStorePassword) {
        this.trustStorePassword = trustStorePassword;
        return this;
    }

    public ConfigurationBuilder withKeyStoreFile(final File keyStoreFile) {
        this.keyStoreFile = keyStoreFile;
        return this;
    }

    public ConfigurationBuilder withKeyStorePassword(final String keyStorePassword) {
        this.keyStorePassword = keyStorePassword;
        return this;
    }

    public ConfigurationBuilder withProxyHost(final String proxyHost) {
        this.proxyHost = proxyHost;
        return this;
    }

    public ConfigurationBuilder withProxyPort(final int proxyPort) {
        this.proxyPort = proxyPort;
        return this;
    }

    public ConfigurationBuilder withProxyScheme(final String proxyScheme) {
        this.proxyScheme = proxyScheme;
        return this;
    }

    public ConfigurationBuilder withProxyUsername(final String proxyUsername) {
        this.proxyUsername = proxyUsername;
        return this;
    }

    public ConfigurationBuilder withProxyPassword(final String proxyPassword) {
        this.proxyPassword = proxyPassword;
        return this;
    }

    /**
     * Validates that the supplied values are correct.
     * @throws BuilderValidationException if not valid or complete.
     */
    private void validate() {
        if (apiToken == null || apiToken.trim().isEmpty()) {
            throw new BuilderValidationException("The 'ApiToken' property must be configured.");
        }
        if (clientFactory == null) {
            throw new BuilderValidationException("The 'ClientFactory' property must be configured.");
        }
        if (apiUrl == null || apiUrl.trim().isEmpty()) {
            throw new BuilderValidationException("The 'ApiUrl' property must be configured.");
        }
    }

    /**
     * Create new Configuration instance from set values.
     * @return Configuration instance.
     * @throws IllegalStateException if improper values defined.
     */
    public Configuration build() {
        validate();
        return new Configuration(
            // Required settings
            apiToken,
            apiUrl,
            // Optional settings
            requestTimeoutInSeconds,
            connectionTimeToLiveInSeconds,
            trustStoreFile,
            trustStorePassword,
            proxyHost,
            proxyScheme,
            proxyPassword,
            ignoreInvalidSslCertificates,
            keyStoreFile,
            keyStorePassword,
            proxyPort,
            proxyUsername,
            clientFactory
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy