Search in sources :

Example 1 with HttpRequest

use of com.netflix.client.http.HttpRequest in project zuul by Netflix.

the class RibbonCommand method forward.

HttpResponse forward() throws Exception {
    NFRequestContext context = NFRequestContext.getCurrentContext();
    HttpRequest.Builder builder = HttpRequest.newBuilder().verb(verb).uri(uri).entity(requestEntity);
    for (String name : headers.keySet()) {
        List<String> values = headers.get(name);
        for (String value : values) {
            builder.header(name, value);
        }
    }
    for (String name : params.keySet()) {
        List<String> values = params.get(name);
        for (String value : values) {
            builder.queryParams(name, value);
        }
    }
    HttpRequest httpClientRequest = builder.build();
    HttpResponse response = restClient.executeWithLoadBalancer(httpClientRequest);
    context.setZuulResponse(response);
    // chain has already continued without us and therefore won't cleanup itself.
    if (isResponseTimedOut()) {
        response.close();
    }
    return response;
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) NFRequestContext(com.netflix.zuul.context.NFRequestContext) HttpResponse(com.netflix.client.http.HttpResponse)

Example 2 with HttpRequest

use of com.netflix.client.http.HttpRequest in project spring-cloud-netflix by spring-cloud.

the class RestClientRibbonCommandTests method testEntity.

void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws Exception {
    String lengthString = String.valueOf(entityValue.length());
    Long length = null;
    URI uri = URI.create("http://example.com");
    LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    if (addContentLengthHeader) {
        headers.add("Content-Length", lengthString);
        length = (long) entityValue.length();
    }
    RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<HttpRequest.Builder>() {

        @Override
        public boolean accepts(Class builderClass) {
            return builderClass == HttpRequest.Builder.class;
        }

        @Override
        public void customize(HttpRequest.Builder builder) {
            builder.header("from-customizer", "foo");
        }
    };
    RibbonCommandContext context = new RibbonCommandContext("example", method, uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
    context.setContentLength(length);
    RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, context, zuulProperties);
    HttpRequest request = command.createRequest();
    assertThat("uri is wrong", request.getUri().toString(), startsWith(uri.toString()));
    if (addContentLengthHeader) {
        assertThat("Content-Length is wrong", request.getHttpHeaders().getFirstValue("Content-Length"), is(equalTo(lengthString)));
    }
    assertThat("from-customizer is wrong", request.getHttpHeaders().getFirstValue("from-customizer"), is(equalTo("foo")));
    if (method.equalsIgnoreCase("DELETE")) {
        assertThat("entity is was non-null", request.getEntity(), is(nullValue()));
    } else {
        assertThat("entity is missing", request.getEntity(), is(notNullValue()));
        assertThat("entity is wrong type", InputStream.class.isAssignableFrom(request.getEntity().getClass()), is(true));
        InputStream entity = (InputStream) request.getEntity();
        String string = StreamUtils.copyToString(entity, Charset.forName("UTF-8"));
        assertThat("content is wrong", string, is(equalTo(entityValue)));
    }
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) URI(java.net.URI)

Example 3 with HttpRequest

use of com.netflix.client.http.HttpRequest in project spring-cloud-netflix by spring-cloud.

the class RestClientRibbonCommandTests method testNullEntityWithOldConstruct.

/**
 * Tests old constructors kept for backwards compatibility with Spring Cloud Sleuth 1.x versions
 */
@Test
@Deprecated
public void testNullEntityWithOldConstruct() throws Exception {
    String uri = "http://example.com";
    LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("my-header", "my-value");
    LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("myparam", "myparamval");
    RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, Verb.GET, uri, false, headers, params, null);
    HttpRequest request = command.createRequest();
    assertThat("uri is wrong", request.getUri().toString(), startsWith(uri));
    assertThat("my-header is wrong", request.getHttpHeaders().getFirstValue("my-header"), is(equalTo("my-value")));
    assertThat("myparam is missing", request.getQueryParams().get("myparam").iterator().next(), is(equalTo("myparamval")));
    command = new RestClientRibbonCommand("cmd", null, new RibbonCommandContext("example", "GET", uri, false, headers, params, null), zuulProperties);
    request = command.createRequest();
    assertThat("uri is wrong", request.getUri().toString(), startsWith(uri));
    assertThat("my-header is wrong", request.getHttpHeaders().getFirstValue("my-header"), is(equalTo("my-value")));
    assertThat("myparam is missing", request.getQueryParams().get("myparam").iterator().next(), is(equalTo("myparamval")));
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) Test(org.junit.Test)

Example 4 with HttpRequest

use of com.netflix.client.http.HttpRequest in project spring-cloud-netflix by spring-cloud.

the class RestClientRibbonCommand method createRequest.

@Override
protected HttpRequest createRequest() throws Exception {
    final InputStream requestEntity;
    // ApacheHttpClient4Handler does not support body in delete requests
    if (getContext().getMethod().equalsIgnoreCase(HttpMethod.DELETE.toString())) {
        requestEntity = null;
    } else {
        requestEntity = this.context.getRequestEntity();
    }
    HttpRequest.Builder builder = HttpRequest.newBuilder().verb(getVerb(this.context.getMethod())).uri(this.context.uri()).entity(requestEntity);
    if (this.context.getRetryable() != null) {
        builder.setRetriable(this.context.getRetryable());
    }
    for (String name : this.context.getHeaders().keySet()) {
        List<String> values = this.context.getHeaders().get(name);
        for (String value : values) {
            builder.header(name, value);
        }
    }
    for (String name : this.context.getParams().keySet()) {
        List<String> values = this.context.getParams().get(name);
        for (String value : values) {
            builder.queryParams(name, value);
        }
    }
    customizeRequest(builder);
    return builder.build();
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) InputStream(java.io.InputStream)

Example 5 with HttpRequest

use of com.netflix.client.http.HttpRequest in project ribbon by Netflix.

the class SecureAcceptAllGetTest method testPositiveAcceptAllSSLSocketFactory.

@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());
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) HttpRequest(com.netflix.client.http.HttpRequest) HttpResponse(com.netflix.client.http.HttpResponse) URI(java.net.URI)

Aggregations

HttpRequest (com.netflix.client.http.HttpRequest)36 URI (java.net.URI)28 Test (org.junit.Test)28 HttpResponse (com.netflix.client.http.HttpResponse)21 ClientException (com.netflix.client.ClientException)9 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)5 ServerStats (com.netflix.loadbalancer.ServerStats)4 MockHttpServer (com.netflix.client.testutil.MockHttpServer)3 Server (com.netflix.loadbalancer.Server)3 RestClient (com.netflix.niws.client.http.RestClient)3 InputStream (java.io.InputStream)3 RibbonCommandContext (org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 IClientConfig (com.netflix.client.config.IClientConfig)2 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)2 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MockResponse (com.google.mockwebserver.MockResponse)1