use of io.netty.handler.codec.http.DefaultHttpResponse in project flink by apache.
the class HandlerUtils method transferFile.
public static void transferFile(ChannelHandlerContext ctx, File file, HttpRequest httpRequest) throws FlinkException {
final RandomAccessFile randomAccessFile;
try {
randomAccessFile = new RandomAccessFile(file, "r");
} catch (FileNotFoundException e) {
throw new FlinkException("Can not find file " + file + ".", e);
}
try {
final long fileLength = randomAccessFile.length();
final FileChannel fileChannel = randomAccessFile.getChannel();
try {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_TYPE, "text/plain");
if (HttpHeaders.isKeepAlive(httpRequest)) {
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
HttpHeaders.setContentLength(response, fileLength);
// write the initial line and the header.
ctx.write(response);
// write the content.
final ChannelFuture lastContentFuture;
final GenericFutureListener<Future<? super Void>> completionListener = future -> {
fileChannel.close();
randomAccessFile.close();
};
if (ctx.pipeline().get(SslHandler.class) == null) {
ctx.write(new DefaultFileRegion(fileChannel, 0, fileLength), ctx.newProgressivePromise()).addListener(completionListener);
lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
} else {
lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(randomAccessFile, 0, fileLength, 8192)), ctx.newProgressivePromise()).addListener(completionListener);
// HttpChunkedInput will write the end marker (LastHttpContent) for us.
}
// close the connection, if no keep-alive is needed
if (!HttpHeaders.isKeepAlive(httpRequest)) {
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
} catch (IOException ex) {
fileChannel.close();
throw ex;
}
} catch (IOException ioe) {
try {
randomAccessFile.close();
} catch (IOException e) {
throw new FlinkException("Close file or channel error.", e);
}
throw new FlinkException("Could not transfer file " + file + " to the client.", ioe);
}
}
use of io.netty.handler.codec.http.DefaultHttpResponse in project crate by crate.
the class HttpBlobHandler method fullContentResponse.
private void fullContentResponse(HttpRequest request, String index, final String digest) throws IOException {
BlobShard blobShard = localBlobShard(index, digest);
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
Netty4CorsHandler.setCorsResponseHeaders(request, response, corsConfig);
final RandomAccessFile raf = blobShard.blobContainer().getRandomAccessFile(digest);
try {
HttpUtil.setContentLength(response, raf.length());
setDefaultGetHeaders(response);
LOGGER.trace("HttpResponse: {}", response);
Channel channel = ctx.channel();
channel.write(response);
ChannelFuture writeFuture = transferFile(digest, raf, 0, raf.length());
if (!HttpUtil.isKeepAlive(request)) {
writeFuture.addListener(ChannelFutureListener.CLOSE);
}
} catch (Throwable t) {
/*
* Make sure RandomAccessFile is closed when exception is raised.
* In case of success, the ChannelFutureListener in "transferFile" will take care
* that the resources are released.
*/
raf.close();
throw t;
}
}
use of io.netty.handler.codec.http.DefaultHttpResponse in project reactor-netty by reactor.
the class HttpOperationsTest method httpAndJsonDecoders.
@Test
void httpAndJsonDecoders() {
EmbeddedChannel channel = new EmbeddedChannel();
Connection testContext = () -> channel;
ChannelHandler handler = new JsonObjectDecoder(true);
testContext.addHandlerLast("foo", handler);
HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
String json1 = "[{\"some\": 1} , {\"valu";
String json2 = "e\": true, \"test\": 1}]";
Object[] content = new Object[3];
content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
channel.writeInbound(content);
Object t = channel.readInbound();
assertThat(t).isInstanceOf(HttpResponse.class);
assertThat(t).isNotInstanceOf(HttpContent.class);
t = channel.readInbound();
assertThat(t).isInstanceOf(ByteBuf.class);
ByteBuf b = (ByteBuf) t;
assertThat(b.readCharSequence(b.readableBytes(), CharsetUtil.UTF_8)).isEqualTo("{\"some\": 1}");
b.release();
t = channel.readInbound();
assertThat(t).isInstanceOf(ByteBuf.class);
b = (ByteBuf) t;
assertThat(b.readCharSequence(b.readableBytes(), CharsetUtil.UTF_8)).isEqualTo("{\"value\": true, \"test\": 1}");
b.release();
t = channel.readInbound();
assertThat(t).isEqualTo(LastHttpContent.EMPTY_LAST_CONTENT);
((LastHttpContent) t).release();
t = channel.readInbound();
assertThat(t).isNull();
}
Aggregations