use of com.checkmarx.sdk.exception.CxTokenExpiredException 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);
}
}
Aggregations