use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.
the class ChannelWriteCallback method handleContent.
/**
* Handles a {@link HttpContent}. Checks state and echoes back the content.
* @param httpContent the {@link HttpContent} that needs to be handled.
* @throws Exception
*/
private void handleContent(HttpContent httpContent) throws Exception {
if (request != null) {
boolean isLast = httpContent instanceof LastHttpContent;
ByteBuffer content = ByteBuffer.wrap(httpContent.content().array());
ChannelWriteCallback callback = new ChannelWriteCallback();
callback.setFuture(restResponseChannel.write(content, callback));
writeCallbacksToVerify.add(callback);
if (isLast) {
restResponseChannel.onResponseComplete(null);
assertFalse("Request channel is not closed", request.isOpen());
}
} else {
throw new RestServiceException("Received data without a request", RestServiceErrorCode.InvalidRequestState);
}
}
use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.
the class ChannelWriteCallback method responsesWithContentLengthTest.
/**
* Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via
* {@link NettyResponseChannel#write(ByteBuffer, Callback)} and then complete the response.
* <p/>
* These responses have the header Content-Length set.
* @throws Exception
*/
@Test
public void responsesWithContentLengthTest() throws Exception {
EmbeddedChannel channel = createEmbeddedChannel();
MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
final int ITERATIONS = 10;
for (int i = 0; i < ITERATIONS; i++) {
boolean isKeepAlive = i != (ITERATIONS - 1);
HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, i);
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, TestingUri.ResponseWithContentLength.toString(), httpHeaders);
HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
channel.writeInbound(httpRequest);
verifyCallbacks(processor);
// first outbound has to be response.
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
long contentLength = HttpUtil.getContentLength(response, -1);
assertEquals("Unexpected Content-Length", MockNettyMessageProcessor.CHUNK.length * i, contentLength);
if (contentLength == 0) {
// special case. Since Content-Length is set, the response should be an instance of FullHttpResponse.
assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse);
} else {
HttpContent httpContent = null;
for (int j = 0; j < i; j++) {
httpContent = (HttpContent) channel.readOutbound();
byte[] returnedContent = httpContent.content().array();
assertArrayEquals("Content does not match with expected content", MockNettyMessageProcessor.CHUNK, returnedContent);
}
// the last HttpContent should also be an instance of LastHttpContent
assertTrue("The last part of the content is not LastHttpContent", httpContent instanceof LastHttpContent);
}
assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive());
}
}
use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.
the class FrontendIntegrationTest method assertNoContent.
/**
* Verifies that no content has been sent as part of the response or readable bytes is equivalent to 0
* @param contents the content of the response.
*/
private void assertNoContent(Queue<HttpObject> contents) {
boolean endMarkerFound = false;
for (HttpObject object : contents) {
assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
HttpContent content = (HttpContent) object;
assertEquals("No content expected ", 0, content.content().readableBytes());
endMarkerFound = object instanceof LastHttpContent;
ReferenceCountUtil.release(content);
}
assertTrue("There should have been an end marker", endMarkerFound);
}
use of io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.
the class Nettys method httpobjs2fullreq.
// retain when build fullreq
public static FullHttpRequest httpobjs2fullreq(final List<HttpObject> httpobjs) {
if (httpobjs.size() > 0 && (httpobjs.get(0) instanceof HttpRequest) && (httpobjs.get(httpobjs.size() - 1) instanceof LastHttpContent)) {
if (httpobjs.get(0) instanceof FullHttpRequest) {
return ((FullHttpRequest) httpobjs.get(0)).retainedDuplicate();
}
final HttpRequest req = (HttpRequest) httpobjs.get(0);
final DefaultFullHttpRequest fullreq = new DefaultFullHttpRequest(req.protocolVersion(), req.method(), req.uri(), tobuf(httpobjs));
fullreq.headers().add(req.headers());
// ? need update Content-Length header field ?
return fullreq;
} else {
throw new RuntimeException("invalid HttpObjects");
}
}
use of io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.
the class DefaultSignalClient method assembleOutgoing.
private Outgoing assembleOutgoing(final HttpRequest request, final BodyForm body, final Attachment[] attachments) throws Exception {
if (0 == attachments.length) {
final LastHttpContent lastContent = buildLastContent(request, body);
return new Outgoing(Observable.<Object>just(request, lastContent), lastContent.content().readableBytes(), new Action0() {
@Override
public void call() {
ReferenceCountUtil.release(request);
ReferenceCountUtil.release(lastContent);
}
});
} else {
// Use the PostBody encoder
final HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(_DATA_FACTORY, request, true, CharsetUtil.UTF_8, // true => multipart
EncoderMode.HTML5);
final long signalSize = addSignalToMultipart(postRequestEncoder, body);
final long attachmentSize = addAttachmentsToMultipart(postRequestEncoder, attachments);
final long total = signalSize + attachmentSize;
// finalize request
final HttpRequest request4send = postRequestEncoder.finalizeRequest();
final Action0 toRelease = new Action0() {
@Override
public void call() {
ReferenceCountUtil.release(request4send);
RxNettys.releaseObjects(postRequestEncoder.getBodyListAttributes());
}
};
return postRequestEncoder.isChunked() ? new Outgoing(Observable.<Object>just(request4send, postRequestEncoder), total, toRelease) : new Outgoing(Observable.<Object>just(request4send), total, toRelease);
}
}
Aggregations