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 2016/09/06 11:46:23 UTC

svn commit: r1759412 - in /sling/trunk/contrib/extensions/sling-pipes/src: main/java/org/apache/sling/pipes/ main/java/org/apache/sling/pipes/impl/ test/java/org/apache/sling/pipes/ test/resources/

Author: rombert
Date: Tue Sep  6 11:46:23 2016
New Revision: 1759412

URL: http://svn.apache.org/viewvc?rev=1759412&view=rev
Log:
SLING-6032 - Not sling pipe
SLING-5818 - Make sling pipe writer a persistent configuration

Added files mistakenly out from previous commits.

Submitted-By: Nicolas Peltier

Added:
    sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java
    sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/NotPipe.java
    sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/OutputWriter.java
    sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java
    sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java
    sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/NotPipeTest.java
    sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java
    sling/trunk/contrib/extensions/sling-pipes/src/test/resources/reference.json

Added: sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,63 @@
+/*
+ * 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;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.io.JSONWriter;
+
+import java.io.IOException;
+
+/**
+ * default output writer with size and output resources' path
+ */
+public class DefaultOutputWriter implements OutputWriter {
+
+    protected JSONWriter writer;
+
+    protected Pipe pipe;
+
+    @Override
+    public boolean handleRequest(SlingHttpServletRequest request) {
+        return true;
+    }
+
+    @Override
+    public void init(SlingHttpServletRequest request, SlingHttpServletResponse response, Pipe pipe) throws IOException, JSONException {
+        response.setCharacterEncoding("utf-8");
+        response.setContentType("application/json");
+        writer = new JSONWriter(response.getWriter());
+        this.pipe = pipe;
+        writer.object();
+        writer.key(KEY_ITEMS);
+        writer.array();
+    }
+
+    @Override
+    public void writeItem(Resource resource) throws JSONException {
+        writer.value(resource.getPath());
+    }
+
+    @Override
+    public void ends(int size) throws JSONException {
+        writer.endArray();
+        writer.key(KEY_SIZE).value(size);
+        writer.endObject();
+    }
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/NotPipe.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/NotPipe.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/NotPipe.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/NotPipe.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+import org.apache.sling.api.resource.Resource;
+
+import java.util.Collections;
+import java.util.Iterator;
+
+/**
+ * executes a pipe referred in the configuration, but invert output:
+ * nothing if the pipe has something, input if the pipe has nothing
+ */
+public class NotPipe extends ReferencePipe {
+
+    public static final String RESOURCE_TYPE = "slingPipes/not";
+
+    public NotPipe(Plumber plumber, Resource resource) throws Exception {
+        super(plumber, resource);
+    }
+
+    @Override
+    public Iterator<Resource> getOutput() {
+        if (reference.getOutput().hasNext()){
+            return EMPTY_ITERATOR;
+        }
+        return Collections.singleton(getInput()).iterator();
+    }
+}

Added: sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/OutputWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/OutputWriter.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/OutputWriter.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/OutputWriter.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,58 @@
+/*
+ * 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;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.commons.json.JSONException;
+
+import java.io.IOException;
+
+/**
+ * defines how pipe's output get written to a servlet response
+ */
+public interface OutputWriter {
+
+    String KEY_SIZE = "size";
+
+    String KEY_ITEMS = "items";
+
+    /**
+     *
+     * @param request
+     * @return
+     */
+    boolean handleRequest(SlingHttpServletRequest request);
+
+    /**
+     * Init the writer
+     * @param response
+     */
+    void init(SlingHttpServletRequest request, SlingHttpServletResponse response, Pipe pipe) throws IOException, JSONException;
+
+    /**
+     * Write a given resource
+     * @param resource
+     */
+    void writeItem(Resource resource) throws JSONException;
+
+    /**
+     * ends write
+     */
+    void ends(int size) throws JSONException;
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,53 @@
+/*
+ * 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.impl;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.commons.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * same thing as CustomWriter, but uses a JSON object passed as writer parameter
+ */
+public class CustomJsonWriter extends CustomWriter {
+
+    private static final Logger log = LoggerFactory.getLogger(CustomJsonWriter.class);
+
+    @Override
+    public boolean handleRequest(SlingHttpServletRequest request) {
+        String writerParam = request.getParameter(PARAM_WRITER);
+        if (StringUtils.isNotBlank(writerParam)){
+            try {
+                JSONObject object = new JSONObject(writerParam);
+                customOutputs = new HashMap<>();
+                for (Iterator<String> keys = object.keys(); keys.hasNext();){
+                    String key = keys.next();
+                    customOutputs.put(key, object.getString(key));
+                }
+                return true;
+            } catch(Exception e){
+                log.error("requested json writer can't be parsed", e);
+            }
+        }
+        return false;
+    }
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,64 @@
+/*
+ * 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.impl;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.pipes.DefaultOutputWriter;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * writes current resource, dubbing a given child resource "writer" property/value pairs, allowing expressions
+ */
+public class CustomWriter extends DefaultOutputWriter {
+    public static final String PATH_KEY = "path";
+
+    public static final String PARAM_WRITER = "writer";
+
+    public static final String[] IGNORED_KEYS = {"jcr:primaryType"};
+
+    Map<String, Object> customOutputs;
+
+    @Override
+    public boolean handleRequest(SlingHttpServletRequest request) {
+        Resource resource = request.getResource().getChild(PARAM_WRITER);
+        if (resource != null){
+            customOutputs = new HashMap<>();
+            customOutputs.putAll(resource.adaptTo(ValueMap.class));
+            for (String ignoredKey : IGNORED_KEYS){
+                customOutputs.remove(ignoredKey);
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public void writeItem(Resource resource) throws JSONException {
+        writer.object();
+        writer.key(PATH_KEY).value(resource.getPath());
+        for (Map.Entry<String, Object> entry : customOutputs.entrySet()){
+            Object o = pipe.getBindings().instantiateObject((String)entry.getValue());
+            writer.key(entry.getKey()).value(o);
+        }
+        writer.endObject();
+    }
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/NotPipeTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/NotPipeTest.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/NotPipeTest.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/NotPipeTest.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+
+public class NotPipeTest extends AbstractPipeTest {
+
+    @Before
+    public void setUp() throws Exception {
+        context.load().json("/reference.json", PATH_PIPE);
+    }
+
+    @Test
+    public void testTrue() throws Exception {
+        assertFalse("working referred pipe should make not pipe fail", getOutput(PATH_PIPE + "/not").hasNext());
+    }
+
+    @Test
+    public void testFalse() throws Exception {
+        //not working referred pipe should stream input of the not pipe
+        testOneResource(PATH_PIPE + "/notfailure", PATH_APPLE);
+    }
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java Tue Sep  6 11:46:23 2016
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+
+/**
+ * testing references
+ */
+public class ReferencePipeTest  extends AbstractPipeTest {
+
+    @Before
+    public void setUp() throws Exception {
+        context.load().json("/reference.json", PATH_PIPE);
+    }
+
+    @Test
+    public void testSimple() throws Exception {
+        testOneResource(PATH_PIPE + "/" + NN_SIMPLE, PATH_APPLE);
+    }
+
+    @Test
+    public void testRefersFailure() throws Exception {
+        assertFalse("Reference a pipe with no result shouldn't have any result", getOutput(PATH_PIPE + "/refersfailure").hasNext());
+    }
+
+    @Test
+    public void testPathBinding() throws Exception {
+        testOneResource(PATH_PIPE + "/testPathBinding", PATH_APPLE + "/isnota/carrot");
+    }
+
+    @Test
+    public void testValueBinding() throws Exception {
+        testOneResource(PATH_PIPE + "/isAppleWormy", PATH_APPLE);
+        assertFalse("Banana should be filtered out", getOutput(PATH_PIPE + "/isBananaWormy").hasNext());
+    }
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/sling-pipes/src/test/resources/reference.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/test/resources/reference.json?rev=1759412&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/test/resources/reference.json (added)
+++ sling/trunk/contrib/extensions/sling-pipes/src/test/resources/reference.json Tue Sep  6 11:46:23 2016
@@ -0,0 +1,105 @@
+{
+  "jcr:primaryType": "nt:unstructured",
+  "exist": {
+    "jcr:primaryType": "nt:unstructured",
+    "path":"/content/fruits/apple",
+    "sling:resourceType": "slingPipes/base"
+  },
+  "doesntexist": {
+    "jcr:primaryType": "nt:unstructured",
+    "path":"/content/fruits/blueapple",
+    "sling:resourceType": "slingPipes/base"
+  },
+  "expectPathBinding": {
+    "jcr:primaryType": "nt:unstructured",
+    "path":"${path.fruit}/isnota/carrot",
+    "sling:resourceType": "slingPipes/base"
+  },
+  "filtersWormyFruit":{
+    "jcr:primaryType": "nt:unstructured",
+    "sling:resourceType": "slingPipes/filter",
+    "conf":{
+      "jcr:primaryType": "nt:unstructured",
+      "slingPipesFilter_test":"${fruit.worm === 'true'}"
+    }
+  },
+  "simple": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "references a pipe with results",
+    "expr": "/etc/pipe/exist",
+    "sling:resourceType": "slingPipes/reference"
+  },
+  "refersfailure": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "references a pipe with results",
+    "expr": "/etc/pipe/doesntexist",
+    "sling:resourceType": "slingPipes/reference"
+  },
+  "not": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "references a pipe with results",
+    "expr": "/etc/pipe/exist",
+    "sling:resourceType": "slingPipes/not"
+  },
+  "notfailure": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "references a pipe with results",
+    "path": "/content/fruits/apple",
+    "expr": "/etc/pipe/doesntexist",
+    "sling:resourceType": "slingPipes/not"
+  },
+  "testPathBinding": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "container with reference as second pipe, using first's path",
+    "sling:resourceType": "slingPipes/container",
+    "conf":{
+      "jcr:primaryType": "sling:OrderedFolder",
+      "fruit":{
+        "jcr:primaryType": "nt:unstructured",
+        "path":"/content/fruits/apple",
+        "sling:resourceType": "slingPipes/base"
+      },
+      "ref":{
+        "jcr:primaryType": "nt:unstructured",
+        "expr":"/etc/pipe/expectPathBinding",
+        "sling:resourceType": "slingPipes/reference"
+      }
+    }
+  },
+  "isAppleWormy": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "container with reference as second pipe, using first's output binding",
+    "sling:resourceType": "slingPipes/container",
+    "conf":{
+      "jcr:primaryType": "sling:OrderedFolder",
+      "fruit":{
+        "jcr:primaryType": "nt:unstructured",
+        "path":"/content/fruits/apple",
+        "sling:resourceType": "slingPipes/base"
+      },
+      "ref":{
+        "jcr:primaryType": "nt:unstructured",
+        "expr":"/etc/pipe/filtersWormyFruit",
+        "sling:resourceType": "slingPipes/reference"
+      }
+    }
+  },
+  "isBananaWormy": {
+    "jcr:primaryType": "nt:unstructured",
+    "jcr:description": "container with reference as second pipe, using first's output binding",
+    "sling:resourceType": "slingPipes/container",
+    "conf":{
+      "jcr:primaryType": "sling:OrderedFolder",
+      "fruit":{
+        "jcr:primaryType": "nt:unstructured",
+        "path":"/content/fruits/banana",
+        "sling:resourceType": "slingPipes/base"
+      },
+      "ref":{
+        "jcr:primaryType": "nt:unstructured",
+        "expr":"/etc/pipe/filtersWormyFruit",
+        "sling:resourceType": "slingPipes/reference"
+      }
+    }
+  }
+}
\ No newline at end of file