Search in sources :

Example 31 with RestVariable

use of org.wso2.carbon.bpmn.rest.engine.variable.RestVariable in project carbon-business-process by wso2.

the class BaseTaskService method addLocalVariables.

protected void addLocalVariables(Task task, Map<String, RestVariable> variableMap, String baseUri) {
    TaskService taskService = BPMNOSGIService.getTaskService();
    Map<String, Object> rawVariables = taskService.getVariablesLocal(task.getId());
    List<RestVariable> localVariables = new RestResponseFactory().createRestVariables(rawVariables, task.getId(), RestResponseFactory.VARIABLE_TASK, RestVariable.RestVariableScope.LOCAL, baseUri);
    for (RestVariable var : localVariables) {
        variableMap.put(var.getName(), var);
    }
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory)

Example 32 with RestVariable

use of org.wso2.carbon.bpmn.rest.engine.variable.RestVariable in project carbon-business-process by wso2.

the class BaseTaskService method getVariableFromRequest.

public RestVariable getVariableFromRequest(String taskId, String variableName, String scope, boolean includeBinary, String baseUri) {
    boolean variableFound = false;
    Object value = null;
    RestVariable.RestVariableScope variableScope = RestVariable.getScopeFromString(scope);
    TaskService taskService = BPMNOSGIService.getTaskService();
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    if (variableScope == null) {
        // First, check local variables (which have precedence when no scope is supplied)
        if (taskService.hasVariableLocal(taskId, variableName)) {
            value = taskService.getVariableLocal(taskId, variableName);
            variableScope = RestVariable.RestVariableScope.LOCAL;
            variableFound = true;
        } else {
            // Revert to execution-variable when not present local on the task
            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
            if (task.getExecutionId() != null && runtimeService.hasVariable(task.getExecutionId(), variableName)) {
                value = runtimeService.getVariable(task.getExecutionId(), variableName);
                variableScope = RestVariable.RestVariableScope.GLOBAL;
                variableFound = true;
            }
        }
    } else if (variableScope == RestVariable.RestVariableScope.GLOBAL) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task.getExecutionId() != null && runtimeService.hasVariable(task.getExecutionId(), variableName)) {
            value = runtimeService.getVariable(task.getExecutionId(), variableName);
            variableFound = true;
        }
    } else if (variableScope == RestVariable.RestVariableScope.LOCAL) {
        if (taskService.hasVariableLocal(taskId, variableName)) {
            value = taskService.getVariableLocal(taskId, variableName);
            variableFound = true;
        }
    }
    if (!variableFound) {
        throw new ActivitiObjectNotFoundException("Task '" + taskId + "' doesn't have a variable with name: '" + variableName + "'.", VariableInstanceEntity.class);
    } else {
        return new RestResponseFactory().createRestVariable(variableName, value, variableScope, taskId, RestResponseFactory.VARIABLE_TASK, includeBinary, baseUri);
    }
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) Task(org.activiti.engine.task.Task) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory)

Example 33 with RestVariable

use of org.wso2.carbon.bpmn.rest.engine.variable.RestVariable in project carbon-business-process by wso2.

the class BaseTaskService method addGlobalVariables.

protected void addGlobalVariables(Task task, Map<String, RestVariable> variableMap, String baseUri) {
    if (task.getExecutionId() != null) {
        RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
        Map<String, Object> rawVariables = runtimeService.getVariables(task.getExecutionId());
        List<RestVariable> globalVariables = new RestResponseFactory().createRestVariables(rawVariables, task.getId(), RestResponseFactory.VARIABLE_TASK, RestVariable.RestVariableScope.GLOBAL, baseUri);
        // since local variables get precedence over global ones at all times.
        for (RestVariable var : globalVariables) {
            if (!variableMap.containsKey(var.getName())) {
                variableMap.put(var.getName(), var);
            }
        }
    }
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory)

Example 34 with RestVariable

use of org.wso2.carbon.bpmn.rest.engine.variable.RestVariable in project carbon-business-process by wso2.

the class BaseTaskService method setBinaryVariable.

protected RestVariable setBinaryVariable(MultipartBody multipartBody, Task task, boolean isNew, UriInfo uriInfo) throws IOException {
    boolean debugEnabled = log.isDebugEnabled();
    if (debugEnabled) {
        log.debug("Processing Binary variables");
    }
    Object result = null;
    List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments = multipartBody.getAllAttachments();
    int attachmentSize = attachments.size();
    if (attachmentSize <= 0) {
        throw new ActivitiIllegalArgumentException("No Attachments found with the request body");
    }
    AttachmentDataHolder attachmentDataHolder = new AttachmentDataHolder();
    for (int i = 0; i < attachmentSize; i++) {
        org.apache.cxf.jaxrs.ext.multipart.Attachment attachment = attachments.get(i);
        String contentDispositionHeaderValue = attachment.getHeader("Content-Disposition");
        String contentType = attachment.getHeader("Content-Type");
        if (debugEnabled) {
            log.debug("Going to iterate:" + i);
            log.debug("contentDisposition:" + contentDispositionHeaderValue);
        }
        if (contentDispositionHeaderValue != null) {
            contentDispositionHeaderValue = contentDispositionHeaderValue.trim();
            Map<String, String> contentDispositionHeaderValueMap = Utils.processContentDispositionHeader(contentDispositionHeaderValue);
            String dispositionName = contentDispositionHeaderValueMap.get("name");
            DataHandler dataHandler = attachment.getDataHandler();
            OutputStream outputStream = null;
            if ("name".equals(dispositionName)) {
                try {
                    outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
                } catch (IOException e) {
                    throw new ActivitiIllegalArgumentException("Binary Variable Name Reading error occured", e);
                }
                if (outputStream != null) {
                    String fileName = outputStream.toString();
                    attachmentDataHolder.setName(fileName);
                }
            } else if ("type".equals(dispositionName)) {
                try {
                    outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
                } catch (IOException e) {
                    throw new ActivitiIllegalArgumentException("\"Binary Variable Type Reading error occured", e);
                }
                if (outputStream != null) {
                    String typeName = outputStream.toString();
                    attachmentDataHolder.setType(typeName);
                }
            } else if ("scope".equals(dispositionName)) {
                try {
                    outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
                } catch (IOException e) {
                    throw new ActivitiIllegalArgumentException("Binary Variable scopeDescription Reading error " + "occured", e);
                }
                if (outputStream != null) {
                    String scope = outputStream.toString();
                    attachmentDataHolder.setScope(scope);
                }
            }
            if (contentType != null) {
                if ("file".equals(dispositionName)) {
                    InputStream inputStream = null;
                    try {
                        inputStream = dataHandler.getInputStream();
                    } catch (IOException e) {
                        throw new ActivitiIllegalArgumentException("Error Occured During processing empty body.", e);
                    }
                    if (inputStream != null) {
                        attachmentDataHolder.setContentType(contentType);
                        byte[] attachmentArray = new byte[0];
                        try {
                            attachmentArray = IOUtils.toByteArray(inputStream);
                        } catch (IOException e) {
                            throw new ActivitiIllegalArgumentException("Processing BinaryV variable Body Failed.", e);
                        }
                        attachmentDataHolder.setAttachmentArray(attachmentArray);
                    }
                }
            }
        }
    }
    attachmentDataHolder.printDebug();
    String variableScope = attachmentDataHolder.getScope();
    String variableName = attachmentDataHolder.getName();
    String variableType = attachmentDataHolder.getType();
    byte[] attachmentArray = attachmentDataHolder.getAttachmentArray();
    try {
        if (variableName == null) {
            throw new ActivitiIllegalArgumentException("No variable name was found in request body.");
        }
        if (attachmentArray == null) {
            throw new ActivitiIllegalArgumentException("Empty attachment body was found in request body after " + "decoding the request" + ".");
        }
        if (variableType != null) {
            if (!RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType) && !RestResponseFactory.SERIALIZABLE_VARIABLE_TYPE.equals(variableType)) {
                throw new ActivitiIllegalArgumentException("Only 'binary' and 'serializable' are supported as variable type.");
            }
        } else {
            attachmentDataHolder.setType(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE);
        }
        RestVariable.RestVariableScope scope = RestVariable.RestVariableScope.LOCAL;
        if (variableScope != null) {
            scope = RestVariable.getScopeFromString(variableScope);
        }
        if (variableScope != null) {
            scope = RestVariable.getScopeFromString(variableScope);
        }
        if (RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType)) {
            // Use raw bytes as variable value
            setVariable(task, variableName, attachmentArray, scope, isNew);
        } else {
            // Try deserializing the object
            try (InputStream inputStream = new ByteArrayInputStream(attachmentArray);
                ObjectInputStream stream = new ObjectInputStream(inputStream)) {
                Object value = stream.readObject();
                setVariable(task, variableName, value, scope, isNew);
            }
        }
        return new RestResponseFactory().createBinaryRestVariable(variableName, scope, variableType, task.getId(), null, null, uriInfo.getBaseUri().toString());
    } catch (IOException ioe) {
        throw new ActivitiIllegalArgumentException("Error getting binary variable", ioe);
    } catch (ClassNotFoundException ioe) {
        throw new BPMNContentNotSupportedException("The provided body contains a serialized object for which the class is nog found: " + ioe.getMessage());
    }
}
Also used : DataHandler(javax.activation.DataHandler) BPMNContentNotSupportedException(org.wso2.carbon.bpmn.rest.common.exception.BPMNContentNotSupportedException) RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) AttachmentDataHolder(org.wso2.carbon.bpmn.rest.model.runtime.AttachmentDataHolder) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory)

Example 35 with RestVariable

use of org.wso2.carbon.bpmn.rest.engine.variable.RestVariable in project carbon-business-process by wso2.

the class BaseRuntimeService method addLocalVariables.

protected void addLocalVariables(Execution execution, int variableType, Map<String, RestVariable> variableMap) {
    Map<String, Object> rawLocalvariables = runtimeService.getVariablesLocal(execution.getId());
    List<RestVariable> localVariables = new RestResponseFactory().createRestVariables(rawLocalvariables, execution.getId(), variableType, RestVariable.RestVariableScope.LOCAL, uriInfo.getBaseUri().toString());
    for (RestVariable var : localVariables) {
        variableMap.put(var.getName(), var);
    }
}
Also used : RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory)

Aggregations

RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)52 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)30 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)15 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)14 IOException (java.io.IOException)11 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)11 Response (javax.ws.rs.core.Response)10 RuntimeService (org.activiti.engine.RuntimeService)10 Execution (org.activiti.engine.runtime.Execution)10 ActivitiException (org.activiti.engine.ActivitiException)9 Path (javax.ws.rs.Path)8 JAXBContext (javax.xml.bind.JAXBContext)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ObjectOutputStream (java.io.ObjectOutputStream)7 Produces (javax.ws.rs.Produces)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Consumes (javax.ws.rs.Consumes)5 JAXBException (javax.xml.bind.JAXBException)5 XMLStreamException (javax.xml.stream.XMLStreamException)5