use of io.netty.handler.codec.http.HttpContent in project jersey by jersey.
the class JerseyClientHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
if (msg instanceof HttpResponse) {
final HttpResponse response = (HttpResponse) msg;
final ClientResponse jerseyResponse = new ClientResponse(new Response.StatusType() {
@Override
public int getStatusCode() {
return response.status().code();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(response.status().code());
}
@Override
public String getReasonPhrase() {
return response.status().reasonPhrase();
}
}, jerseyRequest);
for (Map.Entry<String, String> entry : response.headers().entries()) {
jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue());
}
// request entity handling.
if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) || HttpUtil.isTransferEncodingChunked(response)) {
ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
isList.add(NettyInputStream.END_OF_INPUT_ERROR);
}
});
jerseyResponse.setEntityStream(new NettyInputStream(isList));
} else {
jerseyResponse.setEntityStream(new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
});
}
if (asyncConnectorCallback != null) {
connector.executorService.execute(new Runnable() {
@Override
public void run() {
asyncConnectorCallback.response(jerseyResponse);
future.complete(jerseyResponse);
}
});
}
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
ByteBuf content = httpContent.content();
if (content.isReadable()) {
// copy bytes - when netty reads last chunk, it automatically closes the channel, which invalidates all
// relates ByteBuffs.
byte[] bytes = new byte[content.readableBytes()];
content.getBytes(content.readerIndex(), bytes);
isList.add(new ByteArrayInputStream(bytes));
}
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 HttpUtilsTest method convertContentChunksToRawString_and_convertContentChunksToRawBytes_works_with_EmptyByteBuf_chunks.
@Test
public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_works_with_EmptyByteBuf_chunks() throws IOException {
// given
Charset contentCharset = CharsetUtil.UTF_8;
String chunk1Content = UUID.randomUUID().toString();
String chunk2Content = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1Content.getBytes(contentCharset);
byte[] chunk2Bytes = chunk2Content.getBytes(contentCharset);
ByteBuf chunk1ByteBuf = Unpooled.copiedBuffer(chunk1Bytes);
ByteBuf chunk2ByteBuf = Unpooled.copiedBuffer(chunk2Bytes);
Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(chunk1ByteBuf), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)), new DefaultHttpContent(chunk2ByteBuf), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)));
// when
String resultString = HttpUtils.convertContentChunksToRawString(contentCharset, chunkCollection);
byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection);
// then
String expectedResultString = chunk1Content + chunk2Content;
assertThat(resultString, is(expectedResultString));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(chunk2Bytes);
assertThat(resultBytes, is(baos.toByteArray()));
}
use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method releaseContentChunks_clear_on_chunk_list_but_does_not_release_chunks_if_contentChunksWillBeReleasedExternally_is_true.
@Test
public void releaseContentChunks_clear_on_chunk_list_but_does_not_release_chunks_if_contentChunksWillBeReleasedExternally_is_true() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.contentChunksWillBeReleasedExternally();
List<HttpContent> contentChunkList = Arrays.asList(mock(HttpContent.class), mock(HttpContent.class));
requestInfo.contentChunks.addAll(contentChunkList);
assertThat(requestInfo.contentChunks.size(), is(contentChunkList.size()));
// when
requestInfo.releaseContentChunks();
// then
for (HttpContent chunkMock : contentChunkList) {
verify(chunkMock, never()).release();
}
assertThat(requestInfo.contentChunks.isEmpty(), is(true));
}
use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method releaseContentChunks_calls_release_on_each_chunk_and_calls_clear_on_chunk_list.
@Test
public void releaseContentChunks_calls_release_on_each_chunk_and_calls_clear_on_chunk_list() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
List<HttpContent> contentChunkList = Arrays.asList(mock(HttpContent.class), mock(HttpContent.class));
requestInfo.contentChunks.addAll(contentChunkList);
assertThat(requestInfo.contentChunks.size(), is(contentChunkList.size()));
// when
requestInfo.releaseContentChunks();
// then
for (HttpContent chunkMock : contentChunkList) {
verify(chunkMock).release();
}
assertThat(requestInfo.contentChunks.isEmpty(), is(true));
}
use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk.
@Test
public void addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
String chunk1String = UUID.randomUUID().toString();
String lastChunkString = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1String.getBytes();
byte[] lastChunkBytes = lastChunkString.getBytes();
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer(chunk1Bytes));
HttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastChunkBytes));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
assertThat(requestInfo.getRawContentBytes(), nullValue());
assertThat(requestInfo.getRawContent(), nullValue());
// when
requestInfo.addContentChunk(chunk1);
requestInfo.addContentChunk(lastChunk);
// then
assertThat(chunk1.refCnt(), is(2));
assertThat(lastChunk.refCnt(), is(2));
assertThat(requestInfo.contentChunks.size(), is(2));
assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(lastChunkBytes);
assertThat(requestInfo.getRawContentBytes(), is(baos.toByteArray()));
String rawContentString = requestInfo.getRawContent();
assertThat(requestInfo.getRawContent(), is(chunk1String + lastChunkString));
// Verify that the raw content string is cached the first time it's loaded and reused for subsequent calls
assertThat(requestInfo.getRawContent() == rawContentString, is(true));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
}
Aggregations