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);
}
}
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);
}
}
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;
}
}
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");
}
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());
}
Aggregations