Search in sources :

Example 6 with TemporaryException

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

the class SonarUtils method getDataFromSonar.

public static InputStream getDataFromSonar(String projectKey, String token, URIBuilder uriQuery) {
    StringBuilder errorMessage = new StringBuilder().append("failed to get data from sonar for project key: ").append(projectKey);
    try {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(uriQuery.build());
        setTokenInHttpRequest(request, token);
        HttpResponse httpResponse = httpClient.execute(request);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            return httpResponse.getEntity().getContent();
        } else if (statusCode == HttpStatus.SC_BAD_REQUEST) {
            errorMessage.append(" with status code: ").append(statusCode).append(" and response body: ").append(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
            throw new TemporaryException(errorMessage.toString());
        } else {
            errorMessage.append(" with status code: ").append(statusCode).append(" and response body: ").append(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
            throw new PermanentException(errorMessage.toString());
        }
    } catch (HttpHostConnectException e) {
        throw new TemporaryException(errorMessage.toString(), e);
    } catch (Exception e) {
        throw new PermanentException(errorMessage.toString(), e);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) HttpResponse(org.apache.http.HttpResponse) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) AuthenticationException(org.apache.http.auth.AuthenticationException)

Example 7 with TemporaryException

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

the class EventsServiceImpl method sendEventsData.

private void sendEventsData(CIEventsList eventsList, String correlationId) {
    Map<String, String> headers = new HashMap<>();
    headers.put(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(CORRELATION_ID_HEADER, correlationId);
    OctaneRequest octaneRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(configurer.octaneConfiguration.getUrl() + SHARED_SPACE_INTERNAL_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + ANALYTICS_CI_PATH_PART + "events?ci_server_identity=" + configurer.octaneConfiguration.getInstanceId()).setHeaders(headers).setTimeoutSec(60).setBody(dtoFactory.dtoToJsonStream(eventsList));
    OctaneResponse octaneResponse;
    try {
        octaneResponse = restService.obtainOctaneRestClient().execute(octaneRequest);
    } catch (InterruptedIOException ie) {
        String msg = "!!!!!!!!!!!!!!!!!!! request timeout" + ie.getClass().getCanonicalName() + " - " + ie.getMessage();
        throw new RequestTimeoutException(msg);
    } catch (IOException ioe) {
        throw new TemporaryException(ioe);
    }
    if (octaneResponse.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || octaneResponse.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
        throw new TemporaryException("PUT events failed with status " + octaneResponse.getStatus());
    } else if (octaneResponse.getStatus() == HttpStatus.SC_UNAUTHORIZED || octaneResponse.getStatus() == HttpStatus.SC_FORBIDDEN) {
        CIPluginSDKUtils.doWait(30000);
        throw new PermanentException("PUT events failed with status " + octaneResponse.getStatus());
    } else if (octaneResponse.getStatus() != HttpStatus.SC_OK) {
        if (CIPluginSDKUtils.isServiceTemporaryUnavailable(octaneResponse.getBody())) {
            throw new TemporaryException("Saas service is temporary unavailable.");
        }
        throw new PermanentException("PUT events failed with status " + octaneResponse.getStatus());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) 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 8 with TemporaryException

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

the class LogsServiceImpl method preflightRequest.

private String[] preflightRequest(OctaneConfiguration octaneConfiguration, String encodedServerId, String encodedJobId, String encodedRootJobId, boolean base64) {
    String[] result = new String[0];
    OctaneResponse response;
    // get result
    String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), octaneConfiguration.getSharedSpace()) + "servers/" + encodedServerId + "/jobs/" + encodedJobId + "/workspaceId";
    if (encodedRootJobId != null && !encodedRootJobId.isEmpty()) {
        url += "?rootJobId=" + encodedRootJobId;
    }
    if (base64) {
        url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
    }
    try {
        OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
        response = restService.obtainOctaneRestClient().execute(preflightRequest);
        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 if (response.getStatus() != HttpStatus.SC_OK && response.getStatus() != HttpStatus.SC_NO_CONTENT) {
            throw new PermanentException("preflight request failed with status " + response.getStatus() + ". JobId: '" + encodedJobId + "'. Request URL : " + url);
        }
    } catch (IOException ioe) {
        throw new TemporaryException(ioe);
    }
    // parse result
    if (response.getBody() != null && !response.getBody().isEmpty()) {
        try {
            result = CIPluginSDKUtils.getObjectMapper().readValue(response.getBody(), String[].class);
        } catch (IOException ioe) {
            if (CIPluginSDKUtils.isServiceTemporaryUnavailable(response.getBody())) {
                throw new TemporaryException("Saas service is temporary unavailable.");
            } else {
                throw new PermanentException("failed to parse preflight response '" + response.getBody() + "' for '" + encodedJobId + "'");
            }
        }
    }
    return result;
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) IOException(java.io.IOException)

Example 9 with TemporaryException

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

the class CoverageServiceImpl method isSonarReportRelevant.

@Override
public boolean isSonarReportRelevant(String jobId) {
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
    }
    boolean result = false;
    OctaneResponse response;
    boolean base64 = isEncodeBase64();
    String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodePathParam(jobId);
    // get result
    try {
        String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "servers/" + CIPluginSDKUtils.urlEncodePathParam(configurer.octaneConfiguration.getInstanceId()) + "/jobs/" + encodedJobId + "/workspaceId";
        if (base64) {
            url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        }
        OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
        response = restService.obtainOctaneRestClient().execute(preflightRequest);
        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_OK && response.getStatus() != HttpStatus.SC_NO_CONTENT) {
            throw new PermanentException("preflight request failed with status " + response.getStatus());
        }
    } catch (IOException ioe) {
        throw new TemporaryException("failed to perform preflight request", ioe);
    }
    // parse result
    if (response.getStatus() == HttpStatus.SC_OK && response.getBody() != null && !response.getBody().isEmpty()) {
        try {
            String[] wss = CIPluginSDKUtils.getObjectMapper().readValue(response.getBody(), String[].class);
            if (wss.length > 0) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "coverage of " + jobId + " found " + wss.length + " interested workspace/s in Octane, dispatching the coverage");
                result = true;
            } else {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "coverage of " + jobId + ", found no interested workspace in Octane");
            }
        } catch (IOException ioe) {
            throw new PermanentException("failed to parse preflight response '" + response.getBody() + "' for '" + jobId + "'");
        }
    }
    return result;
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) IOException(java.io.IOException)

Example 10 with TemporaryException

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

the class CoverageServiceImpl method pushCoverage.

@Override
public OctaneResponse pushCoverage(String jobId, String buildId, CoverageReportType reportType, InputStream coverageReport) {
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
    }
    if (buildId == null || buildId.isEmpty()) {
        throw new IllegalArgumentException("build ID MUST NOT be null nor empty");
    }
    if (reportType == null) {
        throw new IllegalArgumentException("report type MUST NOT be null");
    }
    if (coverageReport == null) {
        throw new IllegalArgumentException("coverage report data MUST NOT be null");
    }
    boolean base64 = isEncodeBase64();
    String tempJobId = jobId;
    if (base64) {
        tempJobId = CIPluginSDKUtils.urlEncodeBase64(jobId);
    }
    String url;
    try {
        url = new URIBuilder(getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "coverage").setParameter("ci-server-identity", configurer.octaneConfiguration.getInstanceId()).setParameter("ci-job-id", tempJobId).setParameter("ci-build-id", buildId).setParameter("file-type", reportType.name()).toString();
        if (base64) {
            url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        }
    } catch (URISyntaxException urise) {
        throw new PermanentException("failed to build URL to push coverage report", urise);
    }
    String correlationId = CIPluginSDKUtils.getNextCorrelationId();
    Map<String, String> headers = new HashMap<>();
    headers.put(CORRELATION_ID_HEADER, correlationId);
    OctaneRequest pushCoverageRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setHeaders(headers).setBody(coverageReport);
    try {
        return restService.obtainOctaneRestClient().execute(pushCoverageRequest);
    } catch (IOException ioe) {
        throw new TemporaryException("failed to push coverage report", ioe);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) URISyntaxException(java.net.URISyntaxException) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder)

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