use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testDownloadFileContentUsingTempFile.
/**
* Tests download of file/content using backed temp file for streaming.
* <p>
* GET:
* </p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
*/
@Test
public void testDownloadFileContentUsingTempFile() throws Exception {
setRequestContext(user1);
// This should be grater then TempOutputStream.DEFAULT_MEMORY_THRESHOLD
Long contentSize = 5 * 1024 * 1024L;
String fileName = "tempFile.txt";
File file = null;
try {
file = TempFileProvider.createTempFile(getClass().getSimpleName(), ".txt");
RandomAccessFile rndFile = new RandomAccessFile(file.getPath(), "rw");
rndFile.setLength(contentSize);
rndFile.close();
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
response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
byte[] bytes = response.getResponseAsBytes();
assertEquals(contentSize.intValue(), bytes.length);
} finally {
file.delete();
}
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUploadToMyFiles.
/**
* Tests Multipart upload to user's home (a.k.a My Files).
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
*/
@Test
public void testUploadToMyFiles() throws Exception {
setRequestContext(user1);
// create folder f0
String folder0Name = "f0-testUploadToMyFiles-" + RUNID;
Folder folderResp = createFolder(Nodes.PATH_MY, folder0Name);
String f0Id = folderResp.getId();
final String fileName = "quick.pdf";
final File file = getResourceFile(fileName);
Paging paging = getPaging(0, Integer.MAX_VALUE);
HttpResponse response = getAll(getNodeChildrenUrl(f0Id), 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 into a non-existent folder
post(getNodeChildrenUrl(UUID.randomUUID().toString()), reqBody.getBody(), null, reqBody.getContentType(), 404);
// Upload
response = post(getNodeChildrenUrl(f0Id), 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);
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
// Default encoding
assertEquals("UTF-8", contentInfo.getEncoding());
// Check there is no path info returned.
// The path info should only be returned when it is requested via a include statement.
assertNull(document.getPath());
// 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_PDF, contentInfo.getMimeType());
// Check 'get children' is confirming the upload
response = getAll(getNodeChildrenUrl(f0Id), 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(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 409);
response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals("Duplicate file name. The file shouldn't have been uploaded.", numOfNodes + 1, pagingResult.getCount().intValue());
// Set autoRename=true and upload the same file again
reqBody = MultiPartBuilder.copy(multiPartBuilder).setAutoRename(true).build();
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals("quick-1.pdf", document.getName());
// upload the same file again, and request the path info to be present in the response
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), "?include=path", reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals("quick-2.pdf", document.getName());
assertNotNull(document.getPath());
response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals(numOfNodes + 3, pagingResult.getCount().intValue());
// upload without specifying content type or without overriding filename - hence guess mimetype and use file's name
final String fileName1 = "quick-1.txt";
final File file1 = getResourceFile(fileName1);
reqBody = MultiPartBuilder.create().setFileData(new FileData(null, file1)).build();
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName1, document.getName());
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, document.getContent().getMimeType());
// upload with "default" binary content type and override filename - hence guess mimetype & use overridden name
final String fileName2 = "quick-2.txt";
final String fileName2b = "quick-2b.txt";
final File file2 = getResourceFile(fileName2);
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2b, file2)).build();
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName2b, document.getName());
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, document.getContent().getMimeType());
response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, null, 200);
Folder user1Home = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
// User2 tries to upload a new file into the user1's home folder.
setRequestContext(user2);
final File file3 = getResourceFile(fileName2);
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file3)).build();
post(getNodeChildrenUrl(user1Home.getId()), reqBody.getBody(), null, reqBody.getContentType(), 403);
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 403);
setRequestContext(user1);
response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals("Access Denied. The file shouldn't have been uploaded.", numOfNodes + 5, pagingResult.getCount().intValue());
// User1 tries to upload a file into a document rather than a folder!
post(getNodeChildrenUrl(document.getId()), reqBody.getBody(), null, reqBody.getContentType(), 400);
// Try to upload a file without defining the required formData
reqBody = MultiPartBuilder.create().setAutoRename(true).build();
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
// Test unsupported node type
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setNodeType("cm:link").build();
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
// User1 uploads a new file
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).build();
response = post(getNodeChildrenUrl(f0Id), 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());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
// Test content size limit
final SimpleFixedLimitProvider limitProvider = applicationContext.getBean("defaultContentLimitProvider", SimpleFixedLimitProvider.class);
final long defaultSizeLimit = limitProvider.getSizeLimit();
// 20 KB
limitProvider.setSizeLimitString("20000");
try {
// quick.pdf size is about 23 KB
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setAutoRename(true).build();
// Try to upload a file larger than the configured size limit
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 413);
} finally {
limitProvider.setSizeLimitString(Long.toString(defaultSizeLimit));
}
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest in project alfresco-remote-api by Alfresco.
the class NodeApiTest method updatePropertiesMultivalueTest.
@Test
public void updatePropertiesMultivalueTest() throws Exception {
setRequestContext(user1);
String myNodeId = getMyNodeId();
// create a multiple-value field from multipart\formdata
String fileName = "myfile" + GUID.generate() + ".txt";
File file = getResourceFile("quick-2.pdf");
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setAutoRename(true);
multiPartBuilder.setNodeType("custom:destination");
multiPartBuilder.setOverwrite(false);
multiPartBuilder.setMajorVersion(true);
multiPartBuilder.setDescription("test");
multiPartBuilder.setRenditions(Collections.singletonList("doclib"));
Map<String, String> props = new MultiValueMap();
props.put("cm:title", "test title");
props.put("custom:locations", "loc1");
props.put("custom:locations", "loc2");
props.put("custom:locations", "loc3");
multiPartBuilder.setProperties(props);
MultiPartRequest reqBody = multiPartBuilder.build();
HttpResponse response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// build the multiple-value as json array
String jsonUpdate = "{" + "\"name\":\"My Other Folder\"," + "\"nodeType\":\"custom:destination\"," + "\"properties\":" + "{" + "\"cm:title\":\"Folder title\"," + "\"cm:description\":\"This is an important folder\"," + "\"custom:locations\":[" + "\"location X\"," + "\"location Y\"" + "]" + "}" + "}";
response = put(URL_NODES, documentResponse.getId(), jsonUpdate, null, 200);
Node nodeUpdateResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
Map<String, Object> propUpdateResponse = nodeUpdateResponse.getProperties();
assertNotNull(propUpdateResponse.get("custom:locations"));
assertTrue(((ArrayList) (propUpdateResponse.get("custom:locations"))).size() == 2);
// build the multiple-value as array
Map<String, Object> properties = new HashMap();
List locations = new ArrayList<String>();
locations.add("location X1");
properties.put("custom:locations", locations);
Node nodeUpdate = new Node();
nodeUpdate.setProperties(properties);
response = put(URL_NODES, documentResponse.getId(), toJsonAsStringNonNull(nodeUpdate), null, 200);
nodeUpdateResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
propUpdateResponse = nodeUpdateResponse.getProperties();
assertNotNull(propUpdateResponse.get("custom:locations"));
assertTrue(((ArrayList) (propUpdateResponse.get("custom:locations"))).size() == 1);
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testMultipartFormDataUpload.
/**
* REPO-24
* Tests upload fails with empty or invalid multipart/form-data.
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
*/
@Test
public void testMultipartFormDataUpload() throws Exception {
setRequestContext(user1);
final String fileName = "quick-1.txt";
final File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
// Upload with empty multipart
post(getNodeChildrenUrl(Nodes.PATH_MY), "", null, reqBody.getContentType(), 400);
// Upload with multipart with missing boundary e.g. multipart/form-data; boundary=7cgH0q1hSgrlmU7tUe8kGSWT4Un6aRH
post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, "multipart/form-data", 415);
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest in project alfresco-remote-api by Alfresco.
the class RenditionsTest method testCreateRenditionForNewVersion.
/**
* Tests create rendition after uploading new version(s)
*/
@Test
public void testCreateRenditionForNewVersion() throws Exception {
String PROP_LTM = "cm:lastThumbnailModification";
String RENDITION_NAME = "imgpreview";
String userId = userOneN1.getId();
setRequestContext(networkN1.getId(), userOneN1.getId(), null);
// Create a folder within the site document's library
String folderName = "folder" + System.currentTimeMillis();
String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userId);
// Create multipart request - pdf file
String fileName = "quick.pdf";
File file = getResourceFile(fileName);
MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).build();
Map<String, String> params = Collections.singletonMap("include", "properties");
// Upload quick.pdf file into 'folder' - do not include request to create 'doclib' thumbnail
HttpResponse response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), params, null, "alfresco", reqBody.getContentType(), 201);
Document document1 = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document1.getId();
assertNotNull(document1.getProperties());
assertNull(document1.getProperties().get(PROP_LTM));
// pause briefly
Thread.sleep(DELAY_IN_MS);
// Get rendition (not created yet) information for node
response = getSingle(getNodeRenditionsUrl(contentNodeId), RENDITION_NAME, 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(RenditionStatus.NOT_CREATED, rendition.getStatus());
params = new HashMap<>();
params.put("placeholder", "false");
getSingle(getNodeRenditionsUrl(contentNodeId), (RENDITION_NAME + "/content"), params, 404);
// TODO add test to request creation of rendition as another user (that has read-only access on the content, not write)
// Create and get 'imgpreview' rendition
rendition = createAndGetRendition(contentNodeId, RENDITION_NAME);
assertNotNull(rendition);
assertEquals(RenditionStatus.CREATED, rendition.getStatus());
ContentInfo contentInfo = rendition.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_JPEG, contentInfo.getMimeType());
assertEquals("JPEG Image", contentInfo.getMimeTypeName());
assertNotNull(contentInfo.getEncoding());
assertTrue(contentInfo.getSizeInBytes() > 0);
params = new HashMap<>();
params.put("placeholder", "false");
response = getSingle(getNodeRenditionsUrl(contentNodeId), (RENDITION_NAME + "/content"), params, 200);
byte[] renditionBytes1 = response.getResponseAsBytes();
assertNotNull(renditionBytes1);
// check node details ...
params = Collections.singletonMap("include", "properties");
response = getSingle(NodesEntityResource.class, contentNodeId, params, 200);
Document document1b = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(document1b.getModifiedAt(), document1.getModifiedAt());
assertEquals(document1b.getModifiedByUser().getId(), document1.getModifiedByUser().getId());
assertEquals(document1b.getModifiedByUser().getDisplayName(), document1.getModifiedByUser().getDisplayName());
assertNotEquals(document1b.getProperties().get(PROP_LTM), document1.getProperties().get(PROP_LTM));
// upload another version of "quick.pdf" and check again
fileName = "quick-2.pdf";
file = getResourceFile(fileName);
reqBody = MultiPartBuilder.create().setFileData(new FileData("quick.pdf", file)).setOverwrite(true).build();
response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, null, "alfresco", reqBody.getContentType(), 201);
Document document2 = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(contentNodeId, document2.getId());
// wait to allow new version of the rendition to be created ...
Thread.sleep(DELAY_IN_MS * 4);
params = new HashMap<>();
params.put("placeholder", "false");
response = getSingle(getNodeRenditionsUrl(contentNodeId), (RENDITION_NAME + "/content"), params, 200);
assertNotNull(response.getResponseAsBytes());
// check rendition binary has changed
assertNotEquals(renditionBytes1, response.getResponseAsBytes());
// check node details ...
params = Collections.singletonMap("include", "properties");
response = getSingle(NodesEntityResource.class, contentNodeId, params, 200);
Document document2b = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertTrue(document2b.getModifiedAt().after(document1.getModifiedAt()));
assertEquals(document2b.getModifiedByUser().getId(), document1.getModifiedByUser().getId());
assertEquals(document2b.getModifiedByUser().getDisplayName(), document1.getModifiedByUser().getDisplayName());
// check last thumbnail modification property has changed ! (REPO-1644)
assertNotEquals(document2b.getProperties().get(PROP_LTM), document1b.getProperties().get(PROP_LTM));
}
Aggregations