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 ManyShortLivedRequestsSurvivorTest method survive.
@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();
}
}
Aggregations