You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2020/11/16 11:55:51 UTC

[sling-org-apache-sling-pipes] branch master updated: SLING-9910 some minor improvements

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 f794cf4  SLING-9910 some minor improvements
f794cf4 is described below

commit f794cf45f093bb486470be64a0c8ee854e643e22
Author: Nicolas Peltier <np...@apache.org>
AuthorDate: Mon Nov 16 11:53:09 2020 +0100

    SLING-9910 some minor improvements
    
    - allow path pipe to create node types without resource types,
    - switch build to java 11, with required annotation dependency change,
    - remove test logs,
    - some sonar fixes
---
 pom.xml                                            |  9 ++++--
 src/main/java/org/apache/sling/pipes/BasePipe.java |  4 ++-
 .../java/org/apache/sling/pipes/PipeBindings.java  |  2 +-
 .../sling/pipes/internal/CommandExecutorImpl.java  | 35 ++++++++++++----------
 .../org/apache/sling/pipes/internal/PathPipe.java  | 21 +++++++++++--
 .../sling/pipes/internal/PipeBuilderImpl.java      |  6 +++-
 .../apache/sling/pipes/internal/PlumberImpl.java   |  2 +-
 .../pipes/internal/inputstream/RegexpPipe.java     |  4 +++
 .../org/apache/sling/pipes/models/PipeModel.java   |  8 +++--
 .../java/org/apache/sling/pipes/package-info.java  |  2 +-
 src/test/resources/simplelogger.properties         |  2 +-
 11 files changed, 65 insertions(+), 30 deletions(-)

diff --git a/pom.xml b/pom.xml
index 33ab8d8..0bc4fd4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,7 +35,7 @@
   <description>bulk content changes tool</description>
 
   <properties>
-    <sling.java.version>8</sling.java.version>
+    <sling.java.version>11</sling.java.version>
     <org.ops4j.pax.exam.version>4.13.3</org.ops4j.pax.exam.version>
   </properties>
 
@@ -264,6 +264,12 @@
       <artifactId>annotations</artifactId>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>javax.annotation</groupId>
+      <artifactId>javax.annotation-api</artifactId>
+      <version>1.3.2</version>
+      <scope>provided</scope>
+    </dependency>
     <!-- testing -->
     <dependency>
       <groupId>junit</groupId>
@@ -342,7 +348,6 @@
       <version>3.1.0</version>
       <scope>test</scope>
     </dependency>
-    <!-- jsoup -->
     <dependency>
       <groupId>org.jsoup</groupId>
       <artifactId>jsoup</artifactId>
diff --git a/src/main/java/org/apache/sling/pipes/BasePipe.java b/src/main/java/org/apache/sling/pipes/BasePipe.java
index 450ec16..1cda564 100644
--- a/src/main/java/org/apache/sling/pipes/BasePipe.java
+++ b/src/main/java/org/apache/sling/pipes/BasePipe.java
@@ -72,7 +72,9 @@ public class BasePipe implements Pipe {
     protected String afterHook;
 
     // used by pipes using complex JCR configurations
-    protected static final List<String> IGNORED_PROPERTIES = Arrays.asList("jcr:lastModified", "jcr:primaryType", "jcr:created", "jcr:createdBy", "jcr:uuid");
+    protected static final List<String> IGNORED_PROPERTIES = Arrays.asList("jcr:lastModified", "jcr:primaryType",
+        "jcr:created", "jcr:createdBy", "jcr:versionHistory", "jcr:predecessors", "jcr:baseVersion", "jcr:uuid",
+        "jcr:isCheckedOut");
 
     Boolean dryRunObject = null;
 
diff --git a/src/main/java/org/apache/sling/pipes/PipeBindings.java b/src/main/java/org/apache/sling/pipes/PipeBindings.java
index 889e289..f0e667f 100644
--- a/src/main/java/org/apache/sling/pipes/PipeBindings.java
+++ b/src/main/java/org/apache/sling/pipes/PipeBindings.java
@@ -66,7 +66,7 @@ public class PipeBindings {
      */
     public static final String NAME_BINDING = "name";
 
-    public static final String INJECTED_SCRIPT_REGEXP = "\\$\\{(([^\\{^\\}]*(\\{[0-9,]+\\})?)*)\\}";
+    public static final String INJECTED_SCRIPT_REGEXP = "\\$\\{(([^\\{^\\}]+(\\{[0-9,]+\\})?)*)\\}";
     private static final Pattern INJECTED_SCRIPT = Pattern.compile(INJECTED_SCRIPT_REGEXP);
     protected static final String IF_PREFIX = "$if";
     protected static final Pattern CONDITIONAL_STRING =  Pattern.compile("^\\" + IF_PREFIX + INJECTED_SCRIPT_REGEXP);
diff --git a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
index 198e94d..8121d53 100644
--- a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
@@ -37,6 +37,7 @@ import java.util.regex.Pattern;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestParameter;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.servlets.ServletResolverConstants;
@@ -63,7 +64,6 @@ import static org.apache.sling.pipes.PipeBindings.INJECTED_SCRIPT_REGEXP;
 import static org.apache.sling.pipes.internal.CommandUtil.writeToMap;
 
 import javax.servlet.Servlet;
-import javax.servlet.ServletException;
 
 @Component(service = {Servlet.class, CommandExecutor.class}, property= {
     ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + CommandExecutorImpl.RESOURCE_TYPE,
@@ -125,22 +125,25 @@ public class CommandExecutorImpl extends SlingAllMethodsServlet implements Comma
         List<String> cmds = new ArrayList<>();
         if (request.getParameterMap().containsKey(REQ_PARAM_CMD)) {
             cmds.add(request.getParameter(REQ_PARAM_CMD));
-        } else if (request.getParameterMap().containsKey(REQ_PARAM_FILE)) {
-            InputStream is = request.getRequestParameter(REQ_PARAM_FILE).getInputStream();
-            BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
-            String line;
-            StringBuilder cmdBuilder = new StringBuilder();
-            while ((line = reader.readLine()) != null) {
-                if (isCommandCandidate(line)) {
-                    cmdBuilder.append(LINE_SEPARATOR + line.trim());
-                } else if (cmdBuilder.length() > 0){
+        } else {
+            RequestParameter paramFile = request.getRequestParameter(REQ_PARAM_FILE);
+            if (paramFile != null) {
+                InputStream is = paramFile.getInputStream();
+                BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
+                String line;
+                StringBuilder cmdBuilder = new StringBuilder();
+                while ((line = reader.readLine()) != null) {
+                    if (isCommandCandidate(line)) {
+                        cmdBuilder.append(LINE_SEPARATOR + line.trim());
+                    } else if (cmdBuilder.length() > 0){
+                        cmds.add(cmdBuilder.toString().trim());
+                        cmdBuilder = new StringBuilder();
+                    }
+                }
+                if (cmdBuilder.length() > 0) {
                     cmds.add(cmdBuilder.toString().trim());
-                    cmdBuilder = new StringBuilder();
                 }
             }
-            if (cmdBuilder.length() > 0) {
-                cmds.add(cmdBuilder.toString().trim());
-            }
         }
         return cmds;
     }
@@ -156,7 +159,7 @@ public class CommandExecutorImpl extends SlingAllMethodsServlet implements Comma
             if (cmds.isEmpty()) {
                 writer.println("No command to execute!");
             }
-            short idx_line = 0;
+            short idxLine = 0;
             for (String command : cmds) {
                 if (StringUtils.isNotBlank(command)) {
                     JsonWriter pipeWriter = new JsonWriter();
@@ -164,7 +167,7 @@ public class CommandExecutorImpl extends SlingAllMethodsServlet implements Comma
                     currentCommand = command;
                     PipeBuilder pipeBuilder = parse(resolver, command.split(WHITE_SPACE_SEPARATOR));
                     Pipe pipe = pipeBuilder.build();
-                    bindings.put(CMD_LINE_PREFIX + idx_line++, pipe.getResource().getPath());
+                    bindings.put(CMD_LINE_PREFIX + idxLine ++, pipe.getResource().getPath());
                     writer.println(plumber.execute(resolver, pipe, bindings, pipeWriter, true));
                 }
             }
diff --git a/src/main/java/org/apache/sling/pipes/internal/PathPipe.java b/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
index 1c6946e..e804c11 100644
--- a/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.pipes.internal;
 
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jackrabbit.commons.JcrUtils;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
@@ -25,6 +27,8 @@ import org.apache.sling.pipes.Plumber;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 import java.util.Collections;
 import java.util.Iterator;
 
@@ -34,7 +38,9 @@ import static org.apache.sling.jcr.resource.JcrResourceConstants.NT_SLING_FOLDER
  * creates or get given expression's path and returns corresponding resource
  * this pipe can be configured with the following properties:
  * <ul>
- *     <li><code>nodeType</code> resource type with which the leaf node of the created path will be created</li>
+ *     <li><code>nodeType</code> node type with which the leaf node of the created path will be created.
+ *     Note that in that case Jackrabbit utilitary will be used, as this explicitely set path to be JCR.</li>
+ *     <li><code>resourceType</code> resource type with which the leaf node of the created path will be created</li>
  *     <li><code>intermediateType</code> resource type with which intermediate nodse of the created path will be created</li>
  *     <li><code>autosave</code> flag indicating wether this pipe should triggers a commit at the end of the execution</li>
  * </ul>
@@ -42,10 +48,12 @@ import static org.apache.sling.jcr.resource.JcrResourceConstants.NT_SLING_FOLDER
 public class PathPipe extends BasePipe {
 
     public static final String RESOURCE_TYPE = RT_PREFIX + "path";
+    public static final String PN_NODETYPE = "nodeType";
     public static final String PN_RESOURCETYPE = "resourceType";
     public static final String PN_INTERMEDIATE = "intermediateType";
     public static final String PN_AUTOSAVE = "autosave";
 
+    String nodeType;
     String resourceType;
     String intermediateType;
     boolean autosave;
@@ -54,6 +62,7 @@ public class PathPipe extends BasePipe {
 
     public PathPipe(Plumber plumber, Resource resource, PipeBindings upperBindings) {
         super(plumber, resource, upperBindings);
+        nodeType = properties.get(PN_NODETYPE, String.class);
         resourceType = properties.get(PN_RESOURCETYPE, NT_SLING_FOLDER);
         intermediateType = properties.get(PN_INTERMEDIATE, resourceType);
         autosave = properties.get(PN_AUTOSAVE, false);
@@ -72,10 +81,16 @@ public class PathPipe extends BasePipe {
             String path = isRootPath(expr) ? expr : getInput().getPath() + SLASH + expr;
             logger.info("creating path {}", path);
             if (!isDryRun()) {
-                Resource resource = ResourceUtil.getOrCreateResource(resolver, path, resourceType, intermediateType, autosave);
+                if (StringUtils.isNotBlank(nodeType)) {
+                    //in that case we are in a "JCR" mode
+                    JcrUtils.getOrCreateByPath(path, intermediateType, nodeType, resolver.adaptTo(Session.class), autosave);
+                } else {
+                    ResourceUtil.getOrCreateResource(resolver, path, resourceType, intermediateType, autosave);
+                }
+                Resource resource = resolver.getResource(path);
                 output = Collections.singleton(resource).iterator();
             }
-        } catch (PersistenceException e) {
+        } catch (PersistenceException | RepositoryException e) {
             logger.error ("Not able to create path {}", expr, e);
         }
         return output;
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 791731b..7db39ab 100644
--- a/src/main/java/org/apache/sling/pipes/internal/PipeBuilderImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/PipeBuilderImpl.java
@@ -124,7 +124,11 @@ public class PipeBuilderImpl implements PipeBuilder {
 
     @Override
     public PipeBuilder write(Object... conf) throws IllegalAccessException {
-        return pipe(WritePipe.RESOURCE_TYPE).conf(conf);
+        PipeBuilder instance = pipe(WritePipe.RESOURCE_TYPE);
+        if (conf.length > 0) {
+            instance = instance.conf(conf);
+        }
+        return instance;
     }
 
     @Override
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 09e8c70..a4f2d67 100644
--- a/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/PlumberImpl.java
@@ -161,7 +161,7 @@ public class PlumberImpl implements Plumber, JobConsumer, PlumberMXBean {
         }
     }
 
-    void checkPermissions(ResourceResolver context, String... permissions) throws AccessControlException {
+    void checkPermissions(ResourceResolver context, String... permissions) {
         for (String permission : permissions) {
             if (context.getResource(permission) == null) {
                 log.debug("error trying to check permission {}", permission);
diff --git a/src/main/java/org/apache/sling/pipes/internal/inputstream/RegexpPipe.java b/src/main/java/org/apache/sling/pipes/internal/inputstream/RegexpPipe.java
index ffdbe54..3692d49 100644
--- a/src/main/java/org/apache/sling/pipes/internal/inputstream/RegexpPipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/inputstream/RegexpPipe.java
@@ -32,6 +32,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -74,6 +75,9 @@ public class RegexpPipe extends AbstractInputStreamPipe {
 
                     @Override
                     public Resource next() {
+                        if (! hasNext) {
+                            throw new NoSuchElementException();
+                        }
                         if (!names.isEmpty()){
                             Map<String, Object> map = new HashMap<>();
                             for (String name : names) {
diff --git a/src/main/java/org/apache/sling/pipes/models/PipeModel.java b/src/main/java/org/apache/sling/pipes/models/PipeModel.java
index 809938f..6891efc 100644
--- a/src/main/java/org/apache/sling/pipes/models/PipeModel.java
+++ b/src/main/java/org/apache/sling/pipes/models/PipeModel.java
@@ -25,11 +25,13 @@ import org.apache.sling.pipes.Plumber;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.annotation.PostConstruct;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
+import javax.annotation.PostConstruct;
+
+
 /**
  * Check for pipes presence under <code>pipes</code> node of the given resource, and make their output available as pipes
  * to the script. Note that current resource is passed as a binding to the executed pipes, as a map of properties, plus
@@ -37,7 +39,7 @@ import java.util.Map;
  */
 @Model(adaptables = Resource.class)
 public class PipeModel {
-    Logger LOG = LoggerFactory.getLogger(PipeModel.class);
+    private static final Logger LOG = LoggerFactory.getLogger(PipeModel.class);
 
     /**
      * name of the child nodes under which we should look for pipes
@@ -99,7 +101,7 @@ public class PipeModel {
                     outputs.put(pipe.getName(), pipe.getOutput());
                     LOG.debug("found and initialized {}", pipe.getName());
                 } catch(Exception e){
-                    LOG.error("unable to bind {} pipe", candidate.getPath(), e);
+                    LOG.error("unable to bind {} pipe", candidate.getPath(), e);
                 }
             }
         } else {
diff --git a/src/main/java/org/apache/sling/pipes/package-info.java b/src/main/java/org/apache/sling/pipes/package-info.java
index 4bb3065..8b77a39 100644
--- a/src/main/java/org/apache/sling/pipes/package-info.java
+++ b/src/main/java/org/apache/sling/pipes/package-info.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("4.0.0")
+@Version("4.0.1")
 package org.apache.sling.pipes;
 
 import org.osgi.annotation.versioning.Version;
diff --git a/src/test/resources/simplelogger.properties b/src/test/resources/simplelogger.properties
index 568efd8..16018c4 100644
--- a/src/test/resources/simplelogger.properties
+++ b/src/test/resources/simplelogger.properties
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 org.slf4j.simpleLogger.logFile=System.out
-org.slf4j.simpleLogger.defaultLogLevel=info
+org.slf4j.simpleLogger.defaultLogLevel=off
 org.slf4j.simpleLogger.log.org.apache.jackrabbit.oak.plugins.index.IndexUpdate=warn
 org.slf4j.simpleLogger.log.org.apache.sling.scripting.core.impl.ScriptEngineManagerFactory=warn
 org.slf4j.simpleLogger.log.org.apache.sling.resourceresolver.impl.mapping.MapEntries=warn