use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.
the class UserWorkspaceSyncRootParentFolderItem method getChildren.
@Override
public List<FileSystemItem> getChildren() {
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);
// registered as a synchronization root to avoid recursion
if (!UserWorkspaceHelper.isUserWorkspace(doc)) {
// 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, not including it in children.", idRef));
}
continue;
}
if (log.isDebugEnabled()) {
log.debug(String.format("Including synchronization root %s in children.", idRef));
}
children.add(child);
}
}
}
}
Collections.sort(children);
return children;
}
use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.
the class NuxeoDriveFileSystemDeletionListener method fireVirtualEventLogEntries.
protected void fireVirtualEventLogEntries(DocumentModel doc, String eventName, Principal principal, String impactedUserName, CoreSession session) {
if (Framework.getService(AuditLogger.class) == null) {
// The log is not deployed (probably in unittest)
return;
}
List<LogEntry> entries = new ArrayList<>();
// XXX: shall we use the server local for the event date or UTC?
Date currentDate = Calendar.getInstance(NuxeoDriveManagerImpl.UTC).getTime();
FileSystemItem fsItem = getFileSystemItem(doc, eventName);
if (fsItem == null) {
// roots in order to make Drive add / remove them if needed
if (NuxeoDriveEvents.SECURITY_UPDATED_EVENT.equals(eventName)) {
for (DocumentModel childSyncRoot : getChildSyncRoots(doc, session)) {
FileSystemItem childSyncRootFSItem = getFileSystemItem(childSyncRoot, eventName);
if (childSyncRootFSItem != null) {
entries.add(computeLogEntry(eventName, currentDate, childSyncRoot.getId(), childSyncRoot.getPathAsString(), principal.getName(), childSyncRoot.getType(), childSyncRoot.getRepositoryName(), childSyncRoot.getCurrentLifeCycleState(), impactedUserName, childSyncRootFSItem));
}
}
}
} else {
entries.add(computeLogEntry(eventName, currentDate, doc.getId(), doc.getPathAsString(), principal.getName(), doc.getType(), doc.getRepositoryName(), doc.getCurrentLifeCycleState(), impactedUserName, fsItem));
}
if (!entries.isEmpty()) {
EventContext eventContext = new EventContextImpl(entries.toArray());
Event event = eventContext.newEvent(NuxeoDriveEvents.VIRTUAL_EVENT_CREATED);
Framework.getService(EventProducer.class).fireEvent(event);
}
}
use of org.nuxeo.drive.adapter.FileSystemItem in project nuxeo-drive-server by nuxeo.
the class FileSystemItemManagerImpl method delete.
@Override
public void delete(String id, Principal principal) {
FileSystemItem fsItem = getFileSystemItemById(id, principal);
delete(fsItem);
}
use of org.nuxeo.drive.adapter.FileSystemItem 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);
}
}
use of org.nuxeo.drive.adapter.FileSystemItem 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));
}
Aggregations