use of org.alfresco.rest.api.tests.client.data.Rendition 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));
}
use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.
the class RenditionsTest method testDownloadRendition.
/**
* Tests download rendition.
* <p>GET:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/renditions/<renditionId>/content}
*/
@Test
public void testDownloadRendition() throws Exception {
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, userOneN1.getId());
// Create multipart request
String fileName = "quick.pdf";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
// Upload quick.pdf file into 'folder'
HttpResponse response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document.getId();
// pause briefly
Thread.sleep(DELAY_IN_MS);
// Get rendition (not created yet) information for node
response = getSingle(getNodeRenditionsUrl(contentNodeId), "doclib", 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(RenditionStatus.NOT_CREATED, rendition.getStatus());
// Download placeholder - by default with Content-Disposition header
Map<String, String> params = new HashMap<>();
params.put("placeholder", "true");
response = getSingle(getNodeRenditionsUrl(contentNodeId), ("doclib/content"), params, 200);
assertNotNull(response.getResponseAsBytes());
Map<String, String> responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
String contentDisposition = responseHeaders.get("Content-Disposition");
assertNotNull(contentDisposition);
assertTrue(contentDisposition.contains("filename=\"doclib\""));
String contentType = responseHeaders.get("Content-Type");
assertNotNull(contentType);
assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
// Download placeholder - without Content-Disposition header (attachment=false)
params.put("attachment", "false");
response = getSingle(getNodeRenditionsUrl(contentNodeId), ("doclib/content"), params, 200);
assertNotNull(response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
String cacheControl = responseHeaders.get("Cache-Control");
assertNotNull(cacheControl);
assertTrue(cacheControl.contains("must-revalidate"));
assertNull(responseHeaders.get("Content-Disposition"));
contentType = responseHeaders.get("Content-Type");
assertNotNull(contentType);
assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
// Test 304 response - placeholder=true&attachment=false
String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
// Currently the placeholder file is not cached.
// As the placeholder is not a NodeRef, so we can't get the ContentModel.PROP_MODIFIED date.
getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", params, headers, 200);
// Create and get 'doclib' rendition
rendition = createAndGetRendition(contentNodeId, "doclib");
assertNotNull(rendition);
assertEquals(RenditionStatus.CREATED, rendition.getStatus());
// Download rendition - by default with Content-Disposition header
response = getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", 200);
assertNotNull(response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
contentDisposition = responseHeaders.get("Content-Disposition");
assertNotNull(contentDisposition);
assertTrue(contentDisposition.contains("filename=\"doclib\""));
contentType = responseHeaders.get("Content-Type");
assertNotNull(contentType);
assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
// Download rendition - without Content-Disposition header (attachment=false)
params = Collections.singletonMap("attachment", "false");
response = getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", params, 200);
assertNotNull(response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertNull(responseHeaders.get("Content-Disposition"));
contentType = responseHeaders.get("Content-Type");
assertNotNull(contentType);
assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
// Download rendition - with Content-Disposition header (attachment=true) same as default
params = Collections.singletonMap("attachment", "true");
response = getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", params, 200);
assertNotNull(response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
// Check the cache settings which have been set in the RenditionsImpl#getContent()
cacheControl = responseHeaders.get("Cache-Control");
assertNotNull(cacheControl);
assertFalse(cacheControl.contains("must-revalidate"));
assertTrue(cacheControl.contains("max-age=31536000"));
contentDisposition = responseHeaders.get("Content-Disposition");
assertNotNull(contentDisposition);
assertTrue(contentDisposition.contains("filename=\"doclib\""));
contentType = responseHeaders.get("Content-Type");
assertNotNull(contentType);
assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
// Test 304 response - doclib rendition (attachment=true)
lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", params, headers, 304);
// Here we want to overwrite/update the existing content in order to force a new rendition creation,
// so the ContentModel.PROP_MODIFIED date would be different. Hence, we use the multipart upload by providing
// the old fileName and setting overwrite field to true
file = getResourceFile("quick-2.pdf");
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setOverwrite(true);
reqBody = multiPartBuilder.build();
// Update quick.pdf
post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
// The requested "If-Modified-Since" date is older than rendition modified date
response = getSingleWithDelayRetry(getNodeRenditionsUrl(contentNodeId), "doclib/content", params, headers, MAX_RETRY, PAUSE_TIME, 200);
assertNotNull(response);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
String newLastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(newLastModifiedHeader);
assertNotEquals(lastModifiedHeader, newLastModifiedHeader);
// -ve tests
// nodeId in the path parameter does not represent a file
getSingle(getNodeRenditionsUrl(folder_Id), "doclib/content", 400);
// nodeId in the path parameter does not exist
getSingle(getNodeRenditionsUrl(UUID.randomUUID().toString()), "doclib/content", 404);
// renditionId in the path parameter is not registered/available
getSingle(getNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis() + "/content"), 404);
InputStream inputStream = new ByteArrayInputStream("The quick brown fox jumps over the lazy dog".getBytes());
file = TempFileProvider.createTempFile(inputStream, "RenditionsTest-", ".abcdef");
reqBody = MultiPartBuilder.create().setFileData(new FileData(file.getName(), file)).build();
// Upload temp file into 'folder'
response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentNodeId = document.getId();
// The content of the rendition does not exist and the placeholder parameter is not present
getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", 404);
// The content of the rendition does not exist and the placeholder parameter has a value of "false"
params = Collections.singletonMap("placeholder", "false");
getSingle(getNodeRenditionsUrl(contentNodeId), "doclib/content", params, 404);
// The rendition does not exist, a placeholder is not available and the placeholder parameter has a value of "true"
params = Collections.singletonMap("placeholder", "true");
getSingle(getNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis() + "/content"), params, 404);
// Create a node without any content
String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId());
getSingle(getNodeRenditionsUrl(emptyContentNodeId), "doclib/content", params, 200);
}
use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.
the class SharedLinkApiTest method testSharedLinkCreateGetDelete_MultiTenant.
/**
* Tests shared links to file (content) in a multi-tenant system.
*
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links}
*
* <p>DELETE:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>}
*
* <p>GET:</p>
* The following do not require authentication
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/content}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/renditions}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/renditions/<renditionId>}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/renditions/<renditionId>/content}
*/
// TODO now covered by testSharedLinkCreateGetDelete ? (since base class now uses tenant context by default)
@Test
public void testSharedLinkCreateGetDelete_MultiTenant() throws Exception {
// As user1
setRequestContext(user1);
String docLibNodeId = getSiteContainerNodeId(tSiteId, "documentLibrary");
String folderName = "folder" + System.currentTimeMillis() + "_1";
String folderId = createFolder(docLibNodeId, folderName, null).getId();
// create doc d1 - pdf
String fileName1 = "quick" + RUNID + "_1.pdf";
File file1 = getResourceFile("quick.pdf");
byte[] file1_originalBytes = Files.readAllBytes(Paths.get(file1.getAbsolutePath()));
String file1_MimeType = MimetypeMap.MIMETYPE_PDF;
MultiPartBuilder.MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName1, file1, file1_MimeType)).build();
HttpResponse response = post(getNodeChildrenUrl(folderId), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document doc1 = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String d1Id = doc1.getId();
assertNotNull(d1Id);
// create shared link to document 1
Map<String, String> body = new HashMap<>();
body.put("nodeId", d1Id);
response = post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
QuickShareLink resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
String shared1Id = resp.getId();
assertNotNull(shared1Id);
assertEquals(d1Id, resp.getNodeId());
assertEquals(fileName1, resp.getName());
assertEquals(file1_MimeType, resp.getContent().getMimeType());
assertEquals(user1, resp.getSharedByUser().getId());
// allowable operations not included - no params
response = getSingle(QuickShareLinkEntityResource.class, shared1Id, null, 200);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
assertNull(resp.getAllowableOperations());
setRequestContext(null);
// unauth access to get shared link info
// note: this will be ignore for unauth access
Map<String, String> params = Collections.singletonMap("include", "allowableOperations");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id, params, 200);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
assertEquals(shared1Id, resp.getId());
assertEquals(fileName1, resp.getName());
assertEquals(d1Id, resp.getNodeId());
// include is ignored
assertNull(resp.getAllowableOperations());
// include is ignored
assertNull(resp.getAllowableOperationsOnTarget());
// unauth access to file 1 content (via shared link)
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/content", null, 200);
assertArrayEquals(file1_originalBytes, response.getResponseAsBytes());
Map<String, String> responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(file1_MimeType + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get("Expires"));
assertEquals("attachment; filename=\"" + fileName1 + "\"; filename*=UTF-8''" + fileName1 + "", responseHeaders.get("Content-Disposition"));
String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
// Test 304 response
Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
getSingle(URL_SHARED_LINKS, shared1Id + "/content", null, headers, 304);
// unauth access to file 1 content (via shared link) - without Content-Disposition header (attachment=false)
params = new HashMap<>();
params.put("attachment", "false");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/content", params, 200);
assertArrayEquals(file1_originalBytes, response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(file1_MimeType + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get(LAST_MODIFIED_HEADER));
assertNotNull(responseHeaders.get("Expires"));
assertNull(responseHeaders.get("Content-Disposition"));
// -ve shared link rendition tests
{
// -ve test - try to get non-existent rendition content
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib/content", null, 404);
// -ve test - try to get unregistered rendition content
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/dummy/content", null, 404);
}
// unauth access to get rendition info for a shared link (available => CREATED rendition only)
// -ve shared link rendition tests
{
// -ve test - try to get not created rendition for the given shared link
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib", null, 404);
// -ve test - try to get unregistered rendition
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/dummy", null, 404);
}
// unauth access to get shared link renditions info (available => CREATED renditions only)
response = getAll(URL_SHARED_LINKS + "/" + shared1Id + "/renditions", null, 200);
List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals(0, renditions.size());
// create rendition of pdf doc - note: for some reason create rendition of txt doc fail on build m/c (TBC) ?
setRequestContext(user1);
Rendition rendition = createAndGetRendition(d1Id, "doclib");
assertNotNull(rendition);
assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
setRequestContext(null);
// unauth access to get shared link renditions info (available => CREATED renditions only)
response = getAll(URL_SHARED_LINKS + "/" + shared1Id + "/renditions", null, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals(1, renditions.size());
assertEquals(Rendition.RenditionStatus.CREATED, renditions.get(0).getStatus());
assertEquals("doclib", renditions.get(0).getId());
// unauth access to get rendition info for a shared link (available => CREATED rendition only)
{
// get a created rendition for the given shared link
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib", null, 200);
}
// unauth access to get shared link file rendition content
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib/content", null, 200);
assertTrue(response.getResponseAsBytes().length > 0);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get(LAST_MODIFIED_HEADER));
assertNotNull(responseHeaders.get("Expires"));
String docName = "doclib";
assertEquals("attachment; filename=\"" + docName + "\"; filename*=UTF-8''" + docName + "", responseHeaders.get("Content-Disposition"));
// unauth access to get shared link file rendition content - without Content-Disposition header (attachment=false)
params = new HashMap<>();
params.put("attachment", "false");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib/content", params, 200);
assertTrue(response.getResponseAsBytes().length > 0);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get("Expires"));
assertNull(responseHeaders.get("Content-Disposition"));
lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
// Test 304 response
headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
getSingle(URL_SHARED_LINKS, shared1Id + "/renditions/doclib/content", null, headers, 304);
// -ve test - userTwoN1 cannot delete shared link
setRequestContext(user2);
deleteSharedLink(shared1Id, 403);
// -ve test - unauthenticated
setRequestContext(null);
deleteSharedLink(shared1Id, 401);
// delete shared link
setRequestContext(user1);
deleteSharedLink(shared1Id);
}
use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.
the class SharedLinkApiTest method testSharedLinkCreateGetDelete.
/**
* Tests shared links to file (content)
*
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links}
*
* <p>DELETE:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>}
*
* <p>GET:</p>
* The following do not require authentication
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/content}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/renditions}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/renditions/<renditionId>}
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/shared-links/<sharedId>/renditions/<renditionId>/content}
*/
@Test
public void testSharedLinkCreateGetDelete() throws Exception {
// As user 1 ...
setRequestContext(user1);
// create doc d1 - pdf
String sharedFolderNodeId = getSharedNodeId();
String fileName1 = "quick" + RUNID + "_1.pdf";
File file1 = getResourceFile("quick.pdf");
byte[] file1_originalBytes = Files.readAllBytes(Paths.get(file1.getAbsolutePath()));
String file1_MimeType = MimetypeMap.MIMETYPE_PDF;
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName1, file1, file1_MimeType));
MultiPartBuilder.MultiPartRequest reqBody = multiPartBuilder.build();
HttpResponse response = post(getNodeChildrenUrl(sharedFolderNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document doc1 = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String d1Id = doc1.getId();
// create doc d2 - plain text
String myFolderNodeId = getMyNodeId();
String content2Text = "The quick brown fox jumps over the lazy dog 2.";
String fileName2 = "content" + RUNID + "_2.txt";
Document doc2 = createTextFile(myFolderNodeId, fileName2, content2Text);
String d2Id = doc2.getId();
String file2_MimeType = MimetypeMap.MIMETYPE_TEXT_PLAIN;
// As user 2 ...
setRequestContext(user2);
response = getSingle(NodesEntityResource.class, d1Id, null, 200);
Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
Date docModifiedAt = nodeResp.getModifiedAt();
String docModifiedBy = nodeResp.getModifiedByUser().getId();
assertEquals(user1, docModifiedBy);
// create shared link to document 1
Map<String, String> body = new HashMap<>();
body.put("nodeId", d1Id);
response = post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
QuickShareLink resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
String shared1Id = resp.getId();
assertNotNull(shared1Id);
assertEquals(d1Id, resp.getNodeId());
assertEquals(fileName1, resp.getName());
assertEquals("The quick brown fox jumps over the lazy dog", resp.getTitle());
assertEquals("Gym class featuring a brown fox and lazy dog", resp.getDescription());
assertEquals(file1_MimeType, resp.getContent().getMimeType());
assertEquals("Adobe PDF Document", resp.getContent().getMimeTypeName());
assertEquals(new Long(file1_originalBytes.length), resp.getContent().getSizeInBytes());
assertEquals("UTF-8", resp.getContent().getEncoding());
// not changed
assertEquals(docModifiedAt.getTime(), resp.getModifiedAt().getTime());
// not changed (ie. not user2)
assertEquals(docModifiedBy, resp.getModifiedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(docModifiedBy), resp.getModifiedByUser().getDisplayName());
assertEquals(user2, resp.getSharedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user2), resp.getSharedByUser().getDisplayName());
// -ve test - try to create again (same user) - already exists
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 409);
// As user 1 ...
setRequestContext(user1);
// create shared link to document 2
body = new HashMap<>();
body.put("nodeId", d2Id);
response = post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
String shared2Id = resp.getId();
// currently passing auth should make no difference (irrespective of MT vs non-MY enb)
// access to get shared link info - pass user1 (but ignore in non-MT)
Map<String, String> params = Collections.singletonMap("include", "allowableOperations");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id, params, 200);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
assertEquals(shared1Id, resp.getId());
assertEquals(fileName1, resp.getName());
assertEquals("The quick brown fox jumps over the lazy dog", resp.getTitle());
assertEquals("Gym class featuring a brown fox and lazy dog", resp.getDescription());
assertEquals(d1Id, resp.getNodeId());
// include is ignored
assertNull(resp.getAllowableOperations());
// include is ignored
assertNull(resp.getAllowableOperationsOnTarget());
// userId not returned
assertNull(resp.getModifiedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user1), resp.getModifiedByUser().getDisplayName());
// userId not returned
assertNull(resp.getSharedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user2), resp.getSharedByUser().getDisplayName());
// access to get shared link info - pass user2 (but ignore in non-MT)
params = Collections.singletonMap("include", "allowableOperations");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id, params, 200);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
assertEquals(shared1Id, resp.getId());
assertEquals(fileName1, resp.getName());
assertEquals(d1Id, resp.getNodeId());
// include is ignored
assertNull(resp.getAllowableOperations());
// include is ignored
assertNull(resp.getAllowableOperationsOnTarget());
// userId not returned
assertNull(resp.getModifiedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user1), resp.getModifiedByUser().getDisplayName());
// userId not returned
assertNull(resp.getSharedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user2), resp.getSharedByUser().getDisplayName());
// allowable operations not included - no params
response = getSingle(QuickShareLinkEntityResource.class, shared1Id, null, 200);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
assertNull(resp.getAllowableOperations());
assertNull(resp.getAllowableOperationsOnTarget());
setRequestContext(null);
// unauth access to get shared link info
// note: this will be ignore for unauth access
params = Collections.singletonMap("include", "allowableOperations");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id, params, 200);
resp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), QuickShareLink.class);
assertEquals(shared1Id, resp.getId());
assertEquals(fileName1, resp.getName());
assertEquals(d1Id, resp.getNodeId());
// include is ignored
assertNull(resp.getAllowableOperations());
// include is ignored
assertNull(resp.getAllowableOperationsOnTarget());
// userId not returned
assertNull(resp.getModifiedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user1), resp.getModifiedByUser().getDisplayName());
// userId not returned
assertNull(resp.getSharedByUser().getId());
assertEquals(UserInfo.getTestDisplayName(user2), resp.getSharedByUser().getDisplayName());
// unauth access to file 1 content (via shared link)
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/content", null, 200);
assertArrayEquals(file1_originalBytes, response.getResponseAsBytes());
Map<String, String> responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(file1_MimeType + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get("Expires"));
assertEquals("attachment; filename=\"" + fileName1 + "\"; filename*=UTF-8''" + fileName1 + "", responseHeaders.get("Content-Disposition"));
String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
// Test 304 response
Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
getSingle(URL_SHARED_LINKS, shared1Id + "/content", null, headers, 304);
// unauth access to file 1 content (via shared link) - without Content-Disposition header (attachment=false)
params = new HashMap<>();
params.put("attachment", "false");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/content", params, 200);
assertArrayEquals(file1_originalBytes, response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(file1_MimeType + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get(LAST_MODIFIED_HEADER));
assertNotNull(responseHeaders.get("Expires"));
assertNull(responseHeaders.get("Content-Disposition"));
// unauth access to file 2 content (via shared link)
response = getSingle(QuickShareLinkEntityResource.class, shared2Id + "/content", null, 200);
assertArrayEquals(content2Text.getBytes(), response.getResponseAsBytes());
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(file2_MimeType + ";charset=ISO-8859-1", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get("Expires"));
assertNotNull(responseHeaders.get(LAST_MODIFIED_HEADER));
assertEquals("attachment; filename=\"" + fileName2 + "\"; filename*=UTF-8''" + fileName2 + "", responseHeaders.get("Content-Disposition"));
// -ve test - unauth access to get shared link file content - without Content-Disposition header (attachment=false) - header ignored (plain text is not in white list)
params = new HashMap<>();
params.put("attachment", "false");
response = getSingle(QuickShareLinkEntityResource.class, shared2Id + "/content", params, 200);
assertEquals("attachment; filename=\"" + fileName2 + "\"; filename*=UTF-8''" + fileName2 + "", response.getHeaders().get("Content-Disposition"));
// -ve shared link rendition tests
{
// -ve test - try to get non-existent rendition content
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib/content", null, 404);
// -ve test - try to get unregistered rendition content
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/dummy/content", null, 404);
}
// unauth access to get rendition info for a shared link (available => CREATED rendition only)
// -ve shared link rendition tests
{
// -ve test - try to get not created rendition for the given shared link
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib", null, 404);
// -ve test - try to get unregistered rendition
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/dummy", null, 404);
}
// unauth access to get shared link renditions info (available => CREATED renditions only)
response = getAll(URL_SHARED_LINKS + "/" + shared1Id + "/renditions", null, 200);
List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals(0, renditions.size());
// create rendition of pdf doc - note: for some reason create rendition of txt doc fail on build m/c (TBC) ?
setRequestContext(user2);
Rendition rendition = createAndGetRendition(d1Id, "doclib");
assertNotNull(rendition);
assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
setRequestContext(null);
// unauth access to get shared link renditions info (available => CREATED renditions only)
response = getAll(URL_SHARED_LINKS + "/" + shared1Id + "/renditions", null, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals(1, renditions.size());
assertEquals(Rendition.RenditionStatus.CREATED, renditions.get(0).getStatus());
assertEquals("doclib", renditions.get(0).getId());
{
// try to get a created rendition for the given shared link
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib", null, 200);
}
// unauth access to get shared link file rendition content
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib/content", null, 200);
assertTrue(response.getResponseAsBytes().length > 0);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get(LAST_MODIFIED_HEADER));
assertNotNull(responseHeaders.get("Expires"));
String docName = "doclib";
assertEquals("attachment; filename=\"" + docName + "\"; filename*=UTF-8''" + docName + "", responseHeaders.get("Content-Disposition"));
// unauth access to get shared link file rendition content - without Content-Disposition header (attachment=false)
params = new HashMap<>();
params.put("attachment", "false");
response = getSingle(QuickShareLinkEntityResource.class, shared1Id + "/renditions/doclib/content", params, 200);
assertTrue(response.getResponseAsBytes().length > 0);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG + ";charset=UTF-8", responseHeaders.get("Content-Type"));
assertNotNull(responseHeaders.get("Expires"));
assertNull(responseHeaders.get("Content-Disposition"));
lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
// Test 304 response
headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
getSingle(URL_SHARED_LINKS, shared1Id + "/renditions/doclib/content", null, headers, 304);
// -ve delete tests
{
// -ve test - unauthenticated
setRequestContext(null);
deleteSharedLink(shared1Id, 401);
setRequestContext(user1);
// -ve test - user1 cannot delete shared link
deleteSharedLink(shared1Id, 403);
// -ve test - delete - cannot delete non-existent link
deleteSharedLink("dummy", 404);
}
// -ve create tests
{
// As user 1 ...
// -ve test - try to create again (different user, that has read permission) - already exists
body = new HashMap<>();
body.put("nodeId", d1Id);
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 409);
// -ve - create - missing nodeId
body = new HashMap<>();
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 400);
// -ve - create - unknown nodeId
body = new HashMap<>();
body.put("nodeId", "dummy");
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 404);
// -ve - create - try to link to folder (ie. not a file)
String f1Id = createFolder(myFolderNodeId, "f1 " + RUNID).getId();
body = new HashMap<>();
body.put("nodeId", f1Id);
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 400);
// -ve test - cannot create if user does not have permission to read
setRequestContext(user2);
body = new HashMap<>();
body.put("nodeId", d2Id);
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 403);
// -ve test - unauthenticated
setRequestContext(null);
body = new HashMap<>();
body.put("nodeId", d1Id);
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 401);
}
// delete shared link
setRequestContext(user2);
deleteSharedLink(shared1Id);
// -ve test - delete - cannot delete non-existent link
setRequestContext(user1);
deleteSharedLink(shared1Id, 404);
setRequestContext(user2);
response = getSingle(NodesEntityResource.class, d1Id, null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
// not changed
assertEquals(docModifiedAt.getTime(), nodeResp.getModifiedAt().getTime());
// not changed (ie. not user2)
assertEquals(docModifiedBy, nodeResp.getModifiedByUser().getId());
// -ve get tests
{
// try to get link that has been deleted (see above)
getSingle(QuickShareLinkEntityResource.class, shared1Id, null, 404);
getSingle(QuickShareLinkEntityResource.class, shared1Id + "/content", null, 404);
// try to get non-existent link
getSingle(QuickShareLinkEntityResource.class, "dummy", null, 404);
getSingle(QuickShareLinkEntityResource.class, "dummy/content", null, 404);
}
// TODO if and when these tests are optionally runnable via remote env then we could skip this part of the test
// (else need to verify test mechanism for enterprise admin via jmx ... etc)
QuickShareLinksImpl quickShareLinks = applicationContext.getBean("quickShareLinks", QuickShareLinksImpl.class);
try {
quickShareLinks.setEnabled(false);
setRequestContext(user1);
// -ve - disabled service tests
body.put("nodeId", "dummy");
post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 501);
setRequestContext(null);
getSingle(QuickShareLinkEntityResource.class, "dummy", null, 501);
getSingle(QuickShareLinkEntityResource.class, "dummy/content", null, 501);
setRequestContext(user1);
deleteSharedLink("dummy", 501);
} finally {
quickShareLinks.setEnabled(true);
}
}
use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.
the class AbstractBaseApiTest method waitAndGetRendition.
protected Rendition waitAndGetRendition(String sourceNodeId, String renditionId, int maxRetry, long pauseTime) throws Exception {
int retryCount = 0;
while (retryCount < maxRetry) {
try {
HttpResponse response = getSingle(getNodeRenditionsUrl(sourceNodeId), renditionId, 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
return rendition;
} catch (AssertionError ex) {
// If the asynchronous create rendition action is not finished yet,
// wait for 'PAUSE_TIME' and try again.
retryCount++;
System.out.println("waitAndGetRendition: " + retryCount);
Thread.sleep(pauseTime);
}
}
return null;
}
Aggregations