Skip to content

Commit

Permalink
Improved namespace JAX-RS resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 12, 2013
1 parent 165e352 commit 798627c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 95 deletions.
@@ -1,15 +1,14 @@

package org.fcrepo.api.legacy;

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.created;
import static javax.ws.rs.core.Response.ok;

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

import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
Expand All @@ -22,8 +21,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.responses.NamespaceListing;
import org.fcrepo.jaxb.responses.NamespaceListing.Namespace;
Expand All @@ -44,93 +41,68 @@
@Path("/namespaces")
public class FedoraNamespaces extends AbstractResource {

@POST
@Path("/{ns}")
public Response registerObjectNamespace(@PathParam("ns") final String ns)
throws RepositoryException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();
r.registerNamespace(ns, "info:fedora/" + ns);
session.logout();
return Response.ok().entity(ns).build();
}

@GET
@Path("/{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();

if (ImmutableSet.copyOf(r.getPrefixes()).contains(prefix)) {
session.logout();
return ok("{ \"" + prefix + "\":\"" + r.getURI(prefix) + "\" }")
.build();
} else {
session.logout();
return four04;
}
}

@POST
@Path("")
@Consumes(APPLICATION_JSON)
public Response registerObjectNamespaceJSON(final InputStream message)
throws RepositoryException, JsonParseException,
JsonMappingException, IOException {

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

@SuppressWarnings("unchecked")
final Map<String, String> nses = mapper.readValue(message, Map.class);
for (final Map.Entry<String, String> entry : nses.entrySet()) {
r.registerNamespace(entry.getKey(), entry.getValue());
}
session.logout();
return ok(nses).build();
}

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

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

StringBuffer out = new StringBuffer();
String[] uris = r.getURIs();
String[] prefixes = r.getPrefixes();
for (int i = 0; i < uris.length; i++) {
out.append(prefixes[i] + " : " + uris[i] + "\n");
}
session.logout();
return ok(out.toString()).build();
}

@GET
@Path("")
@Produces({ TEXT_XML, APPLICATION_JSON })
public NamespaceListing getObjectNamespacesInXML()
throws RepositoryException, IOException {

final Session session = repo.login();
final NamespaceRegistry r = session.getWorkspace()
.getNamespaceRegistry();
final Builder<Namespace> b = builder();
for (final String prefix : r.getPrefixes()) {
b.add(new Namespace(prefix, URI.create(r.getURI(prefix))));
}
session.logout();
return new NamespaceListing(b.build());
}
@POST
@Path("/{prefix}")
public Response registerObjectNamespace(@PathParam("prefix")
final String prefix, final String uri) throws RepositoryException {

final Session session = repo.login();
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
r.registerNamespace(prefix, uri);
session.logout();
return created(uriInfo.getAbsolutePath()).build();
}

@POST
@Consumes({TEXT_XML, APPLICATION_JSON})
public Response registerObjectNamespaces(final NamespaceListing nses)
throws RepositoryException {

final Session session = repo.login();
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
for (Namespace ns : nses.namespaces)
r.registerNamespace(ns.prefix, ns.uri.toString());
session.logout();
return created(uriInfo.getAbsolutePath()).build();
}

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

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

if (ImmutableSet.copyOf(r.getPrefixes()).contains(prefix)) {
final Namespace ns =
new Namespace(prefix, URI.create(r.getURI(prefix)));
session.logout();
return ok(ns).build();
} else {
session.logout();
return four04;
}
}

@GET
@Produces({TEXT_XML, APPLICATION_JSON})
public NamespaceListing getNamespaces() throws RepositoryException,
IOException {

final Session session = repo.login();
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
final Builder<Namespace> b = builder();
for (final String prefix : r.getPrefixes()) {
b.add(new Namespace(prefix, URI.create(r.getURI(prefix))));
}
session.logout();
return new NamespaceListing(b.build());
}

}
2 changes: 1 addition & 1 deletion fcrepo-webapp/src/main/resources/config/repository.json
Expand Up @@ -44,7 +44,7 @@
"sequencers" : {
"FOXML Eater" : {
"description" : "FOXML Files loaded under 'fedora:/foxml' and extracted into 'fedora:/'",
"classname" : "org.fcrepo.foxml.FOXMLSequencer",
"classname" : "org.fcrepo.api.legacy.foxml.FOXMLSequencer",
"pathExpressions" : ["fedora:/foxml/(.*)/jcr:content[@jcr:data] => fedora:/$1"]
}
}
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-webapp/src/main/resources/spring/rest.xml
Expand Up @@ -31,7 +31,7 @@
<bean class="org.fcrepo.api.legacy.FedoraIdentifiers"/>
<bean class="org.fcrepo.api.legacy.FedoraNamespaces"/>
<bean class="org.fcrepo.api.legacy.FedoraObjects"/>
<bean class="org.fcrepo.foxml.FedoraOXML"/>
<bean class="org.fcrepo.api.legacy.foxml.FedoraOXML"/>
<bean class="org.fcrepo.generator.DublinCore" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
Expand Down

0 comments on commit 798627c

Please sign in to comment.