use of org.openkilda.exception.ExternalSystemException 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;
}
Aggregations