Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
integrate fcrepo-object-serialization into the default REST API
  • Loading branch information
cbeer committed May 6, 2013
1 parent 9cf9aa3 commit 4e143bb
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 110 deletions.
76 changes: 76 additions & 0 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraExport.java
@@ -0,0 +1,76 @@

package org.fcrepo.api;

import static javax.ws.rs.core.Response.created;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.fcrepo.AbstractResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

@Component
@Path("/rest/{path: .*}/fcr:export")
public class FedoraExport extends AbstractResource {

@Resource
private Map<String, FedoraObjectSerializer> serializers;

private final Logger logger = getLogger(this.getClass());

@GET
public StreamingOutput exportObject(@PathParam("path") final List<PathSegment> pathList, @QueryParam("format")
@DefaultValue("jcr/xml")
final String format) {
final String path = toPath(pathList);

logger.debug("Requested object serialization for: " + path +
" using serialization format " + format);

return new StreamingOutput() {
@Override
public void write(final OutputStream out) throws IOException {
final Session session = getAuthenticatedSession();
try {
logger.debug("Selecting from serializer map: " +
serializers);
final FedoraObjectSerializer serializer =
serializers.get(format);
logger.debug("Retrieved serializer for format: " + format);
serializer.serialize(objectService.getObject(session, path), out);
logger.debug("Successfully serialized object: " + path);
} catch (final RepositoryException e) {
throw new WebApplicationException(e);
} finally {
session.logout();
}
}
};
}

public void setSerializers(
final Map<String, FedoraObjectSerializer> serializers) {
this.serializers = serializers;
}
}
65 changes: 65 additions & 0 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraImport.java
@@ -0,0 +1,65 @@

package org.fcrepo.api;

import org.fcrepo.AbstractResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import static javax.ws.rs.core.Response.created;
import static org.slf4j.LoggerFactory.getLogger;

@Component
@Path("/rest/{path: .*}/fcr:import")
public class FedoraImport extends AbstractResource {

@Resource
private Map<String, FedoraObjectSerializer> serializers;

private final Logger logger = getLogger(this.getClass());

@POST
public Response importObject(@PathParam("path") final List<PathSegment> pathList, @QueryParam("format")
@DefaultValue("jcr/xml")
final String format, final InputStream stream) throws IOException,
RepositoryException, InvalidChecksumException {

final String path = toPath(pathList);

final Session session = getAuthenticatedSession();

try {
serializers.get(format).deserialize(session, path, stream);
session.save();
// TODO return proper URI for new resource
return created(uriInfo.getAbsolutePath()).build();
} finally {
session.logout();
}
}

public void setSerializers(
final Map<String, FedoraObjectSerializer> serializers) {
this.serializers = serializers;
}
}
@@ -0,0 +1,56 @@
package org.fcrepo.integration.api;


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

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

@Test
public void shouldRoundTripOneObject() throws IOException {
final String objName = "JcrXmlSerializerIT1";

// set up the object
client.execute(postObjMethod(objName));
client.execute(postDSMethod(objName, "testDS", "stuff"));

// export it
logger.debug("Attempting to export: " + objName);
final HttpGet getObjMethod =
new HttpGet(serverAddress + "objects/JcrXmlSerializerIT1" + "/fcr:export");
HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
logger.debug("Successfully exported: " + objName);
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Found exported object: " + content);

// delete it
client.execute(new HttpDelete(serverAddress + "objects/JcrXmlSerializerIT1"));
response = client.execute(new HttpGet(serverAddress + "objects/JcrXmlSerializerIT1"));
assertEquals(404, response.getStatusLine().getStatusCode());

// try to import it
final HttpPost importMethod = new HttpPost(serverAddress + "objects/fcr:import");
importMethod.setEntity(new StringEntity(content));
assertEquals("Couldn't import!", 201, getStatus(importMethod));

//check that we made it
response = client.execute(new HttpGet(serverAddress + "objects/JcrXmlSerializerIT1"));
assertEquals(200, response.getStatusLine().getStatusCode());
}

}

This file was deleted.

This file was deleted.

21 changes: 15 additions & 6 deletions fcrepo-http-api/src/test/resources/spring-test/rest.xml
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean class="org.fcrepo.session.SessionFactory" />

Expand All @@ -13,6 +13,15 @@

<context:annotation-config />

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


<util:map id="serializers" key-type="java.lang.String" map-class="java.util.HashMap"
value-type="org.fcrepo.serialization.FedoraObjectSerializer">
<description>Map from formats to serializers for Fedora objects</description>
<entry key="jcr/xml">
<bean class="org.fcrepo.serialization.jcrxml.JcrXmlSerializer"/>
</entry>
</util:map>

</beans>
11 changes: 11 additions & 0 deletions fcrepo-http-commons/pom.xml
Expand Up @@ -38,6 +38,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-serialization</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.modeshape</groupId>
<artifactId>modeshape-jcr-api</artifactId>
Expand Down

0 comments on commit 4e143bb

Please sign in to comment.