You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2021/08/16 03:26:25 UTC
[sling-org-apache-sling-app-cms] branch master updated: SLING-10731
- Adding a post operation to checkin the resource after it is restored
This is an automated email from the ASF dual-hosted git repository.
dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git
The following commit(s) were added to refs/heads/master by this push:
new c833e05 SLING-10731 - Adding a post operation to checkin the resource after it is restored
new 3610779 Merge branch 'master' of https://github.com/apache/sling-org-apache-sling-app-cms
c833e05 is described below
commit c833e05874c0d9fa9393e874d2cabb04d6d3c329
Author: Dan Klco <kl...@adobe.com>
AuthorDate: Sun Aug 15 23:26:00 2021 -0400
SLING-10731 - Adding a post operation to checkin the resource after it is restored
---
.../internal/operations/CheckoutPostOperation.java | 60 +++++++++++++
.../operations/CheckoutPostOperationTest.java | 97 ++++++++++++++++++++++
.../cms/versionmanager/versionmanager.jsp | 1 +
3 files changed, 158 insertions(+)
diff --git a/core/src/main/java/org/apache/sling/cms/core/internal/operations/CheckoutPostOperation.java b/core/src/main/java/org/apache/sling/cms/core/internal/operations/CheckoutPostOperation.java
new file mode 100644
index 0000000..971e3c7
--- /dev/null
+++ b/core/src/main/java/org/apache/sling/cms/core/internal/operations/CheckoutPostOperation.java
@@ -0,0 +1,60 @@
+/*
+ * 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.cms.core.internal.operations;
+
+import java.util.List;
+
+import javax.jcr.Node;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.SlingPostProcessor;
+import org.osgi.service.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The <code>CheckoutPostOperation</code> service will checkout the node after
+ * it is restored so it can be edited.
+ */
+@Component(immediate = true, service = { SlingPostProcessor.class })
+public class CheckoutPostOperation implements SlingPostProcessor {
+
+ private static final Logger log = LoggerFactory.getLogger(CheckoutPostOperation.class);
+
+ private static final String PN_CHECKOUT = ":checkoutPostOp";
+
+ @Override
+ public void process(SlingHttpServletRequest request, final List<Modification> changes) throws Exception {
+ if ("true".equals(request.getParameter(PN_CHECKOUT))) {
+ Resource resource = request.getResource();
+ Node node = resource.adaptTo(Node.class);
+
+ if (node == null) {
+ log.warn("Resource {} not backed by Node", resource);
+ return;
+ }
+
+ log.debug("Checking out node {}", node.getPath());
+ node.getSession().getWorkspace().getVersionManager().checkout(node.getPath());
+ changes.add(Modification.onCheckout(resource.getPath()));
+ }
+
+ }
+
+}
diff --git a/core/src/test/java/org/apache/sling/cms/core/internal/operations/CheckoutPostOperationTest.java b/core/src/test/java/org/apache/sling/cms/core/internal/operations/CheckoutPostOperationTest.java
new file mode 100644
index 0000000..c9695d6
--- /dev/null
+++ b/core/src/test/java/org/apache/sling/cms/core/internal/operations/CheckoutPostOperationTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.cms.core.internal.operations;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+import javax.jcr.Workspace;
+import javax.jcr.version.VersionManager;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class CheckoutPostOperationTest {
+
+ @Rule
+ public SlingContext context = new SlingContext();
+
+ private CheckoutPostOperation operation = new CheckoutPostOperation();
+
+ @Test
+ public void testNotApplicable() throws Exception {
+
+ context.request().addRequestParameter(":checkoutPostOp", "false");
+
+ List<Modification> changes = new ArrayList<>();
+
+ operation.process(context.request(), changes);
+
+ assertEquals(0, changes.size());
+
+ }
+
+ @Test
+ public void testNotNode() throws Exception {
+ Resource resource = mock(Resource.class);
+ context.currentResource(resource);
+ context.request().addRequestParameter(":checkoutPostOp", "true");
+ List<Modification> changes = new ArrayList<>();
+ operation.process(context.request(), changes);
+
+ verify(resource).adaptTo(Node.class);
+ assertEquals(0, changes.size());
+ }
+
+ @Test
+ public void testCheckout() throws Exception {
+ Resource resource = mock(Resource.class);
+ context.currentResource(resource);
+
+ Node node = mock(Node.class);
+ when(node.getPath()).thenReturn("/content");
+ when(resource.adaptTo(Node.class)).thenReturn(node);
+
+ Session session = mock(Session.class);
+ when(node.getSession()).thenReturn(session);
+
+ Workspace workspace = mock(Workspace.class);
+ when(session.getWorkspace()).thenReturn(workspace);
+
+ VersionManager versionManager = mock(VersionManager.class);
+ when(workspace.getVersionManager()).thenReturn(versionManager);
+
+ context.request().addRequestParameter(":checkoutPostOp", "true");
+ List<Modification> changes = new ArrayList<>();
+ operation.process(context.request(), changes);
+
+ assertEquals(1, changes.size());
+
+ verify(versionManager).checkout("/content");
+ }
+
+}
diff --git a/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp b/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp
index f20b8b3..c57862b 100644
--- a/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp
@@ -87,6 +87,7 @@
<form method="post" action="${sling:encode(slingRequest.requestPathInfo.suffix,'HTML_ATTR')}" enctype="multipart/form-data" class="Form-Ajax" data-add-date="false">
<fieldset class="form-wrapper field">
<input type="hidden" name=":operation" value="restore" />
+ <input type="hidden" name=":checkoutPostOp" value="true" />
<input type="hidden" name=":version" value="${version.name}" />
<div class="Field-Group">
<fmt:message key="Restore the content to {0}" var="restoreMessage">