Search in sources :

Example 1 with SimpleFixedLimitProvider

use of org.alfresco.repo.content.ContentLimitProvider.SimpleFixedLimitProvider 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)

Aggregations

File (java.io.File)1 SimpleFixedLimitProvider (org.alfresco.repo.content.ContentLimitProvider.SimpleFixedLimitProvider)1 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)1 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)1 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)1 PublicApiClient (org.alfresco.rest.api.tests.client.PublicApiClient)1 ExpectedPaging (org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging)1 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)1 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)1 Document (org.alfresco.rest.api.tests.client.data.Document)1 Folder (org.alfresco.rest.api.tests.client.data.Folder)1 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)1 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)1 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)1 RestApiUtil.parsePaging (org.alfresco.rest.api.tests.util.RestApiUtil.parsePaging)1 Test (org.junit.Test)1