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 2017/11/07 09:56:58 UTC

[sling-org-apache-sling-pipes] 14/31: SLING-5818 - Make sling pipe writer a persistent configuration

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

rombert pushed a commit to annotated tag org.apache.sling.pipes-0.0.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git

commit c21dae5d108be9ce07efc0b899fcec1e0096eb79
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Tue Sep 6 11:30:13 2016 +0000

    SLING-5818 - Make sling pipe writer a persistent configuration
    
    Submitted-By: Nicolas Peltier
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/sling-pipes@1759410 13f79535-47bb-0310-9956-ffa450edef68
---
 .../java/org/apache/sling/pipes/ContainerPipe.java |  21 +----
 .../java/org/apache/sling/pipes/PipeBindings.java  |   5 +
 .../org/apache/sling/pipes/PlumberServlet.java     | 101 +++++++--------------
 .../org/apache/sling/pipes/PlumberServletTest.java |  11 ++-
 4 files changed, 45 insertions(+), 93 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/ContainerPipe.java b/src/main/java/org/apache/sling/pipes/ContainerPipe.java
index 0c68310..64aa072 100644
--- a/src/main/java/org/apache/sling/pipes/ContainerPipe.java
+++ b/src/main/java/org/apache/sling/pipes/ContainerPipe.java
@@ -17,23 +17,11 @@
 package org.apache.sling.pipes;
 
 import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ValueMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.script.Bindings;
-import javax.script.Invocable;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
-import javax.script.SimpleScriptContext;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Collections;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -84,10 +72,6 @@ public class ContainerPipe extends BasePipe {
 
     @Override
     public Iterator<Resource> getOutput()  {
-        if (pipeList.size() == 1) {
-            //corner case with only one element: no need to have a container resource iterator
-            return pipeList.iterator().next().getOutput();
-        }
         return new ContainerResourceIterator(this);
     }
 
@@ -187,7 +171,8 @@ public class ContainerPipe extends BasePipe {
             //2 choices here:
             // either cursor is at 0 with no resource left: end,
             // either cursor is on last pipe with a resource left: hasNext
-            return cursor > 0;
+            // the second part is for the corner case with only one item
+            return cursor > 0 || (iterators.size() == 1 && it.hasNext());
         }
 
         /**
@@ -220,7 +205,7 @@ public class ContainerPipe extends BasePipe {
 
         @Override
         public void remove() {
-
+            throw new UnsupportedOperationException();
         }
     }
 
diff --git a/src/main/java/org/apache/sling/pipes/PipeBindings.java b/src/main/java/org/apache/sling/pipes/PipeBindings.java
index c1f9f62..7d48e1d 100644
--- a/src/main/java/org/apache/sling/pipes/PipeBindings.java
+++ b/src/main/java/org/apache/sling/pipes/PipeBindings.java
@@ -130,6 +130,10 @@ public class PipeBindings {
         getBindings().putAll(bindings);
     }
 
+    public void copyBindings(PipeBindings original){
+        getBindings().putAll(original.getBindings());
+    }
+
     /**
      * Update current resource of a given pipe, and appropriate binding
      * @param pipe
@@ -144,6 +148,7 @@ public class PipeBindings {
     }
 
     public void addBinding(String name, Object value){
+        log.debug("Adding binding {}={}", name, value);
         getBindings().put(name, value);
     }
 
diff --git a/src/main/java/org/apache/sling/pipes/PlumberServlet.java b/src/main/java/org/apache/sling/pipes/PlumberServlet.java
index c0efe83..807b58e 100644
--- a/src/main/java/org/apache/sling/pipes/PlumberServlet.java
+++ b/src/main/java/org/apache/sling/pipes/PlumberServlet.java
@@ -16,7 +16,6 @@
  */
 package org.apache.sling.pipes;
 
-
 import org.apache.commons.lang.StringUtils;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.sling.SlingServlet;
@@ -25,17 +24,16 @@ import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
-import org.apache.sling.commons.json.io.JSONWriter;
+import org.apache.sling.pipes.impl.CustomJsonWriter;
+import org.apache.sling.pipes.impl.CustomWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.servlet.ServletException;
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
 
 /**
  * Servlet executing plumber for a pipe path given as 'path' parameter,
@@ -54,20 +52,12 @@ public class PlumberServlet extends SlingAllMethodsServlet {
 
     protected static final String PARAM_PATH = "path";
 
-    public static final String PATH_KEY = "path";
-
     protected static final String PARAM_BINDINGS = "bindings";
 
-    protected static final String PARAM_WRITER = "writer";
-
     protected static final String PARAM_SIZE = "size";
 
     public static final int NB_MAX = 10;
 
-    public static final String KEY_SIZE = PARAM_SIZE;
-
-    public static final String KEY_ITEMS = "items";
-
     @Reference
     Plumber plumber;
 
@@ -88,87 +78,58 @@ public class PlumberServlet extends SlingAllMethodsServlet {
                 throw new Exception("path should be provided");
             }
             String dryRun = request.getParameter(BasePipe.DRYRUN_KEY);
-            String paramBindings = request.getParameter(PARAM_BINDINGS);
             int size = request.getParameter(PARAM_SIZE) != null ? Integer.parseInt(request.getParameter(PARAM_SIZE)) : NB_MAX;
             if (size < 0) {
                 size = Integer.MAX_VALUE;
             }
 
-            Map additionalBindings = null;
+            ResourceResolver resolver = request.getResourceResolver();
+            Resource pipeResource = resolver.getResource(path);
+            Pipe pipe = plumber.getPipe(pipeResource);
+            PipeBindings bindings = pipe.getBindings();
+
             if (StringUtils.isNotBlank(dryRun) && dryRun.equals(Boolean.TRUE.toString())) {
-                additionalBindings = new HashMap<>();
-                additionalBindings.put(BasePipe.DRYRUN_KEY, true);
+                bindings.addBinding(BasePipe.DRYRUN_KEY, true);
             }
+
+            String paramBindings = request.getParameter(PARAM_BINDINGS);
             if (StringUtils.isNotBlank(paramBindings)){
                 try {
                     JSONObject bindingJSON = new JSONObject(paramBindings);
-                    additionalBindings = additionalBindings != null ? additionalBindings: new HashMap<>();
                     for (Iterator<String> keys = bindingJSON.keys(); keys.hasNext();){
                         String key = keys.next();
-                        additionalBindings.put(key, bindingJSON.get(key));
+                        bindings.addBinding(key, bindingJSON.get(key));
                     }
                 } catch (Exception e){
                     log.error("Unable to retrieve bindings information", e);
                 }
             }
-
-            String paramWriter = request.getParameter(PARAM_WRITER);
-            JSONObject writerObj = null;
-            if (StringUtils.isNotBlank(paramWriter)){
-                try {
-                    writerObj = new JSONObject(paramWriter);
-                } catch (Exception e) {
-                    log.error("Unable to retrieve the writer object");
-                }
-            }
-            response.setCharacterEncoding("utf-8");
-            response.setContentType("application/json");
-            JSONWriter writer = new JSONWriter(response.getWriter());
-            ResourceResolver resolver = request.getResourceResolver();
-            Resource pipeResource = resolver.getResource(path);
-            Pipe pipe = plumber.getPipe(pipeResource);
             if (!writeAllowed && pipe.modifiesContent()) {
                 throw new Exception("This pipe modifies content, you should use a POST request");
             }
-            writer.object();
+            OutputWriter writer = getWriter(request, response, pipe);
             int i = 0;
-            if (writerObj != null) {
-                pipe.getBindings().addBindings(additionalBindings);
-                Iterator<Resource> resourceIterator = pipe.getOutput();
-                writer.key(KEY_ITEMS).array();
-                while (resourceIterator.hasNext()){
-                    Resource resource = resourceIterator.next();
-                    if (++i < size) {
-                        writer.object();
-                        writer.key(PATH_KEY).value(resource.getPath());
-                        Iterator<String> keys = writerObj.keys();
-                        while (keys.hasNext()) {
-                            String key = keys.next();
-                            writer.key(key).value(pipe.getBindings().instantiateObject(writerObj.getString(key)));
-                        }
-                        writer.endObject();
-                    }
+            Iterator<Resource> resourceIterator = pipe.getOutput();
+            while (resourceIterator.hasNext()){
+                Resource resource = resourceIterator.next();
+                if (++i < size) {
+                    writer.writeItem(resource);
                 }
-                writer.endArray();
-                writer.key(KEY_SIZE).value(i);
-            } else {
-                Set<String> resources = plumber.execute(resolver, pipe, additionalBindings, true);
-                writer.key(KEY_SIZE).value(resources.size());
-                writer.key(KEY_ITEMS);
-                writer.array();
-                for (String resource : resources) {
-                    if (++i > size){
-                        break;
-                    } else {
-                        writer.value(resource);
-                    }
-                }
-                writer.endArray();
             }
-            writer.endObject();
-            response.flushBuffer();
+            writer.ends(i);
         } catch (Exception e) {
             throw new ServletException(e);
         }
     }
-}
+
+    OutputWriter getWriter(SlingHttpServletRequest request, SlingHttpServletResponse response, Pipe pipe) throws IOException, JSONException {
+        OutputWriter[] candidates = new OutputWriter[]{new CustomJsonWriter(), new CustomWriter(), new DefaultOutputWriter()};
+        for (OutputWriter candidate : candidates) {
+            if (candidate.handleRequest(request)) {
+                candidate.init(request, response, pipe);
+                return candidate;
+            }
+        }
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/pipes/PlumberServletTest.java b/src/test/java/org/apache/sling/pipes/PlumberServletTest.java
index 52ecf05..7d63794 100644
--- a/src/test/java/org/apache/sling/pipes/PlumberServletTest.java
+++ b/src/test/java/org/apache/sling/pipes/PlumberServletTest.java
@@ -25,6 +25,7 @@ import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.commons.json.JSONArray;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.pipes.impl.CustomWriter;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -78,8 +79,8 @@ public class PlumberServletTest  extends AbstractPipeTest {
         String finalResponse = stringResponse.toString();
         assertFalse("There should be a response", StringUtils.isBlank(finalResponse));
         JSONObject object = new JSONObject(finalResponse);
-        assertEquals("response should be an obj with size value equals to 4", object.getInt(PlumberServlet.KEY_SIZE), 4);
-        assertEquals("response should be an obj with items value equals to a 4 valued array", object.getJSONArray(PlumberServlet.KEY_ITEMS).length(), 4);
+        assertEquals("response should be an obj with size value equals to 4", object.getInt(OutputWriter.KEY_SIZE), 4);
+        assertEquals("response should be an obj with items value equals to a 4 valued array", object.getJSONArray(OutputWriter.KEY_ITEMS).length(), 4);
     }
 
     @Test
@@ -134,11 +135,11 @@ public class PlumberServletTest  extends AbstractPipeTest {
         servlet.execute(request, response, false);
         assertDummyTree();
         JSONObject response = new JSONObject(stringResponse.toString());
-        JSONArray array = response.getJSONArray(PlumberServlet.KEY_ITEMS);
+        JSONArray array = response.getJSONArray(OutputWriter.KEY_ITEMS);
         for (int i = 0; i < array.length(); i++) {
             JSONObject object = array.optJSONObject(i);
             assertNotNull("there should be an object returned at each time", object);
-            String path = object.optString(PlumberServlet.PATH_KEY);
+            String path = object.optString(CustomWriter.PATH_KEY);
             assertNotNull("the string path should be returned for each item, containing the path of the resource");
             String pathLength = object.optString(pathLengthParam);
             assertNotNull("there should be a pathLength param, as specified in the writer", pathLength);
@@ -176,7 +177,7 @@ public class PlumberServletTest  extends AbstractPipeTest {
         when(request.getResource()).thenReturn(resource);
         when(request.getParameter(PlumberServlet.PARAM_PATH)).thenReturn(pathParam);
         when(request.getParameter(PlumberServlet.PARAM_BINDINGS)).thenReturn(bindings);
-        when(request.getParameter(PlumberServlet.PARAM_WRITER)).thenReturn(writer);
+        when(request.getParameter(CustomWriter.PARAM_WRITER)).thenReturn(writer);
         when(request.getParameter(BasePipe.DRYRUN_KEY)).thenReturn(dryRun);
         return request;
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.