Skip to content

Commit

Permalink
remove fedora:digestAlgorithm property, and just use the fedora:diges…
Browse files Browse the repository at this point in the history
…t URI everywhere.
  • Loading branch information
cbeer committed May 22, 2013
1 parent a2140a3 commit d5be12f
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 53 deletions.
Expand Up @@ -250,7 +250,6 @@ public static Datastream mockDatastream(final String pid,
when(mockDs.getContent()).thenReturn(
IOUtils.toInputStream(content));
when(mockDs.getContentDigest()).thenReturn(cd);
when(mockDs.getContentDigestType()).thenReturn("SHA-1");
}
} catch (final Throwable t) {
}
Expand Down
Expand Up @@ -26,9 +26,7 @@ public interface FedoraJcrTypes {

String CONTENT_SIZE = "fedora:size";

String DIGEST_VALUE = "fedora:digest";

String DIGEST_ALGORITHM = "fedora:digestAlgorithm";
String CONTENT_DIGEST = "fedora:digest";

String FCR_CONTENT = "fcr:content";
}
29 changes: 6 additions & 23 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -13,6 +13,7 @@

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import javax.jcr.Node;
import javax.jcr.Property;
Expand Down Expand Up @@ -199,11 +200,11 @@ public long getContentSize() {
public URI getContentDigest() throws RepositoryException {
final Node contentNode = node.getNode(JCR_CONTENT);
try {
return ContentDigest
.asURI(contentNode.getProperty(DIGEST_ALGORITHM).getString(),
contentNode.getProperty(DIGEST_VALUE).getString());
return new URI(contentNode.getProperty(CONTENT_DIGEST).getString());
} catch (RepositoryException e) {
LOGGER.error("Could not get content digest - " + e.getMessage());
LOGGER.error("Could not get content digest: ", e);
} catch (URISyntaxException e) {
LOGGER.error("Could not get content digest: {}", e);
}
//TODO checksum not stored. recalculating checksum,
//however, this would defeat the purpose validating against the checksum
Expand All @@ -214,23 +215,6 @@ public URI getContentDigest() throws RepositoryException {
return ContentDigest.asURI("SHA-1",dsChecksum);
}

/**
* Get the digest algorithm used to calculate the primary digest of the binary payload
* @return
* @throws RepositoryException
*/
public String getContentDigestType() {
try {
return node.getNode(JCR_CONTENT).getProperty(DIGEST_ALGORITHM)
.getString();
} catch (RepositoryException e) {
LOGGER.error("Could not get content digest type - " + e.getMessage());
}
//only supporting sha-1
LOGGER.debug("Using default digest type of SHA-1");
return "SHA-1";
}

/**
* @return The ID of this datastream, unique within an object. Normally just the name of the backing JCR node.
* @throws RepositoryException
Expand Down Expand Up @@ -281,8 +265,7 @@ private void decorateContentNode(Node contentNode) throws RepositoryException {
contentSizeHistogram.update(dataProperty.getLength());

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

LOGGER.debug("Decorated data property at path: " + dataProperty.getPath());
}
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.fcrepo.binary.PolicyDecisionPoint;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.utils.ContentDigest;
import org.fcrepo.utils.DatastreamIterator;
import org.fcrepo.utils.FixityResult;
import org.fcrepo.utils.JcrRdfTools;
Expand Down Expand Up @@ -219,7 +220,7 @@ public Collection<FixityResult> runFixityAndFixProblems(
fixityCheckCounter.inc();

try {
digest = getInstance(datastream.getContentDigestType());
digest = getInstance(ContentDigest.getAlgorithm(digestUri));
} catch (final NoSuchAlgorithmException e) {
throw new RepositoryException(e.getMessage(), e);
}
Expand Down
Expand Up @@ -9,6 +9,8 @@
import java.net.URISyntaxException;
import java.util.Map;

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import org.slf4j.Logger;

import com.google.common.collect.ImmutableMap;
Expand All @@ -20,6 +22,9 @@ public abstract class ContentDigest {
public static final Map<String, String> algorithmToScheme = ImmutableMap
.of("SHA-1", "urn:sha1", "SHA1", "urn:sha1");

public static final Map<String, String> schemeToAlgorithm = ImmutableMap
.of("urn:sha1", "SHA-1");

public static URI asURI(final String algorithm, final String value) {
try {
final String scheme = algorithmToScheme.get(algorithm);
Expand All @@ -39,4 +44,8 @@ public static URI asURI(final String algorithm, final byte[] data) {
public static String asString(final byte[] data) {
return encodeHexString(data);
}

public static String getAlgorithm(URI digestUri) {
return schemeToAlgorithm.get(digestUri.getScheme() + ":" + digestUri.getSchemeSpecificPart().split(":", 2)[0]);
}
}
3 changes: 1 addition & 2 deletions fcrepo-kernel/src/main/resources/fedora-node-types.cnd
Expand Up @@ -75,5 +75,4 @@
*/
[fedora:binary] > nt:resource mixin
- fedora:size (LONG) COPY
- fedora:digest (STRING) COPY
- fedora:digestAlgorithm (STRING) COPY
- fedora:digest (URI) COPY
18 changes: 2 additions & 16 deletions fcrepo-kernel/src/test/java/org/fcrepo/DatastreamTest.java
Expand Up @@ -139,26 +139,12 @@ public void getContentSize() throws RepositoryException {
public void getContentDigest() throws RepositoryException {
final String content = "asdf";
final URI expected =
URI.create("urn:sha1:" + TestHelpers.checksumString(content));
URI.create(TestHelpers.checksumString(content));
final Node mockContent = TestHelpers.getContentNodeMock(content);
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
final URI actual = testObj.getContentDigest();
assertEquals(expected, actual);
verify(mockContent).getProperty(DIGEST_ALGORITHM);
verify(mockContent).getProperty(DIGEST_VALUE);
}

@SuppressWarnings("unchecked")
@Test
public void getContentDigestType() throws RepositoryException {
final String expected = "SHA-1";
final Node mockContent = TestHelpers.getContentNodeMock(8);
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
assertEquals(expected, testObj.getContentDigestType());
verify(mockContent).getProperty(DIGEST_ALGORITHM);
when(mockDsNode.getNode(JCR_CONTENT)).thenThrow(
RepositoryException.class);
assertEquals(expected, testObj.getContentDigestType());
verify(mockContent).getProperty(CONTENT_DIGEST);
}

@Test
Expand Down
Expand Up @@ -162,7 +162,6 @@ public void testGetFixityResultsModel() throws RepositoryException, URISyntaxExc

when(mockDatastream.getNode()).thenReturn(mockNode);
when(mockDatastream.getContentDigest()).thenReturn(new URI("urn:sha1:abc"));
when(mockDatastream.getContentDigestType()).thenReturn("SHA-1");

when(llStore.transformLowLevelCacheEntries(eq(mockContent), any(Function.class))).thenReturn(mockCollection);

Expand Down Expand Up @@ -231,7 +230,6 @@ public void testRunFixityAndFixProblems() throws RepositoryException,

when(mockDatastream.getNode()).thenReturn(mockNode);
when(mockDatastream.getContentDigest()).thenReturn(new URI("urn:sha1:abc"));
when(mockDatastream.getContentDigestType()).thenReturn("SHA-1");

when(llStore.transformLowLevelCacheEntries(eq(mockContent), any(CheckCacheEntryFixity.class))).thenReturn(mockFixityResults);

Expand Down
Expand Up @@ -20,4 +20,9 @@ public void testSHA1() {
assertEquals("Failed to produce a proper content digest URI!", URI
.create("urn:sha1:fake"), ContentDigest.asURI("SHA1", "fake"));
}

@Test
public void testGetAlgorithm() {
assertEquals("Failed to produce a proper digest algorithm!", "SHA-1", ContentDigest.getAlgorithm(ContentDigest.asURI("SHA-1", "fake")));
}
}
8 changes: 3 additions & 5 deletions fcrepo-kernel/src/test/java/org/fcrepo/utils/TestHelpers.java
Expand Up @@ -2,8 +2,7 @@
package org.fcrepo.utils;

import static org.fcrepo.utils.FedoraJcrTypes.CONTENT_SIZE;
import static org.fcrepo.utils.FedoraJcrTypes.DIGEST_ALGORITHM;
import static org.fcrepo.utils.FedoraJcrTypes.DIGEST_VALUE;
import static org.fcrepo.utils.FedoraJcrTypes.CONTENT_DIGEST;
import static org.fcrepo.utils.FedoraJcrTypes.JCR_CREATEDBY;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -64,8 +63,7 @@ public InputStream answer(final InvocationOnMock inv) {
when(mockDigestType.getString()).thenReturn(digestType);
when(mock.getProperty(JCR_DATA)).thenReturn(mockProp);
when(mock.getProperty(CONTENT_SIZE)).thenReturn(mockFedoraSize);
when(mock.getProperty(DIGEST_ALGORITHM)).thenReturn(mockDigestType);
when(mock.getProperty(DIGEST_VALUE)).thenReturn(mockDigest);
when(mock.getProperty(CONTENT_DIGEST)).thenReturn(mockDigest);
when(mock.getProperty(JCR_CREATEDBY)).thenReturn(mockCreated);
} catch (final RepositoryException e) {
} // shhh
Expand All @@ -80,7 +78,7 @@ public static String checksumString(final byte[] content) {
try {
final MessageDigest d = MessageDigest.getInstance("SHA-1");
final byte[] digest = d.digest(content);
return ContentDigest.asString(digest);
return ContentDigest.asURI("SHA-1", digest).toString();
} catch (final NoSuchAlgorithmException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit d5be12f

Please sign in to comment.