Search in sources :

Example 1 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class TaskVariableCollectionResource method createOrUpdateTaskVariables.

private Object createOrUpdateTaskVariables(HttpServletRequest request, HttpServletResponse response, Task task, boolean override) {
    Object result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, task, true);
    } else {
        List<RestVariable> inputVariables = new ArrayList<RestVariable>();
        List<RestVariable> resultVariables = new ArrayList<RestVariable>();
        result = resultVariables;
        try {
            @SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(request.getInputStream(), List.class);
            for (Object restObject : variableObjects) {
                RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
                inputVariables.add(restVariable);
            }
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
        }
        if (inputVariables == null || inputVariables.size() == 0) {
            throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
        }
        RestVariableScope sharedScope = null;
        RestVariableScope varScope = null;
        Map<String, Object> variablesToSet = new HashMap<String, Object>();
        for (RestVariable var : inputVariables) {
            // Validate if scopes match
            varScope = var.getVariableScope();
            if (var.getName() == null) {
                throw new ActivitiIllegalArgumentException("Variable name is required");
            }
            if (varScope == null) {
                varScope = RestVariableScope.LOCAL;
            }
            if (sharedScope == null) {
                sharedScope = varScope;
            }
            if (varScope != sharedScope) {
                throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
            }
            if (!override && hasVariableOnScope(task, var.getName(), varScope)) {
                throw new ActivitiConflictException("Variable '" + var.getName() + "' is already present on task '" + task.getId() + "'.");
            }
            Object actualVariableValue = restResponseFactory.getVariableValue(var);
            variablesToSet.put(var.getName(), actualVariableValue);
            resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, task.getId(), RestResponseFactory.VARIABLE_TASK, false));
        }
        if (!variablesToSet.isEmpty()) {
            if (sharedScope == RestVariableScope.LOCAL) {
                taskService.setVariablesLocal(task.getId(), variablesToSet);
            } else {
                if (task.getExecutionId() != null) {
                    // Explicitly set on execution, setting non-local variables on task will override local-variables if exists
                    runtimeService.setVariables(task.getExecutionId(), variablesToSet);
                } else {
                    // Standalone task, no global variables possible
                    throw new ActivitiIllegalArgumentException("Cannot set global variables on task '" + task.getId() + "', task is not part of process.");
                }
            }
        }
    }
    response.setStatus(HttpStatus.CREATED.value());
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) ActivitiConflictException(org.activiti.rest.exception.ActivitiConflictException) ActivitiConflictException(org.activiti.rest.exception.ActivitiConflictException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ArrayList(java.util.ArrayList) List(java.util.List) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 2 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class TaskVariableResource method updateVariable.

@RequestMapping(value = "/runtime/tasks/{taskId}/variables/{variableName}", method = RequestMethod.PUT, produces = "application/json")
public RestVariable updateVariable(@PathVariable("taskId") String taskId, @PathVariable("variableName") String variableName, @RequestParam(value = "scope", required = false) String scope, HttpServletRequest request) {
    Task task = getTaskFromRequest(taskId);
    RestVariable result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, task, false);
        if (!result.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
    } else {
        RestVariable restVariable = null;
        try {
            restVariable = objectMapper.readValue(request.getInputStream(), RestVariable.class);
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("Error converting request body to RestVariable instance", e);
        }
        if (restVariable == null) {
            throw new ActivitiException("Invalid body was supplied");
        }
        if (!restVariable.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
        result = setSimpleVariable(restVariable, task, false);
    }
    return result;
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) Task(org.activiti.engine.task.Task) ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class UserPictureResource method updateUserPicture.

@RequestMapping(value = "/identity/users/{userId}/picture", method = RequestMethod.PUT)
public void updateUserPicture(@PathVariable String userId, HttpServletRequest request, HttpServletResponse response) {
    User user = getUserFromRequest(userId);
    if (request instanceof MultipartHttpServletRequest == false) {
        throw new ActivitiIllegalArgumentException("Multipart request is required");
    }
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    if (multipartRequest.getFileMap().size() == 0) {
        throw new ActivitiIllegalArgumentException("Multipart request with file content is required");
    }
    MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
    try {
        String mimeType = file.getContentType();
        int size = ((Long) file.getSize()).intValue();
        // Copy file-body in a bytearray as the engine requires this
        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream(size);
        IOUtils.copy(file.getInputStream(), bytesOutput);
        Picture newPicture = new Picture(bytesOutput.toByteArray(), mimeType);
        identityService.setUserPicture(user.getId(), newPicture);
        response.setStatus(HttpStatus.NO_CONTENT.value());
    } catch (Exception e) {
        throw new ActivitiException("Error while reading uploaded file: " + e.getMessage(), e);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ActivitiException(org.activiti.engine.ActivitiException) User(org.activiti.engine.identity.User) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Picture(org.activiti.engine.identity.Picture) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class DeploymentCollectionResource method uploadDeployment.

@RequestMapping(value = "/repository/deployments", method = RequestMethod.POST, produces = "application/json")
public DeploymentResponse uploadDeployment(@RequestParam(value = "tenantId", required = false) String tenantId, HttpServletRequest request, HttpServletResponse response) {
    if (request instanceof MultipartHttpServletRequest == false) {
        throw new ActivitiIllegalArgumentException("Multipart request is required");
    }
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    if (multipartRequest.getFileMap().size() == 0) {
        throw new ActivitiIllegalArgumentException("Multipart request with file content is required");
    }
    MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
    try {
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        String fileName = file.getOriginalFilename();
        if (StringUtils.isEmpty(fileName) || !(fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn") || fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip"))) {
            fileName = file.getName();
        }
        if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
            deploymentBuilder.addInputStream(fileName, file.getInputStream());
        } else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
            deploymentBuilder.addZipInputStream(new ZipInputStream(file.getInputStream()));
        } else {
            throw new ActivitiIllegalArgumentException("File must be of type .bpmn20.xml, .bpmn, .bar or .zip");
        }
        deploymentBuilder.name(fileName);
        if (tenantId != null) {
            deploymentBuilder.tenantId(tenantId);
        }
        Deployment deployment = deploymentBuilder.deploy();
        response.setStatus(HttpStatus.CREATED.value());
        return restResponseFactory.createDeploymentResponse(deployment);
    } catch (Exception e) {
        if (e instanceof ActivitiException) {
            throw (ActivitiException) e;
        }
        throw new ActivitiException(e.getMessage(), e);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ZipInputStream(java.util.zip.ZipInputStream) ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Deployment(org.activiti.engine.repository.Deployment) DeploymentBuilder(org.activiti.engine.repository.DeploymentBuilder) ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class ModelSourceExtraResource method setModelSource.

@RequestMapping(value = "/repository/models/{modelId}/source-extra", method = RequestMethod.PUT)
protected void setModelSource(@PathVariable String modelId, HttpServletRequest request, HttpServletResponse response) {
    Model model = getModelFromRequest(modelId);
    if (model != null) {
        try {
            if (request instanceof MultipartHttpServletRequest == false) {
                throw new ActivitiIllegalArgumentException("Multipart request is required");
            }
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            if (multipartRequest.getFileMap().size() == 0) {
                throw new ActivitiIllegalArgumentException("Multipart request with file content is required");
            }
            MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
            repositoryService.addModelEditorSourceExtra(model.getId(), file.getBytes());
            response.setStatus(HttpStatus.NO_CONTENT.value());
        } catch (Exception e) {
            throw new ActivitiException("Error adding model editor source extra", e);
        }
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Model(org.activiti.engine.repository.Model) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)39 MultipartFile (org.springframework.web.multipart.MultipartFile)24 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)10 File (java.io.File)8 ActivitiException (org.activiti.engine.ActivitiException)8 ArrayList (java.util.ArrayList)7 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 HashMap (java.util.HashMap)5 RestVariable (org.activiti.rest.service.api.engine.variable.RestVariable)5 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 IOException (java.io.IOException)4 DataGridException (com.emc.metalnx.core.domain.exceptions.DataGridException)2 OptUploadFileRespDto (com.paascloud.provider.model.dto.oss.OptUploadFileRespDto)2 ApiOperation (io.swagger.annotations.ApiOperation)2 InputStreamReader (java.io.InputStreamReader)2 Date (java.util.Date)2 List (java.util.List)2 ZipInputStream (java.util.zip.ZipInputStream)2