Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Brute force concurrency fix
  • Loading branch information
ajs6f committed Jan 29, 2013
1 parent a1b16fd commit 239f9ef
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 49 deletions.
8 changes: 3 additions & 5 deletions src/main/java/org/fcrepo/modeshape/AbstractResource.java
Expand Up @@ -14,8 +14,6 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockManager;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.map.ObjectMapper;
Expand Down Expand Up @@ -63,9 +61,7 @@ public void initialize() throws LoginException, NoSuchWorkspaceException,
protected synchronized Response deleteResource(final String path)
throws RepositoryException {

logger.debug("Attempting to delete datastream resource at path: " + path);


logger.debug("Attempting to delete resource at path: " + path);
final Session session = ws.getSession();

if (session.nodeExists(path)) {
Expand All @@ -74,6 +70,8 @@ protected synchronized Response deleteResource(final String path)
//ws.getLockManager().lock(path, true, true, Long.MAX_VALUE, "");
session.getNode(path).remove();
session.save();
session.logout();
logger.debug("Finished deleting resource at path: " + path);
return Response.status(204).build();
} else {
return four01;
Expand Down
50 changes: 34 additions & 16 deletions src/main/java/org/fcrepo/modeshape/FedoraDatastreams.java
Expand Up @@ -10,8 +10,6 @@
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockManager;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
Expand All @@ -23,15 +21,15 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;

import freemarker.template.TemplateException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Path("/objects/{pid}/datastreams")
public class FedoraDatastreams extends AbstractResource {

Expand All @@ -44,7 +42,8 @@ public class FedoraDatastreams extends AbstractResource {
public Response getDatastreams(@PathParam("pid") final String pid)
throws RepositoryException, IOException, TemplateException {

final Node root = ws.getSession().getRootNode();
final Session session = repo.login();
final Node root = session.getRootNode();

if (root.hasNode(pid)) {

Expand All @@ -53,6 +52,7 @@ public Response getDatastreams(@PathParam("pid") final String pid)
.getNode(pid).getNodes());
final Map<String, ImmutableSet<Node>> map = ImmutableMap.of(
"datastreams", datastreams.build());
session.logout();
return Response.ok()
.entity(renderTemplate("listDatastreams.ftl", map)).build();
} else {
Expand All @@ -62,17 +62,24 @@ public Response getDatastreams(@PathParam("pid") final String pid)

@POST
@Path("/{dsid}")
public Response addDatastream(@PathParam("pid") final String pid,
public synchronized 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 Session session = repo.login();

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

if (!session.nodeExists(dspath.substring(0, dspath.lastIndexOf('/')))) {
logger.debug("This bozo tried to create a datastream for an object that doesn't exist, at resource path: "
+ dspath);
return Response.notAcceptable(null).build();
}

if (session.hasPermission(dspath, "add_node")) {
if (!session.nodeExists(dspath)) {
return Response
Expand All @@ -99,12 +106,13 @@ public Response addDatastream(@PathParam("pid") final String pid,

@PUT
@Path("/{dsid}")
public Response modifyDatastream(@PathParam("pid") final String pid,
public synchronized 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 Session session = repo.login();

contentType = contentType != null ? contentType
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
Expand All @@ -127,7 +135,6 @@ private synchronized Node addDatastreamNode(final String dspath,
final Session session) throws RepositoryException, IOException {

logger.debug("Attempting to add datastream node at path: " + dspath);

Boolean created = false;
if (!session.nodeExists(dspath)) {
created = true;
Expand All @@ -137,8 +144,11 @@ private synchronized Node addDatastreamNode(final String dspath,
ds.addMixin("fedora:datastream");
// ws.getLockManager().lock(dspath, true, true, Long.MAX_VALUE, "");
final Node contentNode = ds.addNode("jcr:content", "nt:resource");
contentNode.setProperty("jcr:data", session.getValueFactory()
.createBinary(requestBodyStream));
logger.debug("Created content node at path: " + contentNode.getPath());
Property dataProperty = contentNode.setProperty("jcr:data", session
.getValueFactory().createBinary(requestBodyStream));
logger.debug("Created data property at path: " + dataProperty.getPath());

ds.setProperty("fedora:contentType", contentType.toString());

ds.addMixin("fedora:owned");
Expand All @@ -149,6 +159,9 @@ private synchronized Node addDatastreamNode(final String dspath,
ds.setProperty("jcr:lastModified", Calendar.getInstance());

session.save();
session.logout();
logger.debug("Finished adding datastream node at path: " + dspath);

return ds;
}

Expand All @@ -159,7 +172,8 @@ public Response getDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid) throws RepositoryException,
IOException, TemplateException {

final Node obj = ws.getSession().getNode("/" + pid);
Session session = repo.login();
final Node obj = session.getNode("/" + pid);

if (obj.hasNode(dsid)) {
Node ds = obj.getNode(dsid);
Expand All @@ -169,6 +183,7 @@ public Response getDatastream(@PathParam("pid") final String pid,
Property p = i.nextProperty();
b.put(p.getName(), p.toString());
}
session.logout();
return Response
.ok()
.entity(renderTemplate("datastreamProfile.ftl",
Expand All @@ -184,13 +199,15 @@ public Response getDatastream(@PathParam("pid") final String pid,
public Response getDatastreamContent(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid) throws RepositoryException {

final Node root = ws.getSession().getRootNode();
final Session session = repo.login();
final Node root = session.getRootNode();

if (root.hasNode(pid + "/" + dsid)) {
final Node ds = root.getNode(pid + "/" + dsid);
final String mimeType = ds.hasProperty("fedora:contentType") ? ds
.getProperty("fedora:contentType").getString()
: "application/octet-stream";
session.logout();
return Response.ok(
ds.getNode("jcr:content").getProperty("jcr:data")
.getBinary().getStream(), mimeType).build();
Expand All @@ -205,7 +222,8 @@ public Response getDatastreamContent(@PathParam("pid") final String pid,
public Response getDatastreamHistory(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid) throws RepositoryException,
IOException, TemplateException {
final Node root = ws.getSession().getRootNode();
final Session session = repo.login();
final Node root = session.getRootNode();
if (root.hasNode(pid + "/" + dsid)) {
final Node ds = root.getNode(pid + "/" + dsid);
return Response
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/fcrepo/modeshape/FedoraNamespaces.java
Expand Up @@ -37,10 +37,8 @@ public FedoraNamespaces() throws ConfigurationException,
@Path("/{ns}")
public Response registerObjectNamespace(@PathParam("ns") final String ns)
throws RepositoryException {
final Session session = ws.getSession();
Workspace w = session.getWorkspace();
NamespaceRegistry r = w.getNamespaceRegistry();

NamespaceRegistry r = ws.getNamespaceRegistry();
r.registerNamespace(ns, "info:fedora/" + ns);

return Response.ok().entity(ns).build();
Expand All @@ -52,8 +50,7 @@ public Response registerObjectNamespace(@PathParam("ns") final String ns)
public Response retrieveObjectNamespace(@PathParam("ns") final String prefix)
throws RepositoryException {

final NamespaceRegistry r = ws.getSession().getWorkspace()
.getNamespaceRegistry();
final NamespaceRegistry r = ws.getNamespaceRegistry();

if (ImmutableSet.copyOf(r.getPrefixes()).contains(prefix)) {
return Response
Expand All @@ -71,8 +68,7 @@ public Response registerObjectNamespaceJSON(final InputStream message)
throws RepositoryException, JsonParseException,
JsonMappingException, IOException {

final NamespaceRegistry r = ws.getSession().getWorkspace()
.getNamespaceRegistry();
final NamespaceRegistry r = ws.getNamespaceRegistry();

@SuppressWarnings("unchecked")
final Map<String, String> nses = mapper.readValue(message, Map.class);
Expand All @@ -87,9 +83,8 @@ public Response registerObjectNamespaceJSON(final InputStream message)
@Path("")
@Produces("text/plain")
public Response getObjectNamespaces() throws RepositoryException {
final Session session = ws.getSession();
Workspace w = session.getWorkspace();
NamespaceRegistry r = w.getNamespaceRegistry();

NamespaceRegistry r = ws.getNamespaceRegistry();

StringBuffer out = new StringBuffer();
String[] uris = r.getURIs();
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/fcrepo/modeshape/FedoraObjects.java
Expand Up @@ -46,19 +46,20 @@ public synchronized Response ingest(@PathParam("pid") final String pid)

logger.debug("Attempting to ingest with pid: " + pid);

final Session session = ws.getSession();
final Session session = repo.login();

// final Node root = session.getRootNode();
if (session.hasPermission("/" + pid, "add_node")) {
final Node obj = jcrTools.findOrCreateNode(session, "/" + pid,
"nt:folder");
obj.addMixin("fedora:object");
// ws.getLockManager().lock("/" + pid, false, true, Long.MAX_VALUE,
// "");
/* ws.getLockManager()
.lock("/" + pid, false, true, Long.MAX_VALUE, ""); */
obj.addMixin("fedora:owned");
obj.setProperty("fedora:ownerId", "Fedo Radmin");
obj.setProperty("jcr:lastModified", Calendar.getInstance());
session.save();
session.logout();
logger.debug("Finished ingest with pid: " + pid);
return Response.status(Response.Status.CREATED).entity(pid).build();
} else {
return four01;
Expand All @@ -71,7 +72,7 @@ public synchronized Response ingest(@PathParam("pid") final String pid)
public Response getObjectInXML(@PathParam("pid") final String pid)
throws RepositoryException, IOException, TemplateException {

final Session session = ws.getSession();
final Session session = repo.login();
logger.debug("Working in repository: "
+ session.getRepository().getDescriptor("custom.rep.name"));
logger.debug("Working in workspace: " + ws.getName());
Expand All @@ -84,6 +85,7 @@ public Response getObjectInXML(@PathParam("pid") final String pid)
Property p = i.nextProperty();
b.put(p.getName(), p.toString());
}
session.logout();
return Response
.ok()
.entity(renderTemplate("objectProfile.ftl", ImmutableMap
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/fcrepo/modeshape/FedoraRepository.java
Expand Up @@ -8,6 +8,7 @@
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.ws.rs.GET;
Expand All @@ -34,7 +35,7 @@
public class FedoraRepository extends AbstractResource {

private final Logger logger = Logger.getLogger(FedoraRepository.class);

@GET
@Path("/describe/modeshape")
public Response describeModeshape() throws JsonGenerationException,
Expand Down Expand Up @@ -66,33 +67,36 @@ public Response describeModeshape() throws JsonGenerationException,
nodetypes.put(nt.getName(), nt.toString());
}
repoproperties.put("node.types", nodetypes.build());

return Response
.ok()
.entity(mapper.writerWithType(Map.class).writeValueAsString(
repoproperties.build())).build();
}
}

@GET
@Path("/describe")
public Response describe() throws RepositoryException,
IOException, TemplateException {
return Response.ok().entity(renderTemplate("describeRepository.ftl",ImmutableMap.of("asdf", (Object)"asdf"))).build();
public Response describe() throws RepositoryException, IOException,
TemplateException {
return Response
.ok()
.entity(renderTemplate("describeRepository.ftl",
ImmutableMap.of("asdf", (Object) "asdf"))).build();
}

@GET
@Path("/objects")
public Response getObjects() throws RepositoryException {

Node root = ws.getSession().getRootNode();
final Session session = ws.getSession();
Node root = session.getRootNode();
StringBuffer nodes = new StringBuffer();

for (NodeIterator i = root.getNodes(); i.hasNext();) {
Node n = i.nextNode();
nodes.append("Name: " + n.getName() + ", Path:" + n.getPath()
+ "\n");
}

session.logout();
return Response.ok().entity(nodes.toString()).build();

}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/freemarker/datastreamProfile.ftl
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"
<#-- <datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"
pid="${ds.getParent().getName()}" dsID="${ds.getName()}">
<dsLabel>${ds.getName()}</dsLabel>
<dsVersionID></dsVersionID>
<#-- <dsCreateDate>${properties.get("jcr:created")}</dsCreateDate> -->
<dsCreateDate>${properties.get("jcr:created")}</dsCreateDate> -->
<dsState>A</dsState>
<dsMIME></dsMIME>
<dsFormatURI></dsFormatURI>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/my_repository.json
Expand Up @@ -35,5 +35,5 @@
}
}
},
"node-types" : ["fedora-node-types.cnd"]
"node-types" : ["fedora-node-types.cnd"]
}

0 comments on commit 239f9ef

Please sign in to comment.