You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2017/01/24 16:24:47 UTC

svn commit: r1780088 - in /sling/trunk/bundles/servlets/post/src: main/java/org/apache/sling/servlets/post/AbstractPostOperation.java test/java/org/apache/sling/servlets/post/AbstractPostOperationTest.java

Author: justin
Date: Tue Jan 24 16:24:47 2017
New Revision: 1780088

URL: http://svn.apache.org/viewvc?rev=1780088&view=rev
Log:
SLING-6187 - check modification list to ensure that no changes include an @postfix where the base path is also present

Added:
    sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/AbstractPostOperationTest.java
Modified:
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractPostOperation.java

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractPostOperation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractPostOperation.java?rev=1780088&r1=1780087&r2=1780088&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractPostOperation.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractPostOperation.java Tue Jan 24 16:24:47 2017
@@ -18,9 +18,12 @@ package org.apache.sling.servlets.post;
 
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
 
@@ -30,6 +33,7 @@ import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.resource.Resource;
@@ -99,6 +103,32 @@ public abstract class AbstractPostOperat
                 }
             }
 
+            // check modifications for remaining postfix and store the base path
+            final Map<String, String> modificationSourcesContainingPostfix = new HashMap<String, String>();
+            final Set<String> allModificationSources = new HashSet<String>(changes.size());
+            for (final Modification modification : changes) {
+                final String source = modification.getSource();
+                if (source != null) {
+                    allModificationSources.add(source);
+                    final int atIndex = source.indexOf('@');
+                    if (atIndex > 0) {
+                        modificationSourcesContainingPostfix.put(source.substring(0, atIndex), source);
+                    }
+                }
+            }
+
+            // fail if any of the base paths (before the postfix) which had a postfix are contained in the modification set
+            if (modificationSourcesContainingPostfix.size() > 0) {
+                for (final Map.Entry<String, String> sourceToCheck : modificationSourcesContainingPostfix.entrySet()) {
+                    if (allModificationSources.contains(sourceToCheck.getKey())) {
+                        response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED,
+                                "Postfix-containing path " + sourceToCheck.getValue() +
+                                " contained in the modification list. Check configuration.");
+                        return;
+                    }
+                }
+            }
+
             final Set<String> nodesToCheckin = new LinkedHashSet<String>();
 
             // set changes on html response

Added: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/AbstractPostOperationTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/AbstractPostOperationTest.java?rev=1780088&view=auto
==============================================================================
--- sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/AbstractPostOperationTest.java (added)
+++ sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/AbstractPostOperationTest.java Tue Jan 24 16:24:47 2017
@@ -0,0 +1,124 @@
+/*
+ * 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.servlets.post;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.commons.testing.sling.MockResourceResolver;
+import org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request;
+import org.junit.Test;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+public class AbstractPostOperationTest {
+
+    @Test
+    public void testRemainingPostfixCausesFailure() {
+        TestingResourceResolver resourceResolver = new TestingResourceResolver();
+
+        MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
+        request.setResourceResolver(resourceResolver);
+
+        final PostOperation operation = new AbstractPostOperation() {
+            @Override
+            protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
+                changes.add(Modification.onChange(ModificationType.CREATE, "/content/test"));
+                changes.add(Modification.onChange(ModificationType.CREATE, "/content/test@Postfix"));
+            }
+        };
+
+        HtmlResponse response = new HtmlResponse();
+        operation.run(request, response, new SlingPostProcessor[0]);
+        assertFalse(response.isSuccessful());
+        assertFalse(resourceResolver.commitCalled);
+        assertTrue(resourceResolver.revertCalled);
+    }
+
+    @Test
+    public void testNoRemainingPostfixIsSuccessful() {
+        TestingResourceResolver resourceResolver = new TestingResourceResolver();
+
+        MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
+        request.setResourceResolver(resourceResolver);
+
+        final PostOperation operation = new AbstractPostOperation() {
+            @Override
+            protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
+                changes.add(Modification.onChange(ModificationType.CREATE, "/content/test"));
+            }
+        };
+
+        HtmlResponse response = new HtmlResponse();
+        operation.run(request, response, new SlingPostProcessor[0]);
+        assertTrue(response.isSuccessful());
+        assertTrue(resourceResolver.commitCalled);
+        assertFalse(resourceResolver.revertCalled);
+    }
+
+    @Test
+    public void testRemainingPostfixWithoutUnPostfixedIsSuccessful() {
+        TestingResourceResolver resourceResolver = new TestingResourceResolver();
+
+        MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
+        request.setResourceResolver(resourceResolver);
+
+        final PostOperation operation = new AbstractPostOperation() {
+            @Override
+            protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
+                changes.add(Modification.onChange(ModificationType.CREATE, "/content/test@Postfix"));
+            }
+        };
+
+        HtmlResponse response = new HtmlResponse();
+        operation.run(request, response, new SlingPostProcessor[0]);
+        assertTrue(response.isSuccessful());
+        assertTrue(resourceResolver.commitCalled);
+        assertFalse(resourceResolver.revertCalled);
+    }
+
+    private class TestingResourceResolver extends MockResourceResolver {
+        private boolean revertCalled;
+        private boolean commitCalled;
+
+        @Override
+        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+            if (type == Session.class) {
+                return null;
+            } else {
+                return super.adaptTo(type);
+            }
+        }
+
+        @Override
+        public boolean hasChanges() {
+            return !commitCalled;
+        }
+
+        @Override
+        public void commit() {
+            commitCalled = true;
+        }
+
+        @Override
+        public void revert() {
+            revertCalled = true;
+        }
+    }
+}