This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.pipes-1.0.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git
commit 72eb7bdb94470306a905d1d5f2b380f2e5e86e40 Author: Nicolas Peltier <[email protected]> AuthorDate: Tue Jul 11 08:24:55 2017 +0000 SLING-6998 use ResourceUtil for path creation also added some unit tests for PathPipe git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/sling-pipes@1801566 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/sling/pipes/internal/PathPipe.java | 72 ++++++++-------------- .../apache/sling/pipes/internal/PathPipeTest.java | 57 +++++++++++++++++ 2 files changed, 82 insertions(+), 47 deletions(-) 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 e9f7d49..02c68f9 100644 --- a/src/main/java/org/apache/sling/pipes/internal/PathPipe.java +++ b/src/main/java/org/apache/sling/pipes/internal/PathPipe.java @@ -18,83 +18,61 @@ package org.apache.sling.pipes.internal; import java.util.Collections; import java.util.Iterator; -import java.util.StringTokenizer; - -import javax.jcr.Node; -import javax.jcr.RepositoryException; -import javax.jcr.Session; +import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.pipes.BasePipe; import org.apache.sling.pipes.Plumber; +import org.apache.sling.query.util.IteratorUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +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>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> */ 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 = "nodeType"; + public static final String PN_INTERMEDIATE = "intermediateType"; public static final String PN_AUTOSAVE = "autosave"; + public static final String SLASH = "/"; - String nodeType; - + String resourceType; + String intermediateType; boolean autosave; private final Logger logger = LoggerFactory.getLogger(PathPipe.class); public PathPipe(Plumber plumber, Resource resource) throws Exception { super(plumber, resource); - nodeType = properties.get(PN_NODETYPE, "sling:Folder"); + resourceType = properties.get(PN_RESOURCETYPE, NT_SLING_FOLDER); + intermediateType = properties.get(PN_INTERMEDIATE, NT_SLING_FOLDER); autosave = properties.get(PN_AUTOSAVE, true); } @Override + public boolean modifiesContent() { + return true; + } + + @Override public Iterator<Resource> getOutput() { Iterator<Resource> output = Collections.emptyIterator(); String expression = getExpr(); - Node leaf = null; - boolean transientChange = false; try { - String relativePath = expression.substring(1); - Node parentNode = resolver.adaptTo(Session.class).getRootNode(); - if (!parentNode.hasNode(relativePath)) { - Node node = parentNode; - int pos = relativePath.lastIndexOf('/'); - if (pos != -1) { - final StringTokenizer st = new StringTokenizer(relativePath.substring(0, pos), "/"); - while (st.hasMoreTokens()) { - final String token = st.nextToken(); - if (!node.hasNode(token)) { - try { - node.addNode(token, nodeType); - transientChange = true; - } catch (RepositoryException re) { - // we ignore this as this folder might be created from a different task - node.getSession().refresh(false); - } - } - node = node.getNode(token); - } - relativePath = relativePath.substring(pos + 1); - } - if (!node.hasNode(relativePath)) { - node.addNode(relativePath, nodeType); - transientChange = true; - } - leaf = node.getNode(relativePath); - } - if (leaf == null) { - leaf = parentNode.getNode(relativePath); - } - if (transientChange && autosave){ - resolver.adaptTo(Session.class).save(); - } - output = Collections.singleton(resolver.getResource(leaf.getPath())).iterator(); - } catch (RepositoryException e){ + String path = expression.startsWith(SLASH) ? expression : getInput().getPath() + SLASH + expression; + output = IteratorUtils.singleElementIterator(ResourceUtil.getOrCreateResource(resolver, path, resourceType, intermediateType, autosave)); + } catch (PersistenceException e){ logger.error ("Not able to create path {}", expression, e); } return output; diff --git a/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java b/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java new file mode 100644 index 0000000..daadfb5 --- /dev/null +++ b/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java @@ -0,0 +1,57 @@ +/* + * 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.PersistenceException; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.pipes.AbstractPipeTest; +import org.apache.sling.pipes.Pipe; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Testing path pipe using pipe builder + */ +public class PathPipeTest extends AbstractPipeTest { + + private static final String WATERMELON = "watermelon"; + private static final String WATERMELON_FULL_PATH = PATH_FRUITS + "/" + WATERMELON; + + @Test + public void modifiesContent() throws IllegalAccessException, PersistenceException { + Pipe pipe = plumber.getBuilder(context.resourceResolver()) + .mkdir(PATH_FRUITS + "/whatever") + .build(); + assertTrue("path pipe should be considered as modifying the content", pipe.modifiesContent()); + } + + @Test + public void getClassicOutput() throws Exception { + ResourceResolver resolver = context.resourceResolver(); + plumber.getBuilder(resolver).mkdir(WATERMELON_FULL_PATH).run(); + resolver.revert(); + assertNotNull("Resource should be here & saved", resolver.getResource(WATERMELON_FULL_PATH)); + } + + @Test + public void getRelativePath() throws Exception { + ResourceResolver resolver = context.resourceResolver(); + plumber.getBuilder(resolver).echo(PATH_FRUITS).mkdir(WATERMELON).run(); + assertNotNull("Resource should be here & saved", resolver.getResource(WATERMELON_FULL_PATH)); + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
