@Override
public AttachmentDto addAttachment(UriInfo uriInfo, MultipartFormData payload) {
ensureHistoryEnabled(Status.FORBIDDEN);
ensureTaskExists(Status.BAD_REQUEST);
FormPart attachmentNamePart = payload.getNamedPart("attachment-name");
FormPart attachmentTypePart = payload.getNamedPart("attachment-type");
FormPart attachmentDescriptionPart = payload.getNamedPart("attachment-description");
FormPart contentPart = payload.getNamedPart("content");
FormPart urlPart = payload.getNamedPart("url");
if (urlPart == null && contentPart == null) {
throw new InvalidRequestException(Status.BAD_REQUEST, "No content or url to remote content exists to create the task attachment.");
}
String attachmentName = null;
String attachmentDescription = null;
String attachmentType = null;
if (attachmentNamePart != null) {
attachmentName = attachmentNamePart.getTextContent();
}
if (attachmentDescriptionPart != null) {
attachmentDescription = attachmentDescriptionPart.getTextContent();
}
if (attachmentTypePart != null) {
attachmentType = attachmentTypePart.getTextContent();
}
Attachment attachment = null;
try {
if (contentPart != null) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(contentPart.getBinaryContent());
attachment = engine.getTaskService().createAttachment(attachmentType, taskId, null, attachmentName, attachmentDescription, byteArrayInputStream);
} else if (urlPart != null) {
attachment = engine.getTaskService().createAttachment(attachmentType, taskId, null, attachmentName, attachmentDescription, urlPart.getTextContent());
}
} catch (ProcessEngineException e) {
throw new InvalidRequestException(Status.BAD_REQUEST, e, "Task id is null");
}