use of org.alfresco.rest.api.tests.util.MultiPartBuilder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testDownloadFileContent.
/**
* Tests download of file/content.
* <p>GET:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
*/
@Test
public void testDownloadFileContent() throws Exception {
setRequestContext(user1);
//
// Test plain text
//
String fileName = "quick-1.txt";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
// Upload text content
HttpResponse response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document.getId();
// Check the upload response
assertEquals(fileName, document.getName());
ContentInfo contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Download text content - by default with Content-Disposition header
response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
String textContent = response.getResponse();
assertEquals("The quick brown fox jumps over the lazy dog", textContent);
Map<String, String> responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals("attachment; filename=\"quick-1.txt\"; filename*=UTF-8''quick-1.txt", responseHeaders.get("Content-Disposition"));
String cacheControl = responseHeaders.get("Cache-Control");
assertNotNull(cacheControl);
assertTrue(cacheControl.contains("must-revalidate"));
assertTrue(cacheControl.contains("max-age=0"));
assertNotNull(responseHeaders.get("Expires"));
String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
// Test 304 response
getSingle(getNodeContentUrl(contentNodeId), null, null, headers, 304);
// Update the content to change the node's modified date
Document docUpdate = new Document();
docUpdate.setProperties(Collections.singletonMap("cm:description", (Object) "desc updated!"));
// Wait a second then update, as the dates will be rounded to
// ignore millisecond when checking for If-Modified-Since
Thread.sleep(1000L);
response = put(URL_NODES, contentNodeId, toJsonAsStringNonNull(docUpdate), null, 200);
Document updatedDocument = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(contentNodeId, updatedDocument.getId());
// The requested "If-Modified-Since" date is older than node's modified date
response = getSingle(getNodeContentUrl(contentNodeId), null, null, headers, 200);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertNotNull(responseHeaders.get("Cache-Control"));
assertNotNull(responseHeaders.get("Expires"));
String newLastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(newLastModifiedHeader);
assertNotEquals(lastModifiedHeader, newLastModifiedHeader);
//
// Test binary (eg. PDF)
//
fileName = "quick.pdf";
file = getResourceFile(fileName);
byte[] originalBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
// Upload binary content
response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentNodeId = document.getId();
// Check the upload response
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
// Download binary content (as bytes) - without Content-Disposition header (attachment=false)
Map<String, String> params = new LinkedHashMap<>();
params.put("attachment", "false");
response = getSingle(NodesEntityResource.class, contentNodeId + "/content", params, 200);
byte[] bytes = response.getResponseAsBytes();
assertArrayEquals(originalBytes, bytes);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertNull(responseHeaders.get("Content-Disposition"));
assertNotNull(responseHeaders.get("Cache-Control"));
assertNotNull(responseHeaders.get("Expires"));
lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
// Test 304 response
getSingle(getNodeContentUrl(contentNodeId), null, null, headers, 304);
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUploadToSite.
/**
* Tests Multipart upload to a Site.
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/children}
*/
@Test
public void testUploadToSite() throws Exception {
setRequestContext(user1);
final String fileName = "quick-1.txt";
final File file = getResourceFile(fileName);
String folderA = "folder" + RUNID + "_A";
String folderA_id = createFolder(tDocLibNodeId, folderA).getId();
Paging paging = getPaging(0, Integer.MAX_VALUE);
HttpResponse response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
PublicApiClient.ExpectedPaging pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
final int numOfNodes = pagingResult.getCount();
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
// Try to upload
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName, document.getName());
ContentInfo contentInfo = document.getContent();
assertNotNull(contentInfo);
// As the client didn't set the mimeType, the API must guess it.
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Retrieve the uploaded file
response = getSingle(NodesEntityResource.class, document.getId(), null, 200);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Check 'get children' is confirming the upload
response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
// Upload the same file again to check the name conflicts handling
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 409);
response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
setRequestContext(user2);
final String fileName2 = "quick-2.txt";
final File file2 = getResourceFile(fileName2);
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).build();
// user2 tries to upload a new file into the folderA of user1
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 403);
setRequestContext(user1);
// Test upload with properties
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName2, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertNotNull(document.getProperties());
assertNull(document.getProperties().get("cm:title"));
assertNull(document.getProperties().get("cm:description"));
// upload a file with properties. Also, set autoRename=true
Map<String, String> props = new HashMap<>(2);
props.put("cm:title", "test title");
props.put("cm:description", "test description");
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setProperties(props).build();
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
// "quick-2-1.txt" => fileName2 + autoRename
assertEquals("quick-2-1.txt", document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertNotNull(document.getProperties());
assertEquals("test title", document.getProperties().get("cm:title"));
assertEquals("test description", document.getProperties().get("cm:description"));
// Test unknown property name
props = new HashMap<>(1);
props.put("unknownPrefix" + System.currentTimeMillis() + ":description", "test description");
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setProperties(props).build();
// Prop prefix is unknown
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 400);
// Test relativePath multi-part field.
// Any folders in the relativePath that do not exist, are created before the content is created.
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setRelativePath("X/Y/Z");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), "?include=path", reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Check the uploaded file parent folders
PathInfo pathInfo = document.getPath();
assertNotNull(pathInfo);
List<ElementInfo> elementInfos = pathInfo.getElements();
assertNotNull(elementInfos);
// /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A/X/Y/Z
assertEquals(8, elementInfos.size());
assertEquals(document.getParentId(), elementInfos.get(7).getId());
assertEquals("Z", elementInfos.get(7).getName());
assertEquals("Y", elementInfos.get(6).getName());
assertEquals("X", elementInfos.get(5).getName());
assertEquals(folderA, elementInfos.get(4).getName());
// Try to create a folder with the same name as the document within the 'Z' folder.
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("X/Y/Z/" + document.getName()).build();
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 409);
// Test the same functionality as "mkdir -p x/y/z" which the folders should be created
// as needed but no errors thrown if the path or any part of the path already exists.
// NOTE: white spaces, leading and trailing "/" are ignored.
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("/X/ Y/Z /CoolFolder/").build();
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Retrieve the uploaded file parent folder
response = getSingle(NodesEntityResource.class, document.getParentId(), null, 200);
Folder coolFolder = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
assertEquals(document.getParentId(), coolFolder.getId());
assertEquals("CoolFolder", coolFolder.getName());
// Try to upload quick-1.txt within coolFolder and set the relativePath to a blank string.
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath(// blank
" ").build();
// 409 -> as the blank string is ignored and quick-1.txt already exists in the coolFolder
post(getNodeChildrenUrl(coolFolder.getId()), reqBody.getBody(), null, reqBody.getContentType(), 409);
setRequestContext(user2);
// user2 tries to upload the same file by creating sub-folders in the folderA of user1
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("userTwoFolder1/userTwoFolder2").build();
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 403);
// -ve test: integrity error
setRequestContext(user1);
reqBody = MultiPartBuilder.create().setFileData(new FileData("invalid:name", file)).build();
// 422 -> invalid name (includes a ':' in this example)
post(getNodeChildrenUrl(coolFolder.getId()), reqBody.getBody(), null, reqBody.getContentType(), 422);
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testGuessMimeTypeAndEncoding.
/**
* Tests guess mimetype & guess encoding when uploading file/content (create or update) - also empty file/content
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
* <p>PUT:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
*/
@Test
public void testGuessMimeTypeAndEncoding() throws Exception {
setRequestContext(user1);
String fId = createFolder(getMyNodeId(), "test-folder-guess-" + RUNID).getId();
// create empty files
Document d = new Document();
d.setName("my doc");
d.setNodeType(TYPE_CM_CONTENT);
HttpResponse response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(MimetypeMap.MIMETYPE_BINARY, documentResp.getContent().getMimeType());
assertEquals("Binary File (Octet Stream)", documentResp.getContent().getMimeTypeName());
assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
assertEquals("UTF-8", documentResp.getContent().getEncoding());
d = new Document();
d.setName("my doc.txt");
d.setNodeType(TYPE_CM_CONTENT);
response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, documentResp.getContent().getMimeType());
assertEquals("Plain Text", documentResp.getContent().getMimeTypeName());
assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
assertEquals("UTF-8", documentResp.getContent().getEncoding());
d = new Document();
d.setName("my doc.pdf");
d.setNodeType(TYPE_CM_CONTENT);
response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(MimetypeMap.MIMETYPE_PDF, documentResp.getContent().getMimeType());
assertEquals("Adobe PDF Document", documentResp.getContent().getMimeTypeName());
assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
assertEquals("UTF-8", documentResp.getContent().getEncoding());
// upload files
String fileName = "quick-2.pdf";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
ContentInfo contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "quick-2.pdf";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(// note: we've deliberately dropped the file ext here
new FileData("quick-2", file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "example-1.txt";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
fileName = "example-2.txt";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "shift-jis.txt";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("Shift_JIS", contentInfo.getEncoding());
fileName = "example-1.xml";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
fileName = "example-2.xml";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// upload file, rename and then update file
fileName = "quick.pdf";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String docId = document.getId();
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// rename (mimeType remains unchanged, binary has not changed)
Document dUpdate = new Document();
dUpdate.setName("quick.docx");
response = put(URL_NODES, docId, toJsonAsStringNonNull(dUpdate), null, 200);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "quick.docx";
file = getResourceFile(fileName);
BinaryPayload payload = new BinaryPayload(file);
response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// additional test
fileName = "CMIS-Delete.json";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData("special-" + GUID.generate(), file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// cleanup
deleteNode(fId);
}
Aggregations