Skip to content

Commit

Permalink
Now using proper JAX-RS ExceptionMapping, thus cleaning up a lot of J…
Browse files Browse the repository at this point in the history
…AX-RS endpoint methods and also installing finally{} statements to be sure that JCR sessions are always logged out: for https://www.pivotaltracker.com/story/show/44821755
  • Loading branch information
ajs6f committed Feb 20, 2013
1 parent 4614024 commit 3c8073d
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 385 deletions.
48 changes: 23 additions & 25 deletions fcrepo-dc/src/main/java/org/fcrepo/generator/DublinCore.java
@@ -1,3 +1,4 @@

package org.fcrepo.generator;

import static javax.ws.rs.core.MediaType.TEXT_XML;
Expand All @@ -8,6 +9,7 @@

import javax.annotation.Resource;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.GET;
Expand All @@ -22,36 +24,32 @@
@Path("/objects/{pid}/oai_dc")
public class DublinCore extends AbstractResource {

@Resource
List<AbstractIndexer> indexers;

@GET
@Produces(TEXT_XML)
public Response getObjectAsDublinCore(@PathParam("pid") final String pid)
throws RepositoryException {
final Session session = repo.login();
@Resource
List<AbstractIndexer> indexers;

try {
if (session.nodeExists("/objects/" + pid)) {
final Node obj = session.getNode("/objects/" + pid);
@GET
@Produces(TEXT_XML)
public Response getObjectAsDublinCore(@PathParam("pid")
final String pid) throws RepositoryException {
final Session session = repo.login();

for (AbstractIndexer indexer : indexers) {
InputStream inputStream = indexer.getStream(obj);
try {

if (inputStream != null) {
return ok(inputStream).build();
}
}
final Node obj = session.getNode("/objects/" + pid);

return four04;
} else {
return four04;
}
for (AbstractIndexer indexer : indexers) {
InputStream inputStream = indexer.getStream(obj);

} finally {
session.logout();
}
if (inputStream != null) {
return ok(inputStream).build();
}
}
// no indexers = no path for DC
throw new PathNotFoundException();
} finally {
session.logout();
}

}
}

}
214 changes: 108 additions & 106 deletions fcrepo-http-commons/src/main/java/org/fcrepo/AbstractResource.java
@@ -1,3 +1,4 @@

package org.fcrepo;

import static com.google.common.collect.ImmutableSet.copyOf;
Expand Down Expand Up @@ -32,110 +33,111 @@
* @author ajs6f
*
*/
public abstract class AbstractResource extends Constants {

final private Logger logger = LoggerFactory
.getLogger(AbstractResource.class);

/**
* Useful for constructing URLs
*/
@Context
protected UriInfo uriInfo;

/**
* The JCR repository at the heart of Fedora.
*/
@Inject
protected Repository repo;

/**
* A resource that can mint new Fedora PIDs.
*/
@Inject
protected PidMinter pidMinter;

/**
* A convenience object provided by ModeShape for acting against the JCR
* repository.
*/
final static protected JcrTools jcrTools = new JcrTools(true);

@PostConstruct
public void initialize() throws LoginException, NoSuchWorkspaceException,
RepositoryException {

final Session session = repo.login();
session.getWorkspace().getNamespaceRegistry()
.registerNamespace("test", "info:fedora/test");
Node objects = jcrTools.findOrCreateNode(session, "/objects");
objects.setProperty("size", 0L);
session.save();
session.logout();
}

protected synchronized Response deleteResource(final Node resource)
throws RepositoryException {

logger.debug("Attempting to delete resource at path: "
+ resource.getPath());
final Session session = resource.getSession();
if (session.hasPermission(resource.getPath(), "remove")) {
resource.remove();
session.save();
session.logout();
return noContent().build();
} else {
return four03;
}
}

public static Long getNodePropertySize(Node node)
throws RepositoryException {
Long size = 0L;
PropertyIterator i = node.getProperties();
while (i.hasNext()) {
Property p = i.nextProperty();
if (p.isMultiple()) {
for (Value v : copyOf(p.getValues())) {
size = size + v.getBinary().getSize();
}
} else {
size = size + p.getBinary().getSize();
}
}
return size;
}

/**
* Alter the total repository size.
*
* @param change
* the amount by which to [de|in]crement the total repository
* size
* @param session
* the javax.jcr.Session in which the originating mutation is
* occurring
* @throws PathNotFoundException
* @throws RepositoryException
*/
protected void updateRepositorySize(Long change, Session session)
throws PathNotFoundException, RepositoryException {
logger.debug("updateRepositorySize called with change quantity: "
+ change);
Property sizeProperty = session.getNode("/objects").getProperty("size");
Long previousSize = sizeProperty.getLong();
logger.debug("Previous repository size: " + previousSize);
synchronized (sizeProperty) {
sizeProperty.setValue(previousSize + change);
session.save();
}
logger.debug("Current repository size: " + sizeProperty.getLong());
}

protected Long getRepositorySize(Session session)
throws ValueFormatException, PathNotFoundException,
RepositoryException {
return session.getNode("/objects").getProperty("size").getLong();
}
public abstract class AbstractResource {

final private Logger logger = LoggerFactory
.getLogger(AbstractResource.class);

/**
* Useful for constructing URLs
*/
@Context
protected UriInfo uriInfo;

/**
* The JCR repository at the heart of Fedora.
*/
@Inject
protected Repository repo;

/**
* A resource that can mint new Fedora PIDs.
*/
@Inject
protected PidMinter pidMinter;

/**
* A convenience object provided by ModeShape for acting against the JCR
* repository.
*/
final static protected JcrTools jcrTools = new JcrTools(true);

@PostConstruct
public void initialize() throws LoginException, NoSuchWorkspaceException,
RepositoryException {

final Session session = repo.login();
session.getWorkspace().getNamespaceRegistry().registerNamespace("test",
"info:fedora/test");
Node objects = jcrTools.findOrCreateNode(session, "/objects");
objects.setProperty("size", 0L);
session.save();
session.logout();
}

protected synchronized Response deleteResource(final Node resource)
throws RepositoryException {

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

try {
resource.remove();
session.save();
} finally {
session.logout();
}
return noContent().build();

}

public static Long getNodePropertySize(Node node)
throws RepositoryException {
Long size = 0L;
PropertyIterator i = node.getProperties();
while (i.hasNext()) {
Property p = i.nextProperty();
if (p.isMultiple()) {
for (Value v : copyOf(p.getValues())) {
size = size + v.getBinary().getSize();
}
} else {
size = size + p.getBinary().getSize();
}
}
return size;
}

/**
* Alter the total repository size.
*
* @param change
* the amount by which to [de|in]crement the total repository
* size
* @param session
* the javax.jcr.Session in which the originating mutation is
* occurring
* @throws PathNotFoundException
* @throws RepositoryException
*/
protected void updateRepositorySize(Long change, Session session)
throws PathNotFoundException, RepositoryException {
logger.debug("updateRepositorySize called with change quantity: " +
change);
Property sizeProperty = session.getNode("/objects").getProperty("size");
Long previousSize = sizeProperty.getLong();
logger.debug("Previous repository size: " + previousSize);
synchronized (sizeProperty) {
sizeProperty.setValue(previousSize + change);
session.save();
}
logger.debug("Current repository size: " + sizeProperty.getLong());
}

protected Long getRepositorySize(Session session)
throws ValueFormatException, PathNotFoundException,
RepositoryException {
return session.getNode("/objects").getProperty("size").getLong();
}
}
19 changes: 0 additions & 19 deletions fcrepo-http-commons/src/main/java/org/fcrepo/Constants.java

This file was deleted.

@@ -0,0 +1,19 @@

package org.fcrepo.exceptionhandlers;

import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;

import javax.jcr.security.AccessControlException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class AccessControlExceptionMapper implements
ExceptionMapper<AccessControlException> {

@Override
public Response toResponse(AccessControlException arg0) {
return status(FORBIDDEN).build();
}

}
@@ -0,0 +1,19 @@

package org.fcrepo.exceptionhandlers;

import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;

import javax.jcr.PathNotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class PathNotFoundExceptionMapper implements
ExceptionMapper<PathNotFoundException> {

@Override
public Response toResponse(PathNotFoundException arg0) {
return status(NOT_FOUND).build();
}

}

0 comments on commit 3c8073d

Please sign in to comment.