Search in sources :

Example 1 with DocumentInstanceList

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

the class DocumentResource method listDocuments.

@ApiOperation(value = "Returns all documents from KIE Server.")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error"), @ApiResponse(code = 200, response = DocumentInstanceList.class, message = "Successful response", examples = @Example(value = { @ExampleProperty(mediaType = JSON, value = GET_DOCUMENTS_RESPONSE_JSON) })) })
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response listDocuments(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = "optional pagination - at which page to start, defaults to 0 (meaning first)", required = false) @QueryParam("page") @DefaultValue("0") Integer page, @ApiParam(value = "optional pagination - size of the result, defaults to 10", required = false) @QueryParam("pageSize") @DefaultValue("10") Integer pageSize) {
    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 {
        DocumentInstanceList documents = documentServiceBase.listDocuments(page, pageSize);
        return createCorrectVariant(documents, headers, Response.Status.OK, conversationIdHeader);
    } 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) WebApplicationException(javax.ws.rs.WebApplicationException) KieServerRuntimeException(org.kie.server.services.api.KieServerRuntimeException) IOException(java.io.IOException) DocumentInstanceList(org.kie.server.api.model.instance.DocumentInstanceList) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with DocumentInstanceList

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

the class DocumentServicesClientImpl method listDocuments.

@Override
public List<DocumentInstance> listDocuments(Integer page, Integer pageSize) {
    DocumentInstanceList result = null;
    if (config.isRest()) {
        Map<String, Object> valuesMap = new HashMap<String, Object>();
        String queryString = getPagingQueryString("", page, pageSize);
        result = makeHttpGetRequestAndCreateCustomResponse(build(loadBalancer.getUrl(), DOCUMENT_URI, valuesMap) + queryString, DocumentInstanceList.class);
    } else {
        CommandScript script = new CommandScript(Collections.singletonList((KieServerCommand) new DescriptorCommand("DocumentService", "listDocuments", new Object[] { page, pageSize })));
        ServiceResponse<DocumentInstanceList> response = (ServiceResponse<DocumentInstanceList>) executeJmsCommand(script, DescriptorCommand.class.getName(), "BPM").getResponses().get(0);
        throwExceptionOnFailure(response);
        if (shouldReturnWithNullResponse(response)) {
            return null;
        }
        result = response.getResult();
    }
    return result.getItems();
}
Also used : DescriptorCommand(org.kie.server.api.commands.DescriptorCommand) ServiceResponse(org.kie.server.api.model.ServiceResponse) KieServerCommand(org.kie.server.api.model.KieServerCommand) HashMap(java.util.HashMap) CommandScript(org.kie.server.api.commands.CommandScript) DocumentInstanceList(org.kie.server.api.model.instance.DocumentInstanceList)

Example 3 with DocumentInstanceList

use of org.kie.server.api.model.instance.DocumentInstanceList 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 4 with DocumentInstanceList

use of org.kie.server.api.model.instance.DocumentInstanceList 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

DocumentInstanceList (org.kie.server.api.model.instance.DocumentInstanceList)4 HashMap (java.util.HashMap)2 DocumentInstance (org.kie.server.api.model.instance.DocumentInstance)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ResponseHeader (io.swagger.annotations.ResponseHeader)1 IOException (java.io.IOException)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 WebTarget (javax.ws.rs.client.WebTarget)1 Response (javax.ws.rs.core.Response)1 Variant (javax.ws.rs.core.Variant)1 Document (org.jbpm.document.Document)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1 CommandScript (org.kie.server.api.commands.CommandScript)1 DescriptorCommand (org.kie.server.api.commands.DescriptorCommand)1 Marshaller (org.kie.server.api.marshalling.Marshaller)1