From: Mathieu Baudier Date: Fri, 18 May 2012 15:43:17 +0000 (+0000) Subject: Improve JCR utilities X-Git-Tag: argeo-commons-2.1.30~937 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=690ded6357e5db68db0e7f508b588b27af65839d;p=lgpl%2Fargeo-commons.git Improve JCR utilities git-svn-id: https://svn.argeo.org/commons/trunk@5305 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index e51ac40cc..7b99f0272 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -42,7 +42,6 @@ import javax.jcr.NamespaceRegistry; import javax.jcr.NoSuchWorkspaceException; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.PathNotFoundException; import javax.jcr.Property; import javax.jcr.PropertyIterator; import javax.jcr.PropertyType; @@ -51,7 +50,6 @@ import javax.jcr.RepositoryException; import javax.jcr.RepositoryFactory; import javax.jcr.Session; import javax.jcr.Value; -import javax.jcr.ValueFormatException; import javax.jcr.Workspace; import javax.jcr.nodetype.NodeType; import javax.jcr.observation.EventListener; @@ -69,6 +67,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; +import org.argeo.util.security.DigestUtils; import org.argeo.util.security.SimplePrincipal; /** Utility methods to simplify common JCR operations. */ @@ -1006,6 +1005,23 @@ public class JcrUtils implements ArgeoJcrConstants { } } + /** Computes the checksum of an nt:file */ + public static String checksumFile(Node fileNode, String algorithm) { + Binary data = null; + InputStream in = null; + try { + data = fileNode.getNode(Node.JCR_CONTENT) + .getProperty(Property.JCR_DATA).getBinary(); + in = data.getStream(); + return DigestUtils.digest(algorithm, in); + } catch (RepositoryException e) { + throw new ArgeoException("Cannot checksum file " + fileNode, e); + } finally { + IOUtils.closeQuietly(in); + closeQuietly(data); + } + } + /** * Creates depth from a string (typically a username) by adding levels based * on its first characters: "aBcD",2 => a/aB