Search in sources :

Example 6 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class HttpUtilsTest method convertContentChunksToRawBytes_returns_null_if_total_bytes_is_zero.

@Test
public void convertContentChunksToRawBytes_returns_null_if_total_bytes_is_zero() {
    // given
    Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)));
    // when
    byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection);
    // then
    assertThat(resultBytes, nullValue());
}
Also used : EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 7 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project jersey by jersey.

the class JerseyServerHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        final HttpRequest req = (HttpRequest) msg;
        if (HttpUtil.is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
        }
        // clearing the content - possible leftover from previous request processing.
        isList.clear();
        final ContainerRequest requestContext = createContainerRequest(ctx, req);
        requestContext.setWriter(new NettyResponseWriter(ctx, req, container));
        // must be like this, since there is a blocking read from Jersey
        container.getExecutorService().execute(new Runnable() {

            @Override
            public void run() {
                container.getApplicationHandler().handle(requestContext);
            }
        });
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            isList.add(new ByteBufInputStream(content));
        }
        if (msg instanceof LastHttpContent) {
            isList.add(NettyInputStream.END_OF_INPUT);
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 8 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    // Deal with the final outbound HttpResponse
    if (msg instanceof HttpResponse) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (state != null)
            state.setActualResponseObject((HttpResponse) msg);
    }
    // Deal with the final outbound body content
    if (msg instanceof HttpContent) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (state != null && state.getResponseInfo() != null) {
            ResponseInfo<?> responseInfo = state.getResponseInfo();
            long contentBytes = ((HttpContent) msg).content().readableBytes();
            if (responseInfo.getFinalContentLength() == null)
                responseInfo.setFinalContentLength(contentBytes);
            else
                responseInfo.setFinalContentLength(responseInfo.getFinalContentLength() + contentBytes);
        }
    }
    super.write(ctx, msg, promise);
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 9 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandlerTest method write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_state_is_null.

@Test
public void write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_state_is_null() throws Exception {
    // given
    HttpContent msgMock = mock(HttpContent.class);
    ByteBuf contentMock = mock(ByteBuf.class);
    int contentBytes = (int) (Math.random() * 10000);
    doReturn(contentMock).when(msgMock).content();
    doReturn(contentBytes).when(contentMock).readableBytes();
    doReturn(null).when(stateAttrMock).get();
    assertThat(responseInfo.getFinalContentLength()).isNull();
    // when
    handler.write(ctxMock, msgMock, promiseMock);
    // then
    assertThat(responseInfo.getFinalContentLength()).isNull();
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 10 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandlerTest method write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null.

@Test
public void write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null() throws Exception {
    // given
    HttpContent msgMock = mock(HttpContent.class);
    ByteBuf contentMock = mock(ByteBuf.class);
    int contentBytes = (int) (Math.random() * 10000);
    doReturn(contentMock).when(msgMock).content();
    doReturn(contentBytes).when(contentMock).readableBytes();
    int initialFinalContentLengthValue = (int) (Math.random() * 10000);
    responseInfo.setFinalContentLength((long) initialFinalContentLengthValue);
    assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue);
    // when
    handler.write(ctxMock, msgMock, promiseMock);
    // then
    assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue + contentBytes);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)146 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)110 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)56 Test (org.junit.Test)55 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)54 ByteBuf (io.netty.buffer.ByteBuf)39 HttpRequest (io.netty.handler.codec.http.HttpRequest)32 HttpResponse (io.netty.handler.codec.http.HttpResponse)30 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)29 ArrayList (java.util.ArrayList)29 HttpObject (io.netty.handler.codec.http.HttpObject)25 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)20 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 Channel (io.netty.channel.Channel)16 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)16 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13 ByteBufferAsyncWritableChannel (com.github.ambry.commons.ByteBufferAsyncWritableChannel)10