Search in sources :

Example 16 with BlackDuckIntegrationException

use of com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException in project synopsys-detect by blackducksoftware.

the class RapidScanUploadService method uploadFiles.

private HttpUrl uploadFiles(UploadTarget uploadTarget, List<BdioFileContent> bdioFiles, RapidScanOptions rapidScanOptions, @Nullable NameVersion nameVersion, @Nullable File rapidScanConfig, @Nullable File rapidScanWorkingDirectory) throws IntegrationException, IOException {
    if (bdioFiles.isEmpty()) {
        throw new IllegalArgumentException("BDIO files cannot be empty.");
    }
    BdioFileContent header = bdioFiles.stream().filter(content -> content.getFileName().equals(FILE_NAME_BDIO_HEADER_JSONLD)).findFirst().orElseThrow(() -> new BlackDuckIntegrationException("Cannot find BDIO header file" + FILE_NAME_BDIO_HEADER_JSONLD + "."));
    List<BdioFileContent> remainingFiles = bdioFiles.stream().filter(content -> !content.getFileName().equals(FILE_NAME_BDIO_HEADER_JSONLD)).collect(Collectors.toList());
    int count = remainingFiles.size();
    logger.debug("BDIO upload file count = " + count);
    BlackDuckRequestBuilderEditor editor = builder -> {
        builder.addHeader(RapidCompareMode.HEADER_NAME, rapidScanOptions.getCompareMode().getHeaderValue());
        if (nameVersion != null) {
            builder.addHeader(Bdio2StreamUploader.PROJECT_NAME_HEADER, nameVersion.getName()).addHeader(Bdio2StreamUploader.VERSION_NAME_HEADER, nameVersion.getVersion());
        }
    };
    HttpUrl url;
    if (rapidScanConfig != null) {
        url = bdio2Uploader.startWithConfig(zip(uploadTarget, rapidScanConfig, header, rapidScanWorkingDirectory), editor);
    } else {
        url = bdio2Uploader.start(header, editor);
    }
    for (BdioFileContent content : remainingFiles) {
        bdio2Uploader.append(url, count, content, editor);
    }
    bdio2Uploader.finish(url, count, editor);
    return url;
}
Also used : FileNameUtils(org.apache.commons.compress.utils.FileNameUtils) IntegrationException(com.synopsys.integration.exception.IntegrationException) DataService(com.synopsys.integration.blackduck.service.DataService) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) HashMap(java.util.HashMap) IntLogger(com.synopsys.integration.log.IntLogger) HttpUrl(com.synopsys.integration.rest.HttpUrl) NameVersion(com.synopsys.integration.util.NameVersion) RapidScanOptions(com.synopsys.integration.detect.workflow.blackduck.developer.RapidScanOptions) Charset(java.nio.charset.Charset) Map(java.util.Map) BdioFileContent(com.synopsys.integration.blackduck.bdio2.model.BdioFileContent) Path(java.nio.file.Path) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) RapidCompareMode(com.synopsys.integration.detect.configuration.enumeration.RapidCompareMode) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) Bdio2ContentExtractor(com.synopsys.integration.blackduck.bdio2.util.Bdio2ContentExtractor) List(java.util.List) BlackDuckRequestBuilderEditor(com.synopsys.integration.blackduck.service.request.BlackDuckRequestBuilderEditor) Bdio2StreamUploader(com.synopsys.integration.blackduck.bdio2.Bdio2StreamUploader) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) DetectZipUtil(com.synopsys.integration.detect.util.DetectZipUtil) UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) BlackDuckRequestBuilderEditor(com.synopsys.integration.blackduck.service.request.BlackDuckRequestBuilderEditor) BdioFileContent(com.synopsys.integration.blackduck.bdio2.model.BdioFileContent) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 17 with BlackDuckIntegrationException

use of com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException in project synopsys-detect by blackducksoftware.

the class ReportService method createReportPdfFile.

public File createReportPdfFile(File outputDirectory, ReportData reportData, FontLoader fontLoader, FontLoader boldFontLoader) throws BlackDuckIntegrationException {
    try {
        logger.trace("Creating Risk Report Pdf in : " + outputDirectory.getCanonicalPath());
        RiskReportPdfWriter writer = new RiskReportPdfWriter(logger, fontLoader, boldFontLoader, Color.BLACK, 10.0f);
        File pdfFile = writer.createPDFReportFile(outputDirectory, reportData);
        logger.trace("Created Risk Report Pdf : " + pdfFile.getCanonicalPath());
        return pdfFile;
    } catch (RiskReportException | IOException e) {
        throw new BlackDuckIntegrationException(e.getMessage(), e);
    }
}
Also used : RiskReportPdfWriter(com.synopsys.integration.detect.workflow.blackduck.report.pdf.RiskReportPdfWriter) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) IOException(java.io.IOException) File(java.io.File)

Example 18 with BlackDuckIntegrationException

use of com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException in project synopsys-detect by blackducksoftware.

the class ReportService method isReportFinishedGenerating.

/**
 * Checks the report URL every 5 seconds until the report has a finished time available, then we know it is done being generated. Throws BlackDuckIntegrationException after 30 minutes if the report has not been generated yet.
 */
public ProjectVersionReportView isReportFinishedGenerating(HttpUrl reportUrl) throws InterruptedException, IntegrationException {
    long startTime = System.currentTimeMillis();
    long elapsedTime = 0;
    Date timeFinished = null;
    ProjectVersionReportView reportInfo = null;
    while (timeFinished == null) {
        reportInfo = blackDuckApiClient.getResponse(reportUrl, ProjectVersionReportView.class);
        timeFinished = reportInfo.getFinishedAt();
        if (timeFinished != null) {
            break;
        }
        if (elapsedTime >= timeoutInMilliseconds) {
            String formattedTime = String.format("%d minutes", TimeUnit.MILLISECONDS.toMinutes(timeoutInMilliseconds));
            throw new BlackDuckIntegrationException("The Report has not finished generating in : " + formattedTime);
        }
        // Retry every 5 seconds
        Thread.sleep(5000);
        elapsedTime = System.currentTimeMillis() - startTime;
    }
    return reportInfo;
}
Also used : ProjectVersionReportView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionReportView) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) Date(java.util.Date)

Example 19 with BlackDuckIntegrationException

use of com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException in project synopsys-detect by blackducksoftware.

the class ReportService method generateBlackDuckNoticesReport.

/**
 * Assumes the BOM has already been updated
 */
public String generateBlackDuckNoticesReport(ProjectVersionView version, ReportFormatType reportFormat) throws InterruptedException, IntegrationException {
    if (version.hasLink(ProjectVersionView.LICENSE_REPORTS_LINK)) {
        try {
            logger.debug("Starting the Notices Report generation.");
            HttpUrl reportUrl = startGeneratingBlackDuckNoticesReport(version, reportFormat);
            logger.debug("Waiting for the Notices Report to complete.");
            ProjectVersionReportView reportInfo = isReportFinishedGenerating(reportUrl);
            HttpUrl contentUrl = reportInfo.getFirstLink(ReportView.CONTENT_LINK);
            if (contentUrl == null) {
                throw new BlackDuckIntegrationException("Could not find content link for the report at : " + reportUrl);
            }
            logger.debug("Getting the Notices Report content.");
            String noticesReport = getNoticesReportContent(contentUrl);
            logger.debug("Finished retrieving the Notices Report.");
            logger.debug("Cleaning up the Notices Report on the server.");
            deleteBlackDuckReport(reportUrl);
            return noticesReport;
        } catch (IntegrationRestException e) {
            if (e.getHttpStatusCode() == 402) {
                // unlike the policy module, the licenseReports link is still present when the module is not enabled
                logger.warn("Can not create the notice report, the Black Duck notice module is not enabled.");
            } else {
                throw e;
            }
        }
    } else {
        logger.warn("Can not create the notice report, the Black Duck notice module is not enabled.");
    }
    return null;
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) ProjectVersionReportView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionReportView) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 20 with BlackDuckIntegrationException

use of com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException in project synopsys-detect by blackducksoftware.

the class ReportService method createNoticesReportFile.

private File createNoticesReportFile(File outputDirectory, String noticesReportContent, String projectName, String projectVersionName) throws BlackDuckIntegrationException {
    if (noticesReportContent == null) {
        return null;
    }
    String escapedProjectName = escapeUtil.replaceWithUnderscore(projectName);
    String escapedProjectVersionName = escapeUtil.replaceWithUnderscore(projectVersionName);
    File noticesReportFile = new File(outputDirectory, escapedProjectName + "_" + escapedProjectVersionName + "_Black_Duck_Notices_Report.txt");
    if (noticesReportFile.exists()) {
        boolean deleted = noticesReportFile.delete();
        if (!deleted) {
            logger.warn(String.format("Unable to delete existing file %s before re-creating it", noticesReportFile.getAbsolutePath()));
        }
    }
    try (FileWriter writer = new FileWriter(noticesReportFile)) {
        logger.trace("Creating Notices Report in : " + outputDirectory.getCanonicalPath());
        writer.write(noticesReportContent);
        logger.trace("Created Notices Report : " + noticesReportFile.getCanonicalPath());
        return noticesReportFile;
    } catch (IOException e) {
        throw new BlackDuckIntegrationException(e.getMessage(), e);
    }
}
Also used : BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Aggregations

BlackDuckIntegrationException (com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException)29 File (java.io.File)8 HttpUrl (com.synopsys.integration.rest.HttpUrl)7 ArrayList (java.util.ArrayList)7 IntegrationException (com.synopsys.integration.exception.IntegrationException)6 IOException (java.io.IOException)6 Future (java.util.concurrent.Future)5 UploadBatchOutput (com.synopsys.integration.blackduck.codelocation.upload.UploadBatchOutput)3 UploadOutput (com.synopsys.integration.blackduck.codelocation.upload.UploadOutput)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 ApiDiscovery (com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery)2 ProjectVersionReportView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionReportView)2 BdioFileContent (com.synopsys.integration.blackduck.bdio2.model.BdioFileContent)2 Bdio2ContentExtractor (com.synopsys.integration.blackduck.bdio2.util.Bdio2ContentExtractor)2 UploadTarget (com.synopsys.integration.blackduck.codelocation.upload.UploadTarget)2 BlackDuckPageResponse (com.synopsys.integration.blackduck.http.BlackDuckPageResponse)2 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)2 DataService (com.synopsys.integration.blackduck.service.DataService)2 BlackDuckRequestBuilderEditor (com.synopsys.integration.blackduck.service.request.BlackDuckRequestBuilderEditor)2 IntLogger (com.synopsys.integration.log.IntLogger)2