Search in sources :

Example 1 with DocumentInstance

use of org.kie.server.api.model.instance.DocumentInstance in project droolsjbpm-integration by kiegroup.

the class DocumentResource method getDocumentContent.

@ApiOperation(value = "Retrieves document's content identified by given documentId", response = byte[].class, code = 200, responseHeaders = { @ResponseHeader(name = "Content-Disposition", description = "provides file name of the document") })
@ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error"), @ApiResponse(code = 404, message = "Document with given id not found") })
@GET
@Path(DOCUMENT_INSTANCE_CONTENT_GET_URI)
@Produces({ MediaType.APPLICATION_OCTET_STREAM })
public Response getDocumentContent(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = "document id of a document that content should be retruned from", required = true, example = "xxx-yyy-zzz") @PathParam("documentId") String documentId) {
    Variant v = getVariant(headers);
    // no container id available so only used to transfer conversation id if given by client
    Header conversationIdHeader = buildConversationIdHeader("", context, headers);
    try {
        final DocumentInstance document = documentServiceBase.getDocument(documentId);
        if (document == null) {
            return notFound("Document with id " + documentId + " not found", v, conversationIdHeader);
        }
        String fileName = MimeUtility.encodeWord(document.getName(), "utf-8", "Q");
        StreamingOutput entity = new StreamingOutput() {

            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException {
                output.write(document.getContent());
            }
        };
        if (conversationIdHeader != null) {
            return Response.ok().entity(entity).header(conversationIdHeader.getName(), conversationIdHeader.getValue()).header("Content-Disposition", "attachment; filename=\"" + fileName + "\"").build();
        }
        return Response.ok().entity(entity).header("Content-Disposition", "attachment; filename=\"" + fileName + "\"").build();
    } catch (Exception e) {
        logger.error("Unexpected error during processing {}", e.getMessage(), e);
        return internalServerError(errorMessage(e), v, conversationIdHeader);
    }
}
Also used : RestUtils.getVariant(org.kie.server.remote.rest.common.util.RestUtils.getVariant) RestUtils.createCorrectVariant(org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant) Variant(javax.ws.rs.core.Variant) Header(org.kie.server.remote.rest.common.Header) RestUtils.buildConversationIdHeader(org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader) ResponseHeader(io.swagger.annotations.ResponseHeader) OutputStream(java.io.OutputStream) DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) StreamingOutput(javax.ws.rs.core.StreamingOutput) WebApplicationException(javax.ws.rs.WebApplicationException) KieServerRuntimeException(org.kie.server.services.api.KieServerRuntimeException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with DocumentInstance

use of org.kie.server.api.model.instance.DocumentInstance in project droolsjbpm-integration by kiegroup.

the class DocumentServiceIntegrationTest method testCreateLoadDeleteDocument.

@Test
@Category(Smoke.class)
public void testCreateLoadDeleteDocument() throws Exception {
    String documentId = documentClient.createDocument(document);
    assertNotNull(documentId);
    DocumentInstance fromServer = documentClient.getDocument(documentId);
    assertEquals(documentId, fromServer.getIdentifier());
    assertDocumentInstances(document, fromServer, true);
    documentClient.deleteDocument(documentId);
    try {
        documentClient.getDocument(documentId);
        fail("Document with id " + documentId + " was deleted so should not be found anymore");
    } catch (KieServicesException e) {
    // expected
    }
}
Also used : DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) KieServicesException(org.kie.server.api.exception.KieServicesException) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 3 with DocumentInstance

use of org.kie.server.api.model.instance.DocumentInstance in project droolsjbpm-integration by kiegroup.

the class DocumentServiceIntegrationTest method testUpdateDocument.

@Test
public void testUpdateDocument() {
    String documentId = documentClient.createDocument(document);
    assertNotNull(documentId);
    DocumentInstance fromServer = documentClient.getDocument(documentId);
    assertEquals(documentId, fromServer.getIdentifier());
    assertDocumentInstances(document, fromServer, true);
    String udpatedDoc = "here comes the update";
    byte[] updateDocBytes = udpatedDoc.getBytes();
    fromServer.setContent(updateDocBytes);
    fromServer.setSize(updateDocBytes.length);
    fromServer.setLastModified(new Date());
    documentClient.updateDocument(fromServer);
    DocumentInstance updatedFromServer = documentClient.getDocument(documentId);
    assertEquals(documentId, updatedFromServer.getIdentifier());
    assertDocumentInstances(fromServer, updatedFromServer, true);
}
Also used : DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) Date(java.util.Date) Test(org.junit.Test)

Example 4 with DocumentInstance

use of org.kie.server.api.model.instance.DocumentInstance in project droolsjbpm-integration by kiegroup.

the class DocumentServiceIntegrationTest method testCreateEmptyDocument.

@Test
public void testCreateEmptyDocument() {
    content = "";
    contentBytes = content.getBytes();
    document = DocumentInstance.builder().name("first document").size(contentBytes.length).lastModified(new Date()).content(contentBytes).build();
    String documentId = documentClient.createDocument(document);
    assertNotNull(documentId);
    DocumentInstance fromServer = documentClient.getDocument(documentId);
    assertEquals(documentId, fromServer.getIdentifier());
    assertDocumentInstances(document, fromServer, true);
}
Also used : DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) Date(java.util.Date) Test(org.junit.Test)

Example 5 with DocumentInstance

use of org.kie.server.api.model.instance.DocumentInstance in project droolsjbpm-integration by kiegroup.

the class DocumentServiceIntegrationTest method testDeleteDocument.

@Test
public void testDeleteDocument() throws Exception {
    String documentId = documentClient.createDocument(document);
    assertNotNull(documentId);
    DocumentInstance fromServer = documentClient.getDocument(documentId);
    assertNotNull(fromServer);
    documentClient.deleteDocument(documentId);
    try {
        documentClient.getDocument(documentId);
        fail("Document with id " + documentId + " was deleted so should not be found anymore");
    } catch (KieServicesException e) {
    // expected
    }
}
Also used : DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) KieServicesException(org.kie.server.api.exception.KieServicesException) Test(org.junit.Test)

Aggregations

DocumentInstance (org.kie.server.api.model.instance.DocumentInstance)17 Test (org.junit.Test)9 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Document (org.jbpm.document.Document)4 KieServerRuntimeException (org.kie.server.services.api.KieServerRuntimeException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ResponseHeader (io.swagger.annotations.ResponseHeader)2 IOException (java.io.IOException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Variant (javax.ws.rs.core.Variant)2 KieServicesException (org.kie.server.api.exception.KieServicesException)2 ServiceResponse (org.kie.server.api.model.ServiceResponse)2 DocumentInstanceList (org.kie.server.api.model.instance.DocumentInstanceList)2 Header (org.kie.server.remote.rest.common.Header)2 RestUtils.buildConversationIdHeader (org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader)2