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 2018/06/01 12:47:11 UTC

[sling-whiteboard] branch master updated: Fixed an issue where the version manager didn't work with a non-published file

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-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new bb03829  Fixed an issue where the version manager didn't work with a non-published file
bb03829 is described below

commit bb038291665cb8543b8adb5aa50efcea877b5c31
Author: Dan Klco <dk...@apache.org>
AuthorDate: Fri Jun 1 08:46:50 2018 -0400

    Fixed an issue where the version manager didn't work with a
    non-published file
---
 cms/core/pom.xml                                   |   6 +
 .../cms/core/operations/CheckpointOperation.java   |  88 ++++++-------
 .../cms/core/servlets/VersionInfoServlet.java      | 142 +++++++++++++++++++++
 cms/ui/src/main/frontend/src/scss/styles.scss      |   5 +
 .../cms/versionmanager/versionmanager.jsp          |   2 +-
 5 files changed, 196 insertions(+), 47 deletions(-)

diff --git a/cms/core/pom.xml b/cms/core/pom.xml
index 7f4ae7e..e9b554c 100644
--- a/cms/core/pom.xml
+++ b/cms/core/pom.xml
@@ -168,5 +168,11 @@
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.jcr.base</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.johnzon</artifactId>
+            <version>1.0.0</version>
+            <scope>provided</scope>
+        </dependency>
 	</dependencies>
 </project>
\ No newline at end of file
diff --git a/cms/core/src/main/java/org/apache/sling/cms/core/operations/CheckpointOperation.java b/cms/core/src/main/java/org/apache/sling/cms/core/operations/CheckpointOperation.java
index 752ed9a..fd4fdbb 100644
--- a/cms/core/src/main/java/org/apache/sling/cms/core/operations/CheckpointOperation.java
+++ b/cms/core/src/main/java/org/apache/sling/cms/core/operations/CheckpointOperation.java
@@ -44,75 +44,71 @@ import org.slf4j.LoggerFactory;
  * depends on the resources being backed up by a JCR node.
  */
 @Component(immediate = true, service = { PostOperation.class }, property = PostOperation.PROP_OPERATION_NAME
-+ "=checkpoint")
+		+ "=checkpoint")
 public class CheckpointOperation implements PostOperation {
-	
-	private static final Logger log = LoggerFactory.getLogger(CheckpointOperation.class);
-	
-
-    public void run(final SlingHttpServletRequest request,
-                    final PostResponse response,
-                    final SlingPostProcessor[] processors) {
-       
-
-        try {
-            // calculate the paths
-            String path = request.getResource().getPath();
-            response.setPath(path);
-
 
-            final List<Modification> changes = new ArrayList<>();
-
-            doRun(request, response, changes);
+	private static final Logger log = LoggerFactory.getLogger(CheckpointOperation.class);
 
-            // invoke processors
-            if (processors != null) {
-                for (SlingPostProcessor processor : processors) {
-                    processor.process(request, changes);
-                }
-            }
+	public void run(final SlingHttpServletRequest request, final PostResponse response,
+			final SlingPostProcessor[] processors) {
 
-            // check modifications for remaining postfix and store the base path
-            final Map<String, String> modificationSourcesContainingPostfix = new HashMap<>();
-            final Set<String> allModificationSources = new HashSet<>(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);
-                    }
-                }
-            }
+		try {
+			// calculate the paths
+			String path = request.getResource().getPath();
+			response.setPath(path);
 
+			final List<Modification> changes = new ArrayList<>();
 
-        } catch (Exception e) {
+			doRun(request, response, changes);
 
-            log.error("Exception during response processing.", e);
-            response.setError(e);
+			// invoke processors
+			if (processors != null) {
+				for (SlingPostProcessor processor : processors) {
+					processor.process(request, changes);
+				}
+			}
+			log.debug("Saving changes...");
+			request.getResourceResolver().commit();
+
+			// check modifications for remaining postfix and store the base path
+			final Map<String, String> modificationSourcesContainingPostfix = new HashMap<>();
+			final Set<String> allModificationSources = new HashSet<>(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);
+					}
+				}
+			}
 
-        } 
-    }
+		} catch (Exception e) {
+			log.error("Exception during response processing.", e);
+			response.setError(e);
+		}
+	}
 
 	protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes)
 			throws PersistenceException {
 		try {
 			Resource resource = request.getResource();
 			Node node = resource.adaptTo(Node.class);
+			
 			if (node == null) {
-				response.setStatus(HttpServletResponse.SC_NOT_FOUND,
-						"Missing source " + resource + " for checkpoint");
+				response.setStatus(HttpServletResponse.SC_NOT_FOUND, "Missing source " + resource + " for checkpoint");
 				return;
 			}
-
+			log.debug("Adding checkpoint for Node {}", node.getPath());
 			node.getSession().getWorkspace().getVersionManager().checkpoint(node.getPath());
+			
 			changes.add(Modification.onCheckin(resource.getPath()));
 			changes.add(Modification.onCheckout(resource.getPath()));
+			
 		} catch (final RepositoryException re) {
 			throw new PersistenceException(re.getMessage(), re);
 		}
 	}
 
-
 }
diff --git a/cms/core/src/main/java/org/apache/sling/cms/core/servlets/VersionInfoServlet.java b/cms/core/src/main/java/org/apache/sling/cms/core/servlets/VersionInfoServlet.java
new file mode 100644
index 0000000..6b427cc
--- /dev/null
+++ b/cms/core/src/main/java/org/apache/sling/cms/core/servlets/VersionInfoServlet.java
@@ -0,0 +1,142 @@
+/*
+ * 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.servlets;
+
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.version.Version;
+import javax.jcr.version.VersionHistory;
+import javax.jcr.version.VersionIterator;
+import javax.json.Json;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The <code>VersionInfoServlet</code> renders list of versions available for
+ * the current resource.
+ *
+ * At the moment only JCR nodes are supported.
+ */
+@Component(service = Servlet.class, property = { "service.description=Sling CMS version info servlet",
+		"service.vendor=The Apache Software Foundation",
+		"sling.servlet.resourceTypes=sling-cms/components/cms/versionmanager", "sling.servlet.methods=GET",
+		"sling.servlet.selectors=VI", "sling.servlet.extensions=json" }, immediate = true)
+public class VersionInfoServlet extends SlingSafeMethodsServlet {
+
+	private static final Logger log = LoggerFactory.getLogger(VersionInfoServlet.class);
+
+	private static final long serialVersionUID = 2980892473913646093L;
+
+	@Override
+	public void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws ServletException, IOException {
+		log.trace("doGet");
+		resp.setContentType(req.getResponseContentType());
+		resp.setCharacterEncoding("UTF-8");
+
+		try {
+			resp.getWriter().write(getJsonObject(req.getRequestPathInfo().getSuffixResource()).toString());
+		} catch (Exception e) {
+			throw new ServletException(e);
+		}
+	}
+
+	private JsonObject getJsonObject(Resource resource) throws RepositoryException {
+		log.debug("Loading version history from {}", resource);
+		final JsonObjectBuilder result = Json.createObjectBuilder();
+		final Node node = resource.adaptTo(Node.class);
+		if (node == null || !node.isNodeType(JcrConstants.MIX_VERSIONABLE)) {
+			return result.build();
+		}
+
+		final VersionHistory history = node.getVersionHistory();
+		final Version baseVersion = node.getBaseVersion();
+		for (final VersionIterator it = history.getAllVersions(); it.hasNext();) {
+			final Version v = it.nextVersion();
+			final JsonObjectBuilder obj = Json.createObjectBuilder();
+			obj.add("created", createdDate(v));
+			obj.add("successors", getArrayBuilder(getNames(v.getSuccessors())));
+			obj.add("predecessors", getArrayBuilder(getNames(v.getPredecessors())));
+
+			obj.add("labels", getArrayBuilder(history.getVersionLabels(v)));
+			obj.add("baseVersion", baseVersion.isSame(v));
+			result.add(v.getName(), obj);
+		}
+
+		return Json.createObjectBuilder().add("versions", result).build();
+	}
+
+	private JsonArrayBuilder getArrayBuilder(String[] values) {
+		JsonArrayBuilder builder = Json.createArrayBuilder();
+
+		for (String value : values) {
+			builder.add(value);
+		}
+
+		return builder;
+	}
+
+	private JsonArrayBuilder getArrayBuilder(Collection<String> values) {
+		JsonArrayBuilder builder = Json.createArrayBuilder();
+
+		for (String value : values) {
+			builder.add(value);
+		}
+
+		return builder;
+	}
+
+	private static Collection<String> getNames(Version[] versions) throws RepositoryException {
+		final List<String> result = new ArrayList<>();
+		for (Version s : versions) {
+			result.add(s.getName());
+		}
+		return result;
+	}
+
+	private static String createdDate(Node node) throws RepositoryException {
+		Property prop = node.getProperty(JcrConstants.JCR_CREATED);
+		if (prop != null) {
+			return new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z").format(prop.getDate().getTime());
+		} else {
+			return "";
+		}
+	}
+
+}
diff --git a/cms/ui/src/main/frontend/src/scss/styles.scss b/cms/ui/src/main/frontend/src/scss/styles.scss
index a2a36fd..ea893de 100644
--- a/cms/ui/src/main/frontend/src/scss/styles.scss
+++ b/cms/ui/src/main/frontend/src/scss/styles.scss
@@ -131,6 +131,11 @@ ul.Breadcrumb  {
   display: none;
 }
 
+.Preview {
+	max-width: 100%;
+	padding:.5em;
+}
+
 .Pull-Right {
 	float:right;
 }
diff --git a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp
index 122fb3a..7748520 100644
--- a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp
+++ b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/versionmanager/versionmanager.jsp
@@ -57,7 +57,7 @@
 				<th>Restore</th>
 			</tr>
 		</thead>
-		<tbody class="fetch-json" data-url="${slingRequest.requestPathInfo.suffix}.V.json" data-template="version-template">
+		<tbody class="fetch-json" data-url="${resource.path}.VI.json${slingRequest.requestPathInfo.suffix}" data-template="version-template">
 			
 		</tbody>
 	</table>

-- 
To stop receiving notification emails like this one, please contact
dklco@apache.org.