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