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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9f77def  SLING-9986 add reference search paths
9f77def is described below

commit 9f77defedff3e1e78461f0be981bccab932ae1cc
Author: Nicolas Peltier <[email protected]>
AuthorDate: Tue Dec 8 18:28:10 2020 +0100

    SLING-9986 add reference search paths
---
 src/main/java/org/apache/sling/pipes/Plumber.java       |  8 ++++++++
 .../org/apache/sling/pipes/internal/PlumberImpl.java    | 17 +++++++++++++++++
 .../org/apache/sling/pipes/internal/ReferencePipe.java  |  9 ++++++---
 .../java/org/apache/sling/pipes/AbstractPipeTest.java   |  3 ++-
 .../apache/sling/pipes/internal/ReferencePipeTest.java  |  9 +++++++--
 5 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/Plumber.java 
b/src/main/java/org/apache/sling/pipes/Plumber.java
index ace9be3..097539c 100644
--- a/src/main/java/org/apache/sling/pipes/Plumber.java
+++ b/src/main/java/org/apache/sling/pipes/Plumber.java
@@ -20,6 +20,7 @@ import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.event.jobs.Job;
+import org.jetbrains.annotations.Nullable;
 import org.osgi.annotation.versioning.ProviderType;
 
 import java.io.IOException;
@@ -160,4 +161,11 @@ public interface Plumber {
      * @return context aware configuration map
      */
     Map getContextAwareConfigurationMap(Resource currentResource);
+
+    /**
+     * @param referrer resource from which is made the fetch
+     * @param reference reference we are searching a resource for (assuming 
this is *not* a full path already)
+     * @return referenced resource, null otherwise
+     */
+    @Nullable Resource getReferencedResource(Resource referrer, String 
reference);
 }
diff --git a/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java 
b/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
index ed966e8..718bca3 100644
--- a/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
@@ -47,6 +47,7 @@ import org.apache.sling.pipes.PipeExecutor;
 import org.apache.sling.pipes.Plumber;
 import org.apache.sling.pipes.PlumberMXBean;
 import org.apache.sling.pipes.internal.bindings.ConfigurationMap;
+import org.jetbrains.annotations.Nullable;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
@@ -83,6 +84,7 @@ import java.util.Map;
 import static org.apache.sling.api.resource.ResourceResolverFactory.SUBSERVICE;
 import static org.apache.sling.pipes.BasePipe.PN_STATUS;
 import static org.apache.sling.pipes.BasePipe.PN_STATUS_MODIFIED;
+import static org.apache.sling.pipes.BasePipe.SLASH;
 import static org.apache.sling.pipes.BasePipe.STATUS_FINISHED;
 import static org.apache.sling.pipes.BasePipe.STATUS_STARTED;
 
@@ -124,6 +126,9 @@ public class PlumberImpl implements Plumber, JobConsumer, 
PlumberMXBean {
 
         @AttributeDefinition(description="Users allowed to register async 
pipes")
         String[] authorizedUsers() default  {"admin"};
+
+        @AttributeDefinition(description = "Paths to search for references in")
+        String[] referencesPaths() default {};
     }
 
     @Reference(policy= ReferencePolicy.DYNAMIC, cardinality= 
ReferenceCardinality.OPTIONAL)
@@ -194,6 +199,18 @@ public class PlumberImpl implements Plumber, JobConsumer, 
PlumberMXBean {
         return new ConfigurationMap(currentResource, configMetadataProvider);
     }
 
+    @Override
+    public @Nullable Resource getReferencedResource(Resource referrer, String 
reference) {
+        ResourceResolver resolver = referrer.getResourceResolver();
+        for (String path : configuration.referencesPaths()) {
+            Resource target = resolver.getResource(path + SLASH + reference);
+            if (target != null) {
+                return target;
+            }
+        }
+        return null;
+    }
+
     @Deactivate
     public void deactivate(){
         toggleJmxRegistration(null, PlumberMXBean.class.getName(), false);
diff --git a/src/main/java/org/apache/sling/pipes/internal/ReferencePipe.java 
b/src/main/java/org/apache/sling/pipes/internal/ReferencePipe.java
index ae780c0..269a197 100644
--- a/src/main/java/org/apache/sling/pipes/internal/ReferencePipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/ReferencePipe.java
@@ -54,14 +54,17 @@ public class ReferencePipe extends SuperPipe {
             referencePath = expression;
             Resource pipeResource = resolver.getResource(referencePath);
             if (pipeResource == null) {
-                throw new IllegalArgumentException("Reference configuration 
error: There is no resource at " + getExpr());
+                pipeResource = plumber.getReferencedResource(resource, 
referencePath);
+            }
+            if (pipeResource == null) {
+                throw new IllegalArgumentException("Reference configuration 
error: There is no resource at " + referencePath);
             }
             reference = plumber.getPipe(pipeResource, bindings);
             if (reference == null) {
-                throw new IllegalArgumentException("Unable to build out pipe 
out of " + getPath());
+                throw new IllegalArgumentException("Unable to build out pipe 
out of " + referencePath);
             }
             reference.setParent(this);
-            log.info("set reference to {}", reference);
+            log.info("sets reference to {}", reference);
 
             subpipes.clear();
             subpipes.add(reference);
diff --git a/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java 
b/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java
index 914f4f6..711a07a 100644
--- a/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java
@@ -78,7 +78,8 @@ public class AbstractPipeTest {
         context.registerService(mock(JobManager.class));
         context.registerInjectActivateService(plumber, "authorizedUsers", new 
String[]{},
                 "bufferSize", PlumberImpl.DEFAULT_BUFFER_SIZE,
-                "executionPermissionResource", PATH_FRUITS);
+                "executionPermissionResource", PATH_FRUITS,
+                "referencesPaths", new String [] { "/conf/global/sling/pipes", 
"/apps/scripts" });
         plumber.registerPipe("slingPipes/dummyNull", DummyNull.class);
         plumber.registerPipe("slingPipes/dummySearch", DummySearch.class);
         commandsExecutor = new CommandExecutorImpl();
diff --git 
a/src/test/java/org/apache/sling/pipes/internal/ReferencePipeTest.java 
b/src/test/java/org/apache/sling/pipes/internal/ReferencePipeTest.java
index a20c64f..646cf5c 100644
--- a/src/test/java/org/apache/sling/pipes/internal/ReferencePipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/ReferencePipeTest.java
@@ -19,11 +19,11 @@ package org.apache.sling.pipes.internal;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.pipes.AbstractPipeTest;
-import org.apache.sling.pipes.ExecutionResult;
 import org.apache.sling.pipes.Pipe;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
@@ -62,7 +62,6 @@ public class ReferencePipeTest  extends AbstractPipeTest {
         testOneResource(PATH_PIPE + "/isAppleWormy", PATH_APPLE);
     }
 
-
     @Test
     public void testSkipExecution() throws PersistenceException, 
IllegalAccessException {
         Pipe pipe = 
plumber.newPipe(context.resourceResolver()).echo(PATH_FRUITS).build();
@@ -88,4 +87,10 @@ public class ReferencePipeTest  extends AbstractPipeTest {
             .run(bindings).getCurrentPathSet();
         assertTrue("paths should contain new path", paths.contains(newPath));
     }
+
+    @Test
+    public void testReferences() throws PersistenceException, 
InvocationTargetException, IllegalAccessException {
+        
plumber.newPipe(context.resourceResolver()).echo("/content/fruits").build("/apps/scripts/fruit-echo");
+        assertTrue(execute("ref fruit-echo").size() > 0);
+    }
 }
\ No newline at end of file

Reply via email to