Search in sources :

Example 1 with FileItem

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

the class DefaultFileSystemItemFactoryFixture method testFileItem.

@Test
public void testFileItem() throws Exception {
    // ------------------------------------------------------
    // FileItem#getDownloadURL
    // ------------------------------------------------------
    FileItem fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
    String downloadURL = fileItem.getDownloadURL();
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/Joe.odt", downloadURL);
    // ------------------------------------------------------------
    // FileItem#getDigestAlgorithm
    // ------------------------------------------------------------
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    FileItem noteItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(note);
    assertEquals("MD5", noteItem.getDigestAlgorithm());
    // ------------------------------------------------------------
    // FileItem#getDigest
    // ------------------------------------------------------------
    assertEquals(file.getAdapter(BlobHolder.class).getBlob().getDigest(), fileItem.getDigest());
    String noteDigest = FileSystemItemHelper.getMD5Digest(note.getAdapter(BlobHolder.class).getBlob());
    assertEquals(noteDigest, noteItem.getDigest());
    assertEquals(custom.getAdapter(BlobHolder.class).getBlob().getDigest(), ((FileItem) defaultFileSystemItemFactory.getFileSystemItem(custom)).getDigest());
    // ------------------------------------------------------------
    // FileItem#getCanUpdate
    // ------------------------------------------------------------
    // As Administrator
    assertTrue(fileItem.getCanUpdate());
    // 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")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertFalse(fileItem.getCanUpdate());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertTrue(fileItem.getCanUpdate());
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        // ------------------------------------------------------
        // FileItem#getBlob
        // ------------------------------------------------------
        Blob fileItemBlob = fileItem.getBlob();
        assertEquals("Joe.odt", fileItemBlob.getFilename());
        assertEquals("Content of Joe's file.", fileItemBlob.getString());
        // Check versioning
        assertVersion("0.0", file);
        // ------------------------------------------------------
        // FileItem#setBlob and versioning
        // ------------------------------------------------------
        Blob newBlob = new StringBlob("This is a new file.");
        newBlob.setFilename("New blob.txt");
        ensureJustModified(file, session);
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        Blob updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("New blob.txt", updatedBlob.getFilename());
        assertEquals("This is a new file.", updatedBlob.getString());
        // Check versioning => should not be versioned since same
        // contributor
        // and last modification was done before the versioning delay
        assertVersion("0.0", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        newBlob.setFilename("File name modified.txt");
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.1", file);
        List<DocumentModel> fileVersions = session.getVersions(file.getRef());
        assertEquals(1, fileVersions.size());
        DocumentModel lastFileVersion = fileVersions.get(0);
        Blob versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("New blob.txt", versionedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        newBlob.setFilename("File name modified by Joe.txt");
        fileItem.setBlob(newBlob);
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.2", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(2, fileVersions.size());
        lastFileVersion = fileVersions.get(1);
        versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("File name modified.txt", versionedBlob.getFilename());
        // ------------------------------------------------------
        // DocumentBackedFileItem#rename and versioning
        // ------------------------------------------------------
        // Save document to trigger the DublinCoreListener and update
        // dc:lastContributor to "Administrator"
        // rename the file to enable dc listener (disable if not dirty)
        file.setPropertyValue("file:content/name", "newTitle");
        file = session.saveDocument(file);
        // Check versioning => should be versioned cause last contributor has changed
        assertVersion("0.3", file);
        // Switch back to Administrator as last contributor
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        ensureJustModified(file, session);
        fileItem.rename("Renamed file.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Check versioning => should not be versioned since same
        // contributor and last modification was done before the
        // versioning delay
        assertVersion("0.3", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        fileItem.rename("Renamed again.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.4", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(4, fileVersions.size());
        lastFileVersion = fileVersions.get(3);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        fileItem.rename("File renamed by Joe.txt");
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File renamed by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.5", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(5, fileVersions.size());
        lastFileVersion = fileVersions.get(4);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 2 with FileItem

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

the class TestCollectionSyncRootFolderItemFactory method testFactory.

@Test
public void testFactory() throws Exception {
    FileSystemItemFactory collectionSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("collectionSyncRootFolderItemFactory");
    DocumentModel collection = collectionManager.createCollection(session, "testCollection", "Test collection.", "/");
    DocumentModel doc1 = session.createDocumentModel("/", "doc1", "File");
    doc1.setPropertyValue("dc:title", "doc1");
    doc1.setPropertyValue("file:content", new StringBlob("Content of file 1."));
    doc1 = session.createDocument(doc1);
    collectionManager.addToCollection(collection, doc1, session);
    assertTrue(collectionManager.isInCollection(collection, doc1, session));
    DocumentModel doc2 = session.createDocumentModel("/", "doc2", "File");
    doc2.setPropertyValue("dc:title", "doc2");
    doc2.setPropertyValue("file:content", new StringBlob("Content of file 2."));
    doc2 = session.createDocument(doc2);
    collectionManager.addToCollection(collection, doc2, session);
    assertTrue(collectionManager.isInCollection(collection, doc2, session));
    log.trace("Check document that is not a Collection");
    assertFalse(collectionSyncRootFolderItemFactory.isFileSystemItem(session.getRootDocument()));
    log.trace("Check Collection not registered as a sync root");
    assertFalse(collectionSyncRootFolderItemFactory.isFileSystemItem(collection));
    log.trace("Check Collection registered as a sync root");
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collection, session);
    assertTrue(collectionSyncRootFolderItemFactory.isFileSystemItem(collection));
    log.trace("Adapt test collection as a FileSystemItem");
    FileSystemItem fsItem = collectionSyncRootFolderItemFactory.getFileSystemItem(collection);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof CollectionSyncRootFolderItem);
    log.trace("Check children");
    FolderItem collectionFSItem = (FolderItem) fsItem;
    List<FileSystemItem> collectionChildren = collectionFSItem.getChildren();
    assertEquals(2, collectionChildren.size());
    FileSystemItem child1 = collectionChildren.get(0);
    assertTrue(child1 instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc1.getId(), child1.getId());
    assertEquals(COLLECTION_SYNC_ROOT_ITEM_ID_PREFIX + collection.getId(), child1.getParentId());
    assertEquals("doc1", child1.getName());
    FileSystemItem child2 = collectionChildren.get(1);
    assertTrue(child2 instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc2.getId(), child2.getId());
    assertEquals(COLLECTION_SYNC_ROOT_ITEM_ID_PREFIX + collection.getId(), child2.getParentId());
    assertEquals("doc2", child2.getName());
    log.trace("Check FolderItem#getCanScrollDescendants");
    assertFalse(collectionFSItem.getCanScrollDescendants());
    log.trace("Check descendants");
    try {
        collectionFSItem.scrollDescendants(null, 10, 1000);
        fail("Should not be able to scroll through the descendants of a CollectionSyncRootFolderItem.");
    } catch (UnsupportedOperationException e) {
        assertEquals("Cannot scroll through the descendants of a collection sync root folder item, please call getChildren() instead.", e.getMessage());
    }
    log.trace("Check FolderItem#getCanCreateChild");
    assertFalse(collectionFSItem.getCanCreateChild());
    log.trace("Check FolderItem#createFile");
    try {
        collectionFSItem.createFile(new StringBlob("Child file content."));
        fail("Should not be able to create a file in a CollectionSyncRootFolderItem.");
    } catch (UnsupportedOperationException e) {
        assertEquals("Cannot create a file in a collection synchronization root.", e.getMessage());
    }
    log.trace("Check FolderItem#createFolder");
    try {
        collectionFSItem.createFolder("Child folder");
        fail("Should not be able to create a folder in a CollectionSyncRootFolderItem.");
    } catch (UnsupportedOperationException e) {
        assertEquals("Cannot create a folder in a collection synchronization root.", e.getMessage());
    }
    log.trace("Test AbstractDocumentBackedFileSystemItem#delete");
    child1.delete();
    doc1 = session.getDocument(doc1.getRef());
    assertFalse(doc1.isTrashed());
    assertFalse(collectionManager.isInCollection(collection, doc1, session));
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) CollectionSyncRootFolderItem(org.nuxeo.drive.adapter.impl.CollectionSyncRootFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) CollectionSyncRootFolderItem(org.nuxeo.drive.adapter.impl.CollectionSyncRootFolderItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 3 with FileItem

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

the class TestFileSystemItemAdapterService method testService.

@Test
public void testService() throws Exception {
    registerRootAndCreateSomeDocs();
    // ------------------------------------------------------
    // Check file system item factory descriptors
    // ------------------------------------------------------
    Map<String, FileSystemItemFactoryDescriptor> fileSystemItemFactoryDescs = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactoryDescriptors();
    assertNotNull(fileSystemItemFactoryDescs);
    assertEquals(12, fileSystemItemFactoryDescs.size());
    FileSystemItemFactoryDescriptor desc = fileSystemItemFactoryDescs.get("defaultSyncRootFolderItemFactory");
    assertNotNull(desc);
    assertEquals(10, desc.getOrder());
    assertEquals("defaultSyncRootFolderItemFactory", desc.getName());
    assertNull(desc.getDocType());
    assertEquals("DriveSynchronized", desc.getFacet());
    FileSystemItemFactory factory = desc.getFactory();
    assertTrue(factory instanceof DefaultSyncRootFolderItemFactory);
    desc = fileSystemItemFactoryDescs.get("dummyDocTypeFactory");
    assertNotNull(desc);
    assertEquals(20, desc.getOrder());
    assertEquals("dummyDocTypeFactory", desc.getName());
    assertEquals("File", desc.getDocType());
    assertNull(desc.getFacet());
    factory = desc.getFactory();
    assertTrue(factory instanceof DummyFileItemFactory);
    desc = fileSystemItemFactoryDescs.get("dummyFacetFactory");
    assertNotNull(desc);
    assertEquals(30, desc.getOrder());
    assertEquals("dummyFacetFactory", desc.getName());
    assertNull(desc.getDocType());
    assertEquals("Folderish", desc.getFacet());
    factory = desc.getFactory();
    assertTrue(factory instanceof DummyFolderItemFactory);
    desc = fileSystemItemFactoryDescs.get("defaultFileSystemItemFactory");
    assertNotNull(desc);
    assertEquals(50, desc.getOrder());
    assertEquals("defaultFileSystemItemFactory", desc.getName());
    assertNull(desc.getDocType());
    assertNull(desc.getFacet());
    factory = desc.getFactory();
    assertTrue(factory instanceof DefaultFileSystemItemFactory);
    desc = fileSystemItemFactoryDescs.get("dummyVirtualFolderItemFactory");
    assertNotNull(desc);
    assertEquals(100, desc.getOrder());
    assertEquals("dummyVirtualFolderItemFactory", desc.getName());
    assertNull(desc.getDocType());
    assertNull(desc.getFacet());
    factory = desc.getFactory();
    assertTrue(factory instanceof VirtualFolderItemFactory);
    assertEquals("Dummy Folder", ((VirtualFolderItemFactory) factory).getFolderName());
    desc = fileSystemItemFactoryDescs.get("nullMergeTestFactory");
    assertNotNull(desc);
    assertEquals(200, desc.getOrder());
    assertEquals("nullMergeTestFactory", desc.getName());
    assertEquals("Note", desc.getDocType());
    assertNull(desc.getFacet());
    factory = desc.getFactory();
    assertTrue(factory instanceof DummyFileItemFactory);
    // ------------------------------------------------------
    // Check ordered file system item factories
    // ------------------------------------------------------
    List<FileSystemItemFactoryWrapper> fileSystemItemFactories = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactories();
    assertNotNull(fileSystemItemFactories);
    assertEquals(7, fileSystemItemFactories.size());
    FileSystemItemFactoryWrapper factoryWrapper = fileSystemItemFactories.get(0);
    assertNotNull(factoryWrapper);
    assertNull(factoryWrapper.getDocType());
    assertEquals("Collection", factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("CollectionSyncRootFolderItemFactory"));
    factoryWrapper = fileSystemItemFactories.get(1);
    assertNotNull(factoryWrapper);
    assertNull(factoryWrapper.getDocType());
    assertEquals("DriveSynchronized", factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("DefaultSyncRootFolderItemFactory"));
    factoryWrapper = fileSystemItemFactories.get(2);
    assertNotNull(factoryWrapper);
    assertEquals("File", factoryWrapper.getDocType());
    assertNull(factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("DummyFileItemFactory"));
    factoryWrapper = fileSystemItemFactories.get(3);
    assertNotNull(factoryWrapper);
    assertNull(factoryWrapper.getDocType());
    assertEquals("Folderish", factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("DummyFolderItemFactory"));
    factoryWrapper = fileSystemItemFactories.get(4);
    assertNotNull(factoryWrapper);
    assertNull(factoryWrapper.getDocType());
    assertNull(factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("DefaultFileSystemItemFactory"));
    factoryWrapper = fileSystemItemFactories.get(5);
    assertNotNull(factoryWrapper);
    assertNull(factoryWrapper.getDocType());
    assertNull(factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("DummyVirtualFolderItemFactory"));
    factoryWrapper = fileSystemItemFactories.get(6);
    assertNotNull(factoryWrapper);
    assertEquals("Note", factoryWrapper.getDocType());
    assertNull(factoryWrapper.getFacet());
    assertTrue(factoryWrapper.getFactory().getClass().getName().endsWith("DummyFileItemFactory"));
    // ------------------------------------------------------
    // Check #getFileSystemItem(DocumentModel doc)
    // ------------------------------------------------------
    // File => should use the dummyDocTypeFactory bound to the
    // DummyFileItemFactory class
    FileSystemItem fsItem = fileSystemItemAdapterService.getFileSystemItem(file);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof DummyFileItem);
    assertEquals("dummyDocTypeFactory#test#" + file.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Dummy file with id " + file.getId(), fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Joe", fsItem.getCreator());
    assertEquals("Joe", fsItem.getLastContributor());
    // Folder => should use the dummyFacetFactory bound to the
    // DummyFolderItemFactory class
    fsItem = fileSystemItemAdapterService.getFileSystemItem(folder);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof DummyFolderItem);
    assertTrue(((FolderItem) fsItem).getCanCreateChild());
    assertEquals("dummyFacetFactory#test#" + folder.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Dummy folder with id " + folder.getId(), fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertEquals("Jack", fsItem.getCreator());
    assertEquals("Jack", fsItem.getLastContributor());
    // Custom => should use the defaultFileSystemItemFactory bound to the
    // DefaultFileSystemItemFactory class
    fsItem = fileSystemItemAdapterService.getFileSystemItem(custom);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals("defaultFileSystemItemFactory#test#" + custom.getId(), fsItem.getId());
    assertEquals("/org.nuxeo.drive.service.impl.DefaultTopLevelFolderItemFactory#/" + syncRootItemId + "/" + fsItem.getId(), fsItem.getPath());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Bonnie's file.txt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Bonnie", fsItem.getCreator());
    assertEquals("Bonnie", fsItem.getLastContributor());
    Blob fileFsItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("Bonnie's file.txt", fileFsItemBlob.getFilename());
    assertEquals("Content of the custom document's blob.", fileFsItemBlob.getString());
    // -------------------------------------------------------------------
    // Check #getFileSystemItem(DocumentModel doc, boolean includeDeleted,
    // boolean relaxSyncRootConstraint)
    // -------------------------------------------------------------------
    fsItem = fileSystemItemAdapterService.getFileSystemItem(custom, false, true);
    assertNotNull(fsItem);
    assertEquals("test#" + custom.getId(), fsItem.getId());
    // ------------------------------------------------------
    // Check #getFileSystemItem(DocumentModel doc, FolderItem parentItem)
    // ------------------------------------------------------
    // File => should use the dummyDocTypeFactory bound to the
    // DummyFileItemFactory class
    fsItem = fileSystemItemAdapterService.getFileSystemItem(file, syncRootItem);
    assertNotNull(fsItem);
    assertEquals(syncRootItemId, fsItem.getParentId());
    // -------------------------------------------------------------
    // Check #getFileSystemItemFactoryForId(String id)
    // -------------------------------------------------------------
    // Default factory
    String fsItemId = "defaultFileSystemItemFactory#test#someId";
    FileSystemItemFactory fsItemFactory = fileSystemItemAdapterService.getFileSystemItemFactoryForId(fsItemId);
    assertNotNull(fsItemFactory);
    assertEquals("defaultFileSystemItemFactory", fsItemFactory.getName());
    assertTrue(fsItemFactory.getClass().getName().endsWith("DefaultFileSystemItemFactory"));
    assertTrue(fsItemFactory.canHandleFileSystemItemId(fsItemId));
    // Top level folder item factory
    fsItemId = "org.nuxeo.drive.service.impl.DefaultTopLevelFolderItemFactory#";
    fsItemFactory = fileSystemItemAdapterService.getFileSystemItemFactoryForId(fsItemId);
    assertNotNull(fsItemFactory);
    assertTrue(fsItemFactory.getName().endsWith("DefaultTopLevelFolderItemFactory"));
    assertTrue(fsItemFactory.getClass().getName().endsWith("DefaultTopLevelFolderItemFactory"));
    assertTrue(fsItemFactory.canHandleFileSystemItemId(fsItemId));
    // Factory with #canHandleFileSystemItemId returning false
    fsItemId = "dummyDocTypeFactory#test#someId";
    try {
        fileSystemItemAdapterService.getFileSystemItemFactoryForId(fsItemId);
        fail("No fileSystemItemFactory should be found FileSystemItem id.");
    } catch (NuxeoDriveContribException e) {
        assertEquals("No fileSystemItemFactory found for FileSystemItem with id dummyDocTypeFactory#test#someId. Please check the contributions to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"fileSystemItemFactory\"> and make sure there is at least one defining a FileSystemItemFactory class for which the #canHandleFileSystemItemId(String id) method returns true.", e.getMessage());
    }
    // Non parsable id
    fsItemId = "nonParsableId";
    try {
        fileSystemItemAdapterService.getFileSystemItemFactoryForId(fsItemId);
        fail("No fileSystemItemFactory should be found for FileSystemItem id.");
    } catch (NuxeoDriveContribException e) {
        assertEquals("No fileSystemItemFactory found for FileSystemItem with id nonParsableId. Please check the contributions to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"fileSystemItemFactory\"> and make sure there is at least one defining a FileSystemItemFactory class for which the #canHandleFileSystemItemId(String id) method returns true.", e.getMessage());
    }
    // Non existent factory name
    fsItemId = "nonExistentFactoryName#test#someId";
    try {
        fileSystemItemAdapterService.getFileSystemItemFactoryForId(fsItemId);
        fail("No fileSystemItemFactory should be found for FileSystemItem id.");
    } catch (NuxeoDriveContribException e) {
        assertEquals("No fileSystemItemFactory found for FileSystemItem with id nonExistentFactoryName#test#someId. Please check the contributions to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"fileSystemItemFactory\"> and make sure there is at least one defining a FileSystemItemFactory class for which the #canHandleFileSystemItemId(String id) method returns true.", e.getMessage());
    }
    // -------------------------------------------------------------
    // Check #getTopLevelFolderItemFactory()
    // -------------------------------------------------------------
    TopLevelFolderItemFactory topLevelFactory = fileSystemItemAdapterService.getTopLevelFolderItemFactory();
    assertNotNull(topLevelFactory);
    assertTrue(topLevelFactory.getClass().getName().endsWith("DefaultTopLevelFolderItemFactory"));
    assertTrue(topLevelFactory instanceof DefaultTopLevelFolderItemFactory);
    // -------------------------------------------------------------
    try {
        fileSystemItemAdapterService.getVirtualFolderItemFactory("nonExistentFactory");
        fail("No VirtualFolderItemFactory should be found for factory name.");
    } catch (NuxeoDriveContribException e) {
        assertEquals("No factory named nonExistentFactory. Please check the contributions to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"fileSystemItemFactory\">.", e.getMessage());
    }
    try {
        fileSystemItemAdapterService.getVirtualFolderItemFactory("defaultFileSystemItemFactory");
        fail("No VirtualFolderItemFactory should be found for factory name.");
    } catch (NuxeoDriveContribException e) {
        assertEquals("Factory class org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory for factory defaultFileSystemItemFactory is not a VirtualFolderItemFactory.", e.getMessage());
    }
    VirtualFolderItemFactory virtualFolderItemFactory = fileSystemItemAdapterService.getVirtualFolderItemFactory("dummyVirtualFolderItemFactory");
    assertNotNull(virtualFolderItemFactory);
    assertTrue(virtualFolderItemFactory.getClass().getName().endsWith("DummyVirtualFolderItemFactory"));
    // -------------------------------------------------------------
    // Check #getActiveFileSystemItemFactories()
    // -------------------------------------------------------------
    Set<String> activeFactories = fileSystemItemAdapterService.getActiveFileSystemItemFactories();
    assertEquals(7, activeFactories.size());
    assertTrue(activeFactories.contains("collectionSyncRootFolderItemFactory"));
    assertTrue(activeFactories.contains("defaultSyncRootFolderItemFactory"));
    assertTrue(activeFactories.contains("defaultFileSystemItemFactory"));
    assertTrue(activeFactories.contains("dummyDocTypeFactory"));
    assertTrue(activeFactories.contains("dummyFacetFactory"));
    assertTrue(activeFactories.contains("dummyVirtualFolderItemFactory"));
    assertTrue(activeFactories.contains("nullMergeTestFactory"));
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) NuxeoDriveContribException(org.nuxeo.drive.adapter.NuxeoDriveContribException) TopLevelFolderItemFactory(org.nuxeo.drive.service.TopLevelFolderItemFactory) DefaultTopLevelFolderItemFactory(org.nuxeo.drive.service.impl.DefaultTopLevelFolderItemFactory) FileItem(org.nuxeo.drive.adapter.FileItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) DefaultTopLevelFolderItemFactory(org.nuxeo.drive.service.impl.DefaultTopLevelFolderItemFactory) FileSystemItemFactoryWrapper(org.nuxeo.drive.service.impl.FileSystemItemFactoryWrapper) DefaultSyncRootFolderItemFactory(org.nuxeo.drive.service.impl.DefaultSyncRootFolderItemFactory) VirtualFolderItemFactory(org.nuxeo.drive.service.VirtualFolderItemFactory) FileSystemItemFactoryDescriptor(org.nuxeo.drive.service.impl.FileSystemItemFactoryDescriptor) Test(org.junit.Test)

Example 4 with FileItem

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

the class TestFileSystemItemManagerService method testWriteOperations.

@Test
public void testWriteOperations() throws Exception {
    // Not allowed to create a folder in a non FolderItem
    try {
        fileSystemItemManagerService.createFolder(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), "A new folder", principal, false);
        fail("Folder creation in a non folder item should fail.");
    } catch (NuxeoException e) {
        assertEquals(String.format("Cannot create a folder in file system item with id %s because it is not a folder but is: " + "DocumentBackedFileItem(id=\"%s\", name=\"Joe.odt\")", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId()), e.getMessage());
    }
    // Folder creation
    FolderItem newFolderItem = fileSystemItemManagerService.createFolder(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), "A new folder", principal, false);
    assertNotNull(newFolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), newFolderItem.getParentId());
    assertEquals("A new folder", newFolderItem.getName());
    DocumentModelList folderChildren = session.query(String.format("select * from Document where ecm:parentId = '%s' and ecm:primaryType = 'Folder' order by dc:title asc", folder.getId()));
    DocumentModel newFolder = folderChildren.get(0);
    assertTrue(newFolder.isFolder());
    assertEquals("A new folder", newFolder.getTitle());
    // Parent folder children check
    assertEquals(6, fileSystemItemManagerService.getChildren(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal).size());
    // NXP-21854: Check overwrite parameter
    // Test overwrite=false
    FolderItem differentFolderItem = fileSystemItemManagerService.createFolder(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), "A new folder", principal, false);
    assertNotNull(differentFolderItem);
    assertNotEquals(newFolderItem.getId(), differentFolderItem.getId());
    assertEquals("A new folder", differentFolderItem.getName());
    // Test overwrite=true
    FolderItem otherFolderItem = fileSystemItemManagerService.createFolder(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), "Test overwrite", principal, false);
    assertNotNull(otherFolderItem);
    assertEquals("Test overwrite", otherFolderItem.getName());
    FolderItem sameFolderItem = fileSystemItemManagerService.createFolder(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), "Test overwrite", principal, true);
    assertNotNull(sameFolderItem);
    assertEquals(otherFolderItem.getId(), sameFolderItem.getId());
    assertEquals("Test overwrite", sameFolderItem.getName());
    // ------------------------------------------------------
    // Check #createFile
    // ------------------------------------------------------
    // File creation
    Blob blob = new StringBlob("Content of a new file.");
    blob.setFilename("New file.odt");
    blob.setMimeType("application/vnd.oasis.opendocument.text");
    FileItem fileItem = fileSystemItemManagerService.createFile(newFolderItem.getId(), blob, principal, false);
    assertNotNull(fileItem);
    assertEquals(newFolderItem.getId(), fileItem.getParentId());
    assertEquals("New file.odt", fileItem.getName());
    folderChildren = session.query(String.format("select * from Document where ecm:parentId = '%s'", newFolder.getId()));
    assertEquals(1, folderChildren.size());
    DocumentModel newFile = folderChildren.get(0);
    assertEquals("File", newFile.getType());
    assertEquals("New file.odt", newFile.getTitle());
    assertEquals("/syncRoot1/aFolder/A new folder/New file.odt", newFile.getPathAsString());
    Blob newFileBlob = (Blob) newFile.getPropertyValue("file:content");
    assertEquals("New file.odt", newFileBlob.getFilename());
    assertEquals("Content of a new file.", newFileBlob.getString());
    assertEquals("nxfile/test/" + newFile.getId() + "/blobholder:0/New%20file.odt", fileItem.getDownloadURL());
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    assertEquals(newFileBlob.getDigest(), fileItem.getDigest());
    // NXP-21854: Check overwrite parameter
    // Test overwrite=false
    FileItem differentFileItem = fileSystemItemManagerService.createFile(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), blob, principal, false);
    assertNotNull(differentFileItem);
    assertNotEquals(fileItem.getId(), differentFileItem.getId());
    assertEquals("New file.odt", differentFileItem.getName());
    // Test overwrite=true
    Blob otherBlob = new StringBlob("Content of a new file.");
    otherBlob.setFilename("Test overwrite.odt");
    otherBlob.setMimeType("application/vnd.oasis.opendocument.text");
    FileItem otherFileItem = fileSystemItemManagerService.createFile(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), otherBlob, principal, false);
    assertNotNull(otherFileItem);
    assertEquals("Test overwrite.odt", otherFileItem.getName());
    FileItem sameFileItem = fileSystemItemManagerService.createFile(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), otherBlob, principal, true);
    assertNotNull(sameFileItem);
    assertEquals(otherFileItem.getId(), sameFileItem.getId());
    assertEquals("Test overwrite.odt", sameFileItem.getName());
    // Parent folder children check
    assertEquals(1, fileSystemItemManagerService.getChildren(newFolderItem.getId(), principal).size());
    // ------------------------------------------------------
    // Check #updateFile
    // ------------------------------------------------------
    String fileItemId = fileItem.getId();
    String fileItemParentId = fileItem.getParentId();
    blob = new StringBlob("Modified content of an existing file.");
    fileItem = fileSystemItemManagerService.updateFile(fileItemId, blob, principal);
    assertNotNull(fileItem);
    assertEquals(fileItemId, fileItem.getId());
    assertEquals(fileItemParentId, fileItem.getParentId());
    assertEquals("New file.odt", fileItem.getName());
    folderChildren = session.query(String.format("select * from Document where ecm:parentId = '%s'", newFolder.getId()));
    assertEquals(1, folderChildren.size());
    DocumentModel updatedFile = folderChildren.get(0);
    assertEquals("File", updatedFile.getType());
    assertEquals("New file.odt", updatedFile.getTitle());
    assertEquals("/syncRoot1/aFolder/A new folder/New file.odt", updatedFile.getPathAsString());
    Blob updatedFileBlob = (Blob) updatedFile.getPropertyValue("file:content");
    assertEquals("New file.odt", updatedFileBlob.getFilename());
    assertEquals("Modified content of an existing file.", updatedFileBlob.getString());
    assertEquals("nxfile/test/" + updatedFile.getId() + "/blobholder:0/New%20file.odt", fileItem.getDownloadURL());
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    assertEquals(updatedFileBlob.getDigest(), fileItem.getDigest());
    // ------------------------------------------------------
    // Check #delete
    // ------------------------------------------------------
    // File deletion
    fileSystemItemManagerService.delete(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + updatedFile.getId(), principal);
    updatedFile = session.getDocument(new IdRef(updatedFile.getId()));
    assertTrue(updatedFile.isTrashed());
    // Parent folder children check
    assertTrue(fileSystemItemManagerService.getChildren(newFolderItem.getId(), principal).isEmpty());
    // ------------------------------------------------------
    // Check #rename
    // ------------------------------------------------------
    // Folder rename
    String fsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId();
    FileSystemItem fsItem = fileSystemItemManagerService.rename(fsItemId, "Jack's folder has a new name", principal);
    assertEquals(fsItemId, fsItem.getId());
    String expectedSyncRoot1Id = DEFAULT_SYNC_ROOT_ITEM_ID_PREFIX + syncRoot1.getId();
    assertEquals(expectedSyncRoot1Id, fsItem.getParentId());
    assertEquals("Jack's folder has a new name", fsItem.getName());
    folder = session.getDocument(folder.getRef());
    assertEquals("Jack's folder has a new name", folder.getTitle());
    // File rename with title != filename
    // => should rename filename but not title
    assertEquals("aFile", file.getTitle());
    assertEquals("Joe.odt", ((Blob) file.getPropertyValue("file:content")).getFilename());
    fsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId();
    fsItem = fileSystemItemManagerService.rename(fsItemId, "File new name.odt", principal);
    assertEquals(fsItemId, fsItem.getId());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getParentId());
    assertEquals("File new name.odt", fsItem.getName());
    file = session.getDocument(file.getRef());
    assertEquals("aFile", file.getTitle());
    Blob fileBlob = (Blob) file.getPropertyValue("file:content");
    assertEquals("File new name.odt", fileBlob.getFilename());
    fileItem = (FileItem) fsItem;
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/File%20new%20name.odt", fileItem.getDownloadURL());
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    assertEquals(fileBlob.getDigest(), fileItem.getDigest());
    // File rename with title == filename
    // => should rename filename and title
    blob = new StringBlob("File for a doc with title == filename.");
    blob.setFilename("Title-filename equality.odt");
    blob.setMimeType("application/vnd.oasis.opendocument.text");
    fileItem = fileSystemItemManagerService.createFile(newFolderItem.getId(), blob, principal, false);
    // Note that the PathSegmentService truncates doc title at 24 characters
    newFile = session.getDocument(new PathRef("/syncRoot1/aFolder/A new folder/Title-filename equality."));
    assertEquals("Title-filename equality.odt", newFile.getTitle());
    assertEquals("Title-filename equality.odt", ((Blob) newFile.getPropertyValue("file:content")).getFilename());
    fileItem = (FileItem) fileSystemItemManagerService.rename(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + newFile.getId(), "Renamed title-filename equality.odt", principal);
    assertEquals("Renamed title-filename equality.odt", fileItem.getName());
    newFile = session.getDocument(newFile.getRef());
    assertEquals("Renamed title-filename equality.odt", newFile.getTitle());
    newFileBlob = (Blob) newFile.getPropertyValue("file:content");
    assertEquals("Renamed title-filename equality.odt", newFileBlob.getFilename());
    assertEquals("nxfile/test/" + newFile.getId() + "/blobholder:0/Renamed%20title-filename%20equality.odt", fileItem.getDownloadURL());
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    assertEquals(newFileBlob.getDigest(), fileItem.getDigest());
    // ------------------------------------------------------
    // Check #move
    // ------------------------------------------------------
    // Not allowed to move a file system item to a non FolderItem
    String srcFsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId();
    String destFsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId();
    try {
        fileSystemItemManagerService.move(srcFsItemId, destFsItemId, principal);
        fail("Move to a non folder item should fail.");
    } catch (NuxeoException e) {
        assertEquals(String.format("Cannot move a file system item to file system item with id %s because it is not a folder.", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId()), e.getMessage());
    }
    // Move to a FolderItem
    destFsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder.getId();
    FileSystemItem movedFsItem = fileSystemItemManagerService.move(srcFsItemId, destFsItemId, principal);
    assertEquals(srcFsItemId, movedFsItem.getId());
    assertEquals(destFsItemId, movedFsItem.getParentId());
    assertEquals("aNote.txt", movedFsItem.getName());
    note = session.getDocument(note.getRef());
    assertEquals("/syncRoot1/aFolder/aSubFolder/aNote", note.getPathAsString());
    assertEquals("aNote", note.getTitle());
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) PathRef(org.nuxeo.ecm.core.api.PathRef) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 5 with FileItem

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

the class NuxeoDriveCreateFile method run.

@OperationMethod
public Blob run(Blob blob) throws ParseException, IOException {
    FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class);
    // correctly if there is non ascii characters in it.
    if (StringUtils.isNotBlank(name)) {
        blob.setFilename(name);
    }
    NuxeoDriveOperationHelper.normalizeMimeTypeAndEncoding(blob);
    FileItem fileItem = fileSystemItemManager.createFile(parentId, blob, ctx.getPrincipal(), overwrite);
    return Blobs.createJSONBlobFromValue(fileItem);
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) FileSystemItemManager(org.nuxeo.drive.service.FileSystemItemManager) OperationMethod(org.nuxeo.ecm.automation.core.annotations.OperationMethod)

Aggregations

FileItem (org.nuxeo.drive.adapter.FileItem)16 Test (org.junit.Test)12 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)12 Blob (org.nuxeo.ecm.core.api.Blob)11 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)9 FolderItem (org.nuxeo.drive.adapter.FolderItem)8 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)7 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)6 ScrollFileSystemItemList (org.nuxeo.drive.adapter.ScrollFileSystemItemList)3 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)3 Deploy (org.nuxeo.runtime.test.runner.Deploy)3 FileSystemItemFactory (org.nuxeo.drive.service.FileSystemItemFactory)2 FileSystemItemAdapterServiceImpl (org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl)2 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1