Search in sources :

Example 6 with ClientHandlerException

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

the class SecureGetTest method testClientRejectsWrongServer.

@Test
public void testClientRejectsWrongServer() throws Exception {
    AbstractConfiguration cm = ConfigurationManager.getConfigInstance();
    String name = "GetPostSecureTest" + ".testClientRejectsWrongServer";
    String configPrefix = name + "." + "ribbon";
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.SecurePort, Integer.toString(PORT2));
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsHostnameValidationRequired, "false");
    // <--
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStore, FILE_TS1.getAbsolutePath());
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStorePassword, PASSWORD);
    RestClient rc = (RestClient) ClientFactory.getNamedClient(name);
    testServer2.accept();
    URI getUri = new URI(SERVICE_URI2 + "test/");
    HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
    try {
        rc.execute(request);
        fail("expecting ssl hostname validation error");
    } catch (ClientHandlerException che) {
        assertTrue(che.getMessage().indexOf("peer not authenticated") > -1);
    }
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) HttpRequest(com.netflix.client.http.HttpRequest) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) URI(java.net.URI) Test(org.junit.Test)

Example 7 with ClientHandlerException

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

the class SecureGetTest method testFailsWithHostNameValidationOn.

@Test
public void testFailsWithHostNameValidationOn() throws Exception {
    AbstractConfiguration cm = ConfigurationManager.getConfigInstance();
    String name = "GetPostSecureTest" + ".testFailsWithHostNameValidationOn";
    String configPrefix = name + "." + "ribbon";
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.SecurePort, Integer.toString(PORT1));
    // <--
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsHostnameValidationRequired, "true");
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsClientAuthRequired, "true");
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.KeyStore, FILE_KS1.getAbsolutePath());
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.KeyStorePassword, PASSWORD);
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStore, FILE_TS1.getAbsolutePath());
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStorePassword, PASSWORD);
    RestClient rc = (RestClient) ClientFactory.getNamedClient(name);
    testServer1.accept();
    URI getUri = new URI(SERVICE_URI1 + "test/");
    MultivaluedMapImpl params = new MultivaluedMapImpl();
    params.add("name", "test");
    HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
    try {
        rc.execute(request);
        fail("expecting ssl hostname validation error");
    } catch (ClientHandlerException che) {
        assertTrue(che.getMessage().indexOf("hostname in certificate didn't match") > -1);
    }
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) HttpRequest(com.netflix.client.http.HttpRequest) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) URI(java.net.URI) Test(org.junit.Test)

Example 8 with ClientHandlerException

use of com.sun.jersey.api.client.ClientHandlerException in project openhab1-addons by openhab.

the class HueBridge method getSettingsJson.

/**
     * Determines the settings of the Hue bridge as a Json raw data String.
     *
     * @return The settings of the bridge if they could be determined. Null
     *         otherwise.
     */
private String getSettingsJson() {
    WebResource webResource = client.resource(getUrl());
    try {
        ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
        String settingsString = response.getEntity(String.class);
        if (response.getStatus() != 200) {
            logger.warn("Failed to connect to Hue bridge: HTTP error code: " + response.getStatus());
            return null;
        }
        logger.trace("Received Hue Bridge Settings: {}", settingsString);
        return settingsString;
    } catch (ClientHandlerException e) {
        logger.warn("Failed to connect to Hue bridge: HTTP request timed out.");
        return null;
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) WebResource(com.sun.jersey.api.client.WebResource)

Example 9 with ClientHandlerException

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

the class AtlasClientTest method shouldRetryIfCannotConnectToServiceInitially.

@Test
public void shouldRetryIfCannotConnectToServiceInitially() {
    setupRetryParams();
    when(client.resource(UriBuilder.fromUri("http://localhost:31000").build())).thenReturn(service);
    WebResource.Builder builder = setupBuilder(AtlasClient.API.STATUS, service);
    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    when(response.getEntity(String.class)).thenReturn("{\"Status\":\"BECOMING_ACTIVE\"}");
    ClientResponse nextResponse = mock(ClientResponse.class);
    when(nextResponse.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.STATUS.getMethod(), ClientResponse.class, null)).thenThrow(new ClientHandlerException("Simulating connection exception")).thenReturn(response).thenReturn(nextResponse);
    AtlasClient atlasClient = new AtlasClient(service, configuration);
    atlasClient.setService(service);
    atlasClient.setConfiguration(configuration);
    String serviceURL = atlasClient.determineActiveServiceURL(new String[] { "http://localhost:31000", "http://localhost:41000" }, client);
    assertEquals(serviceURL, "http://localhost:31000");
}
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) Test(org.testng.annotations.Test)

Example 10 with ClientHandlerException

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

the class AtlasClientTest method shouldRetryAPICallsOnClientHandlerException.

@Test
public void shouldRetryAPICallsOnClientHandlerException() 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 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);
    AtlasClient atlasClient = getClientForTest("http://localhost:31000", "http://localhost:41000");
    atlasClient.setService(service);
    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