com.netflix.client.http.HttpResponse Java Examples

The following examples show how to use com.netflix.client.http.HttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecureClient2()  throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.IsSecure, "true");
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStore, secureServer.getTrustStore().getAbsolutePath());
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStorePassword, SecureGetTest.PASSWORD);
    
    RestClient client = (RestClient) ClientFactory.getNamedClient("test3");
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server[] servers = new Server[]{new Server("localhost", secureServer.getServerPort())}; 
    lb.addServers(Arrays.asList(servers));
    client.setLoadBalancer(lb);
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
    assertEquals(secureServer.getServerPath("/"), response.getRequestedURI().toString());
    
}
 
Example #2
Source File: SecureGetTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testSunnyDayNoClientAuth() throws Exception{

	AbstractConfiguration cm = ConfigurationManager.getConfigInstance();

	String name = "GetPostSecureTest" + ".testSunnyDayNoClientAuth";

	String configPrefix = name + "." + "ribbon";

	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.SecurePort, Integer.toString(testServer2.getPort()));
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsHostnameValidationRequired, "false");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStore, FILE_TS2.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();
	HttpResponse response = rc.execute(request);
	assertEquals(200, response.getStatus());
}
 
Example #3
Source File: SecureGetTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testSunnyDay() throws Exception {

	AbstractConfiguration cm = ConfigurationManager.getConfigInstance();

	String name = "GetPostSecureTest" + ".testSunnyDay";

	String configPrefix = name + "." + "ribbon";

	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.SecurePort, Integer.toString(testServer1.getPort()));
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsHostnameValidationRequired, "false");
	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/");
       HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
	HttpResponse response = rc.execute(request);
	assertEquals(200, response.getStatus());
}
 
Example #4
Source File: ElasticSearchSink.java    From suro with Apache License 2.0 5 votes vote down vote up
public void recover(Message message) {
    IndexInfo info = indexInfo.create(message);

    HttpResponse response = null;
    try {
        response = client.executeWithLoadBalancer(
            HttpRequest.newBuilder()
                .verb(HttpRequest.Verb.POST)
                .setRetriable(true)
                .uri(indexInfo.getIndexUri(info))
                .entity(indexInfo.getSource(info))
                .build());
        if (response.getStatus() / 100 != 2) {
            Servo.getCounter(
                MonitorConfig.builder("unrecoverableRow")
                    .withTag(SINK_ID, getSinkId())
                    .withTag(TagKey.ROUTING_KEY, message.getRoutingKey())
                    .build()).increment();
        }
    } catch (Exception e) {
        log.error("Exception while recover: " + e.getMessage(), e);
        Servo.getCounter("recoverException").increment();
        if (reEnqueueOnException) {
            writeTo(new DefaultMessageContainer(message, jsonMapper));
        } else {
            Servo.getCounter(
                    MonitorConfig.builder("unrecoverableRow")
                            .withTag(SINK_ID, getSinkId())
                            .withTag(TagKey.ROUTING_KEY, message.getRoutingKey())
                            .build()).increment();
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }

}
 
Example #5
Source File: ManyShortLivedRequestsSurvivorTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void survive() throws IOException, ClientException, URISyntaxException, InterruptedException {

    String clientName = "RibbonClientTest-loadBalancingDefaultPolicyRoundRobin";
    String serverListKey = clientName + ".ribbon.listOfServers";
    int nbHitsPerServer = 60;
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();

    for (int i = 0; i < nbHitsPerServer; i++) {
        server1.enqueue(new MockResponse().setResponseCode(200).setBody("server1 success <" + i + ">!"));
        server2.enqueue(new MockResponse().setResponseCode(200).setBody("server2 success <" + i + ">!"));
    }

    server1.play();
    server2.play();

    getConfigInstance().setProperty(serverListKey, hostAndPort(server1.getUrl("")) + "," + hostAndPort(server2.getUrl("")));

    RestClient client = (RestClient) ClientFactory.getNamedClient(clientName);
    HttpRequest request;
    for (int i = 0; i < nbHitsPerServer * 2; i++) {
        request = HttpRequest.newBuilder().uri(new URI("/")).build();
        HttpResponse response = client.executeWithLoadBalancer(request);
        response.close();
    }
}
 
Example #6
Source File: FollowRedirectTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedirectFollowed() throws Exception {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues("myclient2")
            .set(IClientConfigKey.Keys.FollowRedirects, Boolean.TRUE);
    ClientFactory.registerClientFromProperties("myclient2", config);
    com.netflix.niws.client.http.RestClient client = (com.netflix.niws.client.http.RestClient) ClientFactory.getNamedClient("myclient2");
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:" + redirectingServer.getPort())).build();
    HttpResponse response = client.execute(request);
    assertEquals(200, response.getStatus());      
}
 
Example #7
Source File: FollowRedirectTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedirectNotFollowed() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("myclient");
    config.set(CommonClientConfigKey.FollowRedirects, Boolean.FALSE);
    ClientFactory.registerClientFromProperties("myclient", config);
    RestClient client = (RestClient) ClientFactory.getNamedClient("myclient");
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:" + redirectingServer.getPort())).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertEquals(302, response.getStatus());          
}
 
Example #8
Source File: RestClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelete() throws Exception {
    RestClient client = (RestClient) ClientFactory.getNamedClient("google");
    HttpRequest request = HttpRequest.newBuilder().uri(server.getServerURI()).verb(HttpRequest.Verb.DELETE).build();
    HttpResponse response = client.execute(request);
    assertStatusIsOk(response.getStatus());
    
    request = HttpRequest.newBuilder().uri(server.getServerURI()).verb(HttpRequest.Verb.DELETE).entity("").build();
    response = client.execute(request);
    assertStatusIsOk(response.getStatus());
}
 
Example #9
Source File: RestClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecureClient()  throws Exception {
	ConfigurationManager.getConfigInstance().setProperty("test2.ribbon.IsSecure", "true");
	RestClient client = (RestClient) ClientFactory.getNamedClient("test2");
    HttpRequest request = HttpRequest.newBuilder().uri(server.getServerURI()).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
}
 
Example #10
Source File: RestClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testVipAsURI()  throws Exception {
	ConfigurationManager.getConfigInstance().setProperty("test1.ribbon.DeploymentContextBasedVipAddresses", server.getServerPath("/"));
	ConfigurationManager.getConfigInstance().setProperty("test1.ribbon.InitializeNFLoadBalancer", "false");
    RestClient client = (RestClient) ClientFactory.getNamedClient("test1");
    assertNull(client.getLoadBalancer());
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
    assertEquals(server.getServerPath("/"), response.getRequestedURI().toString());
}
 
Example #11
Source File: RestClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWithoutLB() throws Exception {
    RestClient client = (RestClient) ClientFactory.getNamedClient("google");
    HttpRequest request = HttpRequest.newBuilder().uri(server.getServerURI()).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
    response = client.execute(request);
    assertStatusIsOk(response.getStatus());
}
 
Example #12
Source File: RetryTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuccessfulRetries() throws Exception {
    lb.setServersList(Lists.newArrayList(new Server("localhost:12987"), new Server("localhost:12987"), localServer));
    URI localUrl = new URI("/ok");
    HttpRequest request = HttpRequest.newBuilder().uri(localUrl).queryParams("name", "ribbon").build();
    try {
        HttpResponse response = client.executeWithLoadBalancer(request, DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.MaxAutoRetriesNextServer, 2));
        assertEquals(200, response.getStatus());
    } catch (ClientException e) { 
        fail("Unexpected exception");
    }
    ServerStats stats = lb.getLoadBalancerStats().getSingleServerStat(new Server("localhost:12987")); 
    assertEquals(1, stats.getSuccessiveConnectionFailureCount());
}
 
Example #13
Source File: RetryTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetriesOnPostWithConnectException() throws Exception {
    URI localUrl = new URI("/status?code=503");
    lb.setServersList(Lists.newArrayList(localServer));
    HttpRequest request = HttpRequest.newBuilder().uri(localUrl).verb(Verb.POST).setRetriable(true).build();
    try {
        HttpResponse response = client.executeWithLoadBalancer(request, DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.MaxAutoRetriesNextServer, 2));
        fail("Exception expected");
    } catch (ClientException e) { // NOPMD
    }
    ServerStats stats = lb.getLoadBalancerStats().getSingleServerStat(localServer); 
    assertEquals(3, stats.getSuccessiveConnectionFailureCount());
}
 
Example #14
Source File: GetPostTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testChunkedEncoding() throws Exception {
    String obj = "chunked encoded content";
	URI postUri = new URI(SERVICE_URI + "test/postStream");
	InputStream input = new ByteArrayInputStream(obj.getBytes("UTF-8"));
	HttpRequest request = HttpRequest.newBuilder().verb(Verb.POST).uri(postUri).entity(input).build();
	HttpResponse response = client.execute(request);
	assertEquals(200, response.getStatus());
	assertTrue(response.getEntity(String.class).equals(obj));
}
 
Example #15
Source File: GetPostTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testPost() throws Exception {
	URI getUri = new URI(SERVICE_URI + "test/setObject");
	TestObject obj = new TestObject();
	obj.name = "fromClient";
	HttpRequest request = HttpRequest.newBuilder().verb(Verb.POST).uri(getUri).entity(obj).build();
	HttpResponse response = client.execute(request);
	assertEquals(200, response.getStatus());
	assertTrue(response.getEntity(TestObject.class).name.equals("fromClient"));
}
 
Example #16
Source File: GetPostTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {
	URI getUri = new URI(SERVICE_URI + "test/getObject");
	MultivaluedMapImpl params = new MultivaluedMapImpl();
	params.add("name", "test");
	HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
	HttpResponse response = client.execute(request);
	assertEquals(200, response.getStatus());
	assertTrue(response.getEntity(TestObject.class).name.equals("test"));
}
 
Example #17
Source File: RibbonZuulCommon.java    From s2g-zuul with MIT License 4 votes vote down vote up
@Override
protected HttpResponse run() throws Exception {    	
	
	HttpResponse response = (HttpResponse) client.executeWithLoadBalancer(request);
	return response;
}
 
Example #18
Source File: SecureAcceptAllGetTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testPositiveAcceptAllSSLSocketFactory() throws Exception{

    // test connection succeeds connecting to a random SSL endpoint with allow all SSL factory

    AbstractConfiguration cm = ConfigurationManager.getConfigInstance();

    String name = "GetPostSecureTest." + testName.getMethodName();

    String configPrefix = name + "." + "ribbon";

    cm.setProperty(configPrefix + "." + CommonClientConfigKey.CustomSSLSocketFactoryClassName, "com.netflix.http4.ssl.AcceptAllSocketFactory");

    RestClient rc = (RestClient) ClientFactory.getNamedClient(name);

    TEST_SERVER.accept();

    URI getUri = new URI(TEST_SERVICE_URI + "test/");
    HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
    HttpResponse response = rc.execute(request);
    assertEquals(200, response.getStatus());
}
 
Example #19
Source File: RestClient.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Override
public HttpResponse execute(HttpRequest task, IClientConfig requestConfig) throws Exception {
    IClientConfig config = (requestConfig == null) ? task.getOverrideConfig() : requestConfig;
    return execute(task.getVerb(), task.getUri(),
            task.getHeaders(), task.getQueryParams(), config, task.getEntity());
}
 
Example #20
Source File: RestClient.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public HttpResponse execute(HttpRequest task) throws Exception  {
    return execute(task, null);
}
 
Example #21
Source File: RestClient.java    From s2g-zuul with MIT License 4 votes vote down vote up
@Override
public HttpResponse execute(HttpRequest task) throws Exception {
    return execute(task.getVerb(), task.getUri(),
            task.getHeaders(), task.getQueryParams(), task.getOverrideConfig(), task.getEntity());
}