Search in sources :

Example 6 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class GetMethodRegressionTest method createTestContent.

private void createTestContent(NodeRef parentNode, int documentsAmount) {
    for (int i = 0; i < documentsAmount; i++) {
        String testDocumentName = String.format(TEST_DOCUMENT_NAME_PATTERN, i, System.currentTimeMillis());
        FileInfo testDocument = fileFolderService.create(parentNode, testDocumentName, ContentModel.TYPE_CONTENT);
        ContentWriter writer = fileFolderService.getWriter(testDocument.getNodeRef());
        writer.putContent(String.format(TEXT_DOCUMENT_CONTENT_PATTERN, testDocumentName));
        writer.setMimetype(TEST_MIMETYPE);
        writer.setEncoding(TEST_ENCODING);
    }
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo)

Example 7 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class MoveMethodTest method canMoveFileUnlock.

@Test
public void canMoveFileUnlock() throws Exception {
    moveMethod = new MoveMethod() {

        @Override
        protected LockInfo checkNode(FileInfo fileInfo, boolean ignoreShared, boolean lockMethod) throws WebDAVServerException {
            LockInfoImpl lockInfo = new LockInfoImpl();
            lockInfo.setExclusiveLockToken("opaque-lock-token");
            lockInfo.setDepth(WebDAV.INFINITY);
            lockInfo.setScope(WebDAV.XML_EXCLUSIVE);
            return lockInfo;
        }

        @Override
        protected LockInfo checkNode(FileInfo fileInfo) throws WebDAVServerException {
            return checkNode(fileInfo, false, false);
        }
    };
    moveMethod.setDetails(req, resp, davHelper, rootNode);
    sourceFileInfo = Mockito.mock(FileInfo.class);
    when(sourceFileInfo.isFolder()).thenReturn(true);
    destPath = "/path/to/test.doc";
    moveMethod.m_strDestinationPath = destPath;
    sourcePath = "/path/from/test.doc";
    moveMethod.m_strPath = sourcePath;
    when(davHelper.getServiceRegistry()).thenReturn(mockServiceRegistry);
    when(mockServiceRegistry.getContentService()).thenReturn(mockContentService);
    List<String> sourcePathSplit = Arrays.asList("path", "from", "test.doc");
    when(davHelper.splitAllPaths(sourcePath)).thenReturn(sourcePathSplit);
    List<String> destPathSplit = Arrays.asList("path", "to", "dest.doc");
    when(davHelper.splitAllPaths(destPath)).thenReturn(destPathSplit);
    when(mockFileFolderService.resolveNamePath(rootNode, sourcePathSplit)).thenReturn(sourceFileInfo);
    FileInfo destFileInfo = Mockito.mock(FileInfo.class);
    when(mockFileFolderService.resolveNamePath(rootNode, destPathSplit)).thenReturn(destFileInfo);
    sourceParentNodeRef = new NodeRef("workspace://SpacesStore/parent1");
    destParentNodeRef = new NodeRef("workspace://SpacesStore/parent2");
    sourceNodeRef = new NodeRef("workspace://SpacesStore/sourcefile");
    moveMethod.moveOrCopy(sourceNodeRef, sourceParentNodeRef, destParentNodeRef, "test.doc");
    verify(mockFileFolderService).moveFrom(sourceNodeRef, sourceParentNodeRef, destParentNodeRef, "test.doc");
    verify(davLockService).unlock(sourceNodeRef);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) Test(org.junit.Test)

Example 8 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class PutMethodTest method testPutContentToWorkingCopy.

/**
 * Putting a content to a working copy file
 * <p>
 * Create and check out a file by user1
 * <p>
 * Try to put the content to the working copy by user2
 *
 * See MNT-8614.
 */
@SuppressWarnings("deprecation")
@Test
public void testPutContentToWorkingCopy() throws Exception {
    FileInfo folder = fileFolderService.create(companyHomeNodeRef, "folder-" + GUID.generate(), ContentModel.TYPE_FOLDER);
    permissionService.setInheritParentPermissions(folder.getNodeRef(), false);
    permissionService.setPermission(folder.getNodeRef(), USER1_NAME, permissionService.getAllPermission(), true);
    AuthenticationUtil.setFullyAuthenticatedUser(USER1_NAME);
    FileInfo testFileInfo = fileFolderService.create(folder.getNodeRef(), "file-" + GUID.generate(), ContentModel.TYPE_CONTENT);
    NodeRef workingCopyNodeRef = checkOutCheckInService.checkout(testFileInfo.getNodeRef());
    String workingCopyName = fileFolderService.getFileInfo(workingCopyNodeRef).getName();
    String pathToWC = "/" + folder.getName() + "/" + workingCopyName;
    String pathToOriginal = "/" + folder.getName() + "/" + testFileInfo.getName();
    // Negative test, try to edit the WC without permissions.
    AuthenticationUtil.setFullyAuthenticatedUser(USER2_NAME);
    try {
        lockService.lock(workingCopyNodeRef, LockType.WRITE_LOCK);
    } catch (AccessDeniedException ade) {
    // expected
    }
    try {
        executeMethod(WebDAV.METHOD_LOCK, pathToWC, davLockInfoUser2File, null);
        fail("The LOCK execution should fail with a 401 error");
    } catch (WebDAVServerException wse) {
        // The execution failed and it is expected
        assertTrue("The status code was " + wse.getHttpStatusCode() + ", but should be " + HttpServletResponse.SC_UNAUTHORIZED, wse.getHttpStatusCode() == HttpServletResponse.SC_UNAUTHORIZED);
    } catch (Exception e) {
        fail("Unexpected exception occurred: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    // Construct IF HEADER
    String lockToken = workingCopyNodeRef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + USER2_NAME;
    String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(WebDAV.HEADER_IF, lockHeaderValue);
    try {
        executeMethod(WebDAV.METHOD_PUT, pathToWC, testDataFile, headers);
        fail("The PUT execution should fail with a 423 error");
    } catch (WebDAVServerException wse) {
        // The execution failed and it is expected
        assertTrue("The status code was " + wse.getHttpStatusCode() + ", but should be " + HttpServletResponse.SC_UNAUTHORIZED, wse.getHttpStatusCode() == HttpServletResponse.SC_UNAUTHORIZED);
    } catch (Exception e) {
        fail("Unexpected exception occurred: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    // Positive test
    AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
    permissionService.setPermission(folder.getNodeRef(), USER2_NAME, permissionService.getAllPermission(), true);
    AuthenticationUtil.setFullyAuthenticatedUser(USER2_NAME);
    try {
        executeMethod(WebDAV.METHOD_LOCK, pathToWC, davLockInfoUser2File, null);
        assertEquals("File should be locked", LockStatus.LOCK_OWNER, lockService.getLockStatus(workingCopyNodeRef));
    } catch (Exception e) {
        fail("Failed to lock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    headers = new HashMap<String, String>();
    headers.put(WebDAV.HEADER_IF, lockHeaderValue);
    try {
        executeMethod(WebDAV.METHOD_PUT, pathToWC, testDataFile, headers);
        assertTrue("File does not exist.", nodeService.exists(workingCopyNodeRef));
        assertEquals("Filename is not correct", workingCopyName, nodeService.getProperty(workingCopyNodeRef, ContentModel.PROP_NAME));
        assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
        assertTrue("File should have NO_CONTENT aspect", nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_NO_CONTENT));
        InputStream updatedFileIS = fileFolderService.getReader(workingCopyNodeRef).getContentInputStream();
        byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
        updatedFileIS.close();
        assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
    } catch (Exception e) {
        fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    headers = new HashMap<String, String>();
    headers.put(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">");
    try {
        executeMethod(WebDAV.METHOD_UNLOCK, pathToWC, null, headers);
        assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
        assertFalse("File should not have NO_CONTENT aspect", nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_NO_CONTENT));
        assertEquals("File should be unlocked", LockStatus.NO_LOCK, lockService.getLockStatus(workingCopyNodeRef));
    } catch (Exception e) {
        fail("Failed to unlock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    // Negative test try to lock or edit the original file
    AuthenticationUtil.setFullyAuthenticatedUser(USER2_NAME);
    try {
        lockService.lock(testFileInfo.getNodeRef(), LockType.WRITE_LOCK);
    } catch (UnableToAquireLockException uale) {
    // expected
    }
    try {
        executeMethod(WebDAV.METHOD_LOCK, pathToOriginal, davLockInfoUser2File, null);
        fail("The LOCK execution should fail with a 423 error");
    } catch (WebDAVServerException wse) {
        // The execution failed and it is expected
        assertTrue("The status code was " + wse.getHttpStatusCode() + ", but should be " + WebDAV.WEBDAV_SC_LOCKED, wse.getHttpStatusCode() == WebDAV.WEBDAV_SC_LOCKED);
    } catch (Exception e) {
        fail("Unexpected exception occurred: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    // Construct IF HEADER
    lockToken = testFileInfo.getNodeRef().getId() + WebDAV.LOCK_TOKEN_SEPERATOR + USER2_NAME;
    lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
    headers = new HashMap<String, String>();
    headers.put(WebDAV.HEADER_IF, lockHeaderValue);
    try {
        executeMethod(WebDAV.METHOD_PUT, pathToOriginal, testDataFile, headers);
        fail("The PUT execution should fail with a 423 error");
    } catch (WebDAVServerException wse) {
        // The execution failed and it is expected
        assertTrue("The status code was " + wse.getHttpStatusCode() + ", but should be " + WebDAV.WEBDAV_SC_LOCKED, wse.getHttpStatusCode() == WebDAV.WEBDAV_SC_LOCKED);
    } catch (Exception e) {
        fail("Unexpected exception occurred: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    }
    AuthenticationUtil.setFullyAuthenticatedUser(USER1_NAME);
    checkOutCheckInService.checkin(workingCopyNodeRef, null);
    AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
    nodeService.deleteNode(folder.getNodeRef());
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) FileInfo(org.alfresco.service.cmr.model.FileInfo) HashMap(java.util.HashMap) InputStream(java.io.InputStream) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) Test(org.junit.Test)

Example 9 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class PutMethodTest method testPutContentToLockedFIle.

/**
 * Putting a content to a locked file
 * <p>
 * Create and lock a file by admin
 * <p>
 * Try to put the content by user
 */
@SuppressWarnings("deprecation")
@Test
public void testPutContentToLockedFIle() throws Exception {
    FileInfo testFileInfo = fileFolderService.create(companyHomeNodeRef, "file-" + GUID.generate(), ContentModel.TYPE_CONTENT);
    lockService.lock(testFileInfo.getNodeRef(), LockType.WRITE_LOCK);
    try {
        AuthenticationUtil.setFullyAuthenticatedUser(USER1_NAME);
        executeMethod(WebDAV.METHOD_PUT, testFileInfo.getName(), testDataFile, null);
        fail("The PUT execution should fail with a 423 error");
    } catch (WebDAVServerException wse) {
        // The execution failed and it is expected
        assertTrue(wse.getHttpStatusCode() == WebDAV.WEBDAV_SC_LOCKED);
    } catch (Exception e) {
        fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    } finally {
        AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
        nodeService.deleteNode(testFileInfo.getNodeRef());
    }
}
Also used : FileInfo(org.alfresco.service.cmr.model.FileInfo) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) Test(org.junit.Test)

Example 10 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class PutMethodTest method testPutContentToAnExistingFile.

@Test
public void testPutContentToAnExistingFile() throws Exception {
    FileInfo testFileInfo = fileFolderService.create(companyHomeNodeRef, "file-" + GUID.generate(), ContentModel.TYPE_CONTENT);
    try {
        executeMethod(WebDAV.METHOD_PUT, testFileInfo.getName(), testDataFile, null);
        assertTrue("File does not exist.", nodeService.exists(testFileInfo.getNodeRef()));
        assertEquals("Filename is not correct.", testFileInfo.getName(), nodeService.getProperty(testFileInfo.getNodeRef(), ContentModel.PROP_NAME));
        assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
        InputStream updatedFileIS = fileFolderService.getReader(testFileInfo.getNodeRef()).getContentInputStream();
        byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
        updatedFileIS.close();
        assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
    } catch (Exception e) {
        fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    } finally {
        nodeService.deleteNode(testFileInfo.getNodeRef());
    }
}
Also used : FileInfo(org.alfresco.service.cmr.model.FileInfo) InputStream(java.io.InputStream) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) Test(org.junit.Test)

Aggregations

FileInfo (org.alfresco.service.cmr.model.FileInfo)101 NodeRef (org.alfresco.service.cmr.repository.NodeRef)82 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)34 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)26 HashMap (java.util.HashMap)20 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)20 QName (org.alfresco.service.namespace.QName)20 AbstractList (java.util.AbstractList)13 UserInfo (org.alfresco.rest.api.model.UserInfo)13 WebApiParam (org.alfresco.rest.framework.WebApiParam)11 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)10 FileFolderService (org.alfresco.service.cmr.model.FileFolderService)9 IOException (java.io.IOException)8 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)8 Serializable (java.io.Serializable)7 List (java.util.List)7 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)7 FacesContext (javax.faces.context.FacesContext)6