Search in sources :

Example 1 with CustomizableResponseTypeException

use of io.micronaut.http.server.types.CustomizableResponseTypeException in project micronaut-core by micronaut-projects.

the class FileTypeHandler method handle.

@SuppressWarnings("MagicNumber")
@Override
public void handle(Object obj, HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) {
    NettyFileCustomizableResponseType type;
    if (obj instanceof File) {
        type = new NettySystemFileCustomizableResponseType((File) obj);
    } else if (obj instanceof NettyFileCustomizableResponseType) {
        type = (NettyFileCustomizableResponseType) obj;
    } else if (obj instanceof StreamedFile) {
        type = new NettyStreamedFileCustomizableResponseType((StreamedFile) obj);
    } else if (obj instanceof SystemFile) {
        type = new NettySystemFileCustomizableResponseType((SystemFile) obj);
    } else {
        throw new CustomizableResponseTypeException("FileTypeHandler only supports File or FileCustomizableResponseType types");
    }
    long lastModified = type.getLastModified();
    // Cache Validation
    ZonedDateTime ifModifiedSince = request.getHeaders().getDate(HttpHeaders.IF_MODIFIED_SINCE);
    if (ifModifiedSince != null) {
        // Only compare up to the second because the datetime format we send to the client
        // does not have milliseconds
        long ifModifiedSinceDateSeconds = ifModifiedSince.toEpochSecond();
        long fileLastModifiedSeconds = lastModified / 1000;
        if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
            FullHttpResponse nettyResponse = notModified(response);
            if (request instanceof NettyHttpRequest) {
                ((NettyHttpRequest<?>) request).prepareHttp2ResponseIfNecessary(nettyResponse);
            }
            context.writeAndFlush(nettyResponse);
            return;
        }
    }
    if (!response.getHeaders().contains(HttpHeaders.CONTENT_TYPE)) {
        response.header(HttpHeaders.CONTENT_TYPE, type.getMediaType().toString());
    }
    setDateAndCacheHeaders(response, lastModified);
    type.process(response);
    type.write(request, response, context);
    context.read();
}
Also used : NettyFileCustomizableResponseType(io.micronaut.http.server.netty.types.NettyFileCustomizableResponseType) ZonedDateTime(java.time.ZonedDateTime) SystemFile(io.micronaut.http.server.types.files.SystemFile) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) SystemFile(io.micronaut.http.server.types.files.SystemFile) File(java.io.File) CustomizableResponseTypeException(io.micronaut.http.server.types.CustomizableResponseTypeException)

Example 2 with CustomizableResponseTypeException

use of io.micronaut.http.server.types.CustomizableResponseTypeException 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)

Example 3 with CustomizableResponseTypeException

use of io.micronaut.http.server.types.CustomizableResponseTypeException in project micronaut-core by micronaut-projects.

the class StreamTypeHandler method handle.

@Override
public void handle(Object object, HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) {
    NettyStreamedCustomizableResponseType type;
    if (object instanceof InputStream) {
        type = () -> (InputStream) object;
    } else if (object instanceof NettyStreamedCustomizableResponseType) {
        type = (NettyStreamedCustomizableResponseType) object;
    } else {
        throw new CustomizableResponseTypeException("StreamTypeHandler only supports InputStream or StreamedCustomizableResponseType types");
    }
    type.process(response);
    type.write(request, response, context);
    context.read();
}
Also used : InputStream(java.io.InputStream) NettyStreamedCustomizableResponseType(io.micronaut.http.server.netty.types.stream.NettyStreamedCustomizableResponseType) CustomizableResponseTypeException(io.micronaut.http.server.types.CustomizableResponseTypeException)

Aggregations

CustomizableResponseTypeException (io.micronaut.http.server.types.CustomizableResponseTypeException)3 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)2 NettyMutableHttpResponse (io.micronaut.http.netty.NettyMutableHttpResponse)1 NettyFileCustomizableResponseType (io.micronaut.http.server.netty.types.NettyFileCustomizableResponseType)1 NettyStreamedCustomizableResponseType (io.micronaut.http.server.netty.types.stream.NettyStreamedCustomizableResponseType)1 StreamedFile (io.micronaut.http.server.types.files.StreamedFile)1 SystemFile (io.micronaut.http.server.types.files.SystemFile)1 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)1 ChunkedFile (io.netty.handler.stream.ChunkedFile)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZonedDateTime (java.time.ZonedDateTime)1