Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
integrate fcrepo-object-serialization into the default REST API
- Loading branch information
Showing
14 changed files
with
423 additions
and
110 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
65
fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraImport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
fcrepo-http-api/src/test/java/org/fcrepo/integration/api/FedoraExportIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} | ||
|
61 changes: 0 additions & 61 deletions
61
fcrepo-http-api/src/test/java/org/fcrepo/test/util/ContainerWrapper.java
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
fcrepo-http-api/src/test/java/org/fcrepo/test/util/PathSegmentImpl.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.