Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #31 from futures/45628143
45628143: Store fixity data when a datastream is created or modified
  • Loading branch information
cbeer committed Mar 7, 2013
2 parents acdcf66 + ea0f800 commit e8911df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
47 changes: 17 additions & 30 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -11,10 +11,6 @@

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.jcr.Node;
Expand All @@ -26,8 +22,8 @@
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.version.VersionException;

import org.apache.commons.codec.binary.Hex;
import org.fcrepo.utils.ContentDigest;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,8 +36,6 @@
*/
public class Datastream extends JcrTools {

private static MessageDigest md;

private final static String CONTENT_SIZE = "fedora:size";

private final static String DIGEST_VALUE = "fedora:digest";
Expand All @@ -64,19 +58,6 @@ public Node getNode() {
return node;
}

private MessageDigest getMessageDigest() {

if (this.md == null) {
try {
this.md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

return md;
}

/**
* @return The InputStream of content associated with this datastream.
* @throws RepositoryException
Expand All @@ -100,10 +81,19 @@ public void setContent(InputStream content) throws RepositoryException {
contentNode.addMixin("fedora:checksum");
}

final DigestInputStream dis =
new DigestInputStream(content, getMessageDigest());

logger.debug("Created content node at path: " + contentNode.getPath());

/*
* https://docs.jboss.org/author/display/MODE/Binary+values#Binaryvalues-
* ExtendedBinaryinterface
* promises: "All javax.jcr.Binary values returned by ModeShape will
* implement this public interface, so feel free to cast the values to
* gain access to the additional methods."
*/
Binary binary =
(Binary) node.getSession().getValueFactory().createBinary(
content);

/*
* This next line of code deserves explanation. If we chose for the
* simpler line:
Expand All @@ -119,15 +109,12 @@ public void setContent(InputStream content) throws RepositoryException {
* code may still be useful to us for an asynchronous method that we
* develop later.
*/
Property dataProperty =
contentNode.setProperty(JCR_DATA, node.getSession()
.getValueFactory().createBinary(dis));
Property dataProperty = contentNode.setProperty(JCR_DATA, binary);

contentNode.setProperty(CONTENT_SIZE, dataProperty.getLength());
contentNode.setProperty(DIGEST_VALUE, Hex.encodeHexString(dis
.getMessageDigest().digest()));
contentNode.setProperty(DIGEST_ALGORITHM, dis.getMessageDigest()
.getAlgorithm());
contentNode.setProperty(DIGEST_VALUE, binary.getHexHash());
contentNode.setProperty(DIGEST_ALGORITHM, "SHA-1");

logger.debug("Created data property at path: " + dataProperty.getPath());

}
Expand Down
4 changes: 2 additions & 2 deletions fcrepo-kernel/src/test/java/org/fcrepo/DatastreamTest.java
Expand Up @@ -9,7 +9,6 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;

import javax.inject.Inject;
import javax.jcr.Node;
Expand Down Expand Up @@ -91,7 +90,8 @@ public void testDatastreamContentDigestAndLength() throws IOException, Repositor
session.save();

final Datastream ds = getDatastream("testObject", "testDatastreamNode2");
assertEquals("urn:sha1:3da541559918a808c2402bba5012f6c60b27661c", ds.getContentDigest().toString());
assertEquals("urn:sha1:3da541559918a808c2402bba5012f6c60b27661c", ds
.getContentDigest().toString());
assertEquals(4L, ds.getContentSize());


Expand Down

0 comments on commit e8911df

Please sign in to comment.