You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2018/07/24 21:57:55 UTC

[sling-whiteboard] branch master updated (b5d19cf -> 80c1667)

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git.


    from b5d19cf  Additional testing for the whitelist resolver hook
     new aa03cb6  Ensure that index.md files take precedence over other files.
     new 205198f  Some documentation and cleanup
     new 58c67fe  MarkdownResource: adapt to Map
     new 80c1667  Update README

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 mdresourceprovider/README.md                       | 10 ++-
 .../sling/mdresource/impl/MarkdownResource.java    |  2 +-
 .../mdresource/impl/MarkdownResourceProvider.java  | 77 ++++++++++++++--------
 .../impl/MarkdownResourceProviderTest.java         |  3 +
 4 files changed, 64 insertions(+), 28 deletions(-)


[sling-whiteboard] 03/04: MarkdownResource: adapt to Map

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 58c67fe4640dde3a6268ed886351fc43b09d6e5e
Author: Robert Munteanu <ro...@localhost>
AuthorDate: Tue Jul 24 23:57:14 2018 +0200

    MarkdownResource: adapt to Map
    
    Now the default Sling Get servlet properly renders the properties.
---
 .../main/java/org/apache/sling/mdresource/impl/MarkdownResource.java   | 2 +-
 .../org/apache/sling/mdresource/impl/MarkdownResourceProviderTest.java | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResource.java b/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResource.java
index 4a6e83e..0a57b5d 100644
--- a/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResource.java
+++ b/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResource.java
@@ -137,7 +137,7 @@ public class MarkdownResource extends AbstractResource {
     
     @SuppressWarnings("unchecked")
     public <T> T adaptTo(Class<T> type) {
-        if ( type == ValueMap.class ) {
+        if ( type == ValueMap.class || type == Map.class ) {
             return (T) getValueMap();
         }
         
diff --git a/mdresourceprovider/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceProviderTest.java b/mdresourceprovider/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceProviderTest.java
index 6bbe0da..49e4476 100644
--- a/mdresourceprovider/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceProviderTest.java
+++ b/mdresourceprovider/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceProviderTest.java
@@ -28,6 +28,7 @@ import static org.junit.Assert.assertThat;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 import org.apache.sling.api.resource.Resource;
@@ -62,6 +63,8 @@ public class MarkdownResourceProviderTest {
 
         ValueMap adapted = resource.adaptTo(ValueMap.class);
         assertThat("adapted ValueMap", adapted, notNullValue());
+        assertThat("adapted Map", resource.adaptTo(Map.class), notNullValue());
+        
         // TODO - more valueMap tests
     }
     


[sling-whiteboard] 02/04: Some documentation and cleanup

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 205198f3c8dccf24573c86856806ec3c82b39dc3
Author: Robert Munteanu <ro...@localhost>
AuthorDate: Tue Jul 24 23:37:11 2018 +0200

    Some documentation and cleanup
---
 .../mdresource/impl/MarkdownResourceProvider.java  | 70 ++++++++++++++--------
 1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java b/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java
index 159d205..4fa47f4 100644
--- a/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java
+++ b/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java
@@ -38,6 +38,20 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * A <tt>ResourceProvider</tt> based on Markdown files
+ * 
+ * <p>This <tt>ResourceProvider</tt> serves content from a filesystem directory. It maps 
+ * file names to resources, by either:
+ * 
+ * <ul>
+ *  <li>Removing the file name if the name is <em>index.md</em></li>
+ *  <li>Removing the extension otherwise</li>
+ * </ul>
+ * 
+ * <p>In case of a conflict, the file named <em>index.md</em> takes precedence</p>
+ *
+ */
 @Component(
         service = ResourceProvider.class,
         configurationPolicy = ConfigurationPolicy.REQUIRE
@@ -45,7 +59,7 @@ import org.slf4j.LoggerFactory;
 @Designate(ocd = MarkdownResourceProvider.Config.class, factory = true)
 public class MarkdownResourceProvider extends ResourceProvider<Object> {
     
-    @ObjectClassDefinition(name = "Apache Sling Markdown Resource Provider")
+	@ObjectClassDefinition(name = "Apache Sling Markdown Resource Provider")
     public @interface Config {
 
         @AttributeDefinition(name="File system root",
@@ -60,9 +74,10 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
         // Internal Name hint for web console.
         String webconsole_configurationFactory_nameHint() default "{" + ResourceProvider.PROPERTY_ROOT + "}";
     }
-    
-    private final Logger log = LoggerFactory.getLogger(getClass());
-    
+
+    private static final String MARKDOWN_EXTENSION = ".md";
+	private static final String INDEX_FILE_NAME = "index" + MARKDOWN_EXTENSION;
+
     private String fsPath;
     private String repoPath;
     
@@ -76,30 +91,22 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
     public Resource getResource(ResolveContext<Object> ctx, String path, ResourceContext resourceContext,
             Resource parent) {
         
-        log.info("getResource(" + path + ")");
-        
         // try index.md file first
-        Path filePath = Paths.get(fsPath, path, "index.md");
+        Path filePath = Paths.get(fsPath, path, INDEX_FILE_NAME);
         File backingFile = filePath.toFile();
-        if ( !backingFile.exists() ) {
-            log.info("File at " + filePath + " does not exist");
+        if ( !backingFile.canRead() ) {
             // try direct file .md next
-            filePath = Paths.get(fsPath, path + ".md");
+            filePath = Paths.get(fsPath, path + MARKDOWN_EXTENSION);
             backingFile = filePath.toFile();
-            if ( !backingFile.exists() ) {
-                log.info("File at " + filePath + " does not exist");
-                log.info("Returning null");
-                return null;
-            }
+            if ( !backingFile.canRead() )
+				return null;
         }
         
-        log.info("Returning resource");
         return new MarkdownResource(ctx.getResourceResolver(), path, backingFile);
     }
 
     @Override
     public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
-        log.info("listChildren(" + parent.getPath() + ")");
         
         Path root = Paths.get(fsPath, parent.getPath());
         
@@ -119,18 +126,17 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
         File backingFile = path.toFile();
 
         if ( backingFile.isDirectory() ) {
-            backingFile = new File(backingFile, "index.md");
-            if ( backingFile.exists() && backingFile.canRead() ) {
+            backingFile = new File(backingFile, INDEX_FILE_NAME);
+            if ( backingFile.canRead() ) {
                 return asResource0(path, parent, ctx, backingFile);
             }
         }
         
-        if ( backingFile.isFile() && backingFile.canRead() && backingFile.getName().endsWith(".md") && !backingFile.getName().equals("index.md")) {
-        	Path potentialDirectory = Paths.get(backingFile.getAbsolutePath().substring(0, backingFile.getAbsolutePath().length() - ".md".length() ));
-        	if ( potentialDirectory.resolve("index.md").toFile().exists() ) {
+        if ( isRegularMarkdownFile(backingFile) ) {
+        	if ( isShadowed(backingFile) )
         		return null;
-        	}
-            return asResource0(path, parent, ctx, backingFile);
+
+        	return asResource0(path, parent, ctx, backingFile);
         }
         
         return null;
@@ -138,9 +144,23 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
 
     private Resource asResource0(Path path, Path parent, ResolveContext<Object> ctx, File backingFile) {
         Path fsRelativePath = Paths.get(fsPath).relativize(path);
-        Path parentRelativePath = Paths.get("/").relativize(parent);
         
         return new MarkdownResource(ctx.getResourceResolver(), "/" + fsRelativePath.toString().replaceAll("\\.md$", ""), backingFile);
     }
 
+	private boolean isRegularMarkdownFile(File backingFile) {
+		return backingFile.isFile() && backingFile.canRead() 
+				&& backingFile.getName().endsWith(MARKDOWN_EXTENSION) 
+				&& !backingFile.getName().equals(INDEX_FILE_NAME);
+	}
+
+	private boolean isShadowed(File backingFile) {
+		
+		File potentialDir = new File(backingFile.getParentFile(), backingFile.getName().replaceAll("\\.md$", ""));
+		if ( !potentialDir.canRead() )
+			return false;
+		
+		return new File(potentialDir, INDEX_FILE_NAME).canRead();
+	}
+
 }


[sling-whiteboard] 04/04: Update README

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 80c1667411967dd3fb230d197bd9394e9fd0b839
Author: Robert Munteanu <ro...@localhost>
AuthorDate: Tue Jul 24 23:57:23 2018 +0200

    Update README
---
 mdresourceprovider/README.md | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/mdresourceprovider/README.md b/mdresourceprovider/README.md
index b73aa49..b4323e9 100644
--- a/mdresourceprovider/README.md
+++ b/mdresourceprovider/README.md
@@ -3,4 +3,12 @@
 This module is part of the [Apache Sling](https://sling.apache.org) project.
 
 It contains a work-in-progress markdown resource provider. The code is only lightly tested and meant as a proof-of-content
-for now, and the only thing worse that the code is the documentation.
+for now. The only thing worse that the code is the documentation.
+
+## TODO
+
+- third-level recursive JSON access fails, e.g. `curl  http://localhost:8080/md-test.3.json`
+- add arbitrary properties using [YAML Front Matter](https://github.com/vsch/flexmark-java/wiki/Extensions#yaml-front-matter)
+- testing
+- documentation
+- fix rat checks  
\ No newline at end of file


[sling-whiteboard] 01/04: Ensure that index.md files take precedence over other files.

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit aa03cb604a31224678a20958ca3bdb2dee5b31aa
Author: Robert Munteanu <ro...@localhost>
AuthorDate: Tue Jul 24 23:28:18 2018 +0200

    Ensure that index.md files take precedence over other files.
---
 .../mdresource/impl/MarkdownResourceProvider.java   | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java b/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java
index 3e30f52..159d205 100644
--- a/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java
+++ b/mdresourceprovider/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceProvider.java
@@ -78,13 +78,13 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
         
         log.info("getResource(" + path + ")");
         
-        // try .md file first
-        Path filePath = Paths.get(fsPath, path + ".md");
+        // try index.md file first
+        Path filePath = Paths.get(fsPath, path, "index.md");
         File backingFile = filePath.toFile();
         if ( !backingFile.exists() ) {
             log.info("File at " + filePath + " does not exist");
-            // try /index.md next
-            filePath = Paths.get(fsPath, path, "index.md");
+            // try direct file .md next
+            filePath = Paths.get(fsPath, path + ".md");
             backingFile = filePath.toFile();
             if ( !backingFile.exists() ) {
                 log.info("File at " + filePath + " does not exist");
@@ -117,10 +117,7 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
 
     private Resource asResource(Path path, Path parent, ResolveContext<Object> ctx) {
         File backingFile = path.toFile();
-        if ( backingFile.isFile() && backingFile.canRead() && backingFile.getName().endsWith(".md") && !backingFile.getName().equals("index.md")) {
-            return asResource0(path, parent, ctx, backingFile);
-        }
-        
+
         if ( backingFile.isDirectory() ) {
             backingFile = new File(backingFile, "index.md");
             if ( backingFile.exists() && backingFile.canRead() ) {
@@ -128,6 +125,14 @@ public class MarkdownResourceProvider extends ResourceProvider<Object> {
             }
         }
         
+        if ( backingFile.isFile() && backingFile.canRead() && backingFile.getName().endsWith(".md") && !backingFile.getName().equals("index.md")) {
+        	Path potentialDirectory = Paths.get(backingFile.getAbsolutePath().substring(0, backingFile.getAbsolutePath().length() - ".md".length() ));
+        	if ( potentialDirectory.resolve("index.md").toFile().exists() ) {
+        		return null;
+        	}
+            return asResource0(path, parent, ctx, backingFile);
+        }
+        
         return null;
     }