You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by to...@apache.org on 2015/02/10 09:32:48 UTC

svn commit: r1658634 - in /sling/trunk: bundles/servlets/post/ bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/ bundles/servlets/post/src/main/java/org/apache/s...

Author: tomekr
Date: Tue Feb 10 08:32:47 2015
New Revision: 1658634

URL: http://svn.apache.org/r1658634
Log:
SLING-4318 Support for :operation=restore

Added:
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/RestoreOperation.java
Modified:
    sling/trunk/bundles/servlets/post/pom.xml
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/Modification.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ModificationType.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostConstants.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
    sling/trunk/launchpad/builder/src/main/bundles/list.xml
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletVersionableTest.java

Modified: sling/trunk/bundles/servlets/post/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/pom.xml?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/pom.xml (original)
+++ sling/trunk/bundles/servlets/post/pom.xml Tue Feb 10 08:32:47 2015
@@ -59,7 +59,7 @@
                 <configuration>
                     <instructions>
                         <Export-Package>
-                            org.apache.sling.servlets.post;version=2.2.0
+                            org.apache.sling.servlets.post;version=2.3.0
                         </Export-Package>
                         <Private-Package>
                             org.apache.sling.servlets.post.impl.*

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/Modification.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/Modification.java?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/Modification.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/Modification.java Tue Feb 10 08:32:47 2015
@@ -126,6 +126,10 @@ public class Modification {
         return onChange(ModificationType.CHECKOUT, path, null);
     }
 
+    public static Modification onRestore(String path, String version) {
+        return onChange(ModificationType.RESTORE, path, version);
+    }
+
     public String toString() {
         StringBuilder builder = new StringBuilder();
         builder.append("Modification[type=").append(type).append(", source=").append(source);

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ModificationType.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ModificationType.java?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ModificationType.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ModificationType.java Tue Feb 10 08:32:47 2015
@@ -68,5 +68,11 @@ public enum ModificationType {
     /**
      * A Node has been checked in. The source path provides the path of the node.
      */
-    CHECKIN
+    CHECKIN,
+    
+    /**
+     * A Node has been restored to a given version. The soruce path provides the
+     * path of the node and the destination describes the target version.
+     */
+    RESTORE
 }

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostConstants.java?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostConstants.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostConstants.java Tue Feb 10 08:32:47 2015
@@ -145,6 +145,19 @@ public interface SlingPostConstants {
     public static final String OPERATION_CHECKOUT = "checkout";
 
     /**
+     * Name of the predefined restore operation (value is "restore").
+     * <p>
+     * The restore operation requires the {@link #RP_VERSION} request parameter
+     * denoting the name or the label of the version to be restored.
+     * <p>
+     * If the {@link #RP_APPLY_TO} parameter is set the resources listed in that
+     * parameter are restored instead of the request resource. The
+     * {@link #RP_REMOVE_EXISTING} parameter may be set to true to force
+     * the operation even if there is a collision.
+     */
+    public static final String OPERATION_RESTORE = "restore";
+
+    /**
      * Name of the predefined import operation (value is "import").
      *
      * <p>
@@ -205,6 +218,23 @@ public interface SlingPostConstants {
     public static final String RP_ORDER = RP_PREFIX + "order";
 
     /**
+     * Name of the request parameter indicating whether the nodes existing
+     * outside the versioning graph should be removed. See
+     * {@link javax.jcr.version.VersionManager#restore(javax.jcr.version.Version, boolean)
+     * VersionManager#restore()} for more info.
+     * <p>
+     * This request parameter is optional and can be used by the {@link #OPERATION_RESTORE}.
+     */
+    public static final String RP_REMOVE_EXISTING = RP_PREFIX + "removeExisting";
+
+    /**
+     * Name of the request parameter indicating the name or the label of the
+     * resource version. This request parameter is required by the
+     * {@link #OPERATION_RESTORE}.
+     */
+    public static final String RP_VERSION = RP_PREFIX + "version";
+
+    /**
      * Possible value of the {@link #RP_ORDER} parameter indicating that the
      * node by moved to the first position amongst its sibblings (value is
      * "first").

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java Tue Feb 10 08:32:47 2015
@@ -68,6 +68,7 @@ import org.apache.sling.servlets.post.im
 import org.apache.sling.servlets.post.impl.operations.ModifyOperation;
 import org.apache.sling.servlets.post.impl.operations.MoveOperation;
 import org.apache.sling.servlets.post.impl.operations.NopOperation;
+import org.apache.sling.servlets.post.impl.operations.RestoreOperation;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
@@ -401,6 +402,8 @@ public class SlingPostServlet extends Sl
         providedServices.add(registerOperation(bundleContext,
             SlingPostConstants.OPERATION_CHECKOUT, new CheckoutOperation()));
         providedServices.add(registerOperation(bundleContext,
+                SlingPostConstants.OPERATION_RESTORE, new RestoreOperation()));
+        providedServices.add(registerOperation(bundleContext,
             SlingPostConstants.OPERATION_IMPORT, importOperation));
 
         internalOperations = providedServices.toArray(new ServiceRegistration[providedServices.size()]);

Added: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/RestoreOperation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/RestoreOperation.java?rev=1658634&view=auto
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/RestoreOperation.java (added)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/RestoreOperation.java Tue Feb 10 08:32:47 2015
@@ -0,0 +1,91 @@
+/*
+ * 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.impl.operations;
+
+import java.util.Iterator;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.version.Version;
+import javax.jcr.version.VersionHistory;
+import javax.jcr.version.VersionManager;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.servlets.post.AbstractPostOperation;
+import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.PostResponse;
+import org.apache.sling.servlets.post.SlingPostConstants;
+
+/**
+ * The <code>RestoreOperation</code> class implements the
+ * {@link org.apache.sling.servlets.post.SlingPostConstants#OPERATION_RESTORE restore}
+ * operation for the Sling default POST servlet.
+ */
+public class RestoreOperation extends AbstractPostOperation {
+
+    @Override
+    protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes)
+            throws RepositoryException {
+        final String version = request.getParameter(SlingPostConstants.RP_VERSION);
+        if (version == null || version.length() == 0) {
+            throw new IllegalArgumentException("Unable to process restore. Missing version");
+        }
+        final String removeString = request.getParameter(SlingPostConstants.RP_REMOVE_EXISTING);
+        final boolean removeExisting = Boolean.parseBoolean(removeString);
+
+        Iterator<Resource> res = getApplyToResources(request);
+        if (res == null) {
+            Resource resource = request.getResource();
+            Node node = resource.adaptTo(Node.class);
+            if (node == null) {
+                response.setStatus(HttpServletResponse.SC_NOT_FOUND,
+                    "Missing source " + resource + " for restore");
+                return;
+            }
+            restore(node, version, removeExisting);
+            changes.add(Modification.onRestore(resource.getPath(), version));
+        } else {
+            while (res.hasNext()) {
+                Resource resource = res.next();
+                Node node = resource.adaptTo(Node.class);
+                if (node != null) {
+                    restore(node, version, removeExisting);
+                    changes.add(Modification.onRestore(resource.getPath(), version));
+                }
+            }
+        }
+    }
+
+    private void restore(Node node, String versionSpecifier, boolean removeExisting)
+            throws RepositoryException {
+        final VersionManager vm = node.getSession().getWorkspace().getVersionManager();
+        final VersionHistory history = vm.getVersionHistory(node.getPath());
+        final Version version;
+        if (history.hasVersionLabel(versionSpecifier)) {
+            version = history.getVersionByLabel(versionSpecifier);
+        } else if (history.hasNode(versionSpecifier)) {
+            version = history.getVersion(versionSpecifier);
+        } else {
+            throw new IllegalArgumentException("Unable to process restore. Invalid version: "
+                    + versionSpecifier);
+        }
+        vm.restore(version, removeExisting);
+    }
+}

Modified: sling/trunk/launchpad/builder/src/main/bundles/list.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/builder/src/main/bundles/list.xml?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/launchpad/builder/src/main/bundles/list.xml (original)
+++ sling/trunk/launchpad/builder/src/main/bundles/list.xml Tue Feb 10 08:32:47 2015
@@ -225,7 +225,7 @@
         <bundle>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.servlets.post</artifactId>
-            <version>2.3.6</version>
+            <version>2.3.7-SNAPSHOT</version>
         </bundle>
         <bundle>
             <groupId>org.apache.sling</groupId>

Modified: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletVersionableTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletVersionableTest.java?rev=1658634&r1=1658633&r2=1658634&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletVersionableTest.java (original)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletVersionableTest.java Tue Feb 10 08:32:47 2015
@@ -18,6 +18,7 @@ package org.apache.sling.launchpad.webap
 
 import java.io.IOException;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -240,6 +241,32 @@ public class PostServletVersionableTest
 
     }
 
+    public void testRestoreVersion() throws IOException {
+        final String location = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, params);
+        testClient.createNode(location, Collections.singletonMap("key", "valueForVersion1.0"));
+        testClient.createNode(location, Collections.singletonMap(":operation", "checkin"));
+
+        testClient.createNode(location, Collections.singletonMap(":operation", "checkout"));
+        testClient.createNode(location, Collections.singletonMap("key", "valueForVersion1.1"));
+        testClient.createNode(location, Collections.singletonMap(":operation", "checkin"));
+
+        assertTrue(getContent(location + ".txt", CONTENT_TYPE_PLAIN).contains("key: valueForVersion1.1"));
+
+        params.clear();
+        params.put(":operation", "restore");
+        params.put(":version", "1.0");
+        testClient.createNode(location, params);
+
+        assertTrue(getContent(location + ".txt", CONTENT_TYPE_PLAIN).contains("key: valueForVersion1.0"));
+
+        params.clear();
+        params.put(":operation", "restore");
+        params.put(":version", "1.1");
+        testClient.createNode(location, params);
+
+        assertTrue(getContent(location + ".txt", CONTENT_TYPE_PLAIN).contains("key: valueForVersion1.1"));
+    }
+
     public void testCheckingOutACheckedInNode() throws IOException {
         params.put(":checkinNewVersionableNodes", "true");
         final String location = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, params);