Search in sources :

Example 21 with ClientHandlerException

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);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)21 ClientResponse (com.sun.jersey.api.client.ClientResponse)12 WebResource (com.sun.jersey.api.client.WebResource)9 IOException (java.io.IOException)6 Test (org.testng.annotations.Test)6 ConnectException (java.net.ConnectException)5 URI (java.net.URI)5 YarnException (org.apache.hadoop.yarn.exceptions.YarnException)5 Matchers.anyString (org.mockito.Matchers.anyString)5 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)4 TimelineEntity (org.apache.hadoop.yarn.api.records.timeline.TimelineEntity)4 TimelineEvent (org.apache.hadoop.yarn.api.records.timeline.TimelineEvent)4 JSONException (org.codehaus.jettison.json.JSONException)4 TimelinePutResponse (org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 HttpRequest (com.netflix.client.http.HttpRequest)2 Client (com.sun.jersey.api.client.Client)2 InputStream (java.io.InputStream)2 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)2