Search in sources :

Example 16 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project async-http-client by AsyncHttpClient.

the class HttpStaticFileServerHandler method sendError.

private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 17 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class HttpConversionUtil method toHttpResponse.

/**
     * Create a new object to contain the response data
     *
     * @param streamId The stream associated with the response
     * @param http2Headers The initial set of HTTP/2 headers to create the response with
     * @param alloc The {@link ByteBufAllocator} to use to generate the content of the message
     * @param validateHttpHeaders <ul>
     *        <li>{@code true} to validate HTTP headers in the http-codec</li>
     *        <li>{@code false} not to validate HTTP headers in the http-codec</li>
     *        </ul>
     * @return A new response object which represents headers/data
     * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)}
     */
public static FullHttpResponse toHttpResponse(int streamId, Http2Headers http2Headers, ByteBufAllocator alloc, boolean validateHttpHeaders) throws Http2Exception {
    HttpResponseStatus status = parseStatus(http2Headers.status());
    // HTTP/2 does not define a way to carry the version or reason phrase that is included in an
    // HTTP/1.1 status line.
    FullHttpResponse msg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, alloc.buffer(), validateHttpHeaders);
    try {
        addHttp2ToHttpHeaders(streamId, http2Headers, msg, false);
    } catch (Http2Exception e) {
        msg.release();
        throw e;
    } catch (Throwable t) {
        msg.release();
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }
    return msg;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 18 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project camel by apache.

the class HttpClientChannelHandler method getResponseMessage.

@Override
protected Message getResponseMessage(Exchange exchange, ChannelHandlerContext ctx, Object message) throws Exception {
    FullHttpResponse response = (FullHttpResponse) message;
    if (!HttpUtil.isKeepAlive(response)) {
        // just want to make sure we close the channel if the keepAlive is not true
        exchange.setProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
    }
    // handle cookies
    if (producer.getEndpoint().getCookieHandler() != null) {
        String actualUri = exchange.getIn().getHeader(Exchange.HTTP_URL, String.class);
        URI uri = new URI(actualUri);
        Map<String, List<String>> m = new HashMap<String, List<String>>();
        for (String name : response.headers().names()) {
            m.put(name, response.headers().getAll(name));
        }
        producer.getEndpoint().getCookieHandler().storeCookies(exchange, uri, m);
    }
    // use the binding
    return producer.getEndpoint().getNettyHttpBinding().toCamelMessage(response, exchange, producer.getConfiguration());
}
Also used : HashMap(java.util.HashMap) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) List(java.util.List) URI(java.net.URI)

Example 19 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project rest.li by linkedin.

the class TestRAPClientCodec method testResponseDecoder.

@Test(dataProvider = "responseData")
public void testResponseDecoder(int status, String entity, HttpHeaders headers, String[] cookies) {
    final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec());
    ByteBuf content = Unpooled.copiedBuffer(entity, CHARSET);
    FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(status), content);
    nettyResponse.headers().set(headers);
    for (String cookie : cookies) {
        nettyResponse.headers().add(HttpHeaderNames.SET_COOKIE, cookie);
    }
    ch.writeInbound(nettyResponse);
    RestResponse response = (RestResponse) ch.readInbound();
    Assert.assertEquals(response.getStatus(), status);
    Assert.assertEquals(response.getEntity().asString(CHARSET), entity);
    assertList(response.getCookies(), nettyResponse.headers().getAll(HttpConstants.RESPONSE_COOKIE_HEADER_NAME));
    for (Map.Entry<String, String> header : nettyResponse.headers()) {
        if (!header.getKey().equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
            List<String> values = response.getHeaderValues(header.getKey());
            Assert.assertNotNull(values);
            Assert.assertTrue(values.contains(header.getValue()));
        }
    }
    // make sure the incoming ByteBuf is released
    Assert.assertEquals(content.refCnt(), 0);
    ch.finish();
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteString(com.linkedin.data.ByteString) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 20 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project flink by apache.

the class JobCancellationWithSavepointHandlersTest method testFailedCancellation.

/**
	 * Tests response when a request fails.
	 */
@Test
public void testFailedCancellation() throws Exception {
    JobID jobId = new JobID();
    ExecutionGraphHolder holder = mock(ExecutionGraphHolder.class);
    ExecutionGraph graph = mock(ExecutionGraph.class);
    CheckpointCoordinator coord = mock(CheckpointCoordinator.class);
    when(holder.getExecutionGraph(eq(jobId), any(ActorGateway.class))).thenReturn(graph);
    when(graph.getCheckpointCoordinator()).thenReturn(coord);
    JobCancellationWithSavepointHandlers handlers = new JobCancellationWithSavepointHandlers(holder, EC);
    JobCancellationWithSavepointHandlers.TriggerHandler trigger = handlers.getTriggerHandler();
    JobCancellationWithSavepointHandlers.InProgressHandler progress = handlers.getInProgressHandler();
    Map<String, String> params = new HashMap<>();
    params.put("jobid", jobId.toString());
    params.put("targetDirectory", "custom-directory");
    ActorGateway jobManager = mock(ActorGateway.class);
    // Successful
    Future<Object> future = Futures.failed(new Exception("Test Exception"));
    when(jobManager.ask(any(Object.class), any(FiniteDuration.class))).thenReturn(future);
    // Trigger
    trigger.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
    verify(jobManager).ask(eq(new CancelJobWithSavepoint(jobId, "custom-directory")), any(FiniteDuration.class));
    // Query progress
    params.put("requestId", "1");
    FullHttpResponse response = progress.handleRequest(params, Collections.<String, String>emptyMap(), jobManager);
    assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, response.getStatus());
    assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
    String json = response.content().toString(Charset.forName("UTF-8"));
    JsonNode root = new ObjectMapper().readTree(json);
    assertEquals("failed", root.get("status").getValueAsText());
    assertEquals("1", root.get("request-id").getValueAsText());
    assertEquals("Test Exception", root.get("cause").getValueAsText());
}
Also used : HashMap(java.util.HashMap) FiniteDuration(scala.concurrent.duration.FiniteDuration) CancelJobWithSavepoint(org.apache.flink.runtime.messages.JobManagerMessages.CancelJobWithSavepoint) JsonNode(org.codehaus.jackson.JsonNode) ExecutionGraphHolder(org.apache.flink.runtime.webmonitor.ExecutionGraphHolder) CheckpointCoordinator(org.apache.flink.runtime.checkpoint.CheckpointCoordinator) ActorGateway(org.apache.flink.runtime.instance.ActorGateway) ExecutionGraph(org.apache.flink.runtime.executiongraph.ExecutionGraph) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) JobID(org.apache.flink.api.common.JobID) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)260 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19