Search in sources :

Example 1 with StreamedFile

use of io.micronaut.http.server.types.files.StreamedFile in project zuliasearch by zuliaio.

the class AssociatedController method getMetadata.

@Get("/metadata")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public HttpResponse<?> getMetadata(@QueryValue(ZuliaConstants.ID) final String uniqueId, @QueryValue(ZuliaConstants.FILE_NAME) final String fileName, @QueryValue(ZuliaConstants.INDEX) final String indexName) {
    ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
    try {
        if (uniqueId != null && fileName != null && indexName != null) {
            ZuliaBase.AssociatedDocument associatedDocument = indexManager.getAssociatedDocument(indexName, uniqueId, fileName);
            StreamedFile attach = new StreamedFile(new ByteArrayInputStream(associatedDocument.getMetadata().toByteArray()), MediaType.of(MediaType.APPLICATION_JSON)).attach(fileName);
            MutableHttpResponse<StreamedFile> ok = HttpResponse.ok(attach);
            attach.process(ok);
            return ok;
        } else {
            return HttpResponse.serverError(ZuliaConstants.ID + " and " + ZuliaConstants.FILE_NAME + " are required");
        }
    } catch (Exception e) {
        return HttpResponse.serverError(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ZuliaBase(io.zulia.message.ZuliaBase) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) MultipartException(io.micronaut.http.multipart.MultipartException) IOException(java.io.IOException) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 2 with StreamedFile

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

Aggregations

StreamedFile (io.micronaut.http.server.types.files.StreamedFile)2 Get (io.micronaut.http.annotation.Get)1 Produces (io.micronaut.http.annotation.Produces)1 MultipartException (io.micronaut.http.multipart.MultipartException)1 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)1 NettyFileCustomizableResponseType (io.micronaut.http.server.netty.types.NettyFileCustomizableResponseType)1 CustomizableResponseTypeException (io.micronaut.http.server.types.CustomizableResponseTypeException)1 SystemFile (io.micronaut.http.server.types.files.SystemFile)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 ZuliaBase (io.zulia.message.ZuliaBase)1 ZuliaIndexManager (io.zulia.server.index.ZuliaIndexManager)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ZonedDateTime (java.time.ZonedDateTime)1