Skip to content

Commit

Permalink
For legacy-api, less returning JAX-RS Response instances, more return…
Browse files Browse the repository at this point in the history
…ing actual Fedora classes
  • Loading branch information
ajs6f committed Feb 21, 2013
1 parent efe1db1 commit 92209d6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 90 deletions.
29 changes: 2 additions & 27 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -2,29 +2,23 @@
package org.fcrepo;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static org.fcrepo.utils.FedoraJcrTypes.DC_TITLE;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.value2string;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import java.io.InputStream;
import java.util.Collection;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.version.VersionException;

import org.modeshape.common.SystemFailureException;

import com.google.common.base.Function;

public class Datastream {

Node node;
Expand Down Expand Up @@ -72,23 +66,4 @@ public void setLabel(String label) throws ValueFormatException,
node.getSession().save();
}

private Function<Value, String> value2string =
new Function<Value, String>() {

@Override
public String apply(Value v) {
try {
return v.getString();
} catch (RepositoryException e) {
throw new SystemFailureException(e);
} catch (IllegalStateException e) {
throw new SystemFailureException(e);
}
}
};

private static <From, To> Collection<To> map(From[] input,
Function<From, To> f) {
return transform(copyOf(input), f);
}
}
Expand Up @@ -10,11 +10,11 @@ public class PathService {
.getLogger(DatastreamService.class);


public String getObjectJcrNodePath(String pid) {
return "objects/" + pid;
public static String getObjectJcrNodePath(String pid) {
return "/objects/" + pid;
}

public String getDatastreamJcrNodePath(String pid, String dsid) {
public static String getDatastreamJcrNodePath(String pid, String dsid) {
return getObjectJcrNodePath(pid) + "/" + dsid;
}
}
29 changes: 27 additions & 2 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/FedoraTypesUtils.java
Expand Up @@ -10,6 +10,7 @@

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;

import org.modeshape.common.SystemFailureException;
Expand All @@ -32,7 +33,7 @@ public boolean apply(Node node) {
}
}
};

static public Predicate<Node> isFedoraDatastream = new Predicate<Node>() {

@Override
Expand All @@ -56,8 +57,32 @@ public String apply(NodeType t) {
}
};

private static <From, To> Collection<To> map(From[] input,
public static Function<Value, String> value2string =
new Function<Value, String>() {

@Override
public String apply(Value v) {
try {
return v.getString();
} catch (RepositoryException e) {
throw new SystemFailureException(e);
} catch (IllegalStateException e) {
throw new SystemFailureException(e);
}
}
};

public static <From, To> Collection<To> map(From[] input,
Function<From, To> f) {
return transform(copyOf(input), f);
}

public static Function<NodeType, String> nodetype2string =
new Function<NodeType, String>() {

@Override
public String apply(NodeType type) {
return type.getName();
}
};
}
Expand Up @@ -2,16 +2,20 @@

import org.junit.Test;

import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
import static org.junit.Assert.assertEquals;

public class PathServiceTest {
@Test
public void testGetObjectJcrNodePath() throws Exception {
assertEquals("objects/test:123", new PathService().getObjectJcrNodePath("test:123"));
new PathService();
assertEquals("/objects/test:123", getObjectJcrNodePath("test:123"));
}

@Test
public void testGetDatastreamJcrNodePath() throws Exception {
assertEquals("objects/test:123/asdf", new PathService().getDatastreamJcrNodePath("test:123", "asdf"));
new PathService();
assertEquals("/objects/test:123/asdf", getDatastreamJcrNodePath("test:123", "asdf"));
}
}
Expand Up @@ -13,6 +13,8 @@
import static org.fcrepo.services.DatastreamService.createDatastreamNode;
import static org.fcrepo.services.DatastreamService.getDatastreamNode;
import static org.fcrepo.services.ObjectService.getObjectNode;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

Expand Down Expand Up @@ -72,7 +74,7 @@ public class FedoraDatastreams extends AbstractResource {
@GET
@Path("/")
@Produces({TEXT_XML, APPLICATION_JSON})
public Response getDatastreams(@PathParam("pid")
public ObjectDatastreams getDatastreams(@PathParam("pid")
final String pid) throws RepositoryException, IOException {

final ObjectDatastreams objectDatastreams = new ObjectDatastreams();
Expand All @@ -85,7 +87,7 @@ public Response getDatastreams(@PathParam("pid")
getDSMimeType(ds)));
}
objectDatastreams.datastreams = datastreams.build();
return ok(objectDatastreams).build();
return objectDatastreams;

}

Expand All @@ -98,12 +100,12 @@ public Response addDatastreams(@PathParam("pid")
final Session session = repo.login();
try {
Long oldObjectSize =
getObjectSize(session.getNode("/objects/" + pid));
getObjectSize(session.getNode(getObjectJcrNodePath(pid)));

for (final Attachment a : attachmentList) {
final String dsid =
a.getContentDisposition().getParameter("name");
final String dsPath = "/objects/" + pid + "/" + dsid;
final String dsPath = getDatastreamJcrNodePath(pid, dsid);
createDatastreamNode(session, dsPath, a.getDataHandler()
.getContentType(), a.getDataHandler().getInputStream());

Expand All @@ -115,8 +117,8 @@ public Response addDatastreams(@PathParam("pid")
* persisting session state creates new system-curated nodes and
* properties which contribute to the footprint of this resource
*/
updateRepositorySize(getObjectSize(session.getNode("/objects/" +
pid)) -
updateRepositorySize(getObjectSize(session
.getNode(getObjectJcrNodePath(pid))) -
oldObjectSize, session);
// now we save again to persist the repo size
session.save();
Expand Down Expand Up @@ -154,7 +156,7 @@ public Response addDatastream(@PathParam("pid")
contentType =
contentType != null ? contentType
: APPLICATION_OCTET_STREAM_TYPE;
String dspath = "/objects/" + pid + "/" + dsid;
String dspath = getDatastreamJcrNodePath(pid, dsid);

if (!session.nodeExists(dspath)) {
return created(
Expand Down Expand Up @@ -196,7 +198,7 @@ public Response modifyDatastream(@PathParam("pid")
contentType =
contentType != null ? contentType
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
String dspath = "/objects/" + pid + "/" + dsid;
String dspath = getDatastreamJcrNodePath(pid, dsid);

return created(
addDatastreamNode(pid, dspath, contentType, requestBodyStream,
Expand All @@ -208,7 +210,8 @@ private URI addDatastreamNode(final String pid, final String dsPath,
final MediaType contentType, final InputStream requestBodyStream,
final Session session) throws RepositoryException, IOException {

Long oldObjectSize = getObjectSize(session.getNode("/objects/" + pid));
Long oldObjectSize =
getObjectSize(session.getNode(getObjectJcrNodePath(pid)));
logger.debug("Attempting to add datastream node at path: " + dsPath);
try {
boolean created = session.nodeExists(dsPath);
Expand All @@ -221,8 +224,8 @@ private URI addDatastreamNode(final String pid, final String dsPath,
* persisting session state creates new system-curated nodes and
* properties which contribute to the footprint of this resource
*/
updateRepositorySize(getObjectSize(session.getNode("/objects/" +
pid)) -
updateRepositorySize(getObjectSize(session
.getNode(getObjectJcrNodePath(pid))) -
oldObjectSize, session);
// now we save again to persist the repo size
session.save();
Expand Down Expand Up @@ -296,7 +299,7 @@ public Response getDatastreamContent(@PathParam("pid")
@Produces({TEXT_XML, APPLICATION_JSON})
// TODO implement this after deciding on a versioning model
public
Response getDatastreamHistory(@PathParam("pid")
DatastreamHistory getDatastreamHistory(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException, IOException {

Expand All @@ -305,7 +308,7 @@ Response getDatastreamHistory(@PathParam("pid")
new DatastreamHistory(singletonList(getDSProfile(ds.getNode())));
dsHistory.dsID = dsid;
dsHistory.pid = pid;
return ok(dsHistory).build();
return dsHistory;
}

/**
Expand All @@ -327,7 +330,7 @@ Response getDatastreamHistory(@PathParam("pid")
@Path("/{dsid}/history")
@Produces(TEXT_XML)
@Deprecated
public Response getDatastreamHistoryOld(@PathParam("pid")
public DatastreamHistory getDatastreamHistoryOld(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException, IOException {
return getDatastreamHistory(pid, dsid);
Expand All @@ -348,7 +351,7 @@ public Response getDatastreamHistoryOld(@PathParam("pid")
public Response deleteDatastream(@PathParam("pid")
String pid, @PathParam("dsid")
String dsid) throws RepositoryException {
final String dsPath = "/objects/" + pid + "/" + dsid;
final String dsPath = getDatastreamJcrNodePath(pid , dsid);
final Session session = repo.login();
final Node ds = session.getNode(dsPath);
updateRepositorySize(0L - getDatastreamSize(ds), session);
Expand Down
Expand Up @@ -2,8 +2,6 @@
package org.fcrepo.api.legacy;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.created;
Expand All @@ -12,18 +10,19 @@
import static org.fcrepo.jaxb.responses.ObjectProfile.ObjectStates.A;
import static org.fcrepo.services.ObjectService.createObjectNode;
import static org.fcrepo.services.ObjectService.getObjectNode;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
import static org.fcrepo.utils.FedoraJcrTypes.DC_TITLE;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.nodetype2string;
import static org.fcrepo.utils.FedoraTypesUtils.value2string;

import java.io.IOException;
import java.util.Collection;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -36,12 +35,9 @@

import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.responses.ObjectProfile;
import org.modeshape.common.SystemFailureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;

@Path("/objects")
public class FedoraObjects extends AbstractResource {

Expand All @@ -52,7 +48,7 @@ public class FedoraObjects extends AbstractResource {
public Response getObjects() throws RepositoryException {
final Session session = repo.login();
try {
Node objects = session.getNode("/objects");
Node objects = session.getNode(getObjectJcrNodePath(""));
StringBuffer nodes = new StringBuffer();

for (NodeIterator i = objects.getNodes(); i.hasNext();) {
Expand Down Expand Up @@ -99,7 +95,7 @@ public Response ingest(@PathParam("pid")

final Session session = repo.login();
try {
final Node obj = createObjectNode(session, "/objects/" + pid);
final Node obj = createObjectNode(session, getObjectJcrNodePath(pid));
session.save();
/*
* we save before updating the repo size because the act of
Expand All @@ -120,7 +116,7 @@ public Response ingest(@PathParam("pid")
@GET
@Path("/{pid}")
@Produces({TEXT_XML, APPLICATION_JSON})
public Response getObject(@PathParam("pid")
public ObjectProfile getObject(@PathParam("pid")
final String pid) throws RepositoryException, IOException {

final Node obj = getObjectNode(pid);
Expand All @@ -147,7 +143,7 @@ public Response getObject(@PathParam("pid")
uriInfo.getAbsolutePathBuilder().path("datastreams").build();
objectProfile.objState = A;
objectProfile.objModels = map(obj.getMixinNodeTypes(), nodetype2string);
return ok(objectProfile).build();
return objectProfile;

}

Expand All @@ -156,7 +152,7 @@ public Response getObject(@PathParam("pid")
public Response deleteObject(@PathParam("pid")
final String pid) throws RepositoryException {
final Session session = repo.login();
final Node obj = session.getNode("/objects/" + pid);
final Node obj = session.getNode(getObjectJcrNodePath(pid));
updateRepositorySize(0L - getObjectSize(obj), session);
return deleteResource(obj);
}
Expand Down Expand Up @@ -186,32 +182,4 @@ private static Long getObjectDSSize(Node obj) throws RepositoryException {
return size;
}

private Function<Value, String> value2string =
new Function<Value, String>() {

@Override
public String apply(Value v) {
try {
return v.getString();
} catch (RepositoryException e) {
throw new SystemFailureException(e);
} catch (IllegalStateException e) {
throw new SystemFailureException(e);
}
}
};

private static <From, To> Collection<To> map(From[] input,
Function<From, To> f) {
return transform(copyOf(input), f);
}

private Function<NodeType, String> nodetype2string =
new Function<NodeType, String>() {

@Override
public String apply(NodeType type) {
return type.getName();
}
};
}

0 comments on commit 92209d6

Please sign in to comment.