Search in sources :

Example 1 with IdRef

use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-filesystem-connectors by nuxeo.

the class PathCache method get.

public DocumentModel get(String path) {
    Value value = pathToUuidCache.get(path);
    if (value == null) {
        return null;
    }
    if (value.getExpiredTime() < System.currentTimeMillis()) {
        pathToUuidCache.remove(path);
        return null;
    }
    String uuid = value.getValue();
    DocumentModel model = null;
    try {
        model = session.getDocument(new IdRef(uuid));
    } catch (DocumentNotFoundException e) {
    // do nothing
    }
    if (model == null) {
        pathToUuidCache.remove(path);
    }
    return model;
}
Also used : DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 2 with IdRef

use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveManagerImpl method addToLocallyEditedCollection.

@Override
public void addToLocallyEditedCollection(CoreSession session, DocumentModel doc) {
    // Add document to "Locally Edited" collection, creating if if not
    // exists
    CollectionManager cm = Framework.getService(CollectionManager.class);
    DocumentModel userCollections = cm.getUserDefaultCollections(doc, session);
    DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), LOCALLY_EDITED_COLLECTION_NAME);
    DocumentModel locallyEditedCollection = null;
    if (session.exists(locallyEditedCollectionRef)) {
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
        cm.addToCollection(locallyEditedCollection, doc, session);
    } else {
        cm.addToNewCollection(LOCALLY_EDITED_COLLECTION_NAME, "Documents locally edited with Nuxeo Drive", doc, session);
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
    }
    // Register "Locally Edited" collection as a synchronization root if not
    // already the case
    Set<IdRef> syncRootRefs = getSynchronizationRootReferences(session);
    if (!syncRootRefs.contains(new IdRef(locallyEditedCollection.getId()))) {
        registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) PathRef(org.nuxeo.ecm.core.api.PathRef) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 3 with IdRef

use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.

the class AbstractChangeFinderTestCase method toSimpleFileSystemItemChange.

protected SimpleFileSystemItemChange toSimpleFileSystemItemChange(FileSystemItemChange change) {
    SimpleFileSystemItemChange simpleChange = new SimpleFileSystemItemChange(change.getDocUuid(), change.getEventId(), change.getRepositoryId(), change.getFileSystemItemId(), change.getFileSystemItemName());
    DocumentRef changeDocRef = new IdRef(change.getDocUuid());
    if (session.exists(changeDocRef)) {
        simpleChange.setLifeCycleState(session.getDocument(changeDocRef).getCurrentLifeCycleState());
    }
    return simpleChange;
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) IdRef(org.nuxeo.ecm.core.api.IdRef)

Example 4 with IdRef

use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.

the class AuditChangeFinderTestSuite method testSyncUnsyncRootsAsAnotherUser.

@Test
public void testSyncUnsyncRootsAsAnotherUser() throws Exception {
    Principal user1Principal = user1Session.getPrincipal();
    List<FileSystemItemChange> changes;
    try {
        // No sync roots expected for user1
        changes = getChanges(user1Principal);
        assertNotNull(changes);
        assertTrue(changes.isEmpty());
        // Register sync roots for user1 as Administrator
        nuxeoDriveManager.registerSynchronizationRoot(user1Principal, folder1, session);
        nuxeoDriveManager.registerSynchronizationRoot(user1Principal, folder2, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // user1 should have 2 sync roots
        Set<IdRef> activeRootRefs = nuxeoDriveManager.getSynchronizationRootReferences(user1Session);
        assertNotNull(activeRootRefs);
        assertEquals(2, activeRootRefs.size());
        assertTrue(activeRootRefs.contains(folder1.getRef()));
        assertTrue(activeRootRefs.contains(folder2.getRef()));
        // There should be 2 changes detected in the audit
        changes = getChanges(user1Principal);
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder2.getId(), "folder2"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder1.getId(), "folder1"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        for (FileSystemItemChange change : changes) {
            assertNotNull(change.getFileSystemItem());
        }
        // Unregister sync roots for user1 as Administrator
        nuxeoDriveManager.unregisterSynchronizationRoot(user1Principal, folder1, session);
        nuxeoDriveManager.unregisterSynchronizationRoot(user1Principal, folder2, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // user1 should have no sync roots
        Set<IdRef> activeRootRefs = nuxeoDriveManager.getSynchronizationRootReferences(user1Session);
        assertNotNull(activeRootRefs);
        assertTrue(activeRootRefs.isEmpty());
        // There should be 2 changes detected in the audit
        changes = getChanges(user1Principal);
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "deleted", "test", "test#" + folder2.getId(), "folder2"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "deleted", "test", "test#" + folder1.getId(), "folder1"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        // Not adaptable as a FileSystemItem since unregistered
        for (FileSystemItemChange change : changes) {
            assertNull(change.getFileSystemItem());
        }
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) IdRef(org.nuxeo.ecm.core.api.IdRef) Principal(java.security.Principal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with IdRef

use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.

the class AuditChangeFinder method getFileSystemChanges.

protected List<FileSystemItemChange> getFileSystemChanges(CoreSession session, Set<IdRef> lastActiveRootRefs, SynchronizationRoots activeRoots, Set<String> collectionSyncRootMemberIds, long lowerBound, long upperBound, boolean integerBounds, int limit) throws TooManyChangesException {
    String principalName = session.getPrincipal().getName();
    List<FileSystemItemChange> changes = new ArrayList<FileSystemItemChange>();
    // Note: lastActiveRootRefs is not used: we could remove it from the
    // public API
    // and from the client as well but it might be useful to optimize future
    // alternative implementations FileSystemChangeFinder component so it
    // might
    // be better to leave it part of the public API as currently.
    // Find changes from the log under active roots or events that are
    // linked to the un-registration or deletion of formerly synchronized
    // roots
    List<LogEntry> entries = queryAuditEntries(session, activeRoots, collectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
    // query with the actual active roots.
    for (LogEntry entry : entries) {
        if (NuxeoDriveEvents.EVENT_CATEGORY.equals(entry.getCategory())) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Detected sync root change for user '%s' in audit log:" + " invalidating the root cache and refetching the changes.", principalName));
            }
            NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
            driveManager.invalidateSynchronizationRootsCache(principalName);
            driveManager.invalidateCollectionSyncRootMemberCache(principalName);
            Map<String, SynchronizationRoots> synchronizationRoots = driveManager.getSynchronizationRoots(session.getPrincipal());
            SynchronizationRoots updatedActiveRoots = synchronizationRoots.get(session.getRepositoryName());
            Set<String> updatedCollectionSyncRootMemberIds = driveManager.getCollectionSyncRootMemberIds(session.getPrincipal()).get(session.getRepositoryName());
            entries = queryAuditEntries(session, updatedActiveRoots, updatedCollectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
            break;
        }
    }
    if (entries.size() >= limit) {
        throw new TooManyChangesException("Too many changes found in the audit logs.");
    }
    for (LogEntry entry : entries) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Handling log entry %s", entry));
        }
        FileSystemItemChange change = null;
        DocumentRef docRef = new IdRef(entry.getDocUUID());
        ExtendedInfo fsIdInfo = entry.getExtendedInfos().get("fileSystemItemId");
        if (fsIdInfo != null) {
            // been updated, we just know the FileSystemItem id and name.
            if (log.isDebugEnabled()) {
                log.debug(String.format("Found extended info in audit log entry: document has been deleted, moved," + " is an unregistered synchronization root or its security has been updated," + " we just know the FileSystemItem id and name."));
            }
            boolean isChangeSet = false;
            // current user still has access to the document.
            if (!"deleted".equals(entry.getEventId()) && session.exists(docRef)) {
                change = getFileSystemItemChange(session, docRef, entry, fsIdInfo.getValue(String.class));
                if (change != null) {
                    if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
                        // virtual event.
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Document %s (%s) has been moved to another synchronzation root, not adding entry to the change summary.", entry.getDocPath(), docRef));
                        }
                        continue;
                    }
                    isChangeSet = true;
                }
            }
            if (!isChangeSet) {
                // FileSystemItem id and name to the FileSystemItemChange entry.
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Document %s (%s) doesn't exist or is not adaptable as a FileSystemItem, only providing the FileSystemItem id and name to the FileSystemItemChange entry.", entry.getDocPath(), docRef));
                }
                String fsId = fsIdInfo.getValue(String.class);
                String eventId;
                if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
                    // Move to a non synchronization root
                    eventId = NuxeoDriveEvents.DELETED_EVENT;
                } else {
                    // Deletion, unregistration or security update
                    eventId = entry.getEventId();
                }
                change = new FileSystemItemChangeImpl(eventId, entry.getEventDate().getTime(), entry.getRepositoryId(), entry.getDocUUID(), fsId, null);
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
            }
            changes.add(change);
        } else {
            // unregistered synchronization root nor a security update denying access to the current user.
            if (log.isDebugEnabled()) {
                log.debug(String.format("No extended info found in audit log entry %s (%s): this is not a deleted document, a moved document," + " an unregistered synchronization root nor a security update denying access to the current user.", entry.getDocPath(), docRef));
            }
            if (!session.exists(docRef)) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Document %s (%s) doesn't exist, not adding entry to the change summary.", entry.getDocPath(), docRef));
                }
                // try to propagate this event.
                continue;
            }
            // Let's try to adapt the document as a FileSystemItem to
            // provide it to the FileSystemItemChange entry.
            change = getFileSystemItemChange(session, docRef, entry, null);
            if (change == null) {
                // Non-adaptable documents are ignored
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Document %s (%s) is not adaptable as a FileSystemItem, not adding any entry to the change summary.", entry.getDocPath(), docRef));
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
                }
                changes.add(change);
            }
        }
    }
    return changes;
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) ArrayList(java.util.ArrayList) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) ExtendedInfo(org.nuxeo.ecm.platform.audit.api.ExtendedInfo) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) TooManyChangesException(org.nuxeo.drive.service.TooManyChangesException) LogEntry(org.nuxeo.ecm.platform.audit.api.LogEntry) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager)

Aggregations

IdRef (org.nuxeo.ecm.core.api.IdRef)31 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)18 Test (org.junit.Test)13 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)8 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)8 ArrayList (java.util.ArrayList)7 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)7 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)7 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)7 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)6 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)4 Principal (java.security.Principal)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 FolderItem (org.nuxeo.drive.adapter.FolderItem)3 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)3 DocumentBackedFileItem (org.nuxeo.drive.adapter.impl.DocumentBackedFileItem)3 FileSystemChangeSummary (org.nuxeo.drive.service.FileSystemChangeSummary)3 Blob (org.nuxeo.ecm.automation.client.model.Blob)3