use of com.sun.jersey.api.client.ClientHandlerException in project incubator-atlas by apache.
the class AtlasBaseClient method callAPIWithResource.
protected <T> T callAPIWithResource(APIInfo api, WebResource resource, Object requestObject, GenericType<T> responseType) throws AtlasServiceException {
ClientResponse clientResponse = null;
int i = 0;
do {
if (LOG.isDebugEnabled()) {
LOG.debug("Calling API [ {} : {} ] {}", api.getMethod(), api.getPath(), requestObject != null ? "<== " + requestObject : "");
}
clientResponse = resource.accept(JSON_MEDIA_TYPE).type(JSON_MEDIA_TYPE).method(api.getMethod(), ClientResponse.class, requestObject);
if (LOG.isDebugEnabled()) {
LOG.debug("API {} returned status {}", resource.getURI(), clientResponse.getStatus());
}
if (clientResponse.getStatus() == api.getExpectedStatus().getStatusCode()) {
if (null == responseType) {
return null;
}
try {
if (responseType.getRawClass() == JSONObject.class) {
String stringEntity = clientResponse.getEntity(String.class);
try {
JSONObject jsonObject = new JSONObject(stringEntity);
LOG.info("Response = {}", jsonObject);
return (T) jsonObject;
} catch (JSONException e) {
throw new AtlasServiceException(api, e);
}
} else {
T entity = clientResponse.getEntity(responseType);
return entity;
}
} catch (ClientHandlerException e) {
throw new AtlasServiceException(api, e);
}
} else if (clientResponse.getStatus() != ClientResponse.Status.SERVICE_UNAVAILABLE.getStatusCode()) {
break;
} else {
LOG.error("Got a service unavailable when calling: {}, will retry..", resource);
sleepBetweenRetries();
}
i++;
} while (i < getNumberOfRetries());
throw new AtlasServiceException(api, clientResponse);
}
Aggregations