use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class ProcessFinalResponseOutputHandlerTest method write_sets_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_null.
@Test
public void write_sets_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_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();
assertThat(responseInfo.getFinalContentLength()).isNull();
// when
handler.write(ctxMock, msgMock, promiseMock);
// then
assertThat(responseInfo.getFinalContentLength()).isEqualTo(contentBytes);
}
use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class HttpUtils method convertContentChunksToRawBytes.
public static byte[] convertContentChunksToRawBytes(Collection<HttpContent> contentChunks) {
if (contentChunks == null || contentChunks.size() == 0)
return null;
ByteBuf[] chunkByteBufs = contentChunks.stream().map(ByteBufHolder::content).toArray(ByteBuf[]::new);
int totalNumBytes = contentChunks.stream().collect(Collectors.summingInt(chunk -> chunk.content().readableBytes()));
if (totalNumBytes == 0)
return null;
byte[] comboBytes = new byte[totalNumBytes];
int bytesWrittenSoFar = 0;
for (ByteBuf chunk : chunkByteBufs) {
int numBytesInThisChunk = chunk.readableBytes();
chunk.getBytes(0, comboBytes, bytesWrittenSoFar, chunk.readableBytes());
bytesWrittenSoFar += numBytesInThisChunk;
}
return comboBytes;
}
use of io.netty.handler.codec.http.HttpContent in project Glowstone by GlowstoneMC.
the class HttpHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
int responseCode = response.status().code();
if (responseCode == HttpResponseStatus.NO_CONTENT.code()) {
done(ctx);
return;
}
if (responseCode != HttpResponseStatus.OK.code()) {
throw new IllegalStateException("Expected HTTP response 200 OK, got " + responseCode);
}
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
content.append(httpContent.content().toString(Charset.forName("UTF-8")));
if (msg instanceof LastHttpContent) {
done(ctx);
}
}
}
Aggregations