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

io.signpath.signpathclient.SignPathClient Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package io.signpath.signpathclient;

import io.signpath.signpathclient.api.http.SignPathApiHttpClient;
import java.io.File;
import org.apache.commons.lang.Validate;
import io.signpath.signpathclient.api.model.SigningRequest;
import io.signpath.signpathclient.api.model.SigningRequestStatus;
import java.io.PrintStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author robert.benak
 */
public class SignPathClient {

    private final PrintStream logger;
    private final ClientSettings clientSettings;
    private final SignPathApiHttpClient signPathApiHttpClient;

    public SignPathClient(String signPathApiBaseUrl, PrintStream logger, ClientSettings clientSettings) {

        this.logger = logger;
        this.clientSettings = clientSettings;
        signPathApiHttpClient = new SignPathApiHttpClient(signPathApiBaseUrl, logger, clientSettings);
    }

    public SignPathClient(String signPathApiBaseUrl, PrintStream logger) {
        this(signPathApiBaseUrl, logger, new ClientSettings(
                (int) TimeUnit.MINUTES.toSeconds(10), // serviceUnavailableTimeoutInSeconds
                (int) TimeUnit.MINUTES.toSeconds(5), // uploadAndDownloadRequestTimeoutInSeconds
                (int) TimeUnit.MINUTES.toSeconds(10), // waitForCompletionTimeoutInSeconds
                (int) TimeUnit.SECONDS.toSeconds(30), // waitBetweenReadinessChecksInSeconds
                SignPathClient.BuildUserAgent()
        ));
    }
    
    public static String BuildUserAgent(){
        return String.format("SignPathJavaClient/%1$s (OpenJDK %2$s)",
                SignPathClient.class.getPackage().getImplementationVersion(), System.getProperty("java.version"));
    }

    /**
     *
     * @param apiToken
     * @param organizationId
     * @param tbsToken
     * @param artifact
     * @param projectSlug
     * @param signingPolicySlug
     * @param artifactConfigurationSlug
     * @param description
     * @param origin
     * @return Signing Request Id
     */
    public String submitSigningRequest(
            String apiToken,
            String tbsToken,
            String organizationId,
            File artifact,
            String projectSlug,
            String signingPolicySlug,
            String artifactConfigurationSlug,
            String description,
            Map origin) throws SignPathClientException {

        Validate.notEmpty(apiToken);
        //Validate.notEmpty(tbsToken);
        Validate.notEmpty(organizationId);
        Validate.notNull(artifact);
        Validate.notEmpty(projectSlug);
        Validate.notEmpty(signingPolicySlug);
        
        this.log(String.format("[PARAM] serviceUnavailableTimeoutInSeconds: %s", this.clientSettings.getServiceUnavailableTimeoutInSeconds()));
        this.log(String.format("[PARAM] uploadAndDownloadRequestTimeoutInSeconds: %s", this.clientSettings.getUploadAndDownloadRequestTimeoutInSeconds()));
        this.log(String.format("[PARAM] waitForCompletionTimeoutInSeconds: %s", this.clientSettings.getWaitForCompletionTimeoutInSeconds()));
        
        this.log("The signing request is being submitted...");
        String signingRequestId = signPathApiHttpClient.submitSigningRequestHttp(apiToken, tbsToken, organizationId, artifact, projectSlug, signingPolicySlug, artifactConfigurationSlug, description, origin);
        this.log("The signing request has been successfully submitted");
        this.log(String.format("The signing request id: %s", signingRequestId));
        return signingRequestId;
    }

    /**
     *
     * @param apiToken
     * @param tbsToken
     * @param organizationId
     * @param artifact
     * @param projectSlug
     * @param signingPolicySlug
     * @param artifactConfigurationSlug
     * @param artifactTargetFile
     * @param description
     * @param origin
     * @return
     * @throws java.lang.InterruptedException
     */
    public String submitSigningRequestAndWaitForSignedArtifact(
            String apiToken,
            String tbsToken,
            String organizationId,
            File artifact,
            String projectSlug,
            String signingPolicySlug,
            String artifactConfigurationSlug,
            File artifactTargetFile,
            String description,
            Map origin) throws SignPathClientException, InterruptedException {

        Validate.notEmpty(apiToken);
        Validate.notEmpty(tbsToken);
        Validate.notEmpty(organizationId);
        Validate.notNull(artifact);
        Validate.notEmpty(projectSlug);
        Validate.notNull(artifactTargetFile);
        Validate.notEmpty(signingPolicySlug);

        String signingRequestId = this.submitSigningRequest(apiToken, tbsToken, organizationId, artifact, projectSlug, signingPolicySlug, artifactConfigurationSlug, description, origin);
        SigningRequest signingRequest = this.getSigningRequestWaitForFinalStatus(apiToken, tbsToken, organizationId, signingRequestId);
        SigningRequestStatus signingRequestStatus = signingRequest.getStatus();
        if (signingRequestStatus == SigningRequestStatus.Completed) {
            this.log("Loading signed artifact...");
            // request is now approved and we can download the artifacts
            this.downloadSignedArtifact(apiToken, tbsToken, organizationId, signingRequestId, artifactTargetFile);
        }

        return signingRequestId;
    }

    /**
     * @param apiToken
     * @param tbsToken
     * @param organizationId
     * @param signingRequestId
     * @return
     */
    public SigningRequest getSigningRequest(
            String apiToken,
            String tbsToken,
            String organizationId,
            String signingRequestId) {

        Validate.notEmpty(apiToken);
        Validate.notEmpty(tbsToken);
        Validate.notEmpty(organizationId);
        Validate.notEmpty(signingRequestId);

        SigningRequest signingRequest = signPathApiHttpClient.getSigningRequestHttp(apiToken, tbsToken, organizationId, signingRequestId);
        return signingRequest;
    }

    /**
     * @param apiToken
     * @param organizationId
     * @param tbsToken
     * @param signingRequestId
     * @return
     */
    public SigningRequest getSigningRequestWaitForFinalStatus(
            String apiToken,
            String tbsToken,
            String organizationId,
            String signingRequestId) throws SignPathClientException {

        Validate.notEmpty(apiToken);
        Validate.notEmpty(tbsToken);
        Validate.notEmpty(organizationId);
        Validate.notEmpty(signingRequestId);

        long startTime = System.currentTimeMillis();

        this.log("Checking the signing request status...");
        SigningRequest signingRequest = this.getSigningRequest(apiToken, tbsToken, organizationId, signingRequestId);
        int waitForCompletionTimeoutInSeconds = this.clientSettings.getWaitForCompletionTimeoutInSeconds();
        while (!signingRequest.isFinalStatus() && System.currentTimeMillis() < startTime + waitForCompletionTimeoutInSeconds * 1000) {
            try {
                Thread.sleep(this.clientSettings.getWaitBetweenReadinessChecksInSeconds() * 1000);
            } catch (InterruptedException ex) {
                throw new SignPathClientException("The waiting thread was interrupted");
            }
            this.log("The signing request is not yet completed. Please complete the signing request in SignPath in order to proceed.");
            signingRequest = this.getSigningRequest(apiToken, tbsToken, organizationId, signingRequestId);
        }

        SigningRequestStatus signingRequestStatus = signingRequest.getStatus();
        if (signingRequestStatus != SigningRequestStatus.Completed) {
            // we were waiting for a while, but request is still not completed 
            switch (signingRequestStatus) {
                case Denied:
                    throw new SignPathClientException("Signing request was denied");
                case Canceled:
                    throw new SignPathClientException("Signing request was canceled");
                case Failed:
                    throw new SignPathClientException("Signing request failed");
                case InProgress:
                case WaitingForApproval:
                    throw new SignPathClientException("Timeout expired while waiting for signing request to complete");
                default:
                    throw new SignPathClientException(
                            String.format("Terminating because of unexpected signing request status: %s", signingRequestStatus));
            }
        }

        this.log("The signing request is completed");
        return signingRequest;
    }

    /**
     *
     * @param apiToken
     * @param tbsToken
     * @param organizationId
     * @param signingRequestId
     * @param artifactTargetFile
     * @throws SignPathClientException
     */
    public void downloadSignedArtifact(
            String apiToken,
            String tbsToken,
            String organizationId,
            String signingRequestId,
            File artifactTargetFile) throws SignPathClientException {

        Validate.notEmpty(apiToken);
        Validate.notEmpty(tbsToken);
        Validate.notEmpty(organizationId);
        Validate.notEmpty(signingRequestId);
        Validate.notNull(artifactTargetFile);

        signPathApiHttpClient.downloadSignedArtifactHttp(apiToken, tbsToken, organizationId, signingRequestId, artifactTargetFile);
    }

    /**
     * 
     * @param message 
     */
    private void log(String message) {
        if (this.logger != null) {
            this.logger.println(message);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy