/** Writes a {@link Binary} from a byte array */
public static void setBinaryAsBytes(Node node, String property, byte[] bytes) {
- // InputStream in = null;
Binary binary = null;
try (InputStream in = new ByteArrayInputStream(bytes)) {
- // in = new ByteArrayInputStream(bytes);
binary = node.getSession().getValueFactory().createBinary(in);
node.setProperty(property, binary);
} catch (Exception e) {
- throw new ArgeoJcrException("Cannot read binary " + property + " as bytes", e);
+ throw new ArgeoJcrException("Cannot set binary " + property + " as bytes", e);
+ } finally {
+ closeQuietly(binary);
+ }
+ }
+
+ /** Writes a {@link Binary} from a byte array */
+ public static void setBinaryAsBytes(Property prop, byte[] bytes) {
+ Binary binary = null;
+ try (InputStream in = new ByteArrayInputStream(bytes)) {
+ binary = prop.getSession().getValueFactory().createBinary(in);
+ prop.setValue(binary);
+ } catch (RepositoryException | IOException e) {
+ throw new ArgeoJcrException("Cannot set binary " + prop + " as bytes", e);
} finally {
- // IOUtils.closeQuietly(in);
closeQuietly(binary);
}
}
return true;
}
- /** Gets access control list for this path, throws exception if not found */
+ /**
+ * Gets the first available access control list for this path, throws exception
+ * if not found
+ */
public synchronized static AccessControlList getAccessControlList(AccessControlManager acm, String path)
throws RepositoryException {
// search for an access control list
AccessControlList acl = null;
AccessControlPolicyIterator policyIterator = acm.getApplicablePolicies(path);
- if (policyIterator.hasNext()) {
+ applicablePolicies: if (policyIterator.hasNext()) {
while (policyIterator.hasNext()) {
AccessControlPolicy acp = policyIterator.nextAccessControlPolicy();
- if (acp instanceof AccessControlList)
+ if (acp instanceof AccessControlList) {
acl = ((AccessControlList) acp);
+ break applicablePolicies;
+ }
}
} else {
AccessControlPolicy[] existingPolicies = acm.getPolicies(path);
- for (AccessControlPolicy acp : existingPolicies) {
- if (acp instanceof AccessControlList)
+ existingPolicies: for (AccessControlPolicy acp : existingPolicies) {
+ if (acp instanceof AccessControlList) {
acl = ((AccessControlList) acp);
+ break existingPolicies;
+ }
}
}
if (acl != null)
contentNode = fileNode.getNode(Node.JCR_CONTENT);
} else {
fileNode = folderNode.addNode(fileName, NodeType.NT_FILE);
- contentNode = fileNode.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
+ contentNode = fileNode.addNode(Node.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
}
binary = contentNode.getSession().getValueFactory().createBinary(in);
contentNode.setProperty(Property.JCR_DATA, binary);
}
}
+ /** Read an an nt:file as an {@link InputStream}. */
+ public static InputStream getFileAsStream(Node fileNode) throws RepositoryException {
+ return fileNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream();
+ }
+
/**
* Computes the checksum of an nt:file.
*
*/
@Deprecated
public static String checksumFile(Node fileNode, String algorithm) {
- Binary data = null;
try (InputStream in = fileNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary()
.getStream()) {
return digest(algorithm, in);
} catch (RepositoryException | IOException e) {
throw new ArgeoJcrException("Cannot checksum file " + fileNode, e);
- } finally {
- closeQuietly(data);
}
}