Search in sources :

Example 1 with FileSystemItem

use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testCollectionMembership.

@Test
public void testCollectionMembership() {
    DocumentModel doc = session.createDocumentModel(session.getRootDocument().getPathAsString(), "testDoc", "File");
    Blob blob = new StringBlob("Content of Joe's file.");
    blob.setFilename("Joe.odt");
    doc.setPropertyValue("file:content", (Serializable) blob);
    doc = session.createDocument(doc);
    log.trace("Try to adapt a document not member of any collection");
    try {
        defaultFileSystemItemFactory.getFileSystemItem(doc);
        fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
    } catch (RootlessItemException e) {
        log.trace(e);
    }
    log.trace("Try to adapt a document member of a non sync root collection");
    DocumentModel nonSyncrootCollection = collectionManager.createCollection(session, "Non sync root collection", "", session.getRootDocument().getPathAsString());
    collectionManager.addToCollection(nonSyncrootCollection, doc, session);
    try {
        defaultFileSystemItemFactory.getFileSystemItem(doc);
        fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
    } catch (RootlessItemException e) {
        log.trace(e);
    }
    log.trace("Adapt a document member of a non sync root colllection and a sync root collection");
    DocumentModel syncRootCollection = collectionManager.createCollection(session, "Sync root collection", "", session.getRootDocument().getPathAsString());
    nuxeoDriveManager.registerSynchronizationRoot(principal, syncRootCollection, session);
    collectionManager.addToCollection(syncRootCollection, doc, session);
    FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(doc);
    assertNotNull(fsItem);
    log.trace("Adapt a document member of a sync root collection only");
    collectionManager.removeFromCollection(nonSyncrootCollection, doc, session);
    assertEquals(fsItem, defaultFileSystemItemFactory.getFileSystemItem(doc));
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 2 with FileSystemItem

use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testLockedDocument.

@Test
public void testLockedDocument() {
    setPermission(syncRootFolder, "joe", SecurityConstants.READ_WRITE, true);
    setPermission(syncRootFolder, "jack", SecurityConstants.READ_WRITE, true);
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, joeSession);
        DocumentModel joeFile = joeSession.getDocument(file.getRef());
        log.trace("Check readonly flags on an unlocked document");
        FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(joeFile);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
        assertTrue(((FileItem) fsItem).getCanUpdate());
        assertNull(fsItem.getLockInfo());
        log.trace("Check readonly flags on an document locked by the current user");
        joeSession.setLock(joeFile.getRef());
        // Re-fetch document to clear lock info
        joeFile = joeSession.getDocument(file.getRef());
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(joeFile);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
        assertTrue(((FileItem) fsItem).getCanUpdate());
        Lock lockInfo = fsItem.getLockInfo();
        assertNotNull(lockInfo);
        assertEquals("joe", lockInfo.getOwner());
        assertNotNull(lockInfo.getCreated());
        // Check that the lock info is not fetched for FileSystemItem
        // adaptation when calling getChildren or
        // scrollDescendants
        FileSystemItemFactory defaultSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("defaultSyncRootFolderItemFactory");
        FolderItem syncRootFolderItem = (FolderItem) defaultSyncRootFolderItemFactory.getFileSystemItem(syncRootFolder);
        List<FileSystemItem> children = syncRootFolderItem.getChildren();
        assertEquals(5, children.size());
        for (FileSystemItem child : children) {
            assertNull(child.getLockInfo());
        }
        children = syncRootFolderItem.scrollDescendants(null, 10, 1000);
        assertEquals(5, children.size());
        for (FileSystemItem child : children) {
            assertNull(child.getLockInfo());
        }
        try (CloseableCoreSession jackSession = coreFeature.openCoreSession("jack")) {
            nuxeoDriveManager.registerSynchronizationRoot(jackSession.getPrincipal(), syncRootFolder, jackSession);
            DocumentModel jackFile = jackSession.getDocument(file.getRef());
            log.trace("Check readonly flags for a non administrator on a document locked by another user");
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(jackFile);
            assertFalse(fsItem.getCanRename());
            assertFalse(fsItem.getCanDelete());
            assertFalse(((FileItem) fsItem).getCanUpdate());
            lockInfo = fsItem.getLockInfo();
            assertNotNull(lockInfo);
            assertEquals("joe", lockInfo.getOwner());
            assertNotNull(lockInfo.getCreated());
            log.trace("Check readonly flags for an administrator on a document locked by another user");
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
            assertTrue(fsItem.getCanRename());
            assertTrue(fsItem.getCanDelete());
            assertTrue(((FileItem) fsItem).getCanUpdate());
            lockInfo = fsItem.getLockInfo();
            assertNotNull(lockInfo);
            assertEquals("joe", lockInfo.getOwner());
            assertNotNull(lockInfo.getCreated());
            log.trace("Check readonly flags for a non administrator on an unlocked document");
            joeSession.removeLock(joeFile.getRef());
            // Re-fetch document to clear lock info
            jackFile = jackSession.getDocument(file.getRef());
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(jackFile);
            assertTrue(fsItem.getCanRename());
            assertTrue(fsItem.getCanDelete());
            assertTrue(((FileItem) fsItem).getCanUpdate());
            assertNull(fsItem.getLockInfo());
        }
    }
    resetPermissions(syncRootFolder, "jack");
    resetPermissions(syncRootFolder, "joe");
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FolderItem(org.nuxeo.drive.adapter.FolderItem) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Lock(org.nuxeo.ecm.core.api.Lock) Test(org.junit.Test)

Example 3 with FileSystemItem

use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testScrollDescendantsIncludingCollections.

@Test
public void testScrollDescendantsIncludingCollections() {
    log.trace("Add a document to a new collection \"testCollection\" created in \"/default-domain/UserWorkspaces/Administrator/Collections\"");
    collectionManager.addToNewCollection("testCollection", null, file, session);
    DocumentModel userCollections = collectionManager.getUserDefaultCollections(null, session);
    DocumentModel userWorkspace = session.getParentDocument(userCollections.getRef());
    log.trace("Create \"testFolder\" in \"/default-domain/UserWorkspaces/Administrator\"");
    DocumentModel testFolder = session.createDocumentModel(userWorkspace.getPathAsString(), "testFolder", "Folder");
    testFolder = session.createDocument(testFolder);
    log.trace("Register \"/default-domain/UserWorkspaces/Administrator\" as a synchronization root for Administrator");
    nuxeoDriveManager.registerSynchronizationRoot(principal, userWorkspace, session);
    log.trace("Scroll through the descendants of \"/default-domain/UserWorkspaces/Administrator\", expecting one: \"testFolder\", " + "the \"Collections\" folder and its descendants being ignored");
    FileSystemItemFactory defaultSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("defaultSyncRootFolderItemFactory");
    FolderItem userWorkspaceFolderItem = (FolderItem) defaultSyncRootFolderItemFactory.getFileSystemItem(userWorkspace);
    ScrollFileSystemItemList descendants = userWorkspaceFolderItem.scrollDescendants(null, 10, 1000);
    assertEquals(1, descendants.size());
    FileSystemItem descendant = descendants.get(0);
    assertTrue(descendant.isFolder());
    assertEquals("testFolder", descendant.getName());
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FolderItem(org.nuxeo.drive.adapter.FolderItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 4 with FileSystemItem

use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.

the class FileSystemItemAdapterServiceImpl method getFileSystemItem.

/*--------------------------- Protected ---------------------------------------*/
/**
 * Tries to adapt the given document as the top level {@link FolderItem}. If it doesn't match, iterates on the
 * ordered contributed file system item factories until it finds one that matches and retrieves a non null
 * {@link FileSystemItem} for the given document. A file system item factory matches if:
 * <ul>
 * <li>It is not bound to any docType nor facet (this is the case for the default factory contribution
 * {@code defaultFileSystemItemFactory} bound to {@link DefaultFileSystemItemFactory})</li>
 * <li>It is bound to a docType that matches the given doc's type</li>
 * <li>It is bound to a facet that matches one of the given doc's facets</li>
 * </ul>
 */
protected FileSystemItem getFileSystemItem(DocumentModel doc, boolean forceParentItem, FolderItem parentItem, boolean includeDeleted, boolean relaxSyncRootConstraint, boolean getLockInfo) {
    FileSystemItem fileSystemItem;
    // Try the topLevelFolderItemFactory
    if (forceParentItem) {
        fileSystemItem = getTopLevelFolderItemFactory().getFileSystemItem(doc, parentItem, includeDeleted, relaxSyncRootConstraint, getLockInfo);
    } else {
        fileSystemItem = getTopLevelFolderItemFactory().getFileSystemItem(doc, includeDeleted, relaxSyncRootConstraint, getLockInfo);
    }
    if (fileSystemItem != null) {
        return fileSystemItem;
    } else {
        if (log.isDebugEnabled()) {
            log.debug(String.format("The topLevelFolderItemFactory is not able to adapt document %s as a FileSystemItem => trying fileSystemItemFactories.", doc.getId()));
        }
    }
    // Try the fileSystemItemFactories
    FileSystemItemFactoryWrapper matchingFactory = null;
    Iterator<FileSystemItemFactoryWrapper> factoriesIt = fileSystemItemFactories.iterator();
    while (factoriesIt.hasNext()) {
        FileSystemItemFactoryWrapper factory = factoriesIt.next();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Trying to adapt document %s (path: %s) as a FileSystemItem with factory %s", doc.getId(), doc.getPathAsString(), factory.getFactory().getName()));
        }
        if (generalFactoryMatches(factory) || docTypeFactoryMatches(factory, doc) || facetFactoryMatches(factory, doc, relaxSyncRootConstraint)) {
            matchingFactory = factory;
            try {
                if (forceParentItem) {
                    fileSystemItem = factory.getFactory().getFileSystemItem(doc, parentItem, includeDeleted, relaxSyncRootConstraint, getLockInfo);
                } else {
                    fileSystemItem = factory.getFactory().getFileSystemItem(doc, includeDeleted, relaxSyncRootConstraint, getLockInfo);
                }
            } catch (RootlessItemException e) {
                // top level item.
                throw new RootlessItemException(String.format("Cannot find path to registered top" + " level when adapting document " + " '%s' (path: %s) with factory %s", doc.getTitle(), doc.getPathAsString(), factory.getFactory().getName()), e);
            }
            if (fileSystemItem != null) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Adapted document '%s' (path: %s) to item with path %s with factory %s", doc.getTitle(), doc.getPathAsString(), fileSystemItem.getPath(), factory.getFactory().getName()));
                }
                return fileSystemItem;
            }
        }
    }
    if (matchingFactory == null) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("None of the fileSystemItemFactories matches document %s => returning null. Please check the contributions to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"fileSystemItemFactory\">.", doc.getId()));
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug(String.format("None of the fileSystemItemFactories matching document %s were able to adapt this document as a FileSystemItem => returning null.", doc.getId()));
        }
    }
    return fileSystemItem;
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException)

Example 5 with FileSystemItem

use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.

the class UserSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    if (isUserWorkspaceSyncRoot) {
        return super.getChildren();
    } else {
        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
        Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
        for (String repositoryName : syncRootsByRepo.keySet()) {
            try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
                Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
                Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
                while (syncRootRefsIt.hasNext()) {
                    IdRef idRef = syncRootRefsIt.next();
                    // See https://jira.nuxeo.com/browse/NXP-11146
                    if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                        }
                        continue;
                    }
                    DocumentModel doc = session.getDocument(idRef);
                    // principal)
                    if (session.getPrincipal().getName().equals(doc.getPropertyValue("dc:creator"))) {
                        // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                        FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                        if (child == null) {
                            if (log.isDebugEnabled()) {
                                log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, maybe because user %s doesn't have the required permission on it (default required permission is ReadWrite). Not including it in children.", idRef, session.getPrincipal().getName()));
                            }
                            continue;
                        }
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Including synchronization root %s in children.", idRef));
                        }
                        children.add(child);
                    }
                }
            }
        }
        Collections.sort(children);
        return children;
    }
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Aggregations

FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)48 FolderItem (org.nuxeo.drive.adapter.FolderItem)24 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)22 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)12 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)12 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)10 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)9 Blob (org.nuxeo.ecm.core.api.Blob)8 FileItem (org.nuxeo.drive.adapter.FileItem)7 IdRef (org.nuxeo.ecm.core.api.IdRef)7 FileSystemItemFactory (org.nuxeo.drive.service.FileSystemItemFactory)6 FileSystemItemAdapterServiceImpl (org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl)6 RootlessItemException (org.nuxeo.drive.adapter.RootlessItemException)5 ScrollFileSystemItemList (org.nuxeo.drive.adapter.ScrollFileSystemItemList)5 FileSystemItemManager (org.nuxeo.drive.service.FileSystemItemManager)5 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)5 DefaultFileSystemItemFactory (org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory)5 OperationMethod (org.nuxeo.ecm.automation.core.annotations.OperationMethod)5 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)4