use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.
the class Netty4HttpClient method sendRequests.
private synchronized Collection<FullHttpResponse> sendRequests(final SocketAddress remoteAddress, final Collection<HttpRequest> requests) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(requests.size());
final Collection<FullHttpResponse> content = Collections.synchronizedList(new ArrayList<>(requests.size()));
clientBootstrap.handler(new CountDownLatchHandler(latch, content));
ChannelFuture channelFuture = null;
try {
channelFuture = clientBootstrap.connect(remoteAddress);
channelFuture.sync();
for (HttpRequest request : requests) {
channelFuture.channel().writeAndFlush(request);
}
latch.await(10, TimeUnit.SECONDS);
} finally {
if (channelFuture != null) {
channelFuture.channel().close().sync();
}
}
return content;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.
the class Netty4HttpRequestSizeLimitIT method testLimitsInFlightRequests.
public void testLimitsInFlightRequests() throws Exception {
ensureGreen();
// we use the limit size as a (very) rough indication on how many requests we should sent to hit the limit
int numRequests = LIMIT.bytesAsInt() / 100;
StringBuilder bulkRequest = new StringBuilder();
for (int i = 0; i < numRequests; i++) {
bulkRequest.append("{\"index\": {}}");
bulkRequest.append(System.lineSeparator());
bulkRequest.append("{ \"field\" : \"value\" }");
bulkRequest.append(System.lineSeparator());
}
@SuppressWarnings("unchecked") Tuple<String, CharSequence>[] requests = new Tuple[150];
for (int i = 0; i < requests.length; i++) {
requests[i] = Tuple.tuple("/index/type/_bulk", bulkRequest);
}
HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
TransportAddress transportAddress = (TransportAddress) randomFrom(httpServerTransport.boundAddress().boundAddresses());
try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
Collection<FullHttpResponse> singleResponse = nettyHttpClient.post(transportAddress.address(), requests[0]);
assertThat(singleResponse, hasSize(1));
assertAtLeastOnceExpectedStatus(singleResponse, HttpResponseStatus.OK);
Collection<FullHttpResponse> multipleResponses = nettyHttpClient.post(transportAddress.address(), requests);
assertThat(multipleResponses, hasSize(requests.length));
assertAtLeastOnceExpectedStatus(multipleResponses, HttpResponseStatus.SERVICE_UNAVAILABLE);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.
the class Netty4HttpServerPipeliningTests method testThatHttpPipeliningWorksWhenEnabled.
public void testThatHttpPipeliningWorksWhenEnabled() throws Exception {
final Settings settings = Settings.builder().put("http.pipelining", true).put("http.port", "0").build();
try (HttpServerTransport httpServerTransport = new CustomNettyHttpServerTransport(settings)) {
httpServerTransport.start();
final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses());
final int numberOfRequests = randomIntBetween(4, 16);
final List<String> requests = new ArrayList<>(numberOfRequests);
for (int i = 0; i < numberOfRequests; i++) {
if (rarely()) {
requests.add("/slow/" + i);
} else {
requests.add("/" + i);
}
}
try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests.toArray(new String[] {}));
Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses);
assertThat(responseBodies, contains(requests.toArray()));
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project vert.x by eclipse.
the class HttpServerImpl method sendError.
private void sendError(CharSequence err, HttpResponseStatus status, Channel ch) {
FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status);
if (status.code() == METHOD_NOT_ALLOWED.code()) {
// SockJS requires this
resp.headers().set(io.vertx.core.http.HttpHeaders.ALLOW, io.vertx.core.http.HttpHeaders.GET);
}
if (err != null) {
resp.content().writeBytes(err.toString().getBytes(CharsetUtil.UTF_8));
HttpHeaders.setContentLength(resp, err.length());
} else {
HttpHeaders.setContentLength(resp, 0);
}
ch.writeAndFlush(resp);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project flink by apache.
the class RuntimeMonitorHandler method respondAsLeader.
@Override
protected void respondAsLeader(ChannelHandlerContext ctx, Routed routed, ActorGateway jobManager) {
FullHttpResponse response;
try {
// we only pass the first element in the list to the handlers.
Map<String, String> queryParams = new HashMap<>();
for (String key : routed.queryParams().keySet()) {
queryParams.put(key, routed.queryParam(key));
}
Map<String, String> pathParams = new HashMap<>(routed.pathParams().size());
for (String key : routed.pathParams().keySet()) {
pathParams.put(key, URLDecoder.decode(routed.pathParams().get(key), ENCODING.toString()));
}
InetSocketAddress address = (InetSocketAddress) ctx.channel().localAddress();
queryParams.put(WEB_MONITOR_ADDRESS_KEY, (httpsEnabled ? "https://" : "http://") + address.getHostName() + ":" + address.getPort());
response = handler.handleRequest(pathParams, queryParams, jobManager);
} catch (NotFoundException e) {
// this should result in a 404 error code (not found)
ByteBuf message = e.getMessage() == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(e.getMessage().getBytes(ENCODING));
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, message);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
LOG.debug("Error while handling request", e);
} catch (Exception e) {
byte[] bytes = ExceptionUtils.stringifyException(e).getBytes(ENCODING);
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
LOG.debug("Error while handling request", e);
}
response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
// Content-Encoding:utf-8
response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, ENCODING.name());
KeepAliveWrite.flush(ctx, routed.request(), response);
}
Aggregations