Search in sources :

Example 1 with VFSStreamingOutput

use of org.olat.core.util.vfs.restapi.VFSStreamingOutput in project openolat by klemens.

the class ForumWebService method getAttachment.

/**
 * Retrieves the attachment of the message
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.404.doc The identity or the portrait not found
 * @param messageKey The identity key of the user being searched
 * @param filename The name of the attachment
 * @param request The REST request
 * @return The attachment
 */
@GET
@Path("posts/{messageKey}/attachments/{filename}")
@Produces({ "*/*", MediaType.APPLICATION_OCTET_STREAM })
public Response getAttachment(@PathParam("messageKey") Long messageKey, @PathParam("filename") String filename, @Context Request request) {
    // load message
    Message mess = fom.loadMessage(messageKey);
    if (mess == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!forum.equalsByPersistableKey(mess.getForum())) {
        return Response.serverError().status(Status.CONFLICT).build();
    }
    VFSContainer container = fom.getMessageContainer(mess.getForum().getKey(), mess.getKey());
    VFSItem item = container.resolve(filename);
    if (item instanceof LocalFileImpl) {
        // local file -> the length is given to the client which is good
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            File attachment = ((LocalFileImpl) item).getBasefile();
            String mimeType = WebappHelper.getMimeType(attachment.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    } else if (item instanceof VFSLeaf) {
        // stream -> the length is not given to the client which is not nice
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            StreamingOutput attachment = new VFSStreamingOutput((VFSLeaf) item);
            String mimeType = WebappHelper.getMimeType(item.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    }
    return Response.serverError().status(Status.NOT_FOUND).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Message(org.olat.modules.fo.Message) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) StreamingOutput(javax.ws.rs.core.StreamingOutput) Date(java.util.Date) Response(javax.ws.rs.core.Response) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with VFSStreamingOutput

use of org.olat.core.util.vfs.restapi.VFSStreamingOutput in project OpenOLAT by OpenOLAT.

the class ForumWebService method getAttachment.

/**
 * Retrieves the attachment of the message
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.404.doc The identity or the portrait not found
 * @param messageKey The identity key of the user being searched
 * @param filename The name of the attachment
 * @param request The REST request
 * @return The attachment
 */
@GET
@Path("posts/{messageKey}/attachments/{filename}")
@Produces({ "*/*", MediaType.APPLICATION_OCTET_STREAM })
public Response getAttachment(@PathParam("messageKey") Long messageKey, @PathParam("filename") String filename, @Context Request request) {
    // load message
    Message mess = fom.loadMessage(messageKey);
    if (mess == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!forum.equalsByPersistableKey(mess.getForum())) {
        return Response.serverError().status(Status.CONFLICT).build();
    }
    VFSContainer container = fom.getMessageContainer(mess.getForum().getKey(), mess.getKey());
    VFSItem item = container.resolve(filename);
    if (item instanceof LocalFileImpl) {
        // local file -> the length is given to the client which is good
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            File attachment = ((LocalFileImpl) item).getBasefile();
            String mimeType = WebappHelper.getMimeType(attachment.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    } else if (item instanceof VFSLeaf) {
        // stream -> the length is not given to the client which is not nice
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            StreamingOutput attachment = new VFSStreamingOutput((VFSLeaf) item);
            String mimeType = WebappHelper.getMimeType(item.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    }
    return Response.serverError().status(Status.NOT_FOUND).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Message(org.olat.modules.fo.Message) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) StreamingOutput(javax.ws.rs.core.StreamingOutput) Date(java.util.Date) Response(javax.ws.rs.core.Response) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

File (java.io.File)2 Date (java.util.Date)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Response (javax.ws.rs.core.Response)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)2 VFSContainer (org.olat.core.util.vfs.VFSContainer)2 VFSItem (org.olat.core.util.vfs.VFSItem)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2 VFSStreamingOutput (org.olat.core.util.vfs.restapi.VFSStreamingOutput)2 Message (org.olat.modules.fo.Message)2