sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From npelt...@apache.org
Subject [sling-org-apache-sling-pipes] branch master updated: SLING-7640 Introducing Package Pipe
Date Sat, 05 May 2018 22:16:19 GMT
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 c43fec6  SLING-7640 Introducing Package Pipe
c43fec6 is described below

commit c43fec6b6453a465ab1cb0fd8ab8624a9017338d
Author: Nicolas Peltier <peltier.nicolas@gmail.com>
AuthorDate: Sun May 6 00:15:57 2018 +0200

    SLING-7640 Introducing Package Pipe
    
    - only filter collection for now (most useful),
    - unit test does not work because of missing node type definition
---
 pom.xml                                            |  13 ++-
 .../java/org/apache/sling/pipes/PipeBuilder.java   |   6 ++
 .../apache/sling/pipes/internal/PackagePipe.java   | 112 +++++++++++++++++++++
 .../sling/pipes/internal/PipeBuilderImpl.java      |  10 ++
 .../apache/sling/pipes/internal/PlumberImpl.java   |   1 +
 .../sling/pipes/internal/PackagePipeTest.java      |  48 +++++++++
 6 files changed, 189 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 84e3c78..31d622e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -233,7 +233,13 @@
     <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.testing.sling-mock</artifactId>
-      <version>2.2.12</version>
+      <version>2.2.18</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>
+      <version>2.0.2</version>
       <scope>test</scope>
     </dependency>
     <dependency>
@@ -315,5 +321,10 @@
       <version>2.8.2</version>
       <scope>test</scope>
     </dependency>
+      <dependency>
+          <groupId>org.apache.jackrabbit.vault</groupId>
+          <artifactId>org.apache.jackrabbit.vault</artifactId>
+          <version>3.1.44</version>
+      </dependency>
   </dependencies>
 </project>
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/pipes/PipeBuilder.java b/src/main/java/org/apache/sling/pipes/PipeBuilder.java
index 225a20a..6e81756 100644
--- a/src/main/java/org/apache/sling/pipes/PipeBuilder.java
+++ b/src/main/java/org/apache/sling/pipes/PipeBuilder.java
@@ -167,6 +167,12 @@ public interface PipeBuilder {
      */
     PipeBuilder ref(String expr);
 
+    /**
+     * attach a package pipe, in filter collection mode as default
+     * @param expr path of the pipe
+     * @return updated instance of PipeBuilder
+     */
+    PipeBuilder pkg(String expr);
 
     /**
      * attach a not pipe to the current context
diff --git a/src/main/java/org/apache/sling/pipes/internal/PackagePipe.java b/src/main/java/org/apache/sling/pipes/internal/PackagePipe.java
new file mode 100644
index 0000000..548bb97
--- /dev/null
+++ b/src/main/java/org/apache/sling/pipes/internal/PackagePipe.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.pipes.internal;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jackrabbit.vault.fs.api.PathFilterSet;
+import org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter;
+import org.apache.jackrabbit.vault.packaging.JcrPackage;
+import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
+import org.apache.jackrabbit.vault.packaging.PackagingService;
+import org.apache.jackrabbit.vault.util.Text;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.pipes.BasePipe;
+import org.apache.sling.pipes.Plumber;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import java.io.IOException;
+import java.util.Iterator;
+
+/**
+ * Package pipe, creates or read vault package
+ */
+public class PackagePipe extends BasePipe {
+    private static final Logger LOGGER = LoggerFactory.getLogger(PackagePipe.class);
+
+    public static final String RESOURCE_TYPE = "slingPipes/package";
+
+    public static final String PN_FILTERCOLLECTIONMODE = "filterCollectionMode";
+
+    DefaultWorkspaceFilter filters;
+
+    JcrPackage jcrPackage;
+    /**
+     * Pipe Constructor
+     *
+     * @param plumber  plumber
+     * @param resource configuration resource
+     *
+     * @throws Exception in case configuration is not working
+     */
+    public PackagePipe(Plumber plumber, Resource resource) throws Exception {
+        super(plumber, resource);
+    }
+
+    @Override
+    public Iterator<Resource> getOutput() {
+        Iterator<Resource> output = EMPTY_ITERATOR;
+        try {
+            init();
+            if (properties.get(PN_FILTERCOLLECTIONMODE, false)){
+                if (filters == null){
+                    filters = new DefaultWorkspaceFilter();
+                }
+                filters.add(new PathFilterSet(getInput().getPath()));
+                jcrPackage.getDefinition().setFilter(filters, true);
+                output = IteratorUtils.singletonIterator(getInput());
+            }
+        } catch (IOException | RepositoryException e) {
+            LOGGER.error("unable to deal with package persistence", e);
+        }
+        return output;
+    }
+
+
+    /**
+     * computes configured package based on expression configuration (either existing or
creating it)
+     * @throws IOException problem with binary
+     * @throws RepositoryException problem with package persistence
+     */
+    protected void init() throws IOException, RepositoryException {
+        if (jcrPackage == null){
+            String packagePath = getExpr();
+            if (StringUtils.isNotBlank(packagePath)) {
+                JcrPackageManager mgr = PackagingService.getPackageManager(resolver.adaptTo(Session.class));
+                Resource packageResource = resolver.getResource(packagePath);
+                if (packageResource != null) {
+                    jcrPackage = mgr.open(packageResource.adaptTo(Node.class));
+                } else {
+                    String parent = Text.getRelativeParent(packagePath, 1);
+                    Resource folderResource = resolver.getResource(parent);
+                    if (folderResource == null) {
+                        LOGGER.error("folder of configured path should exists");
+                    } else {
+                        String name = Text.getName(packagePath);
+                        jcrPackage = mgr.create(folderResource.adaptTo(Node.class), name);
+                    }
+                }
+            } else {
+                LOGGER.error("expression should not be blank as it's supposed to hold package
path");
+            }
+        }
+    }
+}
diff --git a/src/main/java/org/apache/sling/pipes/internal/PipeBuilderImpl.java b/src/main/java/org/apache/sling/pipes/internal/PipeBuilderImpl.java
index 3d6d10b..d2b1e81 100644
--- a/src/main/java/org/apache/sling/pipes/internal/PipeBuilderImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/PipeBuilderImpl.java
@@ -215,6 +215,16 @@ public class PipeBuilderImpl implements PipeBuilder {
     }
 
     @Override
+    public PipeBuilder pkg(String expr) {
+        try {
+            pipeWithExpr(PackagePipe.RESOURCE_TYPE, expr).with(PackagePipe.PN_FILTERCOLLECTIONMODE,
true);
+        } catch (IllegalAccessException e) {
+            logger.error("error when calling pkg", e);
+        }
+        return this;
+    }
+
+    @Override
     public PipeBuilder not(String expr) {
         return pipeWithExpr(NotPipe.RESOURCE_TYPE, expr);
     }
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 dccca6c..9f8a564 100644
--- a/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
@@ -158,6 +158,7 @@ public class PlumberImpl implements Plumber, JobConsumer, PlumberMXBean
{
         registerPipe(ClosestPipe.RESOURCE_TYPE, ClosestPipe.class);
         registerPipe(FindPipe.RESOURCE_TYPE, FindPipe.class);
         registerPipe(RegexpPipe.RESOURCE_TYPE, RegexpPipe.class);
+        registerPipe(PackagePipe.RESOURCE_TYPE, PackagePipe.class);
         toggleJmxRegistration(this, PlumberMXBean.class.getName(), true);
         refreshMonitoredPipes();
     }
diff --git a/src/test/java/org/apache/sling/pipes/internal/PackagePipeTest.java b/src/test/java/org/apache/sling/pipes/internal/PackagePipeTest.java
new file mode 100644
index 0000000..8f5c246
--- /dev/null
+++ b/src/test/java/org/apache/sling/pipes/internal/PackagePipeTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.pipes.internal;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.pipes.AbstractPipeTest;
+import org.apache.sling.pipes.ExecutionResult;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+@Ignore
+public class PackagePipeTest extends AbstractPipeTest {
+
+    @Rule
+    public SlingContext oak = new SlingContext(ResourceResolverType.JCR_OAK);
+
+    @Test
+    public void filterModeTest() throws Exception {
+        oak.load().json("/SLING-INF/jcr_root/content/fruits.json", PATH_FRUITS);
+        String packagePath = "/content/package";
+        ExecutionResult result = plumber.newPipe(oak.resourceResolver())
+                .echo("/content/fruits")
+                .children("nt:unstructured")
+                .pkg(packagePath).run();
+        assertTrue("there should be more than one output", result.size() > 0);
+        Resource packageResource = context.resourceResolver().getResource(packagePath);
+        assertNotNull(packageResource);
+    }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
npeltier@apache.org.

Mime
View raw message