Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixing Datastream to decorate jcr:content with fedora:checksum when i…
…t's built on a an existing nt:file node
  • Loading branch information
barmintor committed Apr 22, 2013
1 parent 1016470 commit d71355d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
78 changes: 78 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -25,6 +25,11 @@
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.VersionException;

import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.utils.ContentDigest;
Expand Down Expand Up @@ -61,6 +66,26 @@ public Datastream(final Node n) {
logger.debug("Supporting a Fedora Datastream with null backing Node!");
}
node = n;
try {
if (!hasMixin(node)) {
logger.debug("Setting fedora:datastream properties on a nt:file node...");
node.addMixin(FEDORA_DATASTREAM);
node.addMixin(FEDORA_OWNED);
node.setProperty(FEDORA_OWNERID, node.getSession().getUserID());

node.setProperty("jcr:lastModified", Calendar.getInstance());

// TODO: I guess we should also have the PID + DSID..
String[] ids = (node.getParent() != null)
? new String[]{node.getIdentifier(), node.getParent().getName() + "/" + node.getName()}
: new String[]{node.getIdentifier()};
node.setProperty(DC_IDENTIFIER, ids);
Node contentNode = node.getNode(JCR_CONTENT);
decorateContentNode(contentNode);
}
} catch (RepositoryException ex) {
logger.warn("Could not decorate jcr:content with fedora:datastream properties: " + ex.getMessage());
}
}

/**
Expand Down Expand Up @@ -91,6 +116,7 @@ public Datastream(final Session session, final String dsPath)

node = findOrCreateNode(session, dsPath, NT_FILE);
if (node.isNew()) {
logger.debug("Setting fedora:datastream properties on a new DS node...");
node.addMixin(FEDORA_DATASTREAM);
node.addMixin(FEDORA_OWNED);
node.setProperty(FEDORA_OWNERID, session.getUserID());
Expand All @@ -100,7 +126,26 @@ public Datastream(final Session session, final String dsPath)
// TODO: I guess we should also have the PID + DSID..
node.setProperty(DC_IDENTIFIER, new String[] {node.getIdentifier(),
node.getParent().getName() + "/" + node.getName()});
if (node.hasNode(JCR_CONTENT)) {
decorateContentNode(node.getNode(JCR_CONTENT));
}
} else if (!hasMixin(node)) {
logger.debug("Setting fedora:datastream properties on a nt:file node...");
node.addMixin(FEDORA_DATASTREAM);
node.addMixin(FEDORA_OWNED);
node.setProperty(FEDORA_OWNERID, session.getUserID());

node.setProperty("jcr:lastModified", Calendar.getInstance());

// TODO: I guess we should also have the PID + DSID..
String[] ids = (node.getParent() != null)
? new String[]{node.getIdentifier(), node.getParent().getName() + "/" + node.getName()}
: new String[]{node.getIdentifier()};
node.setProperty(DC_IDENTIFIER, ids);
Node contentNode = node.getNode(JCR_CONTENT);
decorateContentNode(contentNode);
}

}

/**
Expand Down Expand Up @@ -391,4 +436,37 @@ public long getSize() throws RepositoryException {
return getNodePropertySize(node) + getContentSize();

}

private void decorateContentNode(Node contentNode) throws RepositoryException {
if (contentNode == null) {
logger.warn("{}/jcr:content appears to be null!");
return;
}
if (contentNode.canAddMixin(FEDORA_CHECKSUM)) {
contentNode.addMixin(FEDORA_CHECKSUM);
}

final Property dataProperty = contentNode.getProperty(JCR_DATA);
Binary binary = (Binary) dataProperty.getBinary();
final String dsChecksum = binary.getHexHash();

contentSizeHistogram.update(dataProperty.getLength());

contentNode.setProperty(CONTENT_SIZE, dataProperty.getLength());
contentNode.setProperty(DIGEST_VALUE, dsChecksum);
contentNode.setProperty(DIGEST_ALGORITHM, "SHA-1");

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

public static boolean hasMixin(Node node) throws RepositoryException {
NodeType[] nodeTypes = node.getMixinNodeTypes();
for (NodeType nodeType: nodeTypes) {
if (FEDORA_DATASTREAM.equals(nodeType.getName())) {
return true;
}
}
return false;
}

}
11 changes: 7 additions & 4 deletions fcrepo-kernel/src/test/java/org/fcrepo/DatastreamTest.java
Expand Up @@ -30,6 +30,7 @@
import javax.jcr.ValueFormatException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.VersionException;

import org.apache.tika.io.IOUtils;
Expand Down Expand Up @@ -70,7 +71,9 @@ public void setUp() {
mockSession = mock(Session.class);
mockRootNode = mock(Node.class);
mockDsNode = mock(Node.class);
NodeType[] nodeTypes = new NodeType[0];
try {
when(mockDsNode.getMixinNodeTypes()).thenReturn(nodeTypes);
when(mockDsNode.getName()).thenReturn(testDsId);
when(mockDsNode.getSession()).thenReturn(mockSession);
when(mockSession.getRootNode()).thenReturn(mockRootNode);
Expand Down Expand Up @@ -102,7 +105,7 @@ public void testGetContent() throws RepositoryException, IOException {
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
final String actual = IOUtils.toString(testObj.getContent());
assertEquals(expected, actual);
verify(mockDsNode).getNode(JCR_CONTENT);
verify(mockDsNode, times(2)).getNode(JCR_CONTENT);
verify(mockContent).getProperty(JCR_DATA);
}

Expand Down Expand Up @@ -132,7 +135,7 @@ public void getContentSize() throws RepositoryException {
final Node mockContent = getContentNodeMock(expectedContentLength);
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
final long actual = testObj.getContentSize();
verify(mockDsNode).getNode(JCR_CONTENT);
verify(mockDsNode, times(2)).getNode(JCR_CONTENT);
verify(mockContent).getProperty(CONTENT_SIZE);
assertEquals(expectedContentLength, actual);
}
Expand Down Expand Up @@ -172,7 +175,7 @@ public void testGetObject() throws RepositoryException {
final FedoraObject actual = testObj.getObject();
assertNotNull(actual);
assertEquals(actual.getNode(), mockObjectNode);
verify(mockDsNode).getParent();
verify(mockDsNode, times(2)).getParent();
}

@Test
Expand Down Expand Up @@ -242,7 +245,7 @@ public PropertyIterator answer(
}
});
final long actual = testObj.getSize();
verify(mockDsNode, times(1)).getNode(JCR_CONTENT);
verify(mockDsNode, times(2)).getNode(JCR_CONTENT);
verify(mockDsNode, times(1)).getProperties();
assertEquals(3, actual);
}
Expand Down

0 comments on commit d71355d

Please sign in to comment.