Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Checkm manifest parsing
  • Loading branch information
cbeer committed Feb 12, 2013
1 parent 2602def commit 1146fa7
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
Expand Up @@ -3,12 +3,15 @@

import org.apache.commons.io.IOUtils;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.fcrepo.AbstractResource;
import org.fcrepo.merritt.checkm.Entry;
import org.fcrepo.merritt.checkm.Reader;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.ObjectService;
import org.slf4j.Logger;
Expand All @@ -28,11 +31,15 @@
import java.util.concurrent.TimeUnit;

import static javax.ws.rs.core.Response.*;
import static javax.ws.rs.core.Response.Status.UNSUPPORTED_MEDIA_TYPE;
import static javax.ws.rs.core.Response.status;
import static org.modeshape.jcr.api.JcrConstants.*;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;

@Path("/content/{node}/{object}")
public class ContentService extends AbstractResource {

public static final MediaType TEXT_CHECKM = new MediaType("text", "checkm");
final private Logger logger = LoggerFactory
.getLogger(ContentService.class);

Expand All @@ -50,6 +57,7 @@ public class ContentService extends AbstractResource {
client = new DefaultHttpClient(connectionManager);
}

public static final String MANIFEST_PATH = "MANIFEST";

@GET
public Response getObjectContent(@PathParam("object") final String object_id) throws RepositoryException {
Expand All @@ -75,30 +83,46 @@ public Response addVersion(@PathParam("object") final String object_id,
final Session session = repo.login();
final String objPath = "/objects/" + object_id;

if(manifest == null && url == null) {
return status(UNSUPPORTED_MEDIA_TYPE).build();
}

boolean created = false;
if (!session.nodeExists(objPath)) {
objectService.createObjectNode(session, objPath);
}

if(manifest != null) {
datastreamService.createDatastreamNode(session, objPath + "/MANIFEST", new MediaType("text", "checkm"), manifest);
datastreamService.createDatastreamNode(session, objPath + "/" + MANIFEST_PATH, TEXT_CHECKM, manifest);
}

if(url != null) {
final HttpGet manifestRequest =
new HttpGet(IOUtils.toString(url, "UTF-8"));
String manifest_body = EntityUtils.toString(client.execute(manifestRequest)
.getEntity());
HttpResponse response = client.execute(manifestRequest);


datastreamService.createDatastreamNode(session, objPath + "/" + MANIFEST_PATH, TEXT_CHECKM, response.getEntity().getContent());
}

Node manifest_node = session.getNode(objPath + "/" + MANIFEST_PATH);

InputStream is = new ByteArrayInputStream(manifest_body.getBytes());
final InputStream responseStream =
manifest_node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();

Reader manifestReader = new Reader(responseStream);

datastreamService.createDatastreamNode(session, objPath + "/MANIFEST", new MediaType("text", "checkm"), is);
List<Entry> entries = manifestReader.getEntries();

for(Entry e : entries) {
final InputStream sis = e.getSourceInputStream();
datastreamService.createDatastreamNode(session, objPath + e.fileName, APPLICATION_OCTET_STREAM_TYPE, sis);
}


session.logout();
return created(uriInfo.getAbsolutePath()).build();
session.logout();
return created(uriInfo.getAbsolutePath()).build();

}

Expand Down
58 changes: 58 additions & 0 deletions fcrepo-merritt/src/main/java/org/fcrepo/merritt/checkm/Entry.java
@@ -0,0 +1,58 @@
package org.fcrepo.merritt.checkm;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

public class Entry {
final public String fileUrl;
final public String hashAlgorithm;
final public String hashValue;
final public String fileSize;
final public String fileLastModified;
final public String fileName;


protected static HttpClient client;
protected static final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

static {
connectionManager.setMaxTotal(Integer.MAX_VALUE);
connectionManager.setDefaultMaxPerRoute(5);
connectionManager.closeIdleConnections(3, TimeUnit.SECONDS);
client = new DefaultHttpClient(connectionManager);
}


public Entry(final String fileUrl, final String hashAlgorithm, final String hashValue, final String fileSize, final String _, final String fileName) {
this.fileUrl = fileUrl;
this.hashAlgorithm = hashAlgorithm;
this.hashValue = hashValue;
this.fileSize = fileSize;
this.fileLastModified = null;
this.fileName = fileName;
}

public Entry(String[] row) {
this(row[0].trim(), row[1].trim(), row[2].trim(), row[3].trim(), row[4].trim(), row[5].trim());
}

public InputStream getSourceInputStream() throws IOException {
final HttpGet manifestRequest =
new HttpGet(fileUrl);

HttpResponse response = client.execute(manifestRequest);

return response.getEntity().getContent();
}

}
39 changes: 39 additions & 0 deletions fcrepo-merritt/src/main/java/org/fcrepo/merritt/checkm/Reader.java
@@ -0,0 +1,39 @@
package org.fcrepo.merritt.checkm;

import org.apache.cxf.helpers.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Reader {
List<Entry> entries;

public Reader(InputStream is) throws IOException {
this.entries = parseManifest(IOUtils.readStringFromStream(is));
}

private List<Entry> parseManifest(String manifest_body) {
ArrayList<Entry> m = new ArrayList<Entry>();

String[] rows = manifest_body.split("\n");

for(String row : rows) {
if(row.startsWith("#")) {
continue;
}

m.add(new Entry(row.split("|")));
}


return m;
}

public List<Entry> getEntries() {
return entries;
}


}

0 comments on commit 1146fa7

Please sign in to comment.