import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.Principal;
import java.text.DateFormat;
import java.text.ParseException;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import javax.jcr.Binary;
import javax.jcr.Credentials;
+import javax.jcr.ImportUUIDBehavior;
import javax.jcr.NamespaceRegistry;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.Workspace;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.observation.EventListener;
import javax.jcr.query.Query;
}
}
+ /** Copy the whole workspace via a system view XML. */
+ public static void copyWorkspaceXml(Session fromSession, Session toSession) {
+ Workspace fromWorkspace = fromSession.getWorkspace();
+ Workspace toWorkspace = toSession.getWorkspace();
+ String errorMsg = "Cannot copy workspace " + fromWorkspace + " to " + toWorkspace + " via XML.";
+
+ try (PipedInputStream in = new PipedInputStream(1024 * 1024);) {
+ new Thread(() -> {
+ try (PipedOutputStream out = new PipedOutputStream(in)) {
+ fromSession.exportSystemView("/", out, false, false);
+ out.flush();
+ } catch (IOException e) {
+ throw new RuntimeException(errorMsg, e);
+ } catch (RepositoryException e) {
+ throw new JcrException(errorMsg, e);
+ }
+ }, "Copy workspace" + fromWorkspace + " to " + toWorkspace).start();
+
+ toSession.importXML("/", in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
+ toSession.save();
+ } catch (IOException e) {
+ throw new RuntimeException(errorMsg, e);
+ } catch (RepositoryException e) {
+ throw new JcrException(errorMsg, e);
+ }
+ }
+
/**
* Copies recursively the content of a node to another one. Do NOT copy the
* property values of {@link NodeType#MIX_CREATED} and
if (toNode.getDefinition().isProtected())
return;
+ // add mixins
+ for (NodeType mixinType : fromNode.getMixinNodeTypes()) {
+ try {
+ toNode.addMixin(mixinType.getName());
+ } catch (NoSuchNodeTypeException e) {
+ // ignore unknown mixins
+ // TODO log it
+ }
+ }
+
// process properties
PropertyIterator pit = fromNode.getProperties();
properties: while (pit.hasNext()) {
// update jcr:lastModified and jcr:lastModifiedBy in toNode in case
// they existed, before adding the mixins
- updateLastModified(toNode);
-
- // add mixins
- for (NodeType mixinType : fromNode.getMixinNodeTypes()) {
- toNode.addMixin(mixinType.getName());
- }
+ updateLastModified(toNode, true);
// process children nodes
NodeIterator nit = fromNode.getNodes();
Node toChild;
if (toNode.hasNode(nodeRelPath))
toChild = toNode.getNode(nodeRelPath);
- else
- toChild = toNode.addNode(fromChild.getName(), fromChild.getPrimaryNodeType().getName());
+ else {
+ try {
+ toChild = toNode.addNode(fromChild.getName(), fromChild.getPrimaryNodeType().getName());
+ } catch (NoSuchNodeTypeException e) {
+ // ignore unknown primary types
+ // TODO log it
+ return;
+ }
+ }
copy(fromChild, toChild);
}
} catch (RepositoryException e) {
}
/**
- * If this node is has the {@link NodeType#MIX_LAST_MODIFIED} mixin, it updates
- * the {@link Property#JCR_LAST_MODIFIED} property with the current time and the
- * {@link Property#JCR_LAST_MODIFIED_BY} property with the underlying session
- * user id. In Jackrabbit 2.x,
+ * Checks whether {@link Property#JCR_LAST_MODIFIED} or (afterwards)
+ * {@link Property#JCR_CREATED} are set and returns it as an {@link Instant}.
+ */
+ public static Instant getModified(Node node) {
+ Calendar calendar = null;
+ try {
+ if (node.hasProperty(Property.JCR_LAST_MODIFIED))
+ calendar = node.getProperty(Property.JCR_LAST_MODIFIED).getDate();
+ else if (node.hasProperty(Property.JCR_CREATED))
+ calendar = node.getProperty(Property.JCR_CREATED).getDate();
+ else
+ throw new IllegalArgumentException("No modification time found in " + node);
+ return calendar.toInstant();
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot get modification time for " + node, e);
+ }
+
+ }
+
+ /**
+ * Get {@link Property#JCR_CREATED} as an {@link Instant}, if it is set.
+ */
+ public static Instant getCreated(Node node) {
+ Calendar calendar = null;
+ try {
+ if (node.hasProperty(Property.JCR_CREATED))
+ calendar = node.getProperty(Property.JCR_CREATED).getDate();
+ else
+ throw new IllegalArgumentException("No created time found in " + node);
+ return calendar.toInstant();
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot get created time for " + node, e);
+ }
+
+ }
+
+ /**
+ * Updates the {@link Property#JCR_LAST_MODIFIED} property with the current time
+ * and the {@link Property#JCR_LAST_MODIFIED_BY} property with the underlying
+ * session user id.
+ */
+ public static void updateLastModified(Node node) {
+ updateLastModified(node, false);
+ }
+
+ /**
+ * Updates the {@link Property#JCR_LAST_MODIFIED} property with the current time
+ * and the {@link Property#JCR_LAST_MODIFIED_BY} property with the underlying
+ * session user id. In Jackrabbit 2.x,
* <a href="https://issues.apache.org/jira/browse/JCR-2233">these properties are
* not automatically updated</a>, hence the need for manual update. The session
* is not saved.
*/
- public static void updateLastModified(Node node) {
+ public static void updateLastModified(Node node, boolean addMixin) {
try {
- if (!node.isNodeType(NodeType.MIX_LAST_MODIFIED))
+ if (addMixin && !node.isNodeType(NodeType.MIX_LAST_MODIFIED))
node.addMixin(NodeType.MIX_LAST_MODIFIED);
node.setProperty(Property.JCR_LAST_MODIFIED, new GregorianCalendar());
node.setProperty(Property.JCR_LAST_MODIFIED_BY, node.getSession().getUserID());
* @param untilPath the base path, null is equivalent to "/"
*/
public static void updateLastModifiedAndParents(Node node, String untilPath) {
+ updateLastModifiedAndParents(node, untilPath, true);
+ }
+
+ /**
+ * Update lastModified recursively until this parent.
+ *
+ * @param node the node
+ * @param untilPath the base path, null is equivalent to "/"
+ */
+ public static void updateLastModifiedAndParents(Node node, String untilPath, boolean addMixin) {
try {
if (untilPath != null && !node.getPath().startsWith(untilPath))
throw new IllegalArgumentException(node + " is not under " + untilPath);
- updateLastModified(node);
+ updateLastModified(node, addMixin);
if (untilPath == null) {
if (!node.getPath().equals("/"))
- updateLastModifiedAndParents(node.getParent(), untilPath);
+ updateLastModifiedAndParents(node.getParent(), untilPath, addMixin);
} else {
if (!node.getPath().equals(untilPath))
- updateLastModifiedAndParents(node.getParent(), untilPath);
+ updateLastModifiedAndParents(node.getParent(), untilPath, addMixin);
}
} catch (RepositoryException e) {
throw new JcrException("Cannot update lastModified from " + node + " until " + untilPath, e);
// the new access control list must be applied otherwise this call:
// acl.removeAccessControlEntry(ace); has no effect
acm.setPolicy(path, acl);
+ session.refresh(true);
+ session.save();
}
/*