Search in sources :

Example 1 with AttachmentDto

use of org.camunda.bpm.engine.rest.dto.task.AttachmentDto in project camunda-bpm-platform by camunda.

the class TaskAttachmentResourceImpl method getAttachments.

@Override
public List<AttachmentDto> getAttachments() {
    if (!isHistoryEnabled()) {
        return Collections.emptyList();
    }
    ensureTaskExists(Status.NOT_FOUND);
    List<Attachment> taskAttachments = engine.getTaskService().getTaskAttachments(taskId);
    List<AttachmentDto> attachments = new ArrayList<AttachmentDto>();
    for (Attachment attachment : taskAttachments) {
        attachments.add(AttachmentDto.fromAttachment(attachment));
    }
    return attachments;
}
Also used : AttachmentDto(org.camunda.bpm.engine.rest.dto.task.AttachmentDto) ArrayList(java.util.ArrayList) Attachment(org.camunda.bpm.engine.task.Attachment)

Example 2 with AttachmentDto

use of org.camunda.bpm.engine.rest.dto.task.AttachmentDto in project camunda-bpm-platform by camunda.

the class TaskAttachmentResourceImpl method addAttachment.

@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");
    }
    URI uri = uriInfo.getBaseUriBuilder().path(rootResourcePath).path(TaskRestService.PATH).path(taskId + "/attachment/" + attachment.getId()).build();
    AttachmentDto attachmentDto = AttachmentDto.fromAttachment(attachment);
    // GET /
    attachmentDto.addReflexiveLink(uri, HttpMethod.GET, "self");
    return attachmentDto;
}
Also used : AttachmentDto(org.camunda.bpm.engine.rest.dto.task.AttachmentDto) FormPart(org.camunda.bpm.engine.rest.mapper.MultipartFormData.FormPart) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) Attachment(org.camunda.bpm.engine.task.Attachment) URI(java.net.URI) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Aggregations

AttachmentDto (org.camunda.bpm.engine.rest.dto.task.AttachmentDto)2 Attachment (org.camunda.bpm.engine.task.Attachment)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)1 InvalidRequestException (org.camunda.bpm.engine.rest.exception.InvalidRequestException)1 FormPart (org.camunda.bpm.engine.rest.mapper.MultipartFormData.FormPart)1