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 e40cd79207cf21bc959d40e3db86308cde48687a Author: Robert Munteanu <[email protected]> AuthorDate: Tue Sep 6 11:46:23 2016 +0000 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 git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/sling-pipes@1759412 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/sling/pipes/DefaultOutputWriter.java | 63 +++++++++++++ src/main/java/org/apache/sling/pipes/NotPipe.java | 43 +++++++++ .../java/org/apache/sling/pipes/OutputWriter.java | 58 ++++++++++++ .../apache/sling/pipes/impl/CustomJsonWriter.java | 53 +++++++++++ .../org/apache/sling/pipes/impl/CustomWriter.java | 64 +++++++++++++ .../java/org/apache/sling/pipes/NotPipeTest.java | 42 +++++++++ .../org/apache/sling/pipes/ReferencePipeTest.java | 54 +++++++++++ src/test/resources/reference.json | 105 +++++++++++++++++++++ 8 files changed, 482 insertions(+) diff --git a/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java b/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java new file mode 100644 index 0000000..b743899 --- /dev/null +++ b/src/main/java/org/apache/sling/pipes/DefaultOutputWriter.java @@ -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 diff --git a/src/main/java/org/apache/sling/pipes/NotPipe.java b/src/main/java/org/apache/sling/pipes/NotPipe.java new file mode 100644 index 0000000..74f980e --- /dev/null +++ b/src/main/java/org/apache/sling/pipes/NotPipe.java @@ -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(); + } +} diff --git a/src/main/java/org/apache/sling/pipes/OutputWriter.java b/src/main/java/org/apache/sling/pipes/OutputWriter.java new file mode 100644 index 0000000..fe0dabd --- /dev/null +++ b/src/main/java/org/apache/sling/pipes/OutputWriter.java @@ -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 diff --git a/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java b/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java new file mode 100644 index 0000000..89eb072 --- /dev/null +++ b/src/main/java/org/apache/sling/pipes/impl/CustomJsonWriter.java @@ -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 diff --git a/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java b/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java new file mode 100644 index 0000000..1841355 --- /dev/null +++ b/src/main/java/org/apache/sling/pipes/impl/CustomWriter.java @@ -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 diff --git a/src/test/java/org/apache/sling/pipes/NotPipeTest.java b/src/test/java/org/apache/sling/pipes/NotPipeTest.java new file mode 100644 index 0000000..11ac4ec --- /dev/null +++ b/src/test/java/org/apache/sling/pipes/NotPipeTest.java @@ -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 diff --git a/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java b/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java new file mode 100644 index 0000000..9652719 --- /dev/null +++ b/src/test/java/org/apache/sling/pipes/ReferencePipeTest.java @@ -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 diff --git a/src/test/resources/reference.json b/src/test/resources/reference.json new file mode 100644 index 0000000..c533408 --- /dev/null +++ b/src/test/resources/reference.json @@ -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 -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
