Search in sources :

Example 1 with ChunkedInput

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");
    }
}
Also used : ChunkedInput(io.netty.handler.stream.ChunkedInput) JarURLConnection(java.net.JarURLConnection) ChunkedStream(io.netty.handler.stream.ChunkedStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Date(java.util.Date)

Example 2 with ChunkedInput

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());
        }
    }));
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Unpooled(io.netty.buffer.Unpooled) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Path(java.nio.file.Path) Subscriber(org.reactivestreams.Subscriber) FileChunkedStrategy(reactor.ipc.netty.channel.data.FileChunkedStrategy) Files(java.nio.file.Files) Publisher(org.reactivestreams.Publisher) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) File(java.io.File) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) ChunkedNioFile(io.netty.handler.stream.ChunkedNioFile) Flux(reactor.core.publisher.Flux) SslHandler(io.netty.handler.ssl.SslHandler) DefaultFileRegion(io.netty.channel.DefaultFileRegion) WritableByteChannel(java.nio.channels.WritableByteChannel) ChunkedInput(io.netty.handler.stream.ChunkedInput) Exceptions(reactor.core.Exceptions) FileChannel(java.nio.channels.FileChannel) AbstractFileChunkedStrategy(reactor.ipc.netty.channel.data.AbstractFileChunkedStrategy) ChunkedInput(io.netty.handler.stream.ChunkedInput) FileChunkedStrategy(reactor.ipc.netty.channel.data.FileChunkedStrategy) AbstractFileChunkedStrategy(reactor.ipc.netty.channel.data.AbstractFileChunkedStrategy) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

ChunkedInput (io.netty.handler.stream.ChunkedInput)2 IOException (java.io.IOException)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 SslHandler (io.netty.handler.ssl.SslHandler)1 ChunkedNioFile (io.netty.handler.stream.ChunkedNioFile)1 ChunkedStream (io.netty.handler.stream.ChunkedStream)1 File (java.io.File)1 JarURLConnection (java.net.JarURLConnection)1 URL (java.net.URL)1 FileChannel (java.nio.channels.FileChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 Date (java.util.Date)1