Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix up bagit serialization after serialization moved into core
  • Loading branch information
cbeer committed May 7, 2013
1 parent 39e8bbd commit d1d0e74
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 86 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -12,7 +12,7 @@
<dependencies>
<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-object-serialization</artifactId>
<artifactId>fcrepo-serialization</artifactId>
<version>${project.version}</version>
</dependency>

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/org/fcrepo/serialization/bagit/BagItSerializer.java
Expand Up @@ -11,7 +11,6 @@
import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.Files.createTempDir;
import static java.io.File.createTempFile;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.utils.FedoraTypesUtils.isFedoraDatastream;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
Expand Down Expand Up @@ -155,7 +154,7 @@ public File serializeToFile(final Node node) throws RepositoryException,
}

@Override
public void deserialize(final Session session, final InputStream stream)
public void deserialize(final Session session, final String path, final InputStream stream)
throws IOException, RepositoryException, InvalidChecksumException {
logger.trace("Deserializing a Fedora object from a BagIt bag.");

Expand All @@ -169,10 +168,12 @@ public void deserialize(final Session session, final InputStream stream)
logger.trace("Created temporary Bag for Fedora object.");
final BagInfoTxt infoTxt = bag.getBagInfoTxt();

final String objectPath = path + "/" + infoTxt.get("Name");

// first make object and add its properties
final FedoraObject object =
objService.createObject(session, infoTxt.get("Name"));
logger.debug("Created Fedora object: " + object.getName());
objService.createObject(session, objectPath);
logger.debug("Created Fedora object: " + objectPath);
for (final String key : filter(infoTxt.keySet(), notNameAndIsPrefixed)) {
final List<String> values = infoTxt.getList(key);
logger.debug("Adding property for: " + key + " with values: " +
Expand Down Expand Up @@ -200,13 +201,9 @@ public void deserialize(final Session session, final InputStream stream)
final Bag dsBag = bagFactory.createBag(importDsFile);
logger.trace("Created temporary Bag file for datastream.");
final BagInfoTxt dsInfoTxt = dsBag.getBagInfoTxt();
final String dsPath =
getDatastreamJcrNodePath(object.getName(), dsInfoTxt
.get("Name"));
final String dsPath = objectPath + "/" + dsInfoTxt.get("Name");
logger.debug("Found ds path: " + dsPath);
final String contentType =
getDatastreamJcrNodePath(object.getName(), dsInfoTxt
.get("fedora_contentType"));
final String contentType = dsInfoTxt.get("fedora_contentType");
logger.debug("Found ds contentType: " + contentType);
final InputStream requestBodyStream =
dsBag.getPayload().iterator().next().newInputStream();
Expand Down
170 changes: 106 additions & 64 deletions src/test/java/org/fcrepo/serialization/bagit/AbstractResourceIT.java
@@ -1,86 +1,128 @@

package org.fcrepo.serialization.bagit;

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.lang.System.getProperty;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.slf4j.LoggerFactory.getLogger;

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

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.fcrepo.jaxb.responses.access.ObjectDatastreams;
import org.fcrepo.jaxb.responses.access.ObjectProfile;
import org.fcrepo.jaxb.responses.management.DatastreamFixity;
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.utils.FedoraJcrTypes;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/test-container.xml")
public abstract class AbstractResourceIT {

protected Logger logger;

@Before
public void setLogger() {
logger = getLogger(this.getClass());
}

protected static final int SERVER_PORT = parseInt(getProperty("test.port",
"8080"));

protected static final String HOSTNAME = "localhost";

protected static final String serverAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + "/rest/";

protected final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

public AbstractResourceIT() {
connectionManager.setMaxTotal(MAX_VALUE);
connectionManager.setDefaultMaxPerRoute(5);
connectionManager.closeIdleConnections(3, SECONDS);
client = new DefaultHttpClient(connectionManager);
}

protected static HttpPost postObjMethod(final String pid) {
return new HttpPost(serverAddress + "objects/" + pid);
}

protected static HttpPost postDSMethod(final String pid, final String ds,
final String content) throws UnsupportedEncodingException {
final HttpPost post =
new HttpPost(serverAddress + "objects/" + pid +
"/datastreams/" + ds);
post.setEntity(new StringEntity(content));
return post;
}

protected static HttpPut putDSMethod(final String pid, final String ds) {
return new HttpPut(serverAddress + "objects/" + pid + "/datastreams/" +
ds);
}

protected HttpResponse execute(final HttpUriRequest method)
throws ClientProtocolException, IOException {
logger.debug("Executing: " + method.getMethod() + " to " +
method.getURI());
return client.execute(method);
}

protected int getStatus(final HttpUriRequest method)
throws ClientProtocolException, IOException {
return execute(method).getStatusLine().getStatusCode();
}

protected Logger logger;

protected JAXBContext context;

protected String OBJECT_PATH = "objects";

@Before
public void setLogger() {
logger = LoggerFactory.getLogger(this.getClass());
}

@Before
public void setContext() throws JAXBException {
context =
JAXBContext.newInstance(ObjectProfile.class,
ObjectDatastreams.class, DatastreamProfile.class,
DatastreamFixity.class);
}

protected static final int SERVER_PORT = Integer.parseInt(System
.getProperty("test.port", "8080"));

protected static final String HOSTNAME = "localhost";

protected static final String serverAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + "/rest/";

protected final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

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

protected static HttpPost postObjMethod(final String pid) {
return new HttpPost(serverAddress + "objects/" + pid);
}

protected static HttpPost
postObjMethod(final String pid, final String query) {
if (query.equals("")) {
return new HttpPost(serverAddress + "objects/" + pid);
} else {
return new HttpPost(serverAddress + "objects/" + pid + "?" + query);
}
}

protected static HttpPost postDSMethod(final String pid, final String ds,
final String content) throws UnsupportedEncodingException {
final HttpPost post =
new HttpPost(serverAddress + "objects/" + pid +
"/" + ds + "/fcr:content");
post.setEntity(new StringEntity(content));
return post;
}

protected static HttpPut putDSMethod(final String pid, final String ds) {
return new HttpPut(serverAddress + "objects/" + pid +
"/" + ds + "/fcr:content");
}

protected HttpResponse execute(final HttpUriRequest method)
throws ClientProtocolException, IOException {
logger.debug("Executing: " + method.getMethod() + " to " +
method.getURI());
return client.execute(method);
}

protected int getStatus(final HttpUriRequest method)
throws ClientProtocolException, IOException {
HttpResponse response = execute(method);
int result = response.getStatusLine().getStatusCode();
if (!(result > 199) || !(result < 400)){
logger.warn(EntityUtils.toString(response.getEntity()));
}
return result;
}

protected ObjectProfile getObject(final String pid)
throws ClientProtocolException, IOException, JAXBException {
final HttpGet get = new HttpGet(serverAddress + "objects/" + pid);
final HttpResponse resp = execute(get);
final Unmarshaller um = context.createUnmarshaller();
return (ObjectProfile) um.unmarshal(resp.getEntity().getContent());
}
}
Expand Up @@ -15,24 +15,22 @@
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration({"/spring-test/repo.xml", "/spring-test/rest.xml",
"/spring-test/test-container.xml"})
public class BagItSerializerIT extends AbstractResourceIT {

@Test
public void tryOneObject() throws ClientProtocolException, IOException {
client.execute(postObjMethod("BagIt1"));
client.execute(postDSMethod("BagIt1", "testDS", "stuff"));
final HttpGet getObjMethod =
new HttpGet(serverAddress + "export/BagIt1?format=bagit");
new HttpGet(serverAddress + "objects/BagIt1/fcr:export?format=bagit");
HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Found exported object: " + content);
client.execute(new HttpDelete(serverAddress + "objects/BagIt1"));
logger.debug("Deleted test object.");
final HttpPost importMethod =
new HttpPost(serverAddress + "import?format=bagit");
new HttpPost(serverAddress + "objects/fcr:import?format=bagit");
importMethod.setEntity(new StringEntity(content));
assertEquals("Couldn't import!", 201, getStatus(importMethod));
response =
Expand All @@ -41,7 +39,7 @@ public void tryOneObject() throws ClientProtocolException, IOException {
.getStatusLine().getStatusCode());
response =
client.execute(new HttpGet(serverAddress +
"objects/BagIt1/datastreams/testDS"));
"objects/BagIt1/testDS"));
assertEquals("Couldn't find reimported datastream!", 200, response
.getStatusLine().getStatusCode());
logger.debug("Successfully reimported!");
Expand Down
6 changes: 0 additions & 6 deletions src/test/resources/spring-test/rest.xml
Expand Up @@ -2,12 +2,10 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:property-placeholder/>
Expand All @@ -28,10 +26,6 @@
</entry>
</util:map>

<bean class="org.fcrepo.serialization.FedoraObjectsSerialized"/>



<context:component-scan
base-package="org.fcrepo.api, org.fcrepo.serialization, org.fcrepo.exceptionhandlers"/>

Expand Down

0 comments on commit d1d0e74

Please sign in to comment.