Search in sources :

Example 11 with PermanentException

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

the class SonarServiceImpl method getPageFromSonar.

private InputStream getPageFromSonar(SonarBuildCoverageQueueItem queueItem, Integer page) {
    String sonarURL = queueItem.sonarURL;
    String projectKey = queueItem.projectKey;
    String token = queueItem.sonarToken;
    StringBuilder errorMessage = new StringBuilder().append("failed to get data from sonar for project key: ").append(projectKey);
    try {
        URIBuilder uriBuilder = new URIBuilder(sonarURL + COMPONENT_TREE_URI);
        uriBuilder.setParameter("metricKeys", "lines_to_cover,uncovered_lines").setParameter("component", projectKey).setParameter("qualifiers", "FIL,TRK").setParameter("ps", "500").setParameter("p", page.toString());
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(uriBuilder.build());
        setTokenInHttpRequest(request, token);
        HttpResponse httpResponse = httpClient.execute(request);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            errorMessage.append(" with status code: ").append(statusCode).append(" and response body: ").append(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
            throw new PermanentException(errorMessage.toString());
        } else {
            return httpResponse.getEntity().getContent();
        }
    } catch (PermanentException e) {
        throw e;
    } catch (Exception e) {
        logger.error(configurer.octaneConfiguration.getLocationForLog() + errorMessage.toString(), e);
        throw new PermanentException(errorMessage.toString(), e);
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) HttpResponse(org.apache.http.HttpResponse) SonarIntegrationException(com.hp.octane.integrations.exceptions.SonarIntegrationException) URISyntaxException(java.net.URISyntaxException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException) AuthenticationException(org.apache.http.auth.AuthenticationException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 12 with PermanentException

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

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

the class FODConnector method getSpeceficFODEntity.

@Override
public <T> T getSpeceficFODEntity(String rawURL, Class<T> targetClass) {
    try {
        T fetchedEntityInstance = targetClass.newInstance();
        String rawResponse = getRawResponseFromFOD(rawURL);
        // Deserialize.
        T entityFetched = new ObjectMapper().readValue(rawResponse, TypeFactory.defaultInstance().constructType((fetchedEntityInstance).getClass()));
        return entityFetched;
    } catch (PermanentException e) {
        throw e;
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : PermanentException(com.hp.octane.integrations.exceptions.PermanentException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) MalformedURLException(java.net.MalformedURLException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException)

Example 14 with PermanentException

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

use of com.hp.octane.integrations.exceptions.PermanentException 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)

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