001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Antoine Taillefer <ataillefer@nuxeo.com> 016 */ 017package org.nuxeo.drive.adapter.impl; 018 019import java.security.Principal; 020import java.util.ArrayList; 021import java.util.Collections; 022import java.util.Iterator; 023import java.util.List; 024import java.util.Map; 025import java.util.Set; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.drive.adapter.FileSystemItem; 030import org.nuxeo.drive.adapter.FolderItem; 031import org.nuxeo.drive.service.NuxeoDriveManager; 032import org.nuxeo.drive.service.SynchronizationRoots; 033import org.nuxeo.ecm.core.api.CoreInstance; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.IdRef; 037import org.nuxeo.ecm.core.api.security.SecurityConstants; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Default implementation of the top level {@link FolderItem}. 042 * 043 * @author Antoine Taillefer 044 */ 045public class DefaultTopLevelFolderItem extends AbstractVirtualFolderItem { 046 047 private static final long serialVersionUID = 1L; 048 049 private static final Log log = LogFactory.getLog(DefaultTopLevelFolderItem.class); 050 051 public DefaultTopLevelFolderItem(String factoryName, Principal principal, String folderName) { 052 super(factoryName, principal, null, null, folderName); 053 } 054 055 protected DefaultTopLevelFolderItem() { 056 // Needed for JSON deserialization 057 } 058 059 /*--------------------- FolderItem -----------------*/ 060 @Override 061 public List<FileSystemItem> getChildren() { 062 063 List<FileSystemItem> children = new ArrayList<FileSystemItem>(); 064 Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getLocalService(NuxeoDriveManager.class).getSynchronizationRoots( 065 principal); 066 for (String repositoryName : syncRootsByRepo.keySet()) { 067 try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) { 068 Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs(); 069 Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator(); 070 while (syncRootRefsIt.hasNext()) { 071 IdRef idRef = syncRootRefsIt.next(); 072 // TODO: ensure sync roots cache is up-to-date if ACL 073 // change, for now need to check permission 074 // See https://jira.nuxeo.com/browse/NXP-11146 075 if (!session.hasPermission(idRef, SecurityConstants.READ)) { 076 if (log.isDebugEnabled()) { 077 log.debug(String.format( 078 "User %s has no READ access on synchronization root %s, not including it in children.", 079 session.getPrincipal().getName(), idRef)); 080 } 081 continue; 082 } 083 DocumentModel doc = session.getDocument(idRef); 084 FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this); 085 if (child == null) { 086 if (log.isDebugEnabled()) { 087 log.debug(String.format( 088 "Synchronization root %s cannot be adapted as a FileSystemItem, not including it in children.", 089 idRef)); 090 } 091 continue; 092 } 093 if (log.isDebugEnabled()) { 094 log.debug(String.format("Including synchronization root %s in children.", idRef)); 095 } 096 children.add(child); 097 } 098 } 099 } 100 Collections.sort(children); 101 return children; 102 } 103 104}