Search in sources :

Example 6 with CloseableCoreSession

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

the class TestNuxeoDriveManager method closeSessionsAndDeleteUsers.

@After
public void closeSessionsAndDeleteUsers() throws Exception {
    if (user1Session != null) {
        ((CloseableCoreSession) user1Session).close();
    }
    if (user2Session != null) {
        ((CloseableCoreSession) user2Session).close();
    }
    try (Session usersDir = directoryService.open("userDirectory")) {
        usersDir.deleteEntry("user1");
        usersDir.deleteEntry("user2");
    }
    // Simulate root deletion to cleanup the cache between the tests
    nuxeoDriveManager.handleFolderDeletion((IdRef) doc("/").getRef());
}
Also used : CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) Session(org.nuxeo.ecm.directory.Session) CoreSession(org.nuxeo.ecm.core.api.CoreSession) After(org.junit.After)

Example 7 with CloseableCoreSession

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

the class FileSystemItemManagerImpl method getSession.

@Deprecated
@Override
public CoreSession getSession(String repositoryName, Principal principal) {
    final String sessionKey = repositoryName + "/" + principal.getName();
    CoreSession session = openedSessions.get().get(sessionKey);
    if (session == null) {
        Map<String, Serializable> context = new HashMap<String, Serializable>();
        context.put("principal", (Serializable) principal);
        final CloseableCoreSession newSession = CoreInstance.openCoreSession(repositoryName, principal);
        openedSessions.get().put(sessionKey, newSession);
        try {
            Transaction t = TransactionHelper.lookupTransactionManager().getTransaction();
            if (t == null) {
                throw new RuntimeException("FileSystemItemManagerImpl requires an active transaction.");
            }
            t.registerSynchronization(new SessionCloser(newSession, sessionKey));
        } catch (SystemException | NamingException | RollbackException e) {
            throw new NuxeoException(e);
        }
        session = newSession;
    }
    return session;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) RollbackException(javax.transaction.RollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NamingException(javax.naming.NamingException) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) CoreSession(org.nuxeo.ecm.core.api.CoreSession) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException)

Example 8 with CloseableCoreSession

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

the class DefaultFileSystemItemFactoryFixture method testPermissionCheckNotOptimized.

@Test
@Deploy({ "org.nuxeo.drive.core:OSGI-INF/test-nuxeodrive-permissions-contrib.xml", "org.nuxeo.drive.core:OSGI-INF/test-nuxeodrive-permission-check-not-optimized-contrib.xml" })
public void testPermissionCheckNotOptimized() {
    setPermission(syncRootFolder, "joe", SecurityConstants.READ, true);
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        log.trace("Register the sync root for Joe's account");
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, joeSession);
        folder = joeSession.getDocument(folder.getRef());
        log.trace("Check canDelete/canCreateChild flags on folder for user joe with Read granted on parent folder");
        FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        assertFalse(folderItem.getCanDelete());
        assertFalse(folderItem.getCanCreateChild());
        log.trace("Check canDelete/canCreateChild flags on folder for user joe with Write granted on folder, AddChildren not granted on folder and RemoveChildren not granted on parent folder");
        setPermission(folder, "joe", SecurityConstants.WRITE, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        // False here as not optimized => explicit check of RemoveChildren
        // on parent folder and AddChildren on
        // folder
        assertFalse(folderItem.getCanDelete());
        assertFalse(folderItem.getCanCreateChild());
        log.trace("Check canDelete flag on folder for user joe with Write (thus RemoveChildren) granted on parent folder");
        setPermission(syncRootFolder, "joe", SecurityConstants.WRITE, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        // True here thanks to RemoveChildren on the parent folder
        assertTrue(folderItem.getCanDelete());
        // Still false here because of missing AddChildren on folder
        assertFalse(folderItem.getCanCreateChild());
        log.trace("Check canCreateChild flag on folder for user joe with AddChildren granted on folder");
        setPermission(folder, "joe", SecurityConstants.ADD_CHILDREN, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        // True here thanks to AddChildren on folder
        assertTrue(folderItem.getCanCreateChild());
    }
    resetPermissions(folder, "joe");
    resetPermissions(syncRootFolder, "joe");
}
Also used : FolderItem(org.nuxeo.drive.adapter.FolderItem) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) Test(org.junit.Test) Deploy(org.nuxeo.runtime.test.runner.Deploy)

Example 9 with CloseableCoreSession

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

the class DefaultFileSystemItemFactoryFixture method testFolderItem.

@Test
public void testFolderItem() throws Exception {
    // ------------------------------------------------------
    // FolderItem#canCreateChild
    // ------------------------------------------------------
    // As Administrator
    FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
    assertTrue(folderItem.getCanCreateChild());
    // As a user with READ permission
    DocumentModel rootDoc = session.getRootDocument();
    setPermission(rootDoc, "joe", SecurityConstants.READ, true);
    // Under Oracle, the READ ACL optims are not visible from the joe
    // session while the transaction has not been committed.
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        folder = joeSession.getDocument(folder.getRef());
        // should not be mappable as an fs item.
        try {
            defaultFileSystemItemFactory.getFileSystemItem(folder);
            fail("Should have raised RootlessItemException as ");
        } catch (RootlessItemException e) {
        // expected
        }
        // Register the sync root for Joe's account
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        assertFalse(folderItem.getCanCreateChild());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        assertTrue(folderItem.getCanCreateChild());
    }
    resetPermissions(rootDoc, "joe");
    // ------------------------------------------------------
    // FolderItem#createFile and FolderItem#createFolder
    // ------------------------------------------------------
    folder = session.getDocument(folder.getRef());
    folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
    // Note
    Blob childBlob = new StringBlob("This is the Note child.");
    childBlob.setFilename("Note child.txt");
    folderItem.createFile(childBlob);
    // File
    childBlob = new StringBlob("This is the File child.");
    childBlob.setFilename("File child.odt");
    childBlob.setMimeType("application/vnd.oasis.opendocument.text");
    folderItem.createFile(childBlob);
    // Folder
    folderItem.createFolder("Sub-folder");
    DocumentModelList children = session.query(String.format("select * from Document where ecm:parentId = '%s' order by ecm:primaryType asc", folder.getId()));
    assertEquals(3, children.size());
    // Check File
    DocumentModel file = children.get(0);
    assertEquals("File", file.getType());
    assertEquals("File child.odt", file.getTitle());
    childBlob = (Blob) file.getPropertyValue("file:content");
    assertEquals("File child.odt", childBlob.getFilename());
    assertEquals("This is the File child.", childBlob.getString());
    // Check Folder
    DocumentModel subFolder = children.get(1);
    assertEquals("Folder", subFolder.getType());
    assertEquals("Sub-folder", subFolder.getTitle());
    // Check Note
    DocumentModel note = children.get(2);
    assertEquals("Note", note.getType());
    assertEquals("Note child.txt", note.getTitle());
    childBlob = note.getAdapter(BlobHolder.class).getBlob();
    assertEquals("Note child.txt", childBlob.getFilename());
    assertEquals("This is the Note child.", childBlob.getString());
    // --------------------------------------------------------------------------------------------
    // FolderItem#getChildren, FolderItem#getCanScrollDescendants and
    // FolderItem#scrollDescendants
    // --------------------------------------------------------------------------------------------
    // Create another child adaptable as a FileSystemItem => should be
    // retrieved
    DocumentModel adaptableChild = session.createDocumentModel("/syncRoot/aFolder", "adaptableChild", "File");
    Blob adaptableChildBlob = new StringBlob("Content of another file.");
    adaptableChildBlob.setFilename("Another file.odt");
    adaptableChild.setPropertyValue("file:content", (Serializable) adaptableChildBlob);
    adaptableChild = session.createDocument(adaptableChild);
    // Create another child not adaptable as a FileSystemItem => should
    // not be retrieved
    session.createDocument(session.createDocumentModel("/syncRoot/aFolder", "notAdaptableChild", "NotSynchronizable"));
    session.save();
    // Check getChildren
    List<FileSystemItem> folderChildren = folderItem.getChildren();
    assertEquals(4, folderChildren.size());
    // Ordered
    checkChildren(folderChildren, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), true);
    // Check scrollDescendants
    assertTrue(folderItem.getCanScrollDescendants());
    // Scroll through all descendants in one breath
    ScrollFileSystemItemList folderDescendants = folderItem.scrollDescendants(null, 10, 1000);
    String scrollId = folderDescendants.getScrollId();
    assertNotNull(scrollId);
    assertEquals(4, folderDescendants.size());
    // Order is not determined
    checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
    // Check that next call to scrollDescendants returns an empty list
    assertTrue(folderItem.scrollDescendants(scrollId, 10, 1000).isEmpty());
    // Scroll through descendants in several steps
    folderDescendants.clear();
    ScrollFileSystemItemList descendantsBatch;
    int batchSize = 2;
    scrollId = null;
    while (!(descendantsBatch = folderItem.scrollDescendants(scrollId, batchSize, 1000)).isEmpty()) {
        assertTrue(descendantsBatch.size() > 0);
        scrollId = descendantsBatch.getScrollId();
        folderDescendants.addAll(descendantsBatch);
    }
    assertEquals(4, folderDescendants.size());
    // Order is not determined
    checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
    // Check batch size limit
    try {
        folderItem.scrollDescendants(null, 10000, 1000);
        fail("Should not be able to scroll through more descendants than the maximum batch size allowed.");
    } catch (NuxeoException e) {
        log.trace(e);
    }
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FolderItem(org.nuxeo.drive.adapter.FolderItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) Test(org.junit.Test)

Example 10 with CloseableCoreSession

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

the class DocumentBackedFolderItem method getChildren.

/*--------------------- FolderItem -----------------*/
@Override
@SuppressWarnings("unchecked")
public List<FileSystemItem> getChildren() {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
        Map<String, Serializable> props = new HashMap<>();
        props.put(CORE_SESSION_PROPERTY, (Serializable) session);
        PageProvider<DocumentModel> childrenPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(FOLDER_ITEM_CHILDREN_PAGE_PROVIDER, null, null, 0L, props, docId);
        long pageSize = childrenPageProvider.getPageSize();
        List<FileSystemItem> children = new ArrayList<>();
        int nbChildren = 0;
        boolean reachedPageSize = false;
        boolean hasNextPage = true;
        // FileSystemItems
        while (nbChildren < pageSize && hasNextPage) {
            List<DocumentModel> dmChildren = childrenPageProvider.getCurrentPage();
            for (DocumentModel dmChild : dmChildren) {
                // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(dmChild, this, false, false, false);
                if (child != null) {
                    children.add(child);
                    nbChildren++;
                    if (nbChildren == pageSize) {
                        reachedPageSize = true;
                        break;
                    }
                }
            }
            if (!reachedPageSize) {
                hasNextPage = childrenPageProvider.isNextPageAvailable();
                if (hasNextPage) {
                    childrenPageProvider.nextPage();
                }
            }
        }
        return children;
    }
}
Also used : Serializable(java.io.Serializable) PageProviderService(org.nuxeo.ecm.platform.query.api.PageProviderService) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) PageProvider(org.nuxeo.ecm.platform.query.api.PageProvider)

Aggregations

CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)35 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)25 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)12 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)9 FolderItem (org.nuxeo.drive.adapter.FolderItem)9 Blob (org.nuxeo.ecm.core.api.Blob)7 IdRef (org.nuxeo.ecm.core.api.IdRef)7 HashMap (java.util.HashMap)6 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)6 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)6 FileItem (org.nuxeo.drive.adapter.FileItem)5 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)5 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)5 RepositoryManager (org.nuxeo.ecm.core.api.repository.RepositoryManager)5 Serializable (java.io.Serializable)4 Deploy (org.nuxeo.runtime.test.runner.Deploy)4 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)3 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)3 PageProvider (org.nuxeo.ecm.platform.query.api.PageProvider)3