Search in sources :

Example 1 with MultipartFile

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

the class TaskVariableBaseResource method setBinaryVariable.

protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, Task task, boolean isNew) {
    // Validate input and set defaults
    if (request.getFileMap().size() == 0) {
        throw new ActivitiIllegalArgumentException("No file content was found in request body.");
    }
    // Get first file in the map, ignore possible other files
    MultipartFile file = request.getFile(request.getFileMap().keySet().iterator().next());
    if (file == null) {
        throw new ActivitiIllegalArgumentException("No file content was found in request body.");
    }
    String variableScope = null;
    String variableName = null;
    String variableType = null;
    Map<String, String[]> paramMap = request.getParameterMap();
    for (String parameterName : paramMap.keySet()) {
        if (paramMap.get(parameterName).length > 0) {
            if (parameterName.equalsIgnoreCase("scope")) {
                variableScope = paramMap.get(parameterName)[0];
            } else if (parameterName.equalsIgnoreCase("name")) {
                variableName = paramMap.get(parameterName)[0];
            } else if (parameterName.equalsIgnoreCase("type")) {
                variableType = paramMap.get(parameterName)[0];
            }
        }
    }
    try {
        if (variableName == null) {
            throw new ActivitiIllegalArgumentException("No variable name was found in request body.");
        }
        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 {
            variableType = RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE;
        }
        RestVariableScope scope = RestVariableScope.LOCAL;
        if (variableScope != null) {
            scope = RestVariable.getScopeFromString(variableScope);
        }
        if (variableType.equals(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE)) {
            // Use raw bytes as variable value
            byte[] variableBytes = IOUtils.toByteArray(file.getInputStream());
            setVariable(task, variableName, variableBytes, scope, isNew);
        } else if (isSerializableVariableAllowed) {
            // Try deserializing the object
            ObjectInputStream stream = new ObjectInputStream(file.getInputStream());
            Object value = stream.readObject();
            setVariable(task, variableName, value, scope, isNew);
            stream.close();
        } else {
            throw new ActivitiContentNotSupportedException("Serialized objects are not allowed");
        }
        return restResponseFactory.createBinaryRestVariable(variableName, scope, variableType, task.getId(), null, null);
    } catch (IOException ioe) {
        throw new ActivitiIllegalArgumentException("Error getting binary variable", ioe);
    } catch (ClassNotFoundException ioe) {
        throw new ActivitiContentNotSupportedException("The provided body contains a serialized object for which the class is nog found: " + ioe.getMessage());
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ActivitiContentNotSupportedException(org.activiti.rest.exception.ActivitiContentNotSupportedException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with MultipartFile

use of org.springframework.web.multipart.MultipartFile 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 3 with MultipartFile

use of org.springframework.web.multipart.MultipartFile 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 4 with MultipartFile

use of org.springframework.web.multipart.MultipartFile 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)

Example 5 with MultipartFile

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

the class ModelSourceResource method setModelSource.

@RequestMapping(value = "/repository/models/{modelId}/source", method = RequestMethod.PUT)
protected void setModelSource(@PathVariable String modelId, HttpServletRequest request, HttpServletResponse response) {
    Model model = getModelFromRequest(modelId);
    if (model != null) {
        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 {
            repositoryService.addModelEditorSource(modelId, 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

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15