Search in sources :

Example 16 with ClientHandlerException

use of com.sun.jersey.api.client.ClientHandlerException in project hadoop by apache.

the class TestTimelineClient method mockDomainClientResponse.

private static ClientResponse mockDomainClientResponse(TimelineWriter spyTimelineWriter, ClientResponse.Status status, boolean hasRuntimeError) {
    ClientResponse response = mock(ClientResponse.class);
    if (hasRuntimeError) {
        doThrow(new ClientHandlerException(new ConnectException())).when(spyTimelineWriter).doPostingObject(any(TimelineDomain.class), any(String.class));
        return response;
    }
    doReturn(response).when(spyTimelineWriter).doPostingObject(any(TimelineDomain.class), any(String.class));
    when(response.getStatusInfo()).thenReturn(status);
    return response;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) TimelineDomain(org.apache.hadoop.yarn.api.records.timeline.TimelineDomain) ConnectException(java.net.ConnectException)

Example 17 with ClientHandlerException

use of com.sun.jersey.api.client.ClientHandlerException in project eureka by Netflix.

the class DynamicGZIPContentEncodingFilter method decompressResponse.

private static void decompressResponse(ClientResponse response) {
    InputStream entityInputStream = response.getEntityInputStream();
    GZIPInputStream uncompressedIS;
    try {
        uncompressedIS = new GZIPInputStream(entityInputStream);
    } catch (IOException ex) {
        try {
            entityInputStream.close();
        } catch (IOException ignored) {
        }
        throw new ClientHandlerException(ex);
    }
    response.setEntityInputStream(uncompressedIS);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 18 with ClientHandlerException

use of com.sun.jersey.api.client.ClientHandlerException in project ORCID-Source by ORCID.

the class SalesForceDaoImpl method getFreshAccessToken.

private String getFreshAccessToken() {
    LOGGER.info("About get SalesForce access token");
    WebResource resource = client.resource(tokenEndPointUrl);
    Form form = new Form();
    form.add("grant_type", "password");
    form.add("client_id", clientId);
    form.add("client_secret", clientSecret);
    form.add("username", username);
    form.add("password", password);
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON_TYPE).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, form);
    if (response.getStatus() == 200) {
        try {
            return response.getEntity(JSONObject.class).getString("access_token");
        } catch (ClientHandlerException | UniformInterfaceException | JSONException e) {
            throw new RuntimeException("Unable to extract access token from response", e);
        }
    } else {
        throw new RuntimeException("Error getting access token from SalesForce, status code =  " + response.getStatus() + ", reason = " + response.getStatusInfo().getReasonPhrase() + ", body = " + response.getEntity(String.class));
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.codehaus.jettison.json.JSONObject) Form(com.sun.jersey.api.representation.Form) WebResource(com.sun.jersey.api.client.WebResource) JSONException(org.codehaus.jettison.json.JSONException)

Example 19 with ClientHandlerException

use of com.sun.jersey.api.client.ClientHandlerException in project incubator-atlas by apache.

the class AtlasClientTest method shouldRetryWithSameClientIfSingleAddressIsUsed.

@Test
public void shouldRetryWithSameClientIfSingleAddressIsUsed() throws URISyntaxException, AtlasServiceException {
    setupRetryParams();
    ResourceCreator resourceCreator = mock(ResourceCreator.class);
    WebResource resourceObject = mock(WebResource.class);
    when(resourceObject.getURI()).thenReturn(new URI("http://localhost:31000/api/atlas/types"));
    WebResource.Builder builder = getBuilder(resourceObject);
    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    String activeStatus = "{\"Status\":\"ACTIVE\"}";
    when(response.getEntity(String.class)).thenReturn(activeStatus);
    when(response.getLength()).thenReturn(activeStatus.length());
    when(builder.method(AtlasClient.API.LIST_TYPES.getMethod(), ClientResponse.class, null)).thenThrow(new ClientHandlerException("simulating exception in calling API", new ConnectException())).thenReturn(response);
    when(resourceCreator.createResource()).thenReturn(resourceObject);
    when(configuration.getString("atlas.http.authentication.type", "simple")).thenReturn("simple");
    AtlasClient atlasClient = getClientForTest("http://localhost:31000");
    atlasClient.setService(resourceObject);
    atlasClient.setConfiguration(configuration);
    atlasClient.callAPIWithRetries(AtlasClient.API.LIST_TYPES, null, resourceCreator);
    verify(client).destroy();
    verify(client, times(2)).resource(UriBuilder.fromUri("http://localhost:31000").build());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) WebResource(com.sun.jersey.api.client.WebResource) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) ConnectException(java.net.ConnectException) Test(org.testng.annotations.Test)

Example 20 with ClientHandlerException

use of com.sun.jersey.api.client.ClientHandlerException in project incubator-atlas by apache.

the class AtlasClientTest method shouldRetryAPICallsOnServiceUnavailable.

@Test
public void shouldRetryAPICallsOnServiceUnavailable() throws AtlasServiceException, URISyntaxException {
    setupRetryParams();
    ResourceCreator resourceCreator = mock(ResourceCreator.class);
    WebResource resourceObject = mock(WebResource.class);
    when(resourceObject.getURI()).thenReturn(new URI("http://localhost:31000/api/atlas/types")).thenReturn(new URI("http://localhost:41000/api/atlas/types")).thenReturn(new URI("http://localhost:41000/api/atlas/types"));
    WebResource.Builder builder = getBuilder(resourceObject);
    ClientResponse firstResponse = mock(ClientResponse.class);
    when(firstResponse.getStatus()).thenReturn(Response.Status.SERVICE_UNAVAILABLE.getStatusCode());
    when(firstResponse.getClientResponseStatus()).thenReturn(ClientResponse.Status.SERVICE_UNAVAILABLE);
    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    String activeStatus = "{\"Status\":\"ACTIVE\"}";
    when(response.getEntity(String.class)).thenReturn(activeStatus);
    when(response.getLength()).thenReturn(activeStatus.length());
    when(builder.method(AtlasClient.API.LIST_TYPES.getMethod(), ClientResponse.class, null)).thenThrow(new ClientHandlerException("simulating exception in calling API", new ConnectException())).thenReturn(firstResponse).thenReturn(response);
    when(resourceCreator.createResource()).thenReturn(resourceObject);
    AtlasClient atlasClient = getClientForTest("http://localhost:31000", "http://localhost:41000");
    atlasClient.setService(resourceObject);
    atlasClient.setConfiguration(configuration);
    atlasClient.callAPIWithRetries(AtlasClient.API.LIST_TYPES, null, resourceCreator);
    verify(client).destroy();
    verify(client).resource(UriBuilder.fromUri("http://localhost:31000").build());
    verify(client).resource(UriBuilder.fromUri("http://localhost:41000").build());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) WebResource(com.sun.jersey.api.client.WebResource) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) ConnectException(java.net.ConnectException) Test(org.testng.annotations.Test)

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