Search in sources :

Example 1 with CxHTTPClientException

use of com.checkmarx.sdk.exception.CxHTTPClientException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class CxHttpClient method request.

private <T> T request(HttpRequestBase httpMethod, String contentType, HttpEntity entity, Class<T> responseType, int expectStatus, String failedMsg, boolean isCollection, boolean retry) throws IOException {
    if (contentType != null) {
        httpMethod.addHeader("Content-type", contentType);
    }
    if (entity != null && httpMethod instanceof HttpEntityEnclosingRequestBase) {
        // Entity for Post methods
        ((HttpEntityEnclosingRequestBase) httpMethod).setEntity(entity);
    }
    HttpResponse response = null;
    int statusCode = 0;
    try {
        httpMethod.addHeader(TEAM_PATH, this.teamPath);
        if (token != null) {
            httpMethod.addHeader(HttpHeaders.AUTHORIZATION, token.getToken_type() + " " + token.getAccess_token());
        }
        for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
            httpMethod.addHeader(entry.getKey(), entry.getValue());
        }
        response = apacheClient.execute(httpMethod);
        statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
            // Token has probably expired
            throw new CxTokenExpiredException(HttpClientHelper.extractResponseBody(response));
        }
        HttpClientHelper.validateResponse(response, expectStatus, "Failed to " + failedMsg);
        // extract response as object and return the link
        return HttpClientHelper.convertToObject(response, responseType, isCollection);
    } catch (UnknownHostException e) {
        throw new CxHTTPClientException("Connection failed. Please recheck the hostname and credentials you provided and try again.");
    } catch (CxTokenExpiredException ex) {
        if (retry) {
            logTokenError(httpMethod, statusCode, ex);
            if (lastLoginSettings != null) {
                login(lastLoginSettings);
                return request(httpMethod, contentType, entity, responseType, expectStatus, failedMsg, isCollection, false);
            }
        }
        throw ex;
    } finally {
        httpMethod.releaseConnection();
        HttpClientUtils.closeQuietly(response);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) CxHTTPClientException(com.checkmarx.sdk.exception.CxHTTPClientException) CxTokenExpiredException(com.checkmarx.sdk.exception.CxTokenExpiredException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with CxHTTPClientException

use of com.checkmarx.sdk.exception.CxHTTPClientException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class AstClientHelper method noFindingsWereDetected.

/**
 * When no findings are detected, AST-SAST API returns the 404 status with a specific
 * error code, which is quite awkward.
 * Response example: {"code":4004,"message":"can't find all the provided scan ids","data":null}
 *
 * @return true: scan completed successfully and the result contains no findings (normal flow).
 * false: some other error has occurred (error flow).
 */
private boolean noFindingsWereDetected(Exception e) {
    boolean result = false;
    if (e instanceof CxHTTPClientException) {
        CxHTTPClientException httpException = (CxHTTPClientException) e;
        if (httpException.getStatusCode() == HttpStatus.SC_NOT_FOUND && StringUtils.isNotEmpty(httpException.getResponseBody())) {
            try {
                JsonNode body = objectMapper.readTree(httpException.getResponseBody());
                result = (body.get("code").asInt() == NO_FINDINGS_CODE);
            } catch (Exception parsingException) {
                log.warn("Error parsing the 'Not found' response.", parsingException);
            }
        }
    }
    return result;
}
Also used : CxHTTPClientException(com.checkmarx.sdk.exception.CxHTTPClientException) JsonNode(com.fasterxml.jackson.databind.JsonNode) URISyntaxException(java.net.URISyntaxException) CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) ScannerRuntimeException(com.checkmarx.sdk.exception.ScannerRuntimeException) CxHTTPClientException(com.checkmarx.sdk.exception.CxHTTPClientException) RestClientException(org.springframework.web.client.RestClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

CxHTTPClientException (com.checkmarx.sdk.exception.CxHTTPClientException)2 CheckmarxException (com.checkmarx.sdk.exception.CheckmarxException)1 CxTokenExpiredException (com.checkmarx.sdk.exception.CxTokenExpiredException)1 ScannerRuntimeException (com.checkmarx.sdk.exception.ScannerRuntimeException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RestClientException (org.springframework.web.client.RestClientException)1