Example usage for org.apache.commons.fileupload FileItem getInputStream

List of usage examples for org.apache.commons.fileupload FileItem getInputStream

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItem getInputStream.

Prototype

InputStream getInputStream() throws IOException;

Source Link

Document

Returns an java.io.InputStream InputStream that can be used to retrieve the contents of the file.

Usage

From source file:org.activiti.rest.api.repository.BaseModelSourceResource.java

@Put
public InputRepresentation setModelSource(Representation representation) {
    if (authenticate() == false)
        return null;

    Model model = getModelFromRequest();

    if (!MediaType.MULTIPART_FORM_DATA.isCompatible(representation.getMediaType())) {
        throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(),
                "The request should be of type 'multipart/form-data'.", null, null);
    }//from w  ww .j a  va2 s. com

    RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
    try {
        FileItem uploadItem = null;
        List<FileItem> items = upload.parseRepresentation(representation);
        for (FileItem fileItem : items) {
            uploadItem = fileItem;
        }
        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        int size = ((Long) uploadItem.getSize()).intValue();

        // Copy file-body in a bytearray as the engine requires this
        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream(size);
        IOUtils.copy(uploadItem.getInputStream(), bytesOutput);

        byte[] byteArray = bytesOutput.toByteArray();
        setModelSource(model, byteArray);

        return new InputRepresentation(new ByteArrayInputStream(byteArray), MediaType.APPLICATION_OCTET_STREAM);

    } catch (FileUploadException e) {
        throw new ActivitiException("Error with uploaded file: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ActivitiException("Error while reading uploaded file: " + e.getMessage(), e);
    }
}

From source file:org.activiti.rest.api.repository.DeploymentCollectionResource.java

@Post
public DeploymentResponse uploadDeployment(Representation entity) {
    try {//  w w  w . j  av a 2s .co  m
        if (authenticate() == false)
            return null;

        if (entity == null || entity.getMediaType() == null
                || !MediaType.MULTIPART_FORM_DATA.isCompatible(entity.getMediaType())) {
            throw new ActivitiIllegalArgumentException(
                    "The request should be of type" + MediaType.MULTIPART_FORM_DATA + ".");
        }

        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(entity);

        FileItem uploadItem = null;
        for (FileItem fileItem : items) {
            if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        DeploymentBuilder deploymentBuilder = ActivitiUtil.getRepositoryService().createDeployment();
        String fileName = uploadItem.getName();
        if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
            deploymentBuilder.addInputStream(fileName, uploadItem.getInputStream());
        } else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
            deploymentBuilder.addZipInputStream(new ZipInputStream(uploadItem.getInputStream()));
        } else {
            throw new ActivitiIllegalArgumentException("File must be of type .bpmn20.xml, .bpmn, .bar or .zip");
        }
        deploymentBuilder.name(fileName);
        Deployment deployment = deploymentBuilder.deploy();

        setStatus(Status.SUCCESS_CREATED);

        return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
                .createDeploymentResponse(this, deployment);

    } catch (Exception e) {
        if (e instanceof ActivitiException) {
            throw (ActivitiException) e;
        }
        throw new ActivitiException(e.getMessage(), e);
    }
}

From source file:org.activiti.rest.api.repository.DeploymentUploadResource.java

@Post
public DeploymentResponse uploadDeployment(Representation entity) {
    try {/*ww  w  .  j  a  va 2  s  .c  om*/
        if (authenticate(SecuredResource.ADMIN) == false)
            return null;

        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(entity);

        FileItem uploadItem = null;
        for (FileItem fileItem : items) {
            if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        DeploymentBuilder deploymentBuilder = ActivitiUtil.getRepositoryService().createDeployment();
        String fileName = uploadItem.getName();
        if (fileName.endsWith(".bpmn20.xml")) {
            deploymentBuilder.addInputStream(fileName, uploadItem.getInputStream());
        } else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
            deploymentBuilder.addZipInputStream(new ZipInputStream(uploadItem.getInputStream()));
        } else {
            throw new BadRequestException("File must be of type .bpmn20.xml, .bar or .zip");
        }
        deploymentBuilder.name(fileName);
        Deployment deployment = deploymentBuilder.deploy();
        return new DeploymentResponse(deployment);

    } catch (Exception e) {
        throw new ActivitiException(e.getMessage(), e);
    }
}

From source file:org.activiti.rest.api.runtime.process.BaseExecutionVariableResource.java

protected RestVariable setBinaryVariable(Representation representation, Execution execution, boolean isNew) {
    try {/* w  w w.j  a v  a2s.  c o  m*/
        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(representation);

        String variableScope = null;
        String variableName = null;
        String variableType = null;
        FileItem uploadItem = null;

        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
                if ("scope".equals(fileItem.getFieldName())) {
                    variableScope = fileItem.getString("UTF-8");
                } else if ("name".equals(fileItem.getFieldName())) {
                    variableName = fileItem.getString("UTF-8");
                } else if ("type".equals(fileItem.getFieldName())) {
                    variableType = fileItem.getString("UTF-8");
                }
            } else if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        // Validate input and set defaults
        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        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
            ByteArrayOutputStream variableOutput = new ByteArrayOutputStream(
                    ((Long) uploadItem.getSize()).intValue());
            IOUtils.copy(uploadItem.getInputStream(), variableOutput);
            setVariable(execution, variableName, variableOutput.toByteArray(), scope, isNew);
        } else {
            // Try deserializing the object
            ObjectInputStream stream = new ObjectInputStream(uploadItem.getInputStream());
            Object value = stream.readObject();
            setVariable(execution, variableName, value, scope, isNew);
            stream.close();
        }

        if (execution instanceof ProcessInstance && allowProcessInstanceUrl()) {
            return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
                    .createBinaryRestVariable(this, variableName, scope, variableType, null, null,
                            execution.getId());
        } else {
            return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
                    .createBinaryRestVariable(this, variableName, scope, variableType, null, execution.getId(),
                            null);
        }

    } catch (FileUploadException fue) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, fue);
    } catch (IOException ioe) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
    } catch (ClassNotFoundException ioe) {
        throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(),
                "The provided body contains a serialized object for which the class is nog found: "
                        + ioe.getMessage(),
                null, null);
    }

}

From source file:org.activiti.rest.api.runtime.task.TaskAttachmentCollectionResource.java

protected AttachmentResponse createBinaryAttachment(Representation representation, Task task)
        throws FileUploadException, IOException {
    RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
    List<FileItem> items = upload.parseRepresentation(representation);

    String name = null;//from w ww. j a va 2s.  c  om
    String description = null;
    String type = null;
    FileItem uploadItem = null;

    for (FileItem fileItem : items) {
        if (fileItem.isFormField()) {
            if ("name".equals(fileItem.getFieldName())) {
                name = fileItem.getString("UTF-8");
            } else if ("description".equals(fileItem.getFieldName())) {
                description = fileItem.getString("UTF-8");
            } else if ("type".equals(fileItem.getFieldName())) {
                type = fileItem.getString("UTF-8");
            }
        } else if (fileItem.getName() != null) {
            uploadItem = fileItem;
        }
    }

    if (name == null) {
        throw new ActivitiIllegalArgumentException("Attachment name is required.");
    }

    if (uploadItem == null) {
        throw new ActivitiIllegalArgumentException("Attachment content is required.");
    }

    Attachment createdAttachment = ActivitiUtil.getTaskService().createAttachment(type, task.getId(), null,
            name, description, uploadItem.getInputStream());

    setStatus(Status.SUCCESS_CREATED);
    return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
            .createAttachmentResponse(this, createdAttachment);
}

From source file:org.activiti.rest.api.runtime.task.TaskVariableBaseResource.java

protected RestVariable setBinaryVariable(Representation representation, Task task, boolean isNew) {
    try {//from  w  w  w. jav  a  2 s .com
        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(representation);

        String variableScope = null;
        String variableName = null;
        String variableType = null;
        FileItem uploadItem = null;

        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
                if ("scope".equals(fileItem.getFieldName())) {
                    variableScope = fileItem.getString("UTF-8");
                } else if ("name".equals(fileItem.getFieldName())) {
                    variableName = fileItem.getString("UTF-8");
                } else if ("type".equals(fileItem.getFieldName())) {
                    variableType = fileItem.getString("UTF-8");
                }
            } else if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        // Validate input and set defaults
        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        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
            ByteArrayOutputStream variableOutput = new ByteArrayOutputStream(
                    ((Long) uploadItem.getSize()).intValue());
            IOUtils.copy(uploadItem.getInputStream(), variableOutput);
            setVariable(task, variableName, variableOutput.toByteArray(), scope, isNew);
        } else {
            // Try deserializing the object
            ObjectInputStream stream = new ObjectInputStream(uploadItem.getInputStream());
            Object value = stream.readObject();
            setVariable(task, variableName, value, scope, isNew);
            stream.close();
        }

        return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
                .createBinaryRestVariable(this, variableName, scope, variableType, task.getId(), null, null);

    } catch (FileUploadException fue) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, fue);
    } catch (IOException ioe) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
    } catch (ClassNotFoundException ioe) {
        throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(),
                "The provided body contains a serialized object for which the class is nog found: "
                        + ioe.getMessage(),
                null, null);
    }

}

From source file:org.activiti.rest.api.task.BaseTaskVariableResource.java

protected RestVariable setBinaryVariable(Representation representation, Task task, boolean isNew) {
    try {//from w  w w  . java  2 s  .c o m
        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(representation);

        String variableScope = null;
        String variableName = null;
        String variableType = null;
        FileItem uploadItem = null;

        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
                if ("scope".equals(fileItem.getFieldName())) {
                    variableScope = fileItem.getString("UTF-8");
                } else if ("name".equals(fileItem.getFieldName())) {
                    variableName = fileItem.getString("UTF-8");
                } else if ("type".equals(fileItem.getFieldName())) {
                    variableType = fileItem.getString("UTF-8");
                }
            } else if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        // Validate input and set defaults
        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        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
            ByteArrayOutputStream variableOutput = new ByteArrayOutputStream(
                    ((Long) uploadItem.getSize()).intValue());
            IOUtils.copy(uploadItem.getInputStream(), variableOutput);
            setVariable(task, variableName, variableOutput.toByteArray(), scope, isNew);
        } else {
            // Try deserializing the object
            ObjectInputStream stream = new ObjectInputStream(uploadItem.getInputStream());
            Object value = stream.readObject();
            setVariable(task, variableName, value, scope, isNew);
            stream.close();
        }

        return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
                .createBinaryRestVariable(this, variableName, scope, variableType, task.getId(), null);

    } catch (FileUploadException fue) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, fue);
    } catch (IOException ioe) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
    } catch (ClassNotFoundException ioe) {
        throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(),
                "The provided body contains a serialized object for which the class is nog found: "
                        + ioe.getMessage(),
                null, null);
    }

}

From source file:org.activiti.rest.api.task.TaskAttachmentAddResource.java

@Put
public AttachmentResponse addAttachment(Representation entity) {
    if (authenticate() == false)
        return null;

    String taskId = (String) getRequest().getAttributes().get("taskId");

    try {// ww w  .  jav a  2s  .co m
        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(entity);

        FileItem uploadItem = null;
        for (FileItem fileItem : items) {
            if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        String fileName = uploadItem.getName();

        Attachment attachment = ActivitiUtil.getTaskService().createAttachment(uploadItem.getContentType(),
                taskId, null, fileName, fileName, uploadItem.getInputStream());

        return new AttachmentResponse(attachment);

    } catch (Exception e) {
        throw new ActivitiException("Unable to add new attachment to task " + taskId);
    }
}

From source file:org.activiti.rest.service.api.identity.UserPictureResource.java

@Put
public void updateUserPicture(Representation representation) {
    if (authenticate() == false)
        return;//from w  ww .j a  v a2s. c  o  m
    User user = getUserFromRequest();

    if (!MediaType.MULTIPART_FORM_DATA.isCompatible(representation.getMediaType())) {
        throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(),
                "The request should be of type 'multipart/form-data'.", null, null);
    }

    RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
    try {
        FileItem uploadItem = null;
        List<FileItem> items = upload.parseRepresentation(representation);
        String mimeType = MediaType.IMAGE_JPEG.toString();

        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
                if ("mimeType".equals(fileItem.getFieldName())) {
                    mimeType = fileItem.getString("UTF-8");
                }
            } else if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        int size = ((Long) uploadItem.getSize()).intValue();

        // Copy file-body in a bytearray as the engine requires this
        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream(size);
        IOUtils.copy(uploadItem.getInputStream(), bytesOutput);

        Picture newPicture = new Picture(bytesOutput.toByteArray(), mimeType);
        ActivitiUtil.getIdentityService().setUserPicture(user.getId(), newPicture);

    } catch (FileUploadException e) {
        throw new ActivitiException("Error with uploaded file: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ActivitiException("Error while reading uploaded file: " + e.getMessage(), e);
    }
}

From source file:org.activiti.rest.service.api.repository.DeploymentCollectionResource.java

@Post
public DeploymentResponse uploadDeployment(Representation entity) {
    if (!authenticate()) {
        return null;
    }//  ww w .j a  va 2  s .  c om

    try {

        if (entity == null || entity.getMediaType() == null
                || !MediaType.MULTIPART_FORM_DATA.isCompatible(entity.getMediaType())) {
            throw new ActivitiIllegalArgumentException(
                    "The request should be of type" + MediaType.MULTIPART_FORM_DATA + ".");
        }

        RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
        List<FileItem> items = upload.parseRepresentation(entity);

        String tenantId = null;

        FileItem uploadItem = null;
        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
                if ("tenantId".equals(fileItem.getFieldName())) {
                    tenantId = fileItem.getString("UTF-8");
                }
            } else if (fileItem.getName() != null) {
                uploadItem = fileItem;
            }
        }

        if (uploadItem == null) {
            throw new ActivitiIllegalArgumentException("No file content was found in request body.");
        }

        DeploymentBuilder deploymentBuilder = ActivitiUtil.getRepositoryService().createDeployment();
        String fileName = uploadItem.getName();
        if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
            deploymentBuilder.addInputStream(fileName, uploadItem.getInputStream());
        } else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
            deploymentBuilder.addZipInputStream(new ZipInputStream(uploadItem.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();

        setStatus(Status.SUCCESS_CREATED);

        return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
                .createDeploymentResponse(this, deployment);

    } catch (Exception e) {
        if (e instanceof ActivitiException) {
            throw (ActivitiException) e;
        }
        throw new ActivitiException(e.getMessage(), e);
    }
}