Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: how to get versioned links and nodes copied from email exchange

...

Now you should be able to see an HTML version of the resource at http://localhost.local:8080/content/greet. This script matches the sling:resourceType we set and the HTTP method we used. Note that resourceType matches must be exact.

Scripts and Servlets

How do I generate links to previous versions of a node?

Assuming a versionable node at /content/versioned, with sling:resourceType=foo, here's the /apps/foo/html.esp script that handles the /content/versioned.html request:

Code Block

<html>

<%
// assume we have a versionable node
var iter = currentNode.getVersionHistory().getAllVersions();
%>

  <body>
    <h1>Versions of node <%= currentNode.getPath() %></h1>
    <%
    	while(iter.hasNext()) {
    	  var v = iter.nextVersion();

    	  // use UUID of version node to build a link, and add a .version
    	  // selector to have another esp script process that request
    	  var uuid = v["jcr:uuid"];
    	  var vPath = currentNode.getPath() + ".version." + uuid + ".html";

    	  // Use Version creation date as the link text
    	  var vDate = v.getCreated().getTime();

    	  %>
    	  <a href="<%= vPath %>"><%= vDate %></a><br/>
    	  <%
    	}
    %>
  </body>
</html>

The links to the individual versions look like: /content/versioned.version.313016e1.html where the first .version. selector causes a different esp script to be called to display the version data, and the 313016e1 selector is the UUID of the versioned node (real UUIDs are longer).

That request is handled by this second script, /apps/foo/version/html.esp (name will change soon, SLING-387):

Code Block

<html>

<%
	// Get version node UUID, which is the second selector
	var uuid = null;
	var sel = request.getRequestPathInfo().getSelectors();
	if(sel.length >= 2) {
		uuid = sel[1];
	} else {
		response.sendError(400, "Version node UUID must be given as second selector");
	}

	// Get version node
	var v = currentNode.getSession().getNodeByUUID(uuid);
	var frozen = v.getNode("jcr:frozenNode");
	var title = frozen.title;		
%>

  <body>
    <h1>Version of node <%= currentNode.getPath() %></h1>
    Name: <b><%= v.getName() %></b><br/>
    UUID: <b><%= uuid %></b><br/>
    Path: <b><%= v.getPath() %></b><br/>
    Frozen node path: <b><%= frozen.getPath() %></b><br/>

    <% if(title) { %>
	    Frozen node title: <b><%= frozen.getProperty("title") %></b><br/>
    <% } else { %>
    	Frozen node does not have a title property
    <% } %>
  </body>
</html>

Which uses the UUID selector to retrieve the versioned node.

The second trick here is that the versioned data is saved as a "jcr:frozenNode" node under the Version node. This is explained for example at http://www.onjava.com/lpt/a/6784Image Added .

How do I find out why a given script or servlet is preferred to another when processing a request?

...