Skip to content

Commit

Permalink
Changed FedoraDatastreams to use JcrTools, also change node definitio…
Browse files Browse the repository at this point in the history
…ns to match standard JCR practice for storing content under nodes
  • Loading branch information
ajs6f authored and cbeer committed Jan 21, 2013
1 parent 5359b23 commit bb36fb7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 103 deletions.
176 changes: 90 additions & 86 deletions src/main/java/org/fcrepo/ffmodeshapeprototype/FedoraDatastreams.java
Expand Up @@ -20,6 +20,7 @@
import javax.ws.rs.core.Response;

import org.modeshape.jcr.ConfigurationException;
import org.modeshape.jcr.api.JcrTools;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet.Builder;
Expand All @@ -29,6 +30,9 @@
@Path("/objects/{pid}/datastreams")
public class FedoraDatastreams extends AbstractResource {

// adding JcrTools in debug mode for now
static private final JcrTools jcrTools = new JcrTools(true);

public FedoraDatastreams() throws ConfigurationException,
RepositoryException, IOException {
super();
Expand Down Expand Up @@ -58,95 +62,95 @@ public Response getDatastreams(@PathParam("pid") final String pid)

@POST
@Path("/{dsid}")
public Response addDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid,
@HeaderParam("Content-Type") MediaType contentType,
InputStream requestBodyStream) throws RepositoryException {
final Session session = ws.getSession();
final Node root = session.getRootNode();
final VersionManager v = ws.getVersionManager();

contentType = contentType != null ? contentType
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
String dspath = pid + "/" + dsid;

if (session.hasPermission("/" + dspath, "add_node")) {
if (!root.hasNode(dspath)) {
return Response
.status(Response.Status.CREATED)
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString()).build();
} else {
if (session.hasPermission("/" + dspath, "remove")) {
root.getNode(dspath).remove();
session.save();
return Response
.ok()
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString())
.build();

} else
return four01;
}
} else {
return four01;
}
}

@PUT
@Path("/{dsid}")
public Response modifyDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid,
@HeaderParam("Content-Type") MediaType contentType,
InputStream requestBodyStream) throws RepositoryException {
final Session session = ws.getSession();
final Node root = session.getRootNode();
final VersionManager v = ws.getVersionManager();

contentType = contentType != null ? contentType
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
String dspath = pid + "/" + dsid;

if (session.hasPermission("/" + dspath, "add_node")) {
if (!root.hasNode(dspath)) {
return Response
.status(Response.Status.CREATED)
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString()).build();
} else {
if (session.hasPermission("/" + dspath, "remove")) {
root.getNode(dspath).remove();
session.save();
return Response
.ok()
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString())
.build();

} else
return four01;
}
} else {
return four01;
}
}
public Response addDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid,
@HeaderParam("Content-Type") MediaType contentType,
InputStream requestBodyStream) throws RepositoryException,
IOException {
final Session session = ws.getSession();
final VersionManager v = ws.getVersionManager();

contentType = contentType != null ? contentType
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
String dspath = "/" + pid + "/" + dsid;

if (session.hasPermission(dspath, "add_node")) {
if (!session.nodeExists(dspath)) {
return Response
.status(Response.Status.CREATED)
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString()).build();
} else {
if (session.hasPermission(dspath, "remove")) {
session.getNode(dspath).remove();
session.save();
return Response
.ok()
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString())
.build();

} else
return four01;
}
} else {
return four01;
}
}

@PUT
@Path("/{dsid}")
public Response modifyDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid,
@HeaderParam("Content-Type") MediaType contentType,
InputStream requestBodyStream) throws RepositoryException,
IOException {
final Session session = ws.getSession();
final VersionManager v = ws.getVersionManager();

contentType = contentType != null ? contentType
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
String dspath = "/" + pid + "/" + dsid;

if (session.hasPermission(dspath, "add_node")) {
if (!session.nodeExists(dspath)) {
return Response
.status(Response.Status.CREATED)
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString()).build();
} else {
if (session.hasPermission(dspath, "remove")) {
session.removeItem(dspath);
session.save();
return Response
.ok()
.entity(addDatastreamNode(dspath, contentType,
requestBodyStream, session).toString())
.build();

} else
return four01;
}
} else {
return four01;
}
}

private Node addDatastreamNode(final String dspath,
final MediaType contentType, final InputStream requestBodyStream,
final Session session) throws ItemExistsException,
PathNotFoundException, NoSuchNodeTypeException, LockException,
VersionException, ConstraintViolationException, RepositoryException {
final Node ds = session.getRootNode().addNode(dspath,
"fedora:datastream");
final Session session) throws RepositoryException, IOException {

if (session.nodeExists(dspath))
session.removeItem(dspath);

final Node ds = jcrTools.uploadFile(session, dspath, requestBodyStream);
ds.addMixin("fedora:datastream");
ds.setProperty("fedora:contentType", contentType.toString());

ds.addMixin("fedora:owned");
ds.addMixin("fedora:created");
ds.setProperty("fedora:ownerId", "Fedo Radmin");
ds.setProperty("fedora:contentType", contentType.toString());
ds.setProperty("fedora:content", session.getValueFactory()
.createBinary(requestBodyStream));
ds.setProperty("jcr:lastModified", Calendar.getInstance());
session.save();
ds.setProperty("jcr:lastModified", Calendar.getInstance());
session.save();
return ds;
}

Expand Down Expand Up @@ -185,8 +189,8 @@ public Response getDatastreamContent(@PathParam("pid") final String pid,
.getProperty("fedora:contentType").getString()
: "application/octet-stream";
return Response.ok(
ds.getProperty("fedora:content").getBinary().getStream(),
mimeType).build();
ds.getNode("jcr:content").getProperty("jcr:data")
.getBinary().getStream(), mimeType).build();
} else {
return four04;
}
Expand Down
Expand Up @@ -47,9 +47,9 @@ public Response ingest(@PathParam("pid") final String pid)
final Node root = session.getRootNode();

if (session.hasPermission("/" + pid, "add_node")) {
final Node obj = root.addNode(pid, "fedora:object");
final Node obj = root.addNode(pid, "nt:folder");
obj.addMixin("fedora:object");
obj.addMixin("fedora:owned");
obj.addMixin("fedora:created");
obj.setProperty("fedora:ownerId", "Fedo Radmin");
obj.setProperty("jcr:lastModified", Calendar.getInstance());
session.save();
Expand Down
18 changes: 3 additions & 15 deletions src/main/resources/fedora-node-types.cnd
Expand Up @@ -20,25 +20,19 @@
/*
* Any Fedora resource.
*/
[fedora:resource] > mix:lastModified, mix:versionable, nt:hierarchyNode abstract
[fedora:resource] > mix:created, mix:lastModified, mix:versionable mixin abstract


/*
* A Fedora object.
*/
[fedora:object] > fedora:resource
+ * (fedora:datastream) = fedora:datastream COPY
[fedora:object] > fedora:resource, nt:folder mixin


/*
* A Fedora datastream.
*/
[fedora:datastream] > fedora:resource primaryitem fedora:content

/*
* The stuff in a datastream. Is left untyped in order to allow for the use of the JCR value type system.
*/
- fedora:content (UNDEFINED) mandatory COPY
[fedora:datastream] > fedora:resource, nt:file mixin

/*
* The MIMEtype of this datastream.
Expand All @@ -62,12 +56,6 @@
- fedora:ownerId (STRING) COPY


/*
* Indicates that this resource is owned.
*/
[fedora:created] > mix:created, mix:lastModified mixin


/*
* Some entity that owns a Fedora resource.
*/
Expand Down

0 comments on commit bb36fb7

Please sign in to comment.