Search in sources :

Example 11 with DocumentInstance

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

the class DocumentServiceBase method updateDocument.

public void updateDocument(String documentId, String documentPayload, String marshallingType) {
    logger.debug("About to unmarshal document payload '{}' with marshaling type {}", documentPayload, marshallingType);
    DocumentInstance documentInstance = marshallerHelper.unmarshal(documentPayload, marshallingType, DocumentInstance.class);
    logger.debug("Document created from payload {}", documentInstance);
    Document document = documentStorageService.getDocument(documentId);
    logger.debug("Document found {}", documentInstance != null);
    if (document == null) {
        throw new KieServerRuntimeException("No document found with id " + documentId);
    }
    documentStorageService.saveDocument(document, documentInstance.getContent());
    logger.debug("Document {} updated successfully", document);
}
Also used : KieServerRuntimeException(org.kie.server.services.api.KieServerRuntimeException) DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) Document(org.jbpm.document.Document)

Example 12 with DocumentInstance

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

the class DocumentServiceIntegrationTest method testListDocuments.

@Test
public void testListDocuments() {
    List<DocumentInstance> docs = documentClient.listDocuments(0, 10);
    assertNotNull(docs);
    assertEquals(0, docs.size());
    String documentId = documentClient.createDocument(document);
    assertNotNull(documentId);
    docs = documentClient.listDocuments(0, 10);
    assertNotNull(docs);
    assertEquals(1, docs.size());
    DocumentInstance fromServer = docs.get(0);
    assertEquals(documentId, fromServer.getIdentifier());
    assertDocumentInstances(document, fromServer, false);
}
Also used : DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) Test(org.junit.Test)

Example 13 with DocumentInstance

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

the class JbpmRestIntegrationTest method testUploadListDownloadDocument.

@Test
public void testUploadListDownloadDocument() throws Exception {
    KieServerUtil.deleteDocumentStorageFolder();
    Marshaller marshaller = MarshallerFactory.getMarshaller(new HashSet<Class<?>>(extraClasses.values()), marshallingFormat, client.getClassLoader());
    DocumentInstance documentInstance = DocumentInstance.builder().name("test file.txt").size(50).content("test content".getBytes()).lastModified(new Date()).build();
    String documentEntity = marshaller.marshall(documentInstance);
    Map<String, Object> empty = new HashMap<>();
    Response response = null;
    try {
        // create document
        WebTarget clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), DOCUMENT_URI, empty));
        logger.info("[POST] " + clientRequest.getUri());
        response = clientRequest.request(acceptHeadersByFormat.get(marshallingFormat)).post(createEntity(documentEntity));
        Assert.assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        String documentId = response.readEntity(JaxbString.class).unwrap();
        assertNotNull(documentId);
        // list available documents without paging info
        Map<String, Object> valuesMap = new HashMap<String, Object>();
        valuesMap.put(DOCUMENT_ID, documentId);
        clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), DOCUMENT_URI, valuesMap));
        logger.info("[GET] " + clientRequest.getUri());
        response = clientRequest.request(getMediaType()).get();
        Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
        DocumentInstanceList docList = marshaller.unmarshall(response.readEntity(String.class), DocumentInstanceList.class);
        assertNotNull(docList);
        List<DocumentInstance> docs = docList.getItems();
        assertNotNull(docs);
        assertEquals(1, docs.size());
        DocumentInstance doc = docs.get(0);
        assertNotNull(doc);
        assertEquals(documentInstance.getName(), doc.getName());
        assertEquals(documentId, doc.getIdentifier());
        // download document content
        valuesMap = new HashMap<String, Object>();
        valuesMap.put(DOCUMENT_ID, documentId);
        clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), DOCUMENT_URI + "/" + DOCUMENT_INSTANCE_CONTENT_GET_URI, valuesMap));
        logger.info("[GET] " + clientRequest.getUri());
        response = clientRequest.request(getMediaType()).accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).get();
        Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
        String contentDisposition = response.getHeaderString("Content-Disposition");
        assertTrue(contentDisposition.contains(documentInstance.getName()));
        byte[] content = response.readEntity(byte[].class);
        assertNotNull(content);
        String stringContent = new String(content);
        assertEquals("test content", stringContent);
        response.close();
        // delete document
        valuesMap = new HashMap<String, Object>();
        valuesMap.put(DOCUMENT_ID, documentId);
        clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), DOCUMENT_URI + "/" + DOCUMENT_INSTANCE_DELETE_URI, valuesMap));
        logger.info("[DELETE] " + clientRequest.getUri());
        response = clientRequest.request(getMediaType()).delete();
        int noContentStatusCode = Response.Status.NO_CONTENT.getStatusCode();
        int okStatusCode = Response.Status.OK.getStatusCode();
        assertTrue("Wrong status code returned: " + response.getStatus(), response.getStatus() == noContentStatusCode || response.getStatus() == okStatusCode);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : JaxbString(org.kie.server.api.model.type.JaxbString) Marshaller(org.kie.server.api.marshalling.Marshaller) HashMap(java.util.HashMap) JaxbString(org.kie.server.api.model.type.JaxbString) Date(java.util.Date) Response(javax.ws.rs.core.Response) DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) BeforeClass(org.junit.BeforeClass) WebTarget(javax.ws.rs.client.WebTarget) DocumentInstanceList(org.kie.server.api.model.instance.DocumentInstanceList) Test(org.junit.Test)

Example 14 with DocumentInstance

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

the class DocumentServiceBase method convertDocument.

protected DocumentInstance convertDocument(Document document, boolean withContent) {
    if (document == null) {
        return null;
    }
    Builder documentBuilder = DocumentInstance.builder();
    documentBuilder.id(document.getIdentifier()).name(document.getName()).link(document.getLink()).size(document.getSize()).lastModified(document.getLastModified());
    if (withContent) {
        documentBuilder.content(document.getContent());
    }
    DocumentInstance documentInstance = documentBuilder.build();
    return documentInstance;
}
Also used : Builder(org.kie.server.api.model.instance.DocumentInstance.Builder) DocumentInstance(org.kie.server.api.model.instance.DocumentInstance)

Example 15 with DocumentInstance

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

the class DocumentServiceBase method listDocuments.

public DocumentInstanceList listDocuments(Integer page, Integer pageSize) {
    logger.debug("About to list documents with page {} and pageSize {}", page, pageSize);
    final List<Document> documents = documentStorageService.listDocuments(page, pageSize);
    logger.debug("Documents loaded from repository {}", documents);
    DocumentInstanceList result = new DocumentInstanceList(Collections.emptyList());
    if (documents == null) {
        return result;
    }
    List<DocumentInstance> list = convertDocumentList(documents);
    result.setDocumentInstances(list.toArray(new DocumentInstance[list.size()]));
    return result;
}
Also used : DocumentInstance(org.kie.server.api.model.instance.DocumentInstance) Document(org.jbpm.document.Document) DocumentInstanceList(org.kie.server.api.model.instance.DocumentInstanceList)

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