Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
context is good for nothing, try the output node. Adding a README
  • Loading branch information
barmintor committed Feb 10, 2013
1 parent 17dd4e9 commit bef8650
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
4 changes: 4 additions & 0 deletions fcrepo-glacier/README.txt
@@ -0,0 +1,4 @@
1. Unit test sequencer - done
2. Integration test against Icemelt
3. Integration test for sequencer mapping
4. validate resource for nodes with the glacier-backup mixin
@@ -1,23 +1,37 @@
package org.fcrepo.glacier;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.nodetype.ConstraintViolationException;

import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.jcr.api.sequencer.Sequencer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.glacier.AmazonGlacierClient;
import com.amazonaws.services.glacier.TreeHashGenerator;
import com.amazonaws.services.glacier.internal.TreeHashInputStream;
import com.amazonaws.services.glacier.model.UploadArchiveRequest;
import com.amazonaws.services.glacier.model.UploadArchiveResult;

public class GlacierBackupSequencer extends Sequencer {

public static String GLACIER_URI_PROPERTY = "fcrepo:glacier:location";
public static String GLACIER_ARCHIVE_ID_PROPERTY = "fcrepo:glacier:archive-id";
public static String GLACIER_BACKUP_MIXIN = "org.fcrepo:glacier-backup";
public static String GLACIER_LOCATION_PROPERTY = "org.fcrepo.glacier:location";
public static String GLACIER_ARCHIVE_ID_PROPERTY = "org.fcrepo.glacier:archive-id";
public static String GLACIER_CHECKSUM_PROPERTY = "org.fcrepo.glacier:checksum";

private static Logger LOG = LoggerFactory.getLogger(GlacierBackupSequencer.class);

private AmazonGlacierClient m_client;
private String m_defaultArchive;
Expand All @@ -29,16 +43,48 @@ public GlacierBackupSequencer(AmazonGlacierClient client, String defaultArchive)
@Override
public boolean execute(Property inputProperty, Node outputNode, Context context)
throws Exception {
if (JcrConstants.JCR_CONTENT.equals(inputProperty.getName())) {
InputStream in = inputProperty.getBinary().getStream();
if (JcrConstants.JCR_DATA.equals(inputProperty.getName())) {
if (!outputNode.canAddMixin(GLACIER_BACKUP_MIXIN)) {
throw new ConstraintViolationException("Cannot add mixin \"" + GLACIER_BACKUP_MIXIN + "\" to this node");
}
Binary inputBinary = inputProperty.getBinary();
InputStream in = inputBinary.getStream();
long inputSize = inputBinary.getSize();
UploadArchiveRequest request = getRequest(context);
request.setBody(in);
request.setContentLength(inputProperty.getBinary().getSize());
File outFile = null;

// read input to get tree hash, cache it in a temporary file
TreeHashInputStream treeIn = new TreeHashInputStream(in);
byte [] buf = new byte[1024];
outFile = File.createTempFile("fcrepo", null);
OutputStream out = new FileOutputStream(outFile);
int len = -1;
long read = 0;
while ((len = treeIn.read(buf)) > -1) {
out.write(buf,0,len);
read += len;
}
treeIn.close();
out.flush();
out.close();
if (inputSize != read) {
LOG.warn("input Binary size did not match bytes read: {} != {}", inputSize, read);
}
request.setContentLength(read);
request.setChecksum(TreeHashGenerator.calculateTreeHash(treeIn.getChecksums()));
request.setBody(new FileInputStream(outFile));

request.setContentLength(inputBinary.getSize());
//TODO Need to calculate the checksum!
request.setChecksum("foo");
UploadArchiveResult result = m_client.uploadArchive(request);
outputNode.setProperty(GLACIER_URI_PROPERTY, result.getLocation());

outputNode.addMixin(GLACIER_BACKUP_MIXIN);
outputNode.setProperty(GLACIER_LOCATION_PROPERTY, result.getLocation());
outputNode.setProperty(GLACIER_ARCHIVE_ID_PROPERTY, result.getArchiveId());
if (outFile != null) {
outFile.delete();
}
return true;
} else {
return false;
Expand Down
5 changes: 5 additions & 0 deletions fcrepo-glacier/src/main/resources/glacier-backup.cnd
@@ -0,0 +1,5 @@
[org.fcrepo:glacier-candidate] > nt:resource mixin
[org.fcrepo:glacier-backup] > org.fcrepo:glacier-candidate mixin query primaryitem org.fcrepo.glacier:location
- org.fcrepo.glacier:checksum (STRING) mandatory
- org.fcrepo.glacier:archive-id (STRING) mandatory
- org.fcrepo.glacier:location (STRING) mandatory
Expand Up @@ -51,7 +51,7 @@ public static Test suite()
/**
* Rigourous Test :-)
*/
public void testSequencer()
public void testSequencerValid()
{
try {
UploadArchiveResult upload = mock(UploadArchiveResult.class);
Expand All @@ -66,14 +66,40 @@ public void testSequencer()
StringInputStream binValue = new StringInputStream(binString);
when(binary.getSize()).thenReturn(Long.valueOf(binString.getBytes().length));
when(binary.getStream()).thenReturn(binValue);
when(input.getName()).thenReturn(JcrConstants.JCR_CONTENT);
when(input.getName()).thenReturn(JcrConstants.JCR_DATA);
when(input.getBinary()).thenReturn(binary);
Node output = mock(Node.class);
when(output.canAddMixin(GlacierBackupSequencer.GLACIER_BACKUP_MIXIN)).thenReturn(true);
Context context = mock(Context.class);
assertTrue(test.execute(input, output, context));
verify(output).setProperty(GlacierBackupSequencer.GLACIER_ARCHIVE_ID_PROPERTY, UPLOAD_ID);
verify(output).setProperty(GlacierBackupSequencer.GLACIER_URI_PROPERTY, UPLOAD_LOC);
when(input.getName()).thenReturn(JcrConstants.JCR_DATA);
verify(output).setProperty(GlacierBackupSequencer.GLACIER_LOCATION_PROPERTY, UPLOAD_LOC);
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
public void testSequencerInvalid()
{
try {
UploadArchiveResult upload = mock(UploadArchiveResult.class);
when(upload.getArchiveId()).thenReturn(UPLOAD_ID);
when(upload.getLocation()).thenReturn(UPLOAD_LOC);
AmazonGlacierClient client = mock(AmazonGlacierClient.class);
when(client.uploadArchive(any(UploadArchiveRequest.class))).thenReturn(upload);
Sequencer test = new GlacierBackupSequencer(client,DEFAULT_ARCHIVE);
Property input = mock(Property.class);
InMemoryBinaryValue binary = mock(InMemoryBinaryValue.class);
String binString = "the quick brown fox";
StringInputStream binValue = new StringInputStream(binString);
when(binary.getSize()).thenReturn(Long.valueOf(binString.getBytes().length));
when(binary.getStream()).thenReturn(binValue);
when(input.getName()).thenReturn(JcrConstants.JCR_PATH);
when(input.getBinary()).thenReturn(binary);
Node output = mock(Node.class);
when(output.canAddMixin(GlacierBackupSequencer.GLACIER_BACKUP_MIXIN)).thenReturn(false);
Context context = mock(Context.class);
verify(output, times(0)).setProperty(anyString(), anyString());
assertFalse(test.execute(input, output, context));
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit bef8650

Please sign in to comment.