Author: npeltier
Date: Tue Jul 11 08:24:55 2017
New Revision: 1801566
URL: http://svn.apache.org/viewvc?rev=1801566&view=rev
Log:
SLING-6998 use ResourceUtil for path creation
also added some unit tests for PathPipe
Added:
sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java
Modified:
sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
Modified:
sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/internal/PathPipe.java?rev=1801566&r1=1801565&r2=1801566&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
(original)
+++
sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/internal/PathPipe.java
Tue Jul 11 08:24:55 2017
@@ -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;
Added:
sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java?rev=1801566&view=auto
==============================================================================
---
sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java
(added)
+++
sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/internal/PathPipeTest.java
Tue Jul 11 08:24:55 2017
@@ -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