Skip to content

Commit

Permalink
Almost done with JAXB
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 5, 2013
1 parent 2bd5b0e commit 0a0b029
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 91 deletions.
51 changes: 25 additions & 26 deletions src/main/java/org/fcrepo/modeshape/FedoraDatastreams.java
@@ -1,8 +1,9 @@
package org.fcrepo.modeshape;

import static com.google.common.collect.ImmutableMap.builder;
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_XML;
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;

Expand All @@ -13,10 +14,11 @@

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
Expand All @@ -27,8 +29,8 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.fcrepo.modeshape.jaxb.responses.DatastreamProfile;
import org.fcrepo.modeshape.jaxb.responses.ObjectDatastreams;
import org.fcrepo.modeshape.jaxb.responses.ObjectDatastreams.Datastream;
import org.modeshape.jcr.api.Binary;
Expand All @@ -37,8 +39,7 @@
import org.slf4j.LoggerFactory;

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

import freemarker.template.TemplateException;

Expand Down Expand Up @@ -69,16 +70,13 @@ public Response getDatastreams(@PathParam("pid") final String pid)

if (session.nodeExists("/" + pid)) {
final ObjectDatastreams objectDatastreams = new ObjectDatastreams();
final ImmutableSet.Builder<Datastream> datastreams = ImmutableSet
.builder();
final Builder<Datastream> datastreams = builder();

NodeIterator i = session.getNode("/" + pid).getNodes();
while (i.hasNext()) {
final Node ds = i.nextNode();
final Binary content = (Binary) ds.getNode(JCR_CONTENT)
.getProperty(JCR_DATA).getBinary();
datastreams.add(new Datastream(ds.getName(), ds.getName(),
content.getMimeType()));
getDSMimeType(ds)));
}
objectDatastreams.datastreams = datastreams.build();
session.logout();
Expand Down Expand Up @@ -232,12 +230,10 @@ private URI addDatastreamNode(final String dsPath,
ds.setProperty("fedora:created", Calendar.getInstance());
}
ds.setProperty("jcr:lastModified", Calendar.getInstance());
final String dsId = ds.getName();
session.save();
session.logout();
logger.debug("Finished adding datastream node at path: " + dsPath);
final UriBuilder ub = uriInfo.getAbsolutePathBuilder();
return ub.path(dsId).build();
return uriInfo.getAbsolutePath();
}

/**
Expand Down Expand Up @@ -268,20 +264,16 @@ public Response getDatastream(@PathParam("pid") final String pid,
final Node obj = session.getNode("/" + pid);

if (obj.hasNode(dsid)) {
Node ds = obj.getNode(dsid);
PropertyIterator i = ds.getProperties();
Builder<String, String> b = builder();
while (i.hasNext()) {
Property p = i.nextProperty();
b.put(p.getName(), p.toString());
}

final InputStream content = renderTemplate("datastreamProfile.ftl",
ImmutableMap.of("ds", ds, "properties", b.build(), "obj",
ds.getParent()));

final Node ds = obj.getNode(dsid);
DatastreamProfile dsProfile = new DatastreamProfile();
dsProfile.dsID = dsid;
dsProfile.pid = pid;
dsProfile.dsLabel = dsid;
dsProfile.dsState = A;
dsProfile.dsMIME = getDSMimeType(ds);
dsProfile.dsCreateDate = ds.getProperty("jcr:created").getString();
session.logout();
return Response.ok().entity(content).build();
return Response.ok(dsProfile).build();
} else {
session.logout();
return four04;
Expand Down Expand Up @@ -397,4 +389,11 @@ public Response deleteDatastream(@PathParam("pid") String pid,
@PathParam("dsid") String dsid) throws RepositoryException {
return deleteResource("/" + pid + "/" + dsid);
}

private String getDSMimeType(Node ds) throws ValueFormatException,
PathNotFoundException, RepositoryException, IOException {
Binary b = (Binary) ds.getNode(JCR_CONTENT).getProperty(JCR_DATA)
.getBinary();
return b.getMimeType();
}
}
40 changes: 22 additions & 18 deletions src/main/java/org/fcrepo/modeshape/FedoraNamespaces.java
@@ -1,7 +1,10 @@
package org.fcrepo.modeshape;

import static com.google.common.collect.ImmutableSet.builder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;

import javax.jcr.NamespaceRegistry;
Expand All @@ -17,10 +20,11 @@

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.fcrepo.modeshape.jaxb.responses.NamespaceListing;
import org.fcrepo.modeshape.jaxb.responses.NamespaceListing.Namespace;

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

import com.google.common.collect.ImmutableSet.Builder;
import freemarker.template.TemplateException;

/**
Expand All @@ -40,9 +44,10 @@ public class FedoraNamespaces extends AbstractResource {
@Path("/{ns}")
public Response registerObjectNamespace(@PathParam("ns") final String ns)
throws RepositoryException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace().getNamespaceRegistry();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();
r.registerNamespace(ns, "info:fedora/" + ns);
session.logout();
return Response.ok().entity(ns).build();
Expand All @@ -53,9 +58,10 @@ public Response registerObjectNamespace(@PathParam("ns") final String ns)
@Produces("application/json")
public Response retrieveObjectNamespace(@PathParam("ns") final String prefix)
throws RepositoryException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace().getNamespaceRegistry();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();

if (ImmutableSet.copyOf(r.getPrefixes()).contains(prefix)) {
session.logout();
Expand All @@ -77,7 +83,8 @@ public Response registerObjectNamespaceJSON(final InputStream message)
JsonMappingException, IOException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace().getNamespaceRegistry();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();

@SuppressWarnings("unchecked")
final Map<String, String> nses = mapper.readValue(message, Map.class);
Expand All @@ -94,7 +101,8 @@ public Response registerObjectNamespaceJSON(final InputStream message)
public Response getObjectNamespaces() throws RepositoryException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace().getNamespaceRegistry();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();

StringBuffer out = new StringBuffer();
String[] uris = r.getURIs();
Expand All @@ -111,20 +119,16 @@ public Response getObjectNamespaces() throws RepositoryException {
@Produces("text/xml")
public Response getObjectNamespacesInXML() throws RepositoryException,
IOException, TemplateException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace().getNamespaceRegistry();

final ImmutableMap.Builder<String, Object> b = ImmutableMap.builder();
final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();
final Builder<Namespace> b = builder();
for (final String prefix : r.getPrefixes()) {
b.put(prefix, r.getURI(prefix));
b.add(new Namespace(prefix, URI.create(r.getURI(prefix))));
}
session.logout();
return Response
.ok()
.entity(renderTemplate("namespaceRegistry.ftl",
ImmutableMap.of("namespaces", (Object) b.build())))
.build();
return Response.ok(new NamespaceListing(b.build())).build();
}

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

import static org.fcrepo.modeshape.jaxb.responses.ObjectProfile.ObjectStates.A;

import java.io.IOException;
import java.util.Calendar;

Expand All @@ -14,9 +16,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.fcrepo.modeshape.jaxb.FedoraObjectStates;
import org.fcrepo.modeshape.jaxb.responses.ObjectProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -70,8 +70,7 @@ public Response ingest(@PathParam("pid") final String pid)
session.save();
session.logout();
logger.debug("Finished ingest with pid: " + pid);
final UriBuilder ub = uriInfo.getAbsolutePathBuilder();
return Response.created(ub.path(pid).build()).build();
return Response.created(uriInfo.getAbsolutePath()).build();
} else {
session.logout();
return four01;
Expand All @@ -88,8 +87,6 @@ public Response getObjectInXML(@PathParam("pid") final String pid)

if (session.nodeExists("/" + pid)) {

final UriBuilder ub = uriInfo.getAbsolutePathBuilder();

final Node obj = session.getNode("/" + pid);
final ObjectProfile objectProfile = new ObjectProfile();

Expand All @@ -100,8 +97,9 @@ public Response getObjectInXML(@PathParam("pid") final String pid)
.getString();
objectProfile.objLastModDate = obj.getProperty("jcr:lastModified")
.getString();
objectProfile.objItemIndexViewURL = ub.path("datastreams").build();
objectProfile.objState = FedoraObjectStates.valueOf("A");
objectProfile.objItemIndexViewURL = uriInfo
.getAbsolutePathBuilder().path("datastreams").build();
objectProfile.objState = A;

session.logout();
return Response.ok(objectProfile).build();
Expand Down

This file was deleted.

@@ -0,0 +1,67 @@
package org.fcrepo.modeshape.jaxb.responses;

import java.net.URI;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "datastreamProfile", namespace = "http://www.fedora.info/definitions/1/0/management/")
public class DatastreamProfile {

@XmlAttribute
public String pid;

@XmlAttribute
public String dsID;

@XmlAttribute
public String dsLabel;

@XmlAttribute
public String dsVersionID;

@XmlAttribute
public String dsCreateDate;

@XmlAttribute
public DatastreamStates dsState;

@XmlAttribute
public String dsMIME;

@XmlAttribute
public URI dsFormatURI;

@XmlAttribute
public DatastreamControlGroup dsControlGroup;

@XmlAttribute
public String dsSize;

@XmlAttribute
public String dsVersionable;

@XmlAttribute
public String dsInfoType;

@XmlAttribute
public String dsLocation;

@XmlAttribute
public String dsLocationType;

@XmlAttribute
public String dsChecksumType;

@XmlAttribute
public String dsChecksum;

public static enum DatastreamControlGroup {
M, E, R
}

public static enum DatastreamStates {
A, D, I
}

}
@@ -0,0 +1,40 @@
package org.fcrepo.modeshape.jaxb.responses;

import java.net.URI;
import java.util.Set;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "namespaceRegistry", namespace = "http://www.fedora.info/definitions/1/0/management/")
public class NamespaceListing {

@XmlElement(name = "namespace")
public Set<Namespace> namespaces;

public NamespaceListing(Set<Namespace> nses) {
this.namespaces = nses;
}

public NamespaceListing() {
}

public static class Namespace {

@XmlAttribute
public String prefix;

@XmlAttribute(name = "URI")
public URI uri;

public Namespace(String prefix, URI uri) {
this.prefix = prefix;
this.uri = uri;
}

public Namespace() {
}
}

}

0 comments on commit 0a0b029

Please sign in to comment.