try {
Task task = taskService.newTask();
taskService.saveTask(task);
// Create URL-attachment
Attachment urlAttachment = taskService.createAttachment("simpleType", task.getId(), null, "Simple attachment", "Simple attachment description",
"http://activiti.org");
taskService.saveAttachment(urlAttachment);
// Create Binary-attachment
Attachment binaryAttachment = taskService.createAttachment("binaryType", task.getId(), null, "Binary attachment", "Binary attachment description",
new ByteArrayInputStream("This is binary content".getBytes()));
taskService.saveAttachment(binaryAttachment);
// Get external url attachment
CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX +
RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT, task.getId(), urlAttachment.getId())), HttpStatus.SC_OK);
JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
closeResponse(response);
assertEquals(urlAttachment.getId(), responseNode.get("id").textValue());
assertEquals("simpleType", responseNode.get("type").textValue());
assertEquals("Simple attachment", responseNode.get("name").textValue());
assertEquals("Simple attachment description", responseNode.get("description").textValue());
assertEquals("http://activiti.org", responseNode.get("externalUrl").textValue());
assertTrue(responseNode.get("url").textValue()
.endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT, task.getId(), urlAttachment.getId())));
assertTrue(responseNode.get("taskUrl").textValue().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK, task.getId())));
assertTrue(responseNode.get("contentUrl").isNull());
assertTrue(responseNode.get("processInstanceUrl").isNull());
// Get binary attachment
response = executeRequest(new HttpGet(SERVER_URL_PREFIX +
RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT, task.getId(), binaryAttachment.getId())), HttpStatus.SC_OK);
responseNode = objectMapper.readTree(response.getEntity().getContent());
closeResponse(response);
assertEquals(binaryAttachment.getId(), responseNode.get("id").textValue());
assertEquals("binaryType", responseNode.get("type").textValue());
assertEquals("Binary attachment", responseNode.get("name").textValue());
assertEquals("Binary attachment description", responseNode.get("description").textValue());
assertTrue(responseNode.get("url").textValue()
.endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT, task.getId(), binaryAttachment.getId())));
assertTrue(responseNode.get("contentUrl").textValue()
.endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT_DATA, task.getId(), binaryAttachment.getId())));
assertTrue(responseNode.get("taskUrl").textValue().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK, task.getId())));
assertTrue(responseNode.get("externalUrl").isNull());
assertTrue(responseNode.get("processInstanceUrl").isNull());