use of org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload in project alfresco-remote-api by Alfresco.
the class AbstractBaseApiTest method updateTextFile.
protected Document updateTextFile(String contentId, String textContent, Map<String, String> params, int expectedStatus) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(textContent.getBytes());
File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
BinaryPayload payload = new BinaryPayload(txtFile);
HttpResponse response = putBinary(getNodeContentUrl(contentId), payload, null, params, expectedStatus);
if (expectedStatus != 200) {
return null;
}
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
use of org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload 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);
}
use of org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUpdateFileWithBinaryUpload.
/**
* Tests update file content
* <p>PUT:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
*/
@Test
public void testUpdateFileWithBinaryUpload() throws Exception {
setRequestContext(user1);
String myNodeId = getMyNodeId();
String folderName = "f1-testUpdateFileWithBinaryUpload-" + RUNID;
Folder folderResp = createFolder(myNodeId, folderName);
String f1_nodeId = folderResp.getId();
String anoNodeName = "another";
createFolder(f1_nodeId, anoNodeName);
Document doc = new Document();
final String docName = "testdoc.txt";
doc.setName(docName);
doc.setNodeType(TYPE_CM_CONTENT);
doc.setProperties(Collections.singletonMap("cm:title", (Object) "test title"));
ContentInfo contentInfo = new ContentInfo();
doc.setContent(contentInfo);
// create an empty file within F1 folder
HttpResponse response = post(getNodeChildrenUrl(f1_nodeId), toJsonAsStringNonNull(doc), 201);
Document docResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(docName, docResp.getName());
assertNotNull(docResp.getContent());
assertEquals(0, docResp.getContent().getSizeInBytes().intValue());
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, docResp.getContent().getMimeType());
// Default encoding
assertEquals("UTF-8", docResp.getContent().getEncoding());
// Update the empty node's content
String content = "The quick brown fox jumps over the lazy dog.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
BinaryPayload payload = new BinaryPayload(txtFile);
// Try to update a folder!
putBinary(getNodeContentUrl(f1_nodeId), payload, null, null, 400);
// Try to update a non-existent file
putBinary(getNodeContentUrl(UUID.randomUUID().toString()), payload, null, null, 404);
final String url = getNodeContentUrl(docResp.getId());
// Update the empty file
response = putBinary(url, payload, null, null, 200);
docResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(docName, docResp.getName());
assertNotNull(docResp.getId());
assertNotNull(docResp.getCreatedAt());
assertNotNull(docResp.getCreatedByUser());
assertNotNull(docResp.getModifiedAt());
assertNotNull(docResp.getModifiedByUser());
assertFalse(docResp.getIsFolder());
assertTrue(docResp.getIsFile());
assertNull(docResp.getIsLink());
assertEquals(TYPE_CM_CONTENT, docResp.getNodeType());
assertNotNull(docResp.getParentId());
assertEquals(f1_nodeId, docResp.getParentId());
assertNotNull(docResp.getProperties());
assertNotNull(docResp.getAspectNames());
contentInfo = docResp.getContent();
assertNotNull(contentInfo);
assertNotNull(contentInfo.getEncoding());
assertTrue(contentInfo.getSizeInBytes() > 0);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertNotNull(contentInfo.getMimeTypeName());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
// path is not part of the default response
assertNull(docResp.getPath());
// Download the file
response = getSingle(url, user1, null, 200);
assertEquals(content, response.getResponse());
// Update the node's content again. Also make the response return the path!
content = "The quick brown fox jumps over the lazy dog updated !";
inputStream = new ByteArrayInputStream(content.getBytes());
txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
payload = new BinaryPayload(txtFile);
response = putBinary(url + "?include=path", payload, null, null, 200);
docResp = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
assertEquals(docName, docResp.getName());
assertNotNull(docResp.getContent());
assertTrue(docResp.getContent().getSizeInBytes().intValue() > 0);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, docResp.getContent().getMimeType());
assertEquals("ISO-8859-1", docResp.getContent().getEncoding());
PathInfo pathInfo = docResp.getPath();
assertNotNull(pathInfo);
assertTrue(pathInfo.getIsComplete());
List<ElementInfo> pathElements = pathInfo.getElements();
assertNotNull(pathElements);
assertTrue(pathElements.size() > 0);
// check the last element is F1
assertEquals(folderResp.getName(), pathElements.get(pathElements.size() - 1).getName());
// Download the file
response = getSingle(url, user1, null, 200);
assertEquals(content, response.getResponse());
// Update the node's content again. Also rename the file !
content = "The quick brown fox jumps over the lazy dog updated again !";
inputStream = new ByteArrayInputStream(content.getBytes());
txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
payload = new BinaryPayload(txtFile);
String docName2 = "hello-world.txt";
Map params = new HashMap<>();
params.put(Nodes.PARAM_NAME, docName2);
response = putBinary(url, payload, null, params, 200);
docResp = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
assertEquals(docName2, docResp.getName());
// Download the file
response = getSingle(url, user1, null, 200);
assertEquals(content, response.getResponse());
// -ve - optional "name" is invalid
params = new HashMap<>();
params.put(Nodes.PARAM_NAME, "hello/world.txt");
payload = new BinaryPayload(txtFile);
putBinary(url, payload, null, params, 422);
// -ve - optional "name" already exists ...
params = new HashMap<>();
params.put(Nodes.PARAM_NAME, anoNodeName);
payload = new BinaryPayload(txtFile);
putBinary(url, payload, null, params, 409);
// -ve - try to update content using multi-part form data
payload = new BinaryPayload(txtFile, "multipart/form-data", null);
putBinary(url, payload, null, null, 415);
// -ve - try to invalid media type argument (when parsing request)
payload = new BinaryPayload(txtFile, "/jpeg", null);
putBinary(url, payload, null, null, 415);
}
Aggregations