001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Antoine Taillefer <ataillefer@nuxeo.com> 018 */ 019package org.nuxeo.drive.adapter.impl; 020 021import java.security.Principal; 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.Iterator; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.drive.adapter.FileSystemItem; 032import org.nuxeo.drive.adapter.FolderItem; 033import org.nuxeo.drive.service.NuxeoDriveManager; 034import org.nuxeo.drive.service.SynchronizationRoots; 035import org.nuxeo.ecm.core.api.CoreInstance; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.IdRef; 039import org.nuxeo.ecm.core.api.security.SecurityConstants; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * Default implementation of the top level {@link FolderItem}. 044 * 045 * @author Antoine Taillefer 046 */ 047public class DefaultTopLevelFolderItem extends AbstractVirtualFolderItem { 048 049 private static final long serialVersionUID = 1L; 050 051 private static final Log log = LogFactory.getLog(DefaultTopLevelFolderItem.class); 052 053 public DefaultTopLevelFolderItem(String factoryName, Principal principal, String folderName) { 054 super(factoryName, principal, null, null, folderName); 055 } 056 057 protected DefaultTopLevelFolderItem() { 058 // Needed for JSON deserialization 059 } 060 061 /*--------------------- FolderItem -----------------*/ 062 @Override 063 public List<FileSystemItem> getChildren() { 064 065 List<FileSystemItem> children = new ArrayList<FileSystemItem>(); 066 Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getLocalService(NuxeoDriveManager.class) 067 .getSynchronizationRoots(principal); 068 for (String repositoryName : syncRootsByRepo.keySet()) { 069 try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) { 070 Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs(); 071 Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator(); 072 while (syncRootRefsIt.hasNext()) { 073 IdRef idRef = syncRootRefsIt.next(); 074 // TODO: ensure sync roots cache is up-to-date if ACL 075 // change, for now need to check permission 076 // See https://jira.nuxeo.com/browse/NXP-11146 077 if (!session.hasPermission(idRef, SecurityConstants.READ)) { 078 if (log.isDebugEnabled()) { 079 log.debug(String.format( 080 "User %s has no READ access on synchronization root %s, not including it in children.", 081 session.getPrincipal().getName(), idRef)); 082 } 083 continue; 084 } 085 DocumentModel doc = session.getDocument(idRef); 086 // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo 087 FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, 088 false); 089 if (child == null) { 090 if (log.isDebugEnabled()) { 091 log.debug(String.format( 092 "Synchronization root %s cannot be adapted as a FileSystemItem, not including it in children.", 093 idRef)); 094 } 095 continue; 096 } 097 if (log.isDebugEnabled()) { 098 log.debug(String.format("Including synchronization root %s in children.", idRef)); 099 } 100 children.add(child); 101 } 102 } 103 } 104 Collections.sort(children); 105 return children; 106 } 107 108}