Search in sources :

Example 1 with FileData

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData 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));
    }
}
Also used : ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) RestApiUtil.parsePaging(org.alfresco.rest.api.tests.util.RestApiUtil.parsePaging) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) SimpleFixedLimitProvider(org.alfresco.repo.content.ContentLimitProvider.SimpleFixedLimitProvider) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 2 with FileData

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData 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));
}
Also used : ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) RestApiUtil.toJsonAsString(org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Document(org.alfresco.rest.api.tests.client.data.Document) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test)

Example 3 with FileData

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData 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);
}
Also used : MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) RestApiUtil.toJsonAsString(org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString) Document(org.alfresco.rest.api.tests.client.data.Document) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test)

Example 4 with FileData

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testDownloadFileContentReadPermission.

/**
 * Tests download of file/content - basic read permission
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
 */
@Test
public void testDownloadFileContentReadPermission() throws Exception {
    setRequestContext(user1);
    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();
    // Download text content
    response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
    String textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    // Also test versions endpoint (1.0 in this case)
    response = getSingle(NodesEntityResource.class, contentNodeId + "/versions/1.0/content", null, 200);
    textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    // -ve test: user2 does not have read permission
    setRequestContext(user2);
    getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 403);
    getSingle(NodesEntityResource.class, contentNodeId + "/versions/1.0/content", null, 403);
    // add Consumer (~ Read) permission
    setRequestContext(user1);
    Document dUpdate = new Document();
    NodePermissions nodePermissions = new NodePermissions();
    List<NodePermissions.NodePermission> locallySetPermissions = new ArrayList<>();
    locallySetPermissions.add(new NodePermissions.NodePermission(user2, PermissionService.CONSUMER, AccessStatus.ALLOWED.toString()));
    nodePermissions.setLocallySet(locallySetPermissions);
    dUpdate.setPermissions(nodePermissions);
    // update node
    response = put(URL_NODES, contentNodeId, toJsonAsStringNonNull(dUpdate), null, 200);
    setRequestContext(user2);
    // Download text content
    response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
    textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    // Also test versions endpoint (1.0 in this case)
    response = getSingle(NodesEntityResource.class, contentNodeId + "/versions/1.0/content", null, 200);
    textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
}
Also used : NodePermissions(org.alfresco.rest.api.model.NodePermissions) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Document(org.alfresco.rest.api.tests.client.data.Document) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 5 with FileData

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData in project alfresco-remote-api by Alfresco.

the class RenditionsTest method testListNodeRenditions.

/**
 * Tests get node renditions.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/renditions}
 */
@Test
public void testListNodeRenditions() 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();
    Paging paging = getPaging(0, 50);
    // List all available renditions (includes those that have been created and those that are yet to be created)
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 5);
    Rendition docLib = getRendition(renditions, "doclib");
    assertNotNull(docLib);
    assertEquals(RenditionStatus.NOT_CREATED, docLib.getStatus());
    ContentInfo contentInfo = docLib.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNull(contentInfo.getEncoding());
    assertNull(contentInfo.getSizeInBytes());
    // Add a filter to select the renditions based on the given status
    Map<String, String> params = new HashMap<>(1);
    params.put("where", "(status='NOT_CREATED')");
    // List only the NOT_CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 5);
    params.put("where", "(status='CREATED')");
    // List only the CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals("There is no rendition created yet.", 0, renditions.size());
    // Test paging
    // SkipCount=0,MaxItems=2
    paging = getPaging(0, 2);
    // List all available renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals(2, renditions.size());
    ExpectedPaging expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
    assertEquals(2, expectedPaging.getCount().intValue());
    assertEquals(0, expectedPaging.getSkipCount().intValue());
    assertEquals(2, expectedPaging.getMaxItems().intValue());
    assertTrue(expectedPaging.getTotalItems() >= 5);
    assertTrue(expectedPaging.getHasMoreItems());
    // SkipCount=2,MaxItems=3
    paging = getPaging(2, 3);
    // List all available renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals(3, renditions.size());
    expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
    assertEquals(3, expectedPaging.getCount().intValue());
    assertEquals(2, expectedPaging.getSkipCount().intValue());
    assertEquals(3, expectedPaging.getMaxItems().intValue());
    assertTrue(expectedPaging.getTotalItems() >= 5);
    // Create 'doclib' rendition
    createAndGetRendition(contentNodeId, docLib.getId());
    // List all available renditions (includes those that have been created and those that are yet to be created)
    paging = getPaging(0, 50);
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 5);
    docLib = getRendition(renditions, "doclib");
    assertNotNull(docLib);
    assertEquals(RenditionStatus.CREATED, docLib.getStatus());
    contentInfo = docLib.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNotNull(contentInfo.getEncoding());
    assertTrue(contentInfo.getSizeInBytes() > 0);
    // List only the CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals("Should've only returned the 'doclib' rendition.", 1, renditions.size());
    params.put("where", "(status='NOT_CREATED')");
    // List only the NOT_CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() > 0);
    docLib = getRendition(renditions, "doclib");
    assertNull("'doclib' rendition has already been created.", docLib);
    // Test returned renditions are ordered (natural sort order)
    // List all renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(Ordering.natural().isOrdered(renditions));
    // Try again to make sure the ordering wasn't coincidental
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(Ordering.natural().isOrdered(renditions));
    // nodeId in the path parameter does not represent a file
    getAll(getNodeRenditionsUrl(folder_Id), paging, params, 400);
    // nodeId in the path parameter does not exist
    getAll(getNodeRenditionsUrl(UUID.randomUUID().toString()), paging, params, 404);
    // Create a node without any content
    String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId());
    getAll(getNodeRenditionsUrl(emptyContentNodeId), paging, params, 200);
    // Invalid status value
    params.put("where", "(status='WRONG')");
    getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 400);
    // Invalid filter (only 'status' is supported)
    params.put("where", "(id='doclib')");
    getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 400);
}
Also used : HashMap(java.util.HashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) RestApiUtil.toJsonAsString(org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString) Document(org.alfresco.rest.api.tests.client.data.Document) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test)

Aggregations

File (java.io.File)13 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)13 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)13 Test (org.junit.Test)13 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)11 Document (org.alfresco.rest.api.tests.client.data.Document)11 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)9 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)8 HashMap (java.util.HashMap)6 Rendition (org.alfresco.rest.api.tests.client.data.Rendition)6 RestApiUtil.toJsonAsString (org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString)6 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)5 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)5 ExpectedPaging (org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging)3 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 PublicApiClient (org.alfresco.rest.api.tests.client.PublicApiClient)2