Skip to content

Commit

Permalink
Changed to use MediaType constants instead of literal strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 5, 2013
1 parent b6f6fff commit 8578b27
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 53 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/fcrepo/modeshape/AbstractResource.java
@@ -1,5 +1,7 @@
package org.fcrepo.modeshape;

import static javax.ws.rs.core.Response.noContent;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.LoginException;
Expand Down Expand Up @@ -86,9 +88,9 @@ protected synchronized Response deleteResource(final String path)
session.save();
session.logout();
logger.debug("Finished deleting resource at path: " + path);
return Response.status(204).build();
return noContent().build();
} else {
return four01;
return four03;
}
} else {
return four04;
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/fcrepo/modeshape/Constants.java
@@ -1,17 +1,19 @@
package org.fcrepo.modeshape;

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

import javax.ws.rs.core.Response;

/**
* Convenience class for constructs useful to many JAX-RS methods.
*
* @author ajs6f
*
*
*/
public abstract class Constants {

protected static final Response four01 = Response.status(404).entity("401")
.build();
protected static final Response four04 = Response.status(404).entity("404")
.build();

protected static final Response four03 = status(FORBIDDEN).build();
protected static final Response four04 = status(NOT_FOUND).build();
}
50 changes: 26 additions & 24 deletions src/main/java/org/fcrepo/modeshape/FedoraDatastreams.java
Expand Up @@ -4,9 +4,14 @@
import static java.util.Collections.singletonList;
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;
import static javax.ws.rs.core.Response.notAcceptable;
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.modeshape.jaxb.responses.DatastreamProfile.DatastreamStates.A;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
import static org.modeshape.jcr.api.JcrConstants.NT_FILE;
import static org.modeshape.jcr.api.JcrConstants.NT_RESOURCE;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -36,7 +41,6 @@
import org.fcrepo.modeshape.jaxb.responses.ObjectDatastreams;
import org.fcrepo.modeshape.jaxb.responses.ObjectDatastreams.Datastream;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.JcrConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -79,7 +83,7 @@ public Response getDatastreams(@PathParam("pid") final String pid)
}
objectDatastreams.datastreams = datastreams.build();
session.logout();
return Response.ok().entity(objectDatastreams).build();
return ok(objectDatastreams).build();
} else {
session.logout();
return four04;
Expand Down Expand Up @@ -117,30 +121,30 @@ public Response addDatastream(@PathParam("pid") final String pid,
if (!session.nodeExists("/" + pid)) {
logger.debug("Tried to create a datastream for an object that doesn't exist, at resource path: "
+ dspath);
return Response.notAcceptable(null).build();
return notAcceptable(null).build();
}

if (session.hasPermission(dspath, "add_node")) {
if (!session.nodeExists(dspath)) {
return Response.created(
return created(
addDatastreamNode(dspath, contentType,
requestBodyStream, session)).build();
} else {
if (session.hasPermission(dspath, "remove")) {
session.getNode(dspath).remove();
session.save();
return Response.created(
return created(
addDatastreamNode(dspath, contentType,
requestBodyStream, session)).build();

} else {
session.logout();
return four01;
return four03;
}
}
} else {
session.logout();
return four01;
return four03;
}
}

Expand Down Expand Up @@ -178,7 +182,7 @@ public Response modifyDatastream(@PathParam("pid") final String pid,
session)).build();
} else {
session.logout();
return four01;
return four03;
}
}

Expand All @@ -192,17 +196,16 @@ private URI addDatastreamNode(final String dsPath,
created = true;
}

final Node ds = jcrTools.findOrCreateNode(session, dsPath,
JcrConstants.NT_FILE);
final Node ds = jcrTools.findOrCreateNode(session, dsPath, NT_FILE);
ds.addMixin("fedora:datastream");
final Node contentNode = jcrTools.findOrCreateChild(ds,
JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE);
final Node contentNode = jcrTools.findOrCreateChild(ds, JCR_CONTENT,
NT_RESOURCE);
logger.debug("Created content node at path: " + contentNode.getPath());
/*
* This next line of code deserves explanation. If we chose for the
* simpler line:
*
* Property dataProperty = contentNode.setProperty("jcr:data",
* Property dataProperty = contentNode.setProperty(JCR_DATA,
* requestBodyStream);
*
* then the JCR would not block on the stream's completion, and we would
Expand All @@ -217,8 +220,8 @@ private URI addDatastreamNode(final String dsPath,
* code may still be useful to us for an asychronous method that we
* develop later.
*/
Property dataProperty = contentNode.setProperty(JcrConstants.JCR_DATA,
session.getValueFactory().createBinary(requestBodyStream));
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());
Expand Down Expand Up @@ -249,7 +252,7 @@ private URI addDatastreamNode(final String dsPath,
*/
@GET
@Path("/{dsid}")
@Produces("text/xml")
@Produces(TEXT_XML)
public Response getDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid) throws RepositoryException,
IOException {
Expand All @@ -266,7 +269,7 @@ public Response getDatastream(@PathParam("pid") final String pid,
final Node ds = obj.getNode(dsid);
DatastreamProfile dsProfile = getDSProfile(ds);
session.logout();
return Response.ok(dsProfile).build();
return ok(dsProfile).build();
} else {
session.logout();
return four04;
Expand Down Expand Up @@ -296,11 +299,10 @@ public Response getDatastreamContent(@PathParam("pid") final String pid,
final String mimeType = ds.hasProperty("fedora:contentType") ? ds
.getProperty("fedora:contentType").getString()
: "application/octet-stream";
final InputStream responseStream = ds
.getNode(JcrConstants.JCR_CONTENT)
.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
final InputStream responseStream = ds.getNode(JCR_CONTENT)
.getProperty(JCR_DATA).getBinary().getStream();
session.logout();
return Response.ok(responseStream, mimeType).build();
return ok(responseStream, mimeType).build();
} else {
session.logout();
return four04;
Expand All @@ -321,7 +323,7 @@ public Response getDatastreamContent(@PathParam("pid") final String pid,
*/
@GET
@Path("/{dsid}/versions")
@Produces("text/xml")
@Produces(TEXT_XML)
// TODO implement this after deciding on a versioning model
public Response getDatastreamHistory(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid) throws RepositoryException,
Expand All @@ -337,7 +339,7 @@ public Response getDatastreamHistory(@PathParam("pid") final String pid,
dsHistory.dsID = dsid;
dsHistory.pid = pid;
session.logout();
return Response.ok().entity(dsHistory).build();
return ok(dsHistory).build();
} else {
session.logout();
return four04;
Expand All @@ -361,7 +363,7 @@ public Response getDatastreamHistory(@PathParam("pid") final String pid,
*/
@GET
@Path("/{dsid}/history")
@Produces("text/xml")
@Produces(TEXT_XML)
@Deprecated
public Response getDatastreamHistoryOld(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid) throws RepositoryException,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/fcrepo/modeshape/FedoraIdentifiers.java
Expand Up @@ -4,6 +4,7 @@
import static com.google.common.collect.DiscreteDomains.integers;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Ranges.closed;
import static javax.ws.rs.core.MediaType.TEXT_XML;

import java.io.IOException;

Expand Down Expand Up @@ -35,7 +36,7 @@ public class FedoraIdentifiers extends AbstractResource {
* @throws TemplateException
*/
@POST
@Produces("text/xml")
@Produces(TEXT_XML)
public NextPid getNextPid(
@QueryParam("numPids") @DefaultValue("1") Integer numPids)
throws RepositoryException, IOException {
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/fcrepo/modeshape/FedoraNamespaces.java
@@ -1,6 +1,10 @@
package org.fcrepo.modeshape;

import static com.google.common.collect.ImmutableSet.builder;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.ok;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -54,7 +58,7 @@ public Response registerObjectNamespace(@PathParam("ns") final String ns)

@GET
@Path("/{ns}")
@Produces("application/json")
@Produces(APPLICATION_JSON)
public Response retrieveObjectNamespace(@PathParam("ns") final String prefix)
throws RepositoryException {

Expand All @@ -64,10 +68,8 @@ public Response retrieveObjectNamespace(@PathParam("ns") final String prefix)

if (ImmutableSet.copyOf(r.getPrefixes()).contains(prefix)) {
session.logout();
return Response
.ok()
.entity("{ \"" + prefix + "\":\"" + r.getURI(prefix)
+ "\" }").build();
return ok("{ \"" + prefix + "\":\"" + r.getURI(prefix) + "\" }")
.build();
} else {
session.logout();
return four04;
Expand All @@ -76,7 +78,7 @@ public Response retrieveObjectNamespace(@PathParam("ns") final String prefix)

@POST
@Path("")
@Consumes("application/json")
@Consumes(APPLICATION_JSON)
public Response registerObjectNamespaceJSON(final InputStream message)
throws RepositoryException, JsonParseException,
JsonMappingException, IOException {
Expand All @@ -91,12 +93,12 @@ public Response registerObjectNamespaceJSON(final InputStream message)
r.registerNamespace(entry.getKey(), entry.getValue());
}
session.logout();
return Response.ok().entity(nses).build();
return ok(nses).build();
}

@GET
@Path("")
@Produces("text/plain")
@Produces(TEXT_PLAIN)
public Response getObjectNamespaces() throws RepositoryException {

final Session session = repo.login();
Expand All @@ -110,12 +112,12 @@ public Response getObjectNamespaces() throws RepositoryException {
out.append(prefixes[i] + " : " + uris[i] + "\n");
}
session.logout();
return Response.ok().entity(out.toString()).build();
return ok(out.toString()).build();
}

@GET
@Path("")
@Produces("text/xml")
@Produces(TEXT_XML)
public Response getObjectNamespacesInXML() throws RepositoryException,
IOException {

Expand All @@ -127,7 +129,7 @@ public Response getObjectNamespacesInXML() throws RepositoryException,
b.add(new Namespace(prefix, URI.create(r.getURI(prefix))));
}
session.logout();
return Response.ok(new NamespaceListing(b.build())).build();
return ok(new NamespaceListing(b.build())).build();
}

}
13 changes: 8 additions & 5 deletions src/main/java/org/fcrepo/modeshape/FedoraObjects.java
@@ -1,5 +1,8 @@
package org.fcrepo.modeshape;

import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.modeshape.jaxb.responses.ObjectProfile.ObjectStates.A;

import java.io.IOException;
Expand Down Expand Up @@ -39,7 +42,7 @@ public Response getObjects() throws RepositoryException {
+ "\n");
}
session.logout();
return Response.ok().entity(nodes.toString()).build();
return ok(nodes.toString()).build();

}

Expand Down Expand Up @@ -68,16 +71,16 @@ public Response ingest(@PathParam("pid") final String pid)
session.save();
session.logout();
logger.debug("Finished ingest with pid: " + pid);
return Response.created(uriInfo.getAbsolutePath()).build();
return created(uriInfo.getAbsolutePath()).build();
} else {
session.logout();
return four01;
return four03;
}
}

@GET
@Path("/{pid}")
@Produces("text/xml")
@Produces(TEXT_XML)
public Response getObjectInXML(@PathParam("pid") final String pid)
throws RepositoryException, IOException {

Expand All @@ -100,7 +103,7 @@ public Response getObjectInXML(@PathParam("pid") final String pid)
objectProfile.objState = A;

session.logout();
return Response.ok(objectProfile).build();
return ok(objectProfile).build();
} else {
session.logout();
return four04;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/fcrepo/modeshape/FedoraRepository.java
@@ -1,5 +1,8 @@
package org.fcrepo.modeshape;

import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.ok;

import java.io.IOException;
import java.util.Map;

Expand All @@ -10,6 +13,7 @@
import javax.jcr.nodetype.NodeTypeIterator;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonGenerationException;
Expand Down Expand Up @@ -66,14 +70,14 @@ public Response describeModeshape() throws JsonGenerationException,
}
repoproperties.put("node.types", nodetypes.build());

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

@GET
@Path("/describe")
@Produces(TEXT_XML)
public DescribeRepository describe() {
return new DescribeRepository();
}
Expand Down

0 comments on commit 8578b27

Please sign in to comment.