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);
}
}
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;
}
Aggregations