Skip to content

Commit

Permalink
update applyDigestToBlobs to return a FixityResult object with the ch…
Browse files Browse the repository at this point in the history
…ecksum and size
  • Loading branch information
cbeer committed Mar 12, 2013
1 parent d15d16b commit 575c38e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
1 change: 0 additions & 1 deletion fcrepo-kernel/pom.xml
Expand Up @@ -61,7 +61,6 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Expand Up @@ -24,6 +24,9 @@
import javax.jcr.Session;

import org.apache.commons.codec.binary.Hex;
import org.fcrepo.utils.ContentDigest;
import org.fcrepo.utils.FixityInputStream;
import org.fcrepo.utils.FixityResult;
import org.fcrepo.utils.LowLevelCacheStore;
import org.infinispan.Cache;
import org.infinispan.loaders.CacheLoaderManager;
Expand Down Expand Up @@ -60,19 +63,20 @@ private static JcrRepository getRepositoryInstance() {
return (JcrRepository) readOnlySession.getRepository();
}

public static Map<LowLevelCacheStore, Boolean> applyDigestToBlobs(
public static Map<LowLevelCacheStore, FixityResult> applyDigestToBlobs(
final Node resource, final MessageDigest digest,
final String checksum) throws RepositoryException {
return applyToBlob(
resource,
new Maps.EntryTransformer<LowLevelCacheStore, InputStream, Boolean>() {
new Maps.EntryTransformer<LowLevelCacheStore, InputStream, FixityResult>() {

public Boolean transformEntry(LowLevelCacheStore store,
public FixityResult transformEntry(LowLevelCacheStore store,
InputStream is) {
DigestInputStream ds = null;
FixityResult result = new FixityResult();
FixityInputStream ds = null;
try {
ds =
new DigestInputStream(is,
new FixityInputStream(is,
(MessageDigest) digest.clone());

while (ds.read() != -1);
Expand All @@ -81,14 +85,16 @@ public Boolean transformEntry(LowLevelCacheStore store,
Hex.encodeHexString(ds.getMessageDigest()
.digest());

return checksum.equals(calculatedDigest);
result.computedChecksum = ContentDigest.asURI(digest.getAlgorithm(), calculatedDigest).toString();
result.computedSize = ds.getByteCount();

} catch (CloneNotSupportedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return FALSE;
return result;
}
});

Expand Down
@@ -0,0 +1,34 @@
package org.fcrepo.utils;

import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.io.input.ProxyInputStream;

import java.io.FilterInputStream;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class FixityInputStream extends ProxyInputStream {

/**
* Creates a <code>FilterInputStream</code>
* by assigning the argument <code>in</code>
* to the field <code>this.in</code> so as
* to remember it for later use.
*
* @param in the underlying input stream, or <code>null</code> if
* this instance is to be created without an underlying stream.
*/
public FixityInputStream(InputStream in, MessageDigest digest) {
super(new CountingInputStream(new DigestInputStream(in, digest)));
}

public long getByteCount() {
return ((CountingInputStream)in).getByteCount();
}

public MessageDigest getMessageDigest() {
return ((DigestInputStream)in).getMessageDigest();
}

}
19 changes: 19 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/FixityResult.java
@@ -0,0 +1,19 @@
package org.fcrepo.utils;

public class FixityResult {
public long computedSize;

public String computedChecksum;

public boolean equals(Object obj) {

boolean result = false;
if (obj instanceof FixityResult) {
FixityResult that = (FixityResult) obj;
result = this.computedSize == that.computedSize && this.computedChecksum.equals(that.computedChecksum);

}

return result;
}
}
@@ -0,0 +1,27 @@
package org.fcrepo.utils;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static junit.framework.Assert.assertEquals;

public class FixityInputStreamTest {

@Test
public void SimpleFixityInputStreamTest() throws NoSuchAlgorithmException {
FixityInputStream is = new FixityInputStream(new ByteArrayInputStream("0123456789".getBytes()), MessageDigest.getInstance("SHA-1"));

try {
while(is.read() != -1);
} catch (IOException e) {

}

assertEquals(10, is.getByteCount());
}
}

0 comments on commit 575c38e

Please sign in to comment.