use of io.netty.handler.stream.ChunkedInput in project LogHub by fbacchella.
the class ResourceFiles method processRequest.
@Override
protected boolean processRequest(FullHttpRequest request, ChannelHandlerContext ctx) throws HttpRequestFailure {
String name = ROOT.relativize(Paths.get(request.uri()).normalize()).toString();
if (!name.startsWith("static/")) {
throw new HttpRequestFailure(HttpResponseStatus.FORBIDDEN, "Access to " + name + " forbiden");
}
URL resourceUrl = getClass().getClassLoader().getResource(name);
if (resourceUrl == null) {
throw new HttpRequestFailure(HttpResponseStatus.NOT_FOUND, request.uri() + " not found");
} else if ("jar".equals(resourceUrl.getProtocol())) {
try {
JarURLConnection jarConnection = (JarURLConnection) resourceUrl.openConnection();
JarEntry entry = jarConnection.getJarEntry();
if (entry.isDirectory()) {
throw new HttpRequestFailure(HttpResponseStatus.FORBIDDEN, "Directory listing refused");
}
int length = jarConnection.getContentLength();
internalPath = entry.getName();
internalDate = new Date(entry.getLastModifiedTime().toMillis());
ChunkedInput<ByteBuf> content = new ChunkedStream(jarConnection.getInputStream());
return writeResponse(ctx, request, content, length);
} catch (IOException e) {
throw new HttpRequestFailure(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
} else {
throw new HttpRequestFailure(HttpResponseStatus.INTERNAL_SERVER_ERROR, request.uri() + " not managed");
}
}
use of io.netty.handler.stream.ChunkedInput in project reactor-netty by reactor.
the class NettyOutbound method sendFileChunked.
default NettyOutbound sendFileChunked(Path file, long position, long count) {
Objects.requireNonNull(file);
final FileChunkedStrategy strategy = getFileChunkedStrategy();
final boolean needChunkedWriteHandler = context().channel().pipeline().get(NettyPipeline.ChunkedWriter) == null;
if (needChunkedWriteHandler) {
strategy.preparePipeline(context());
}
return then(Mono.using(() -> FileChannel.open(file, StandardOpenOption.READ), fc -> {
try {
ChunkedInput<?> message = strategy.chunkFile(fc);
return FutureMono.from(context().channel().writeAndFlush(message));
} catch (Exception e) {
return Mono.error(e);
}
}, fc -> {
try {
fc.close();
} catch (IOException ioe) {
/*IGNORE*/
} finally {
strategy.cleanupPipeline(context());
}
}));
}
Aggregations