From commits-return-64953-archive-asf-public=cust-asf.ponee.io@sling.apache.org Thu Feb 1 16:16:08 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id E99B2180652 for ; Thu, 1 Feb 2018 16:16:08 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D8405160C56; Thu, 1 Feb 2018 15:16:08 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 05266160C44 for ; Thu, 1 Feb 2018 16:16:07 +0100 (CET) Received: (qmail 38641 invoked by uid 500); 1 Feb 2018 15:16:07 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 38632 invoked by uid 99); 1 Feb 2018 15:16:07 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Feb 2018 15:16:07 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 75BFC85824; Thu, 1 Feb 2018 15:16:04 +0000 (UTC) Date: Thu, 01 Feb 2018 15:16:05 +0000 To: "commits@sling.apache.org" Subject: [sling-org-apache-sling-fsresource] 02/02: SLING-7464 - FS Resource Provider for initial content missing children MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: jsedding@apache.org In-Reply-To: <151749816399.467.6717524728012389425@gitbox.apache.org> References: <151749816399.467.6717524728012389425@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: sling-org-apache-sling-fsresource X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Rev: 6cf2b45af1e4d8c86648092cff90bcf92812ada7 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20180201151605.75BFC85824@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. jsedding pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-fsresource.git commit 6cf2b45af1e4d8c86648092cff90bcf92812ada7 Author: Julian Sedding AuthorDate: Thu Feb 1 15:06:53 2018 +0100 SLING-7464 - FS Resource Provider for initial content missing children --- .../internal/mapper/ContentFileResourceMapper.java | 79 ++++++++++------------ .../sling/fsprovider/internal/JsonContentTest.java | 9 ++- .../resources/fs-test/folder2/content/child.json | 7 ++ .../fs-test/folder2/content/child/grandchild.json | 7 ++ 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/apache/sling/fsprovider/internal/mapper/ContentFileResourceMapper.java b/src/main/java/org/apache/sling/fsprovider/internal/mapper/ContentFileResourceMapper.java index bbf2af6..e7daf29 100644 --- a/src/main/java/org/apache/sling/fsprovider/internal/mapper/ContentFileResourceMapper.java +++ b/src/main/java/org/apache/sling/fsprovider/internal/mapper/ContentFileResourceMapper.java @@ -22,7 +22,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.Map; import org.apache.commons.collections.IteratorUtils; import org.apache.commons.collections.Transformer; @@ -32,7 +31,6 @@ import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.fsprovider.internal.ContentFileExtensions; import org.apache.sling.fsprovider.internal.FsResourceMapper; -import org.apache.sling.fsprovider.internal.parser.ContentElement; import org.apache.sling.fsprovider.internal.parser.ContentFileCache; public final class ContentFileResourceMapper implements FsResourceMapper { @@ -45,7 +43,7 @@ public final class ContentFileResourceMapper implements FsResourceMapper { private final ContentFileExtensions contentFileExtensions; private final ContentFileCache contentFileCache; - + public ContentFileResourceMapper(String providerRoot, File providerFile, ContentFileExtensions contentFileExtensions, ContentFileCache contentFileCache) { this.providerRootPrefix = providerRoot.concat("/"); @@ -74,57 +72,52 @@ public final class ContentFileResourceMapper implements FsResourceMapper { if (contentFileExtensions.isEmpty()) { return null; } + final String parentPath = parent.getPath(); - ContentFile parentContentFile = parent.adaptTo(ContentFile.class); + final ContentFile parentContentFile = getFile(parentPath, null);; + + + final List> childIterators = new ArrayList<>(2); + + // add children from parsed content + if (parentContentFile != null && parentContentFile.hasContent()) { + childIterators.add(IteratorUtils.transformedIterator(parentContentFile.getContent().getChildren().keySet().iterator(), new Transformer() { + @Override + public Object transform(final Object input) { + String name = (String)input; + return new ContentFileResource(resolver, parentContentFile.navigateToRelative(name)); + } + })); + } - // not a FsResource, try to create one from the resource - if (parentContentFile == null) { - parentContentFile = getFile(parentPath, null); - if (parentContentFile == null) { - - // check if parent is a file resource that contains a file content resource - File parentFile = parent.adaptTo(File.class); - if (parentFile != null && parentFile.isDirectory()) { - List childResources = new ArrayList<>(); - for (File file : parentFile.listFiles()) { + // add children from filesystem folder + File parentDir = new File(providerFile, StringUtils.removeStart(parentPath, providerRootPrefix)); + if (parentDir.isDirectory()) { + File[] files = parentDir.listFiles(); + if (files != null) { + childIterators.add(IteratorUtils.transformedIterator(IteratorUtils.arrayIterator(files), new Transformer() { + @Override + public Object transform(final Object input) { + File file = (File)input; + String path = parentPath + "/" + Escape.fileToResourceName(file.getName()); String filenameSuffix = contentFileExtensions.getSuffix(file); - if (filenameSuffix != null && !isNodeDescriptor(file)) { - String path = parentPath + "/" + StringUtils.substringBeforeLast(file.getName(), filenameSuffix); + if (filenameSuffix != null) { + path = StringUtils.substringBeforeLast(path, filenameSuffix); ContentFile contentFile = new ContentFile(file, path, null, contentFileCache); - childResources.add(new ContentFileResource(resolver, contentFile)); + return new ContentFileResource(resolver, contentFile); + } else { + return new FileResource(resolver, path, file, contentFileExtensions, contentFileCache); } } - if (!childResources.isEmpty()) { - return childResources.iterator(); - } - } - - // no children here - return null; + })); } } - // get child resources from content fragments in content file - List children = new ArrayList<>(); - if (parentContentFile.hasContent()) { - Iterator> childMaps = parentContentFile.getChildren(); - while (childMaps.hasNext()) { - Map.Entry entry = childMaps.next(); - children.add(parentContentFile.navigateToRelative(entry.getKey())); - } - } - if (children.isEmpty()) { + Iterator children = IteratorUtils.chainedIterator(childIterators); + if (!children.hasNext()) { return null; } - else { - return IteratorUtils.transformedIterator(children.iterator(), new Transformer() { - @Override - public Object transform(Object input) { - ContentFile contentFile = (ContentFile)input; - return new ContentFileResource(resolver, contentFile); - } - }); - } + return children; } private ContentFile getFile(String path, String subPath) { diff --git a/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java b/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java index ce21a72..e33f1d1 100644 --- a/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java +++ b/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java @@ -100,8 +100,10 @@ public class JsonContentTest { public void testListChildren() { assertThat(root, ResourceMatchers.containsChildren("fs-test")); assertThat(fsroot, ResourceMatchers.hasChildren("folder1", "folder2")); - assertThat(fsroot.getChild("folder1"), ResourceMatchers.hasChildren("folder11", "file1a.txt", "sling:file1b.txt")); - assertThat(fsroot.getChild("folder2"), ResourceMatchers.hasChildren("folder21", "content")); + assertThat(fsroot.getChild("folder1"), ResourceMatchers.containsChildrenInAnyOrder("folder11", "file1a.txt", "sling:file1b.txt")); + assertThat(fsroot.getChild("folder2"), ResourceMatchers.containsChildrenInAnyOrder("folder21", "content")); + assertThat(fsroot.getChild("folder2/content"), ResourceMatchers.containsChildrenInAnyOrder("jcr:content", "toolbar", "child", "file2content.txt", "sling:content2")); + assertThat(fsroot.getChild("folder2/content/child"), ResourceMatchers.containsChildrenInAnyOrder("jcr:content", "grandchild")); } @Test @@ -287,6 +289,9 @@ public class JsonContentTest { Resource content2 = fsroot.getChild("folder2/content/sling:content2"); assertNotNull(content2); assertEquals("app:Page", content2.getResourceType()); + + Resource content = fsroot.getChild("folder2/content"); + assertThat(content, ResourceMatchers.hasChildren("sling:content2")); } @Test diff --git a/src/test/resources/fs-test/folder2/content/child.json b/src/test/resources/fs-test/folder2/content/child.json new file mode 100644 index 0000000..ec451fb --- /dev/null +++ b/src/test/resources/fs-test/folder2/content/child.json @@ -0,0 +1,7 @@ +{ + "jcr:primaryType":"app:Page", + "jcr:content": { + "jcr:title": "Child", + "sling:resourceType":"sling/page/base" + } +} \ No newline at end of file diff --git a/src/test/resources/fs-test/folder2/content/child/grandchild.json b/src/test/resources/fs-test/folder2/content/child/grandchild.json new file mode 100644 index 0000000..1a50b1e --- /dev/null +++ b/src/test/resources/fs-test/folder2/content/child/grandchild.json @@ -0,0 +1,7 @@ +{ + "jcr:primaryType":"app:Page", + "jcr:content": { + "jcr:title": "Grandchild", + "sling:resourceType":"sling/page/base" + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact jsedding@apache.org.