Search in sources :

Example 1 with CxTokenExpiredException

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

Aggregations

CxHTTPClientException (com.checkmarx.sdk.exception.CxHTTPClientException)1 CxTokenExpiredException (com.checkmarx.sdk.exception.CxTokenExpiredException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1