use of io.netty.handler.codec.http.DefaultFullHttpResponse in project jersey by jersey.
the class NettyResponseWriter method writeResponseStatusAndHeaders.
@Override
public synchronized OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException {
if (responseWritten) {
LOGGER.log(Level.FINE, "Response already written.");
return null;
}
responseWritten = true;
String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
int statusCode = responseContext.getStatus();
HttpResponseStatus status = reasonPhrase == null ? HttpResponseStatus.valueOf(statusCode) : new HttpResponseStatus(statusCode, reasonPhrase);
DefaultHttpResponse response;
if (contentLength == 0) {
response = new DefaultFullHttpResponse(req.protocolVersion(), status);
} else {
response = new DefaultHttpResponse(req.protocolVersion(), status);
}
for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
response.headers().add(e.getKey(), e.getValue());
}
if (contentLength == -1) {
HttpUtil.setTransferEncodingChunked(response, true);
} else {
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, contentLength);
}
if (HttpUtil.isKeepAlive(req)) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ctx.writeAndFlush(response);
if (req.method() != HttpMethod.HEAD && (contentLength > 0 || contentLength == -1)) {
JerseyChunkedInput jerseyChunkedInput = new JerseyChunkedInput(ctx.channel());
if (HttpUtil.isTransferEncodingChunked(response)) {
ctx.write(new HttpChunkedInput(jerseyChunkedInput)).addListener(FLUSH_FUTURE);
} else {
ctx.write(new HttpChunkedInput(jerseyChunkedInput)).addListener(FLUSH_FUTURE);
}
return jerseyChunkedInput;
} else {
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
return null;
}
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse 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.DefaultFullHttpResponse in project netty by netty.
the class FallbackRequestHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(response.duplicate());
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class Http2RequestHandler method handleImage.
private void handleImage(String x, String y, ChannelHandlerContext ctx, String streamId, int latency, FullHttpRequest request) {
ByteBuf image = ImageCache.INSTANCE.image(parseInt(x), parseInt(y));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, image.duplicate());
response.headers().set(CONTENT_TYPE, "image/jpeg");
sendResponse(ctx, streamId, latency, response, request);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class Http2RequestHandler method sendBadRequest.
private void sendBadRequest(ChannelHandlerContext ctx, String streamId) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, EMPTY_BUFFER);
streamId(response, streamId);
ctx.writeAndFlush(response);
}
Aggregations