Search in sources :

Example 1 with TemporaryException

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

the class TestsServiceImpl method isTestsResultRelevant.

public boolean isTestsResultRelevant(String jobId, String rootJobId) {
    String serverCiId = configurer.octaneConfiguration.getInstanceId();
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job CI ID MUST NOT be null nor empty");
    }
    boolean base64 = isEncodeBase64();
    String jobIdEncoded = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodePathParam(jobId);
    String rootJobIdEncoded = base64 ? CIPluginSDKUtils.urlEncodeBase64(rootJobId) : CIPluginSDKUtils.urlEncodeQueryParam(rootJobId);
    String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "servers/" + CIPluginSDKUtils.urlEncodePathParam(serverCiId) + "/jobs/" + jobIdEncoded + "/tests-result-preflight";
    if (rootJobId != null && !rootJobId.isEmpty()) {
        url += "?rootJobId=" + rootJobIdEncoded;
    }
    if (base64) {
        url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        logger.info("Using base64, " + url);
    }
    OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setTimeoutSec(60).setUrl(url);
    try {
        OctaneResponse response = restService.obtainOctaneRestClient().execute(preflightRequest);
        if (response.getStatus() == HttpStatus.SC_OK) {
            return String.valueOf(true).equals(response.getBody());
        } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
            throw new TemporaryException("preflight request failed with status " + response.getStatus());
        } else if (response.getStatus() == HttpStatus.SC_UNAUTHORIZED || response.getStatus() == HttpStatus.SC_FORBIDDEN) {
            CIPluginSDKUtils.doWait(30000);
            throw new PermanentException("preflight request failed with status " + response.getStatus());
        } else {
            throw new PermanentException("preflight request failed with status " + response.getStatus() + ". JobId: '" + jobId + "'. Request URL : " + url);
        }
    } catch (InterruptedIOException ie) {
        throw new RequestTimeoutException("!!!!!!!!!!!!!!!!!!! request timeout during preflight : " + ie.getClass().getCanonicalName() + " - " + ie.getMessage());
    } catch (IOException ioe) {
        throw new TemporaryException(ioe);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) InterruptedIOException(java.io.InterruptedIOException) RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 2 with TemporaryException

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

the class SSCProjectConnector method sendGetEntity.

private String sendGetEntity(String urlSuffix) {
    String url = sscProjectConfiguration.getSSCUrl() + "/api/v1/" + urlSuffix;
    CloseableHttpResponse response = sscRestClient.sendGetRequest(sscProjectConfiguration, url);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
        throw new TemporaryException("SSC Server is not available:" + response.getStatusLine().getStatusCode());
    } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new PermanentException("Error from SSC:" + response.getStatusLine().getStatusCode());
    }
    try {
        return CIPluginSDKUtils.inputStreamToUTF8String(response.getEntity().getContent());
    } catch (IOException e) {
        throw new PermanentException(e);
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
        HttpClientUtils.closeQuietly(response);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 3 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException 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 4 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException 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 5 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException 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)17 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)17 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)10 IOException (java.io.IOException)10 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)7 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)4 InputStream (java.io.InputStream)3 InterruptedIOException (java.io.InterruptedIOException)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpGet (org.apache.http.client.methods.HttpGet)3 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BuildCoverage (com.hp.octane.integrations.dto.coverage.BuildCoverage)1 CIEvent (com.hp.octane.integrations.dto.events.CIEvent)1 CIEventsList (com.hp.octane.integrations.dto.events.CIEventsList)1 CIServerInfo (com.hp.octane.integrations.dto.general.CIServerInfo)1 Scan (com.hp.octane.integrations.services.vulnerabilities.fod.dto.pojos.Scan)1