@Override
    public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
        if(directory.isRoot()) {
            return new DeepBoxesListService().list(directory, listener);
        }
        if(containerService.isDeepbox(directory)) { // in DeepBox
            return new BoxesListService().list(directory, listener);
        }
        if(containerService.isBox(directory)) { // in Box
            return new BoxListService().list(directory, listener);
        }
        final String deepBoxNodeId = fileid.getDeepBoxNodeId(directory);
        final String boxNodeId = fileid.getBoxNodeId(directory);
        if(containerService.isThirdLevel(directory)) { // in Inbox/Documents/Trash
            // N.B. although Documents and Trash have a nodeId, calling the listFiles1/listTrash1 API with
            // parentNode may fail!
            if(containerService.isInInbox(directory)) {
                return new NodeListService(new Contents() {
                    @Override
                    public NodeContent getNodes(final int offset) throws ApiException {
                        return new BoxRestControllerApi(session.getClient()).listQueue(deepBoxNodeId,
                                boxNodeId,
                                null,
                                offset, chunksize, "displayName asc");
                    }
                }).list(directory, listener);
            }
            if(containerService.isInDocuments(directory)) {
                return new NodeListService(new Contents() {
                    @Override
                    public NodeContent getNodes(final int offset) throws ApiException {
                        return new BoxRestControllerApi(session.getClient()).listFiles(
                                deepBoxNodeId,
                                boxNodeId,
                                offset, chunksize, "displayName asc");
                    }
                }).list(directory, listener);
            }
            if(containerService.isInTrash(directory)) {
                return new NodeListService(new Contents() {
                    @Override
                    public NodeContent getNodes(final int offset) throws ApiException {
                        return new BoxRestControllerApi(session.getClient()).listTrash(
                                deepBoxNodeId,
                                boxNodeId,
                                offset, chunksize, "displayName asc");
                    }
                }).list(directory, listener);
            }
        }
        // in subfolder of  Documents/Trash (Inbox has no subfolders)
        final String nodeId = fileid.getFileId(directory);
        if(containerService.isInTrash(directory)) {
            return new NodeListService(new Contents() {
                @Override
                public NodeContent getNodes(final int offset) throws ApiException {
                    return new BoxRestControllerApi(session.getClient()).listTrash1(
                            deepBoxNodeId,
                            boxNodeId,
                            nodeId,
                            offset, chunksize, "displayName asc");
                }
            }).list(directory, listener);
        }
        return new NodeListService(new Contents() {
            @Override
            public NodeContent getNodes(final int offset) throws ApiException {
                return new BoxRestControllerApi(session.getClient()).listFiles1(
                        deepBoxNodeId,
                        boxNodeId,
                        nodeId,
                        offset, chunksize, "displayName asc");
            }
        }).list(directory, listener);
    }
@Test
    // In this test setting, we get a NodeId from Box REST API and have canListFilesRoot==true for the documents folder. 
    // When listing the files for this nodId or when we try to get its NodeInfo, we get 403. 
    // Still, subfolders of documents are accessible (by listing the boxes files, but without passing the nodeId).
    // See also comment at {@link DeepboxIdProvider#lookupDocumentsNodeId}.
    public void testNoListChildrenTrashInbox() throws Exception {
        final DeepboxIdProvider nodeid = new DeepboxIdProvider(session);
        final Path box = new Path("/ORG 1 - DeepBox Desktop App/ORG1:Box2", EnumSet.of(Path.Type.directory, Path.Type.volume));
        final AttributedList<Path> list = new DeepboxListService(session, nodeid).list(box, new DisabledListProgressListener());
        final String deepBoxNodeId = nodeid.getDeepBoxNodeId(box);
        final String boxNodeId = nodeid.getBoxNodeId(box);
        final BoxAccessPolicy boxPolicy = new BoxRestControllerApi(session.getClient()).getBox(deepBoxNodeId, boxNodeId).getBoxPolicy();

        assertTrue(boxPolicy.isCanListFilesRoot());
        assertFalse(boxPolicy.isCanAccessTrash());
        assertFalse(boxPolicy.isCanListQueue());

        final Path documents = new Path("/ORG 1 - DeepBox Desktop App/ORG1:Box2/Documents", EnumSet.of(Path.Type.directory, Path.Type.volume));
        final String documentsNodeId = nodeid.getFileId(documents);
        final ApiException apiExceptionGetNodeInfo = assertThrows(ApiException.class, () -> new CoreRestControllerApi(session.getClient()).getNodeInfo(documentsNodeId, null, null, null));
        assertEquals(403, apiExceptionGetNodeInfo.getCode());
        final ApiException apiExceptionListFilestWithDocumentsNodeId = assertThrows(ApiException.class, () -> new BoxRestControllerApi(session.getClient()).listFiles1(deepBoxNodeId, boxNodeId, documentsNodeId, null, null, null));
        assertEquals(403, apiExceptionListFilestWithDocumentsNodeId.getCode());

        assertNotNull(list.find(new SimplePathPredicate(documents)));
        assertNull(list.find(new SimplePathPredicate(new Path("/ORG 1 - DeepBox Desktop App/ORG1:Box2/Inbox", EnumSet.of(Path.Type.directory, Path.Type.volume)))));
        assertNull(list.find(new SimplePathPredicate(new Path("/ORG 1 - DeepBox Desktop App/ORG1:Box2/Trash", EnumSet.of(Path.Type.directory, Path.Type.volume)))));
    }