Search in sources :

Example 11 with FileSystemItemChange

use of org.nuxeo.drive.service.FileSystemItemChange in project nuxeo-drive-server by nuxeo.

the class TestGetChangeSummaryMultiRepo method testGetDocumentChangesSummary.

@Test
public void testGetDocumentChangesSummary() throws Exception {
    // Register 3 sync roots and create 3 documents: 2 in the 'test'
    // repository, 1 in the 'other' repository
    DocumentModel doc1;
    DocumentModel doc2;
    DocumentModel doc3;
    Principal administrator = session.getPrincipal();
    nuxeoDriveManager.registerSynchronizationRoot(administrator, folder1, session);
    nuxeoDriveManager.registerSynchronizationRoot(administrator, folder2, session);
    nuxeoDriveManager.registerSynchronizationRoot(administrator, folder3, otherSession);
    doc1 = session.createDocumentModel("/folder1", "doc1", "File");
    doc1.setPropertyValue("file:content", new StringBlob("The content of file 1."));
    doc1 = session.createDocument(doc1);
    doc2 = session.createDocumentModel("/folder2", "doc2", "File");
    doc2.setPropertyValue("file:content", new StringBlob("The content of file 2."));
    doc2 = session.createDocument(doc2);
    doc3 = otherSession.createDocumentModel("/folder3", "doc3", "File");
    doc3.setPropertyValue("file:content", new StringBlob("The content of file 3."));
    doc3 = otherSession.createDocument(doc3);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // Look in all repositories => should find 3 changes
    FileSystemChangeSummary changeSummary = getChangeSummary();
    List<FileSystemItemChange> docChanges = changeSummary.getFileSystemChanges();
    assertEquals(3, docChanges.size());
    Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
    expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "documentChanged", "test"));
    expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "documentChanged", "test"));
    expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "documentChanged", "other"));
    Set<SimpleFileSystemItemChange> changes = new HashSet<>();
    docChanges.forEach(docChange -> {
        changes.add(new SimpleFileSystemItemChange(docChange.getDocUuid(), docChange.getEventId(), docChange.getRepositoryId()));
        assertNotNull(docChange.getFileSystemItem());
    });
    assertTrue(CollectionUtils.isEqualCollection(expectedChanges, changes));
    assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
    // Update documents
    doc1.setPropertyValue("dc:description", "Added description to doc1.");
    doc2.setPropertyValue("dc:description", "Added description to doc1.");
    doc3.setPropertyValue("dc:description", "Added description to doc1.");
    session.saveDocument(doc1);
    session.saveDocument(doc2);
    otherSession.saveDocument(doc3);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    changeSummary = getChangeSummary();
    docChanges = changeSummary.getFileSystemChanges();
    assertEquals(3, docChanges.size());
}
Also used : FileSystemChangeSummary(org.nuxeo.drive.service.FileSystemChangeSummary) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) SimpleFileSystemItemChange(org.nuxeo.drive.fixtures.SimpleFileSystemItemChange) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) SimpleFileSystemItemChange(org.nuxeo.drive.fixtures.SimpleFileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Principal(java.security.Principal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with FileSystemItemChange

use of org.nuxeo.drive.service.FileSystemItemChange in project nuxeo-drive-server by nuxeo.

the class TestPermissionHierarchyFileSystemChanges method testRegisteredSyncRootChildChange.

@Test
public void testRegisteredSyncRootChildChange() throws InterruptedException {
    commitAndWaitForAsyncCompletion();
    DocumentModel folderA;
    DocumentModel folderC;
    // Create the tree structure
    try {
        createFolderTree(session1, userWorkspace1.getPathAsString(), 5);
        folderA = session1.getChild(userWorkspace1.getRef(), "Folder A");
        DocumentModel folderB = session1.getChild(folderA.getRef(), "Folder B");
        folderC = session1.getChild(folderB.getRef(), "Folder C");
        setPermission(session1, folderA, "user2", SecurityConstants.EVERYTHING, true);
        setPermission(session1, folderC, "user2", SecurityConstants.READ, true);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    lastEventLogId = nuxeoDriveManager.getChangeFinder().getUpperBound();
    // Register FolderA as a synchronization root for user2
    try {
        nuxeoDriveManager.registerSynchronizationRoot(session2.getPrincipal(), folderA, session2);
        assertTrue(nuxeoDriveManager.getSynchronizationRootReferences(session2).contains(new IdRef(folderA.getId())));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    // Check file system item change
    try {
        List<FileSystemItemChange> changes = getChanges(principal2);
        assertEquals(1, changes.size());
        FileSystemItemChange change = changes.get(0);
        assertEquals("rootRegistered", change.getEventId());
        assertEquals("Folder A", change.getFileSystemItemName());
        assertNotNull(change.getFileSystemItem());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        resetPermissions(session1, folderA.getRef(), "user2");
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    // Check file system item change
    try {
        List<FileSystemItemChange> changes = getChanges(principal2);
        assertEquals(1, changes.size());
        FileSystemItemChange change = changes.get(0);
        assertEquals("securityUpdated", change.getEventId());
        assertNull(change.getFileSystemItemName());
        assertNull(change.getFileSystemItem());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    // Register Folder C as a synchronization root for user2
    try {
        nuxeoDriveManager.registerSynchronizationRoot(session2.getPrincipal(), folderC, session2);
        assertFalse(nuxeoDriveManager.getSynchronizationRootReferences(session2).contains(new IdRef(folderA.getId())));
        assertTrue(nuxeoDriveManager.getSynchronizationRootReferences(session2).contains(new IdRef(folderC.getId())));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    // Check file system item change
    try {
        assertEquals(1, session2.getChildren(folderC.getRef()).size());
        List<FileSystemItemChange> changes = getChanges(principal2);
        assertEquals(1, changes.size());
        FileSystemItemChange change = changes.get(0);
        assertEquals("rootRegistered", change.getEventId());
        assertEquals("Folder C", change.getFileSystemItemName());
        assertNotNull(change.getFileSystemItem());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 13 with FileSystemItemChange

use of org.nuxeo.drive.service.FileSystemItemChange in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveManagerImpl method getChangeSummary.

protected FileSystemChangeSummary getChangeSummary(Principal principal, Map<String, Set<IdRef>> lastActiveRootRefs, Map<String, SynchronizationRoots> roots, Map<String, Set<String>> collectionSyncRootMemberIds, long lowerBound, boolean integerBounds) {
    List<FileSystemItemChange> allChanges = new ArrayList<FileSystemItemChange>();
    // Compute the list of all repositories to consider for the aggregate summary
    Set<String> allRepositories = new TreeSet<String>();
    allRepositories.addAll(roots.keySet());
    allRepositories.addAll(lastActiveRootRefs.keySet());
    allRepositories.addAll(collectionSyncRootMemberIds.keySet());
    long syncDate;
    long upperBound;
    if (integerBounds) {
        upperBound = changeFinder.getUpperBound(allRepositories);
        // Truncate sync date to 0 milliseconds
        syncDate = System.currentTimeMillis();
        syncDate = syncDate - (syncDate % 1000);
    } else {
        upperBound = changeFinder.getCurrentDate();
        syncDate = upperBound;
    }
    Boolean hasTooManyChanges = Boolean.FALSE;
    int limit = Integer.parseInt(Framework.getProperty(DOCUMENT_CHANGE_LIMIT_PROPERTY, "1000"));
    if (!allRepositories.isEmpty() && lowerBound >= 0 && upperBound > lowerBound) {
        for (String repositoryName : allRepositories) {
            try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
                // Get document changes
                Set<IdRef> lastRefs = lastActiveRootRefs.get(repositoryName);
                if (lastRefs == null) {
                    lastRefs = Collections.emptySet();
                }
                SynchronizationRoots activeRoots = roots.get(repositoryName);
                if (activeRoots == null) {
                    activeRoots = SynchronizationRoots.getEmptyRoots(repositoryName);
                }
                Set<String> repoCollectionSyncRootMemberIds = collectionSyncRootMemberIds.get(repositoryName);
                if (repoCollectionSyncRootMemberIds == null) {
                    repoCollectionSyncRootMemberIds = Collections.emptySet();
                }
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Start: getting FileSystemItem changes for repository %s / user %s between %s and %s with activeRoots = %s", repositoryName, principal.getName(), lowerBound, upperBound, activeRoots.getPaths()));
                }
                List<FileSystemItemChange> changes;
                if (integerBounds) {
                    changes = changeFinder.getFileSystemChangesIntegerBounds(session, lastRefs, activeRoots, repoCollectionSyncRootMemberIds, lowerBound, upperBound, limit);
                } else {
                    changes = changeFinder.getFileSystemChanges(session, lastRefs, activeRoots, lowerBound, upperBound, limit);
                }
                allChanges.addAll(changes);
            } catch (TooManyChangesException e) {
                hasTooManyChanges = Boolean.TRUE;
                allChanges.clear();
                break;
            }
        }
    }
    // Send back to the client the list of currently active roots to be able
    // to efficiently detect root unregistration events for the next
    // incremental change summary
    Map<String, Set<IdRef>> activeRootRefs = new HashMap<String, Set<IdRef>>();
    for (Map.Entry<String, SynchronizationRoots> rootsEntry : roots.entrySet()) {
        activeRootRefs.put(rootsEntry.getKey(), rootsEntry.getValue().getRefs());
    }
    FileSystemChangeSummary summary = new FileSystemChangeSummaryImpl(allChanges, activeRootRefs, syncDate, upperBound, hasTooManyChanges);
    if (log.isDebugEnabled()) {
        log.debug(String.format("End: getting %d FileSystemItem changes for user %s between %s and %s with activeRoots = %s -> %s", allChanges.size(), principal.getName(), lowerBound, upperBound, roots, summary));
    }
    return summary;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) FileSystemChangeSummary(org.nuxeo.drive.service.FileSystemChangeSummary) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) TreeSet(java.util.TreeSet) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) TooManyChangesException(org.nuxeo.drive.service.TooManyChangesException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with FileSystemItemChange

use of org.nuxeo.drive.service.FileSystemItemChange in project nuxeo-drive-server by nuxeo.

the class AuditChangeFinderTestSuite method testFolderishCollection3.

/**
 * <pre>
 * /folder1                 -> isMemberOf(collectionFolder)
 *   |-- collectionFolder   -> synchronization root
 * /collectionSyncRoot      -> synchronization root
 * /testDoc                 -> isMemberOf(collectionFolder, collectionSyncRoot)
 * </pre>
 */
@Test
public void testFolderishCollection3() throws Exception {
    DocumentModel collectionFolder;
    DocumentModel collectionSyncRoot;
    DocumentModel testDoc;
    List<FileSystemItemChange> changes;
    try {
        log.trace("testFolderishCollection3():" + "\nCreate a folder with the Collection facet (\"collectionFolder\") inside a folder (\"folder1\") and register it as a sycnhronization root;" + "\nAdd \"folder1\" to the \"collectionFolder\" collection;" + "\nCreate a collection \"collectionSyncRoot\" and register it as a synchronization root;" + "\nCreate a document \"testDoc\" and add it to both collections \"collectionFolder\" and \"collectionSyncRoot\".\n");
        collectionFolder = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder = session.createDocument(collectionFolder);
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionFolder, session);
        collectionManager.addToCollection(collectionFolder, folder1, session);
        collectionSyncRoot = collectionManager.createCollection(session, "collectionSyncRoot", null, "/");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionSyncRoot, session);
        testDoc = session.createDocumentModel("/", "testDoc", "File");
        testDoc.setPropertyValue("file:content", new StringBlob("The content of testDoc."));
        testDoc = session.createDocument(testDoc);
        collectionManager.addToCollection(collectionFolder, testDoc, session);
        collectionManager.addToCollection(collectionSyncRoot, testDoc, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 12 (among which 10 distinct) changes:
        // - addedToCollection for testDoc
        // - documentModified for collectionSyncRoot
        // - addedToCollection for testDoc
        // - documentModified for collectionFolder
        // - documentCreated for testDoc
        // - rootRegistered for collectionSyncRoot
        // - documentCreated for collectionSyncRoot
        // - addedToCollection for folder1
        // - documentModified for collectionFolder
        // - rootRegistered for collectionFolder
        // - documentCreated for collectionFolder
        // - documentCreated for folder1
        changes = getChanges(session.getPrincipal());
        assertEquals(12, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with FileSystemItemChange

use of org.nuxeo.drive.service.FileSystemItemChange in project nuxeo-drive-server by nuxeo.

the class AuditChangeFinderTestSuite method testSyncRootParentWithSingleQuotePermissionChange.

@Test
public void testSyncRootParentWithSingleQuotePermissionChange() throws Exception {
    List<FileSystemItemChange> changes;
    DocumentModel folder;
    DocumentModel subFolder;
    try {
        // No sync roots
        changes = getChanges();
        assertTrue(changes.isEmpty());
        // Create a folder with a single quote including a subfolder
        folder = session.createDocumentModel("/", "fol'der", "Folder");
        folder = session.createDocument(folder);
        subFolder = session.createDocumentModel("/fol'der", "subFolder", "Folder");
        subFolder = session.createDocument(subFolder);
        // Grant READ_WRITE permission to user1 on the parent folder
        setPermissions(folder, new ACE("user1", SecurityConstants.READ_WRITE));
        // Mark subfolder as a sync root for user1
        nuxeoDriveManager.registerSynchronizationRoot(user1Session.getPrincipal(), subFolder, user1Session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Get changes for user1
        changes = getChanges(user1Session.getPrincipal());
        // Folder creation and sync root registration events
        assertEquals(2, changes.size());
        // Remove READ_WRITE permission granted to user1 on the parent folder
        resetPermissions(folder, "user1");
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges(user1Session.getPrincipal());
        // Expecting 1 change: securityUpdated for the subfolder with a null FileSystemItem and FileSystemItem name
        // since the user cannot access it anymore
        assertEquals(1, changes.size());
        FileSystemItemChange change = changes.get(0);
        SimpleFileSystemItemChange simpleChange = new SimpleFileSystemItemChange(subFolder.getId(), "securityUpdated", "test", "test#" + subFolder.getId(), "subFolder");
        assertEquals(simpleChange, toSimpleFileSystemItemChange(change));
        assertNull(change.getFileSystemItem());
        assertNull(change.getFileSystemItemName());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : ACE(org.nuxeo.ecm.core.api.security.ACE) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Aggregations

FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)30 Test (org.junit.Test)26 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)24 HashSet (java.util.HashSet)17 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)13 IdRef (org.nuxeo.ecm.core.api.IdRef)7 ACE (org.nuxeo.ecm.core.api.security.ACE)7 FileSystemChangeSummary (org.nuxeo.drive.service.FileSystemChangeSummary)6 Principal (java.security.Principal)5 ArrayList (java.util.ArrayList)3 SimpleFileSystemItemChange (org.nuxeo.drive.fixtures.SimpleFileSystemItemChange)2 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)2 TooManyChangesException (org.nuxeo.drive.service.TooManyChangesException)2 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)2 NuxeoPrincipalImpl (org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1