use of io.micronaut.http.server.types.files.SystemFile 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();
}
Aggregations