securitytools.veracode.VeracodeClient.java Source code

Java tutorial

Introduction

Here is the source code for securitytools.veracode.VeracodeClient.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 Security Tools SDK for Java.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package securitytools.veracode;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import securitytools.common.ClientConfiguration;
import securitytools.common.http.HttpClientFactory;
import securitytools.veracode.http.VeracodeResponseHandlers;
import securitytools.veracode.http.request.BeginPreScanRequest;
import securitytools.veracode.http.request.BeginScanRequest;
import securitytools.veracode.http.request.CreateBuildRequest;
import securitytools.veracode.http.request.DeleteBuildRequest;
import securitytools.veracode.http.request.DownloadDetailedReportPDFRequest;
import securitytools.veracode.http.request.DownloadSummaryReportPDFRequest;
import securitytools.veracode.http.request.DownloadThirdPartyReportPDFRequest;
import securitytools.veracode.http.request.GetApplicationInfoRequest;
import securitytools.veracode.http.request.GetApplicationListRequest;
import securitytools.veracode.http.request.GetBuildInfoRequest;
import securitytools.veracode.http.request.GetBuildListRequest;
import securitytools.veracode.http.request.GetDetailedReportRequest;
import securitytools.veracode.http.request.GetFileListRequest;
import securitytools.veracode.http.request.GetPreScanResultsRequest;
import securitytools.veracode.http.request.UploadFileRequest;
import securitytools.veracode.model.appinfo.ApplicationInfo;
import securitytools.veracode.model.applist.ApplicationList;
import securitytools.veracode.model.buildinfo.BuildInfo;
import securitytools.veracode.model.buildlist.BuildList;
import securitytools.veracode.model.detailedreport.DetailedReport;
import securitytools.veracode.model.filelist.FileList;
import securitytools.veracode.model.prescanresults.PreScanResults;

/**
 * Client for accessing Veracode services.
 *
 * @author Adam Parsons
 * @version 0.0.1, 05/20/14
 * @since 1.0.0
 */
public class VeracodeClient implements Veracode, Closeable {

    private static final Log LOG = LogFactory.getLog(VeracodeClient.class);

    private static final HttpHost TARGET = new HttpHost("analysiscenter.veracode.com", 443, "https");

    private final CloseableHttpClient client;
    private final HttpClientContext context;

    /**
     * Constructs a new VeracodeClient with the specified username and password
     * using the default configuration.
     * 
     * @param username The username credential.
     * @param password The password credential.
     */
    public VeracodeClient(String username, String password) {
        this(new VeracodeCredentials(username, password), new ClientConfiguration());
    }

    /**
     * Constructs a new VeracodeClient with the specified username and password
     * using the specified configuration.
     *     
     * @param username The username credential.
     * @param password The password credential.
     * @param clientConfiguration Client configuration for options such as proxy
     * settings, user-agent header, and network timeouts.
     */
    public VeracodeClient(String username, String password, ClientConfiguration clientConfiguration) {
        this(new VeracodeCredentials(username, password), clientConfiguration);
    }

    /**
     * Constructs a new VeracodeClient using the default configuration.
     *     
     * @param credentials Credentials used to access Veracode services.
     */
    public VeracodeClient(VeracodeCredentials credentials) {
        this(credentials, new ClientConfiguration());
    }

    /**
     * Constructs a new VeracodeClient using the specified configuration.
     *     
     * @param credentials Credentials used to access Veracode services.   
     * @param clientConfiguration Client configuration for options such as proxy
     * settings, user-agent header, and network timeouts.
     */
    public VeracodeClient(VeracodeCredentials credentials, ClientConfiguration clientConfiguration) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(TARGET), credentials);

        AuthCache authCache = new BasicAuthCache();
        authCache.put(TARGET, new BasicScheme());

        context = HttpClientContext.create();
        context.setCredentialsProvider(credentialsProvider);
        context.setAuthCache(authCache);

        try {
            client = HttpClientFactory.build(clientConfiguration);
        } catch (NoSuchAlgorithmException nsae) {
            throw new VeracodeClientException(nsae.getMessage(), nsae);
        }
    }

    @Override
    public void close() throws IOException {
        if (client != null) {
            client.close();
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginPreScan(java.lang.String)
     */
    @Override
    public BuildInfo beginPreScan(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        return beginPreScan(applicationId, false, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginPreScan(java.lang.String, boolean)
     */
    @Override
    public BuildInfo beginPreScan(String applicationId, boolean autoScan)
            throws VeracodeClientException, VeracodeServiceException {
        return beginPreScan(applicationId, autoScan, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginPreScan(java.lang.String, java.lang.String)
     */
    @Override
    public BuildInfo beginPreScan(String applicationId, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        return beginPreScan(applicationId, false, sandboxId);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginPreScan(java.lang.String, boolean, java.lang.String)
     */
    @Override
    public BuildInfo beginPreScan(String applicationId, boolean autoScan, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new BeginPreScanRequest(applicationId, autoScan, sandboxId),
                    VeracodeResponseHandlers.getBuildInfoHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginScan(java.lang.String)
     */
    @Override
    public BuildInfo beginScan(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        return beginScan(applicationId, null, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginScan(java.lang.String, java.lang.String)
     */
    @Override
    public BuildInfo beginScan(String applicationId, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        return beginScan(applicationId, null, sandboxId);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginScan(java.lang.String, java.lang.String[])
     */
    @Override
    public BuildInfo beginScan(String applicationId, String[] modules)
            throws VeracodeClientException, VeracodeServiceException {
        return beginScan(applicationId, modules, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#beginScan(java.lang.String, java.lang.String[], java.lang.String)
     */
    @Override
    public BuildInfo beginScan(String applicationId, String[] modules, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new BeginScanRequest(applicationId, modules, sandboxId),
                    VeracodeResponseHandlers.getBuildInfoHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#createBuild(java.lang.String)
     */
    @Override
    public BuildInfo createBuild(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        return createBuild(applicationId, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#createBuild(java.lang.String, java.lang.String)
     */
    @Override
    public BuildInfo createBuild(String applicationId, String versionName)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new CreateBuildRequest(applicationId, versionName),
                    VeracodeResponseHandlers.getBuildInfoHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#deleteBuild(java.lang.String)
     */
    @Override
    public BuildList deleteBuild(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        return deleteBuild(applicationId, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#deleteBuild(java.lang.String, java.lang.String)
     */
    @Override
    public BuildList deleteBuild(String applicationId, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new DeleteBuildRequest(applicationId, sandboxId),
                    VeracodeResponseHandlers.getBuildListHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#downloadDetailedReportPDF(java.lang.String, java.io.File)
     */
    @Override
    public File downloadDetailedReportPDF(String buildId, File targetFile)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new DownloadDetailedReportPDFRequest(buildId),
                    VeracodeResponseHandlers.getFileHandler(targetFile), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#downloadSummaryReportPDF(java.lang.String, java.io.File)
     */
    @Override
    public File downloadSummaryReportPDF(String buildId, File targetFile)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new DownloadSummaryReportPDFRequest(buildId),
                    VeracodeResponseHandlers.getFileHandler(targetFile), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#downloadThirdPartyReportPDF(java.lang.String, java.io.File)
     */
    @Override
    public File downloadThirdPartyReportPDF(String buildId, File targetFile)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new DownloadThirdPartyReportPDFRequest(buildId),
                    VeracodeResponseHandlers.getFileHandler(targetFile), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getApplicationInfo(java.lang.String)
     */
    @Override
    public ApplicationInfo getApplicationInfo(String applicationId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetApplicationInfoRequest(applicationId),
                    VeracodeResponseHandlers.getApplicationInfoHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getApplicationList()
     */
    @Override
    public ApplicationList getApplicationList() throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetApplicationListRequest(),
                    VeracodeResponseHandlers.getApplicationListHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getBuildInfo(java.lang.String)
     */
    @Override
    public BuildInfo getBuildInfo(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        return getBuildInfo(applicationId, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getBuildInfo(java.lang.String, java.lang.String)
     */
    @Override
    public BuildInfo getBuildInfo(String applicationId, String buildId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetBuildInfoRequest(applicationId, buildId),
                    VeracodeResponseHandlers.getBuildInfoHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getBuildList(java.lang.String)
     */
    @Override
    public BuildList getBuildList(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetBuildListRequest(applicationId),
                    VeracodeResponseHandlers.getBuildListHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getDetailedReport(java.lang.String)
     */
    @Override
    public DetailedReport getDetailedReport(String buildId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetDetailedReportRequest(buildId),
                    VeracodeResponseHandlers.getDetailedReportHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getFileList(java.lang.String)
     */
    @Override
    public FileList getFileList(String applicationId) throws VeracodeClientException, VeracodeServiceException {
        return getFileList(applicationId, null, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getFileList(java.lang.String, java.lang.String)
     */
    @Override
    public FileList getFileList(String applicationId, String buildId)
            throws VeracodeClientException, VeracodeServiceException {
        return getFileList(applicationId, buildId, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getFileList(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public FileList getFileList(String applicationId, String buildId, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetFileListRequest(applicationId, buildId, sandboxId),
                    VeracodeResponseHandlers.getFileListHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getPreScanResults(java.lang.String)
     */
    @Override
    public PreScanResults getPreScanResults(String applicationId)
            throws VeracodeClientException, VeracodeServiceException {
        return getPreScanResults(applicationId, null, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getPreScanResults(java.lang.String, java.lang.String)
     */
    @Override
    public PreScanResults getPreScanResults(String applicationId, String buildId)
            throws VeracodeClientException, VeracodeServiceException {
        return getPreScanResults(applicationId, buildId, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#getPreScanResults(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public PreScanResults getPreScanResults(String applicationId, String buildId, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new GetPreScanResultsRequest(applicationId, buildId, sandboxId),
                    VeracodeResponseHandlers.getPreScanResultsHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#uploadFile(java.lang.String, java.io.File)
     */
    @Override
    public FileList uploadFile(String applicationId, File file)
            throws VeracodeClientException, VeracodeServiceException {
        return uploadFile(applicationId, file, null);
    }

    /* (non-Javadoc)
     * @see securitytools.veracode.Veracode#uploadFile(java.lang.String, java.io.File, java.lang.String)
     */
    @Override
    public FileList uploadFile(String applicationId, File file, String sandboxId)
            throws VeracodeClientException, VeracodeServiceException {
        try {
            return client.execute(TARGET, new UploadFileRequest(applicationId, file, sandboxId),
                    VeracodeResponseHandlers.getFileListHandler(), context);
        } catch (IOException ioe) {
            throw new VeracodeClientException("Unable to complete Veracode request.", ioe);
        }
    }

}