Search in sources :

Example 6 with NettyMutableHttpResponse

use of io.micronaut.http.netty.NettyMutableHttpResponse in project micronaut-core by micronaut-projects.

the class NettySystemFileCustomizableResponseType method write.

@Override
public void write(HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) {
    if (response instanceof NettyMutableHttpResponse) {
        NettyMutableHttpResponse nettyResponse = ((NettyMutableHttpResponse) response);
        // Write the request data
        final DefaultHttpResponse finalResponse = new DefaultHttpResponse(nettyResponse.getNettyHttpVersion(), nettyResponse.getNettyHttpStatus(), nettyResponse.getNettyHeaders());
        if (request instanceof NettyHttpRequest) {
            ((NettyHttpRequest<?>) request).prepareHttp2ResponseIfNecessary(finalResponse);
        }
        context.write(finalResponse, context.voidPromise());
        FileHolder file = new FileHolder(getFile());
        // Write the content.
        if (context.pipeline().get(SslHandler.class) == null && context.pipeline().get(SmartHttpContentCompressor.class).shouldSkip(finalResponse)) {
            // SSL not enabled - can use zero-copy file transfer.
            context.write(new DefaultFileRegion(file.raf.getChannel(), 0, getLength()), context.newProgressivePromise()).addListener(file);
            context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        } else {
            // SSL enabled - cannot use zero-copy file transfer.
            try {
                // HttpChunkedInput will write the end marker (LastHttpContent) for us.
                final HttpChunkedInput chunkedInput = new HttpChunkedInput(new ChunkedFile(file.raf, 0, getLength(), LENGTH_8K));
                context.writeAndFlush(chunkedInput, context.newProgressivePromise()).addListener(file);
            } catch (IOException e) {
                throw new CustomizableResponseTypeException("Could not read file", e);
            }
        }
    } else {
        throw new IllegalArgumentException("Unsupported response type. Not a Netty response: " + response);
    }
}
Also used : NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) ChunkedFile(io.netty.handler.stream.ChunkedFile) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) IOException(java.io.IOException) DefaultFileRegion(io.netty.channel.DefaultFileRegion) CustomizableResponseTypeException(io.micronaut.http.server.types.CustomizableResponseTypeException)

Aggregations

NettyMutableHttpResponse (io.micronaut.http.netty.NettyMutableHttpResponse)6 Internal (io.micronaut.core.annotation.Internal)2 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)2 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)2 ByteBuf (io.netty.buffer.ByteBuf)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)2 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)2 IOException (java.io.IOException)2 List (java.util.List)2 NonNull (io.micronaut.core.annotation.NonNull)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpStatus (io.micronaut.http.HttpStatus)1 MediaType (io.micronaut.http.MediaType)1 MediaTypeCodec (io.micronaut.http.codec.MediaTypeCodec)1 NettyCustomizableResponseType (io.micronaut.http.server.netty.types.NettyCustomizableResponseType)1 CustomizableResponseTypeException (io.micronaut.http.server.types.CustomizableResponseTypeException)1 TextPlainCodec (io.micronaut.runtime.http.codec.TextPlainCodec)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1