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