sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1777534 - /sling/trunk/bundles/extensions/fsresource/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
Date Fri, 06 Jan 2017 06:20:01 GMT
Author: cziegeler
Date: Fri Jan  6 06:20:00 2017
New Revision: 1777534

URL: http://svn.apache.org/viewvc?rev=1777534&view=rev
Log:
SLING-6439 : Filesystem Resource Provider does not support overlaying nodes from repository

Modified:
    sling/trunk/bundles/extensions/fsresource/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java

Modified: sling/trunk/bundles/extensions/fsresource/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/fsresource/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java?rev=1777534&r1=1777533&r2=1777534&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/fsresource/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
(original)
+++ sling/trunk/bundles/extensions/fsresource/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
Fri Jan  6 06:20:00 2017
@@ -20,8 +20,10 @@ package org.apache.sling.fsprovider.inte
 
 import java.io.File;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
+import java.util.Set;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -120,12 +122,23 @@ public class FsResourceProvider extends
      * to access the file or folder. If no such file or folder exists, this
      * method returns <code>null</code>.
      */
-    @Override
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+	@Override
     public Resource getResource(final ResolveContext<Object> ctx,
             final String path,
             final ResourceContext resourceContext,
             final Resource parent) {
-        return getResource(ctx.getResourceResolver(), path, getFile(path));
+        Resource rsrc = getResource(ctx.getResourceResolver(), path, getFile(path));
+        if ( rsrc == null ) {
+        	// get resource from shadowed provider
+        	final ResourceProvider rp = ctx.getParentResourceProvider();
+        	if ( rp != null ) {
+	            rsrc = rp.getResource((ResolveContext)ctx.getParentResolveContext(), 
+	            		path, 
+	            		resourceContext, parent);
+        	}        	
+        }
+        return rsrc;
     }
 
     /**
@@ -165,55 +178,70 @@ public class FsResourceProvider extends
             }
         }
 
+    	// get children from from shadowed provider
+    	final ResourceProvider rp = ctx.getParentResourceProvider();
+    	final Iterator<Resource> parentChildrenIterator;
+    	if ( rp != null ) {
+    		parentChildrenIterator = rp.listChildren(ctx.getParentResolveContext(), parent);
+    	} else {
+    		parentChildrenIterator = null;
+    	}
         final File[] children = parentFile.listFiles();
 
-        if (children != null && children.length > 0) {
-            final ResourceResolver resolver = parent.getResourceResolver();
-            final String parentPath = parent.getPath();
-            return new Iterator<Resource>() {
-                int index = 0;
-
-                Resource next = seek();
-
-                @Override
-                public boolean hasNext() {
-                    return next != null;
-                }
+        final ResourceResolver resolver = ctx.getResourceResolver();
+        final String parentPath = parent.getPath();
+        return new Iterator<Resource>() {
 
-                @Override
-                public Resource next() {
-                    if (!hasNext()) {
-                        throw new NoSuchElementException();
-                    }
+            final Set<String> names = new HashSet<>();
 
-                    Resource result = next;
-                    next = seek();
-                    return result;
-                }
+            int index = 0;
 
-                @Override
-                public void remove() {
-                    throw new UnsupportedOperationException("remove");
-                }
+            Resource next = seek();
 
-                private Resource seek() {
-                    while (index < children.length) {
-                        File file = children[index++];
-                        String path = parentPath + "/" + file.getName();
-                        Resource result = getResource(resolver, path, file);
-                        if (result != null) {
-                            return result;
-                        }
-                    }
+            @Override
+            public boolean hasNext() {
+                return next != null;
+            }
 
-                    // nothing found any more
-                    return null;
+            @Override
+            public Resource next() {
+                if (!hasNext()) {
+                    throw new NoSuchElementException();
                 }
-            };
-        }
 
-        // no children
-        return null;
+                Resource result = next;
+                next = seek();
+                return result;
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException("remove");
+            }
+
+            private Resource seek() {
+                while (children != null && index < children.length) {
+                    File file = children[index++];
+                    String path = parentPath + "/" + file.getName();
+                    Resource result = getResource(resolver, path, file);
+                    if (result != null) {
+                    	names.add(file.getName());
+                        return result;
+                    }
+                }
+                if ( parentChildrenIterator != null ) {
+                	while ( parentChildrenIterator.hasNext() ) {
+                		final Resource result = parentChildrenIterator.next();
+                		if ( !names.contains(result.getName()) ) {
+                			names.add(result.getName());
+                			return result;
+                		}
+                	}
+                }
+                // nothing found any more
+                return null;
+            }
+        };
     }
 
     // ---------- SCR Integration
@@ -307,15 +335,16 @@ public class FsResourceProvider extends
         return null;
     }
 
-    private Resource getResource(ResourceResolver resourceResolver,
-            String resourcePath, File file) {
+    private Resource getResource(final ResourceResolver resolver,
+            final String resourcePath, 
+            final File file) {
 
         if (file != null) {
 
             // if the file exists, but is not a directory or no repository entry
             // exists, return it as a resource
             if (file.exists()) {
-                return new FsResource(resourceResolver, resourcePath, file);
+                return new FsResource(resolver, resourcePath, file);
             }
 
         }



Mime
View raw message