Search in sources :

Example 1 with UnauthorizedException

use of org.openkilda.exception.UnauthorizedException in project open-kilda by telstra.

the class RestClientManager method getResponse.

/**
 * Gets the response.
 *
 * @param <T> the generic type
 * @param <E> the element type
 * @param response the response
 * @param responseClass the response class
 * @param dependentClass the dependent class
 * @return the response
 */
private <T, E> T getResponse(final HttpResponse response, final Class<T> responseClass, final Class<E> dependentClass) {
    T obj = null;
    try {
        LOGGER.info("[getResponse]  : StatusCode " + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() != HttpStatus.NO_CONTENT.value()) {
            String responseEntity = IoUtil.toString(response.getEntity().getContent());
            LOGGER.debug("[getResponse]  : response object " + responseEntity);
            if (!(HttpStatus.valueOf(response.getStatusLine().getStatusCode()).is2xxSuccessful() && response.getEntity() != null)) {
                String errorMessage = null;
                try {
                    if (responseEntity.startsWith("[")) {
                        responseEntity = responseEntity.replaceFirst("]", "").trim();
                    }
                    if (responseEntity.endsWith("]")) {
                        responseEntity = responseEntity.replace("]", "").trim();
                    }
                    errorMessage = mapper.readValue(responseEntity, ErrorMessage.class).getMessage();
                } catch (Exception e) {
                    if (response.getStatusLine().getStatusCode() == HttpStatus.UNAUTHORIZED.value()) {
                        throw new UnauthorizedException(HttpError.UNAUTHORIZED.getMessage());
                    }
                    LOGGER.error("Error occurred while retriving response from third party service provider", e);
                    errorMessage = authPropertyService.getError(IAuthConstants.Code.RESPONSE_PARSING_FAIL_ERROR).getMessage();
                    throw new RestCallFailedException(errorMessage);
                }
                LOGGER.error("Error occurred while retriving response from third party service provider:" + responseEntity);
                throw new ExternalSystemException(response.getStatusLine().getStatusCode(), errorMessage);
            } else {
                if (dependentClass == null) {
                    if (responseClass != null) {
                        obj = mapper.readValue(responseEntity, responseClass);
                    }
                } else {
                    obj = mapper.readValue(responseEntity, TypeFactory.defaultInstance().constructCollectionLikeType(responseClass, dependentClass));
                }
            }
        }
    } catch (IOException e) {
        throw new RestCallFailedException(e.getMessage());
    }
    return obj;
}
Also used : ExternalSystemException(org.openkilda.exception.ExternalSystemException) RestCallFailedException(org.openkilda.exception.RestCallFailedException) UnauthorizedException(org.openkilda.exception.UnauthorizedException) IOException(java.io.IOException) ExternalSystemException(org.openkilda.exception.ExternalSystemException) RestCallFailedException(org.openkilda.exception.RestCallFailedException) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) UnauthorizedException(org.openkilda.exception.UnauthorizedException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ExternalSystemException (org.openkilda.exception.ExternalSystemException)1 RestCallFailedException (org.openkilda.exception.RestCallFailedException)1 UnauthorizedException (org.openkilda.exception.UnauthorizedException)1 InvalidResponseException (org.openkilda.integration.exception.InvalidResponseException)1