package solutions.digamma.damas.rs.content;

import solutions.digamma.damas.common.WorkspaceException;
import solutions.digamma.damas.content.Document;
import solutions.digamma.damas.content.DocumentManager;
import solutions.digamma.damas.rs.common.Authenticated;
import solutions.digamma.damas.rs.common.CrudResource;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.util.Optional;

/**
 * Document REST endpoint.
 *
 * @label Document
 * @author Ahmad Shahwan
 */
@Path("documents")
public class DocumentResource
        extends CrudResource<Document, DocumentSerialization> {

    @Inject
    protected DocumentManager manager;

    /**
     * Provide detailed description of folder.
     */
    @QueryParam("full")
    private boolean full;

    @Override
    protected DocumentManager getManager() {
        return this.manager;
    }

    /**
     * Upload content of an existing document, erase the old content if any.
     *
     * @param id        document ID
     * @param content   content's input stream
     * @throws WorkspaceException
     */
    @PUT
    @Path("/{id}/upload")
    @Authenticated
    public void upload(
            @PathParam("id") String id,
            InputStream content)
            throws WorkspaceException {
        this.manager.upload(id, content);
    }

    @GET
    @Path("/{id}/download")
    @Authenticated
    public Response download(
            @PathParam("id") String id,
            InputStream content)
            throws WorkspaceException {
        Document document = this.manager.retrieve(id);
        InputStream is = this.manager.download(id).getStream();
        String type = Optional.ofNullable(document.getMimeType())
                .orElse(MediaType.APPLICATION_OCTET_STREAM);
        return Response.ok(is, type).build();
    }

    @Override
    protected DocumentSerialization wrap(Document entity) {
        return DocumentSerialization.from(entity, this.full);
    }
}

@@ -345,37 +345,8 @@
 ct;%0A
-import javax.ws.rs.Consumes;%0A
 impo
@@ -448,37 +448,8 @@
 am;%0A
-import javax.ws.rs.Produces;%0A
 impo
@@ -575,35 +575,8 @@
 eam;
-%0Aimport java.util.Optional;
 %0A%0A/*
@@ -769,16 +769,136 @@
 ion%3E %7B%0A%0A
+    /**%0A     * Wild-card media type.%0A     */%0A    private final static MediaType WILDCARD_MEDIA_TYPE = new MediaType();%0A%0A
     @Inj
@@ -1937,124 +1937,59 @@
     
-String type = Optional.ofNullable(document.getMimeType())%0A                .orElse(MediaType.APPLICATION_OCTET_STREAM
+MediaType type = toMediaType(document.getMimeType()
 );%0A 
@@ -2181,11 +2181,303 @@
 );%0A    %7D
+%0A%0A    private MediaType toMediaType(String value) %7B%0A        if (value == null) %7B%0A            return WILDCARD_MEDIA_TYPE;%0A        %7D%0A        try %7B%0A            return MediaType.valueOf(value);%0A        %7D catch (IllegalArgumentException e) %7B%0A            return WILDCARD_MEDIA_TYPE;%0A        %7D%0A    %7D
 %0A%7D%0A

Fix a bug when the mime-type is invalid