Search in sources :

Example 6 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class IssuesFileSerializer method serializeIssues.

public static InputStream serializeIssues(List<OctaneIssue> octaneIssues) {
    try {
        Map<String, List<OctaneIssue>> dataFormat = new HashMap<>();
        dataFormat.put("data", octaneIssues);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mapper.writeValue(baos, dataFormat);
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        return is;
    } catch (Exception e) {
        throw new PermanentException(e);
    }
}
Also used : HashMap(java.util.HashMap) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneSDKGeneralException(com.hp.octane.integrations.exceptions.OctaneSDKGeneralException)

Example 7 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class IssuesFileSerializer method cacheIssues.

public static void cacheIssues(String targetDir, List<OctaneIssue> octaneIssues) {
    try {
        if (targetDir != null) {
            validateFolderExists(targetDir);
            Map<String, List<OctaneIssue>> dataFormat = new HashMap<>();
            dataFormat.put("data", octaneIssues);
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            // send to cache
            String vulnerabilitiesScanFilePath = targetDir + File.separator + SSCHandler.SCAN_RESULT_FILE;
            PrintWriter fw = new PrintWriter(vulnerabilitiesScanFilePath, "UTF-8");
            mapper.writeValue(fw, dataFormat);
            fw.flush();
            fw.close();
        }
    } catch (Exception e) {
        throw new PermanentException(e);
    }
}
Also used : HashMap(java.util.HashMap) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneSDKGeneralException(com.hp.octane.integrations.exceptions.OctaneSDKGeneralException)

Example 8 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class VulnerabilitiesServiceImpl method pushVulnerabilities.

private void pushVulnerabilities(InputStream vulnerabilities, String jobId, String buildId) throws IOException {
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(RestService.CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    boolean base64 = isEncodeBase64();
    String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodePathParam(jobId);
    String encodedBuildId = CIPluginSDKUtils.urlEncodePathParam(buildId);
    String url = getVulnerabilitiesContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "?instance-id=" + configurer.octaneConfiguration.getInstanceId() + "&job-ci-id=" + encodedJobId + "&build-ci-id=" + encodedBuildId;
    if (base64) {
        url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
    }
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setUrl(url).setHeaders(headers).setBody(vulnerabilities);
    OctaneResponse response = octaneRestClient.execute(request);
    logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities pushed; status: " + response.getStatus() + ", response: " + response.getBody());
    if (response.getStatus() == HttpStatus.SC_ACCEPTED) {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities push SUCCEED for " + jobId + " #" + buildId);
    } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
        throw new TemporaryException("vulnerabilities push FAILED, service unavailable");
    } else {
        throw new PermanentException("vulnerabilities push FAILED, status " + response.getStatus() + "; dropping this item from the queue \n" + response.getBody());
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Example 9 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class VulnerabilitiesServiceImpl method vulnerabilitiesPreflightRequest.

private Date vulnerabilitiesPreflightRequest(String jobId, String buildId) throws IOException {
    OctaneResponse response = getBaselineDateFromOctane(jobId, buildId);
    if (response.getStatus() == HttpStatus.SC_OK) {
        if (response.getBody() == null || "".equals(response.getBody())) {
            logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities data of " + jobId + " #" + buildId + " is not relevant to Octane");
            return null;
        } else {
            logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities data of " + jobId + " #" + buildId + " found to be relevant to Octane");
            boolean forTest = false;
            // backward compatibility with Octane
            if ("true".equals(response.getBody()) || forTest) {
                return DateUtils.getDateFromUTCString("2000-01-01", "yyyy-MM-dd");
            }
            return DateUtils.getDateFromUTCString(response.getBody(), DateUtils.octaneFormat);
        }
    }
    if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
        throw new TemporaryException("vulnerabilities preflight request FAILED, service unavailable");
    } else {
        throw new PermanentException("vulnerabilities preflight request FAILED with " + response.getStatus() + "");
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse)

Example 10 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class SSCRestClientImpl method sendGetRequest.

@Override
public CloseableHttpResponse sendGetRequest(SSCProjectConfiguration sscProjectConfiguration, String url) {
    HttpGet request = new HttpGet(url);
    request.addHeader("Authorization", "FortifyToken " + getToken(sscProjectConfiguration, false));
    request.addHeader("Accept", "application/json");
    request.addHeader("Host", getNetHost(sscProjectConfiguration.getSSCUrl()));
    CloseableHttpResponse response;
    try {
        response = httpClient.execute(request);
        // 401. Access..
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            request.removeHeaders("Authorization");
            request.addHeader("Authorization", "FortifyToken " + getToken(sscProjectConfiguration, true));
            response = httpClient.execute(request);
        }
        return response;
    } catch (IOException e) {
        throw new TemporaryException(e);
    } catch (Exception e) {
        throw new PermanentException(e);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HttpGet(org.apache.http.client.methods.HttpGet) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException)

Aggregations

PermanentException (com.hp.octane.integrations.exceptions.PermanentException)31 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)19 IOException (java.io.IOException)15 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)10 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)8 InputStream (java.io.InputStream)6 URISyntaxException (java.net.URISyntaxException)6 URIBuilder (org.apache.http.client.utils.URIBuilder)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)5 HashMap (java.util.HashMap)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 InterruptedIOException (java.io.InterruptedIOException)4 HttpGet (org.apache.http.client.methods.HttpGet)4 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)3 LinkedHashMap (java.util.LinkedHashMap)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 OctaneSDKGeneralException (com.hp.octane.integrations.exceptions.OctaneSDKGeneralException)2 List (java.util.List)2 HttpEntity (org.apache.http.HttpEntity)2