Search in sources :

Example 11 with MultiPartRequest

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

the class NodeApiTest method testGuessMimeTypeAndEncoding.

/**
 * Tests guess mimetype & guess encoding when uploading file/content (create or update) - also empty file/content
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 * <p>PUT:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
 */
@Test
public void testGuessMimeTypeAndEncoding() throws Exception {
    setRequestContext(user1);
    String fId = createFolder(getMyNodeId(), "test-folder-guess-" + RUNID).getId();
    // create empty files
    Document d = new Document();
    d.setName("my doc");
    d.setNodeType(TYPE_CM_CONTENT);
    HttpResponse response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(MimetypeMap.MIMETYPE_BINARY, documentResp.getContent().getMimeType());
    assertEquals("Binary File (Octet Stream)", documentResp.getContent().getMimeTypeName());
    assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
    assertEquals("UTF-8", documentResp.getContent().getEncoding());
    d = new Document();
    d.setName("my doc.txt");
    d.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, documentResp.getContent().getMimeType());
    assertEquals("Plain Text", documentResp.getContent().getMimeTypeName());
    assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
    assertEquals("UTF-8", documentResp.getContent().getEncoding());
    d = new Document();
    d.setName("my doc.pdf");
    d.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(MimetypeMap.MIMETYPE_PDF, documentResp.getContent().getMimeType());
    assertEquals("Adobe PDF Document", documentResp.getContent().getMimeTypeName());
    assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
    assertEquals("UTF-8", documentResp.getContent().getEncoding());
    // upload files
    String fileName = "quick-2.pdf";
    File file = getResourceFile(fileName);
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    MultiPartRequest reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    ContentInfo contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "quick-2.pdf";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(// note: we've deliberately dropped the file ext here
    new FileData("quick-2", file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "example-1.txt";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("ISO-8859-1", contentInfo.getEncoding());
    fileName = "example-2.txt";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "shift-jis.txt";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("Shift_JIS", contentInfo.getEncoding());
    fileName = "example-1.xml";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
    assertEquals("ISO-8859-1", contentInfo.getEncoding());
    fileName = "example-2.xml";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // upload file, rename and then update file
    fileName = "quick.pdf";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String docId = document.getId();
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // rename (mimeType remains unchanged, binary has not changed)
    Document dUpdate = new Document();
    dUpdate.setName("quick.docx");
    response = put(URL_NODES, docId, toJsonAsStringNonNull(dUpdate), null, 200);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "quick.docx";
    file = getResourceFile(fileName);
    BinaryPayload payload = new BinaryPayload(file);
    response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // additional test
    fileName = "CMIS-Delete.json";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData("special-" + GUID.generate(), file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // cleanup
    deleteNode(fId);
}
Also used : MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) BinaryPayload(org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) 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) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 12 with MultiPartRequest

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

the class ParamsExtractorTests method testMultiPartPostExtractor.

@Test
public void testMultiPartPostExtractor() throws Exception {
    ResourceWebScriptPost extractor = new ResourceWebScriptPost();
    extractor.setAssistant(assistant);
    Map<String, String> templateVars = new HashMap<String, String>();
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
    File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
    PrintWriter writer = new PrintWriter(file);
    writer.println("Multipart Mock test.");
    writer.close();
    MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN)).build();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
    mockRequest.setContent(reqBody.getBody());
    mockRequest.setContentType(reqBody.getContentType());
    when(request.getContentType()).thenReturn("multipart/form-data");
    when(request.parseContent()).thenReturn(new FormData(mockRequest));
    Params params = extractor.extractParams(mockEntity(), request);
    assertNotNull(params);
    Object passed = params.getPassedIn();
    assertNotNull(passed);
    assertTrue(FormData.class.isAssignableFrom(passed.getClass()));
    FormData formData = (FormData) passed;
    assertTrue(formData.getIsMultiPart());
    // No entity id for POST
    templateVars.put(ResourceLocator.ENTITY_ID, "1234");
    try {
        params = extractor.extractParams(mockEntity(), request);
        fail("Should not get here. No entity id for POST");
    } catch (UnsupportedResourceOperationException uoe) {
        assertNotNull(uoe);
    }
    params = extractor.extractParams(mockRelationship(), request);
    assertNotNull(params);
    assertEquals("1234", params.getEntityId());
    passed = params.getPassedIn();
    assertNotNull(passed);
    assertTrue(FormData.class.isAssignableFrom(passed.getClass()));
    formData = (FormData) passed;
    assertTrue(formData.getIsMultiPart());
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) FormData(org.springframework.extensions.webscripts.servlet.FormData) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Params(org.alfresco.rest.framework.resource.parameters.Params) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) Match(org.springframework.extensions.webscripts.Match) ResourceWebScriptPost(org.alfresco.rest.framework.webscripts.ResourceWebScriptPost) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 13 with MultiPartRequest

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

the class SerializeTests method testInvokeMultiPartEntity.

@Test
public void testInvokeMultiPartEntity() throws IOException {
    ResourceWithMetadata entityResource = locator.locateEntityResource(api, "multiparttest", HttpMethod.POST);
    assertNotNull(entityResource);
    MultiPartResourceAction.Create<?> resource = (MultiPartResourceAction.Create<?>) entityResource.getResource();
    File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
    PrintWriter writer = new PrintWriter(file);
    writer.println("Multipart Mock test2.");
    writer.close();
    MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN)).build();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
    mockRequest.setContent(reqBody.getBody());
    mockRequest.setContentType(reqBody.getContentType());
    String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, NOT_USED, resource.create(new FormData(mockRequest), NOT_USED, callBack)));
    assertTrue("There must be json output", StringUtils.startsWith(out, "{\"entry\":"));
}
Also used : FormData(org.springframework.extensions.webscripts.servlet.FormData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MultiPartResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) PrintWriter(java.io.PrintWriter) 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