X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2FJcrUtils.java;h=e7b3656cfd8e5e7e195a873924f65b92c95eefcb;hb=a093e5c849e70bbc754fa4462dbb6c2b59979973;hp=e51ac40cce522efb908827153b3b14d04c9e141c;hpb=394d49c6ca688a96478a9eaad005f0041787cd76;p=lgpl%2Fargeo-commons.git 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..e7b3656cf 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. */ @@ -308,9 +307,14 @@ public class JcrUtils implements ArgeoJcrConstants { * PROPERTIES */ - /** Concisely get the string value of a property */ + /** + * Concisely get the string value of a property or null if this node doesn't + * have this property + */ public static String get(Node node, String propertyName) { try { + if (!node.hasProperty(propertyName)) + return null; return node.getProperty(propertyName).getString(); } catch (RepositoryException e) { throw new ArgeoException("Cannot get property " + propertyName @@ -1006,6 +1010,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 @@ -1530,7 +1551,7 @@ public class JcrUtils implements ArgeoJcrConstants { /** * Add privileges on a path to a {@link Principal}. The path must already - * exist. + * exist. Session is saved. */ public static void addPrivileges(Session session, String path, Principal principal, List privs) @@ -1566,6 +1587,7 @@ public class JcrUtils implements ArgeoJcrConstants { throw new ArgeoException("Don't know how to apply privileges " + privs + " to " + principal + " on " + path); } + session.save(); } }