Add dspace-rest, using Jersey, light support for Collection / Community

This commit is contained in:
Peter Dietz
2013-09-19 13:35:40 -04:00
parent 84e9f8e718
commit 6ac5842e0a
11 changed files with 544 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
package org.dspace.rest;
import org.dspace.content.Collection;
import org.dspace.core.Context;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.SQLException;
import java.util.ArrayList;
/*
The "Path" annotation indicates the URI this class will be available at relative to your base URL. For
example, if this web-app is launched at localhost using a context of "hello" and no URL pattern is defined
in the web.xml servlet mapping section, then the web service will be available at:
http://localhost:8080/<webapp>/collections
*/
@Path("/collections")
public class CollectionsResource {
final String collectionsPath = "/dspace-rest/collections/";
private static Context context;
/*
The "GET" annotation indicates this method will respond to HTTP Get requests.
The "Produces" annotation indicates the MIME response the method will return.
*/
@GET
@Path("/")
@Produces(MediaType.TEXT_HTML)
public String listHTML() {
StringBuilder everything = new StringBuilder();
try {
Context context = new Context();
Collection[] collections = Collection.findAll(context);
for(Collection collection : collections) {
everything.append("<li><a href='" + collectionsPath + collection.getID() + "'>" + collection.getID() + " - " + collection.getName() + "</a></li>\n");
}
} catch (SQLException e) {
return "ERROR: " + e.getMessage();
}
return "<html><title>Hello!</title><body>Collections<br/><ul>" + everything.toString() + "</ul>.</body></html> ";
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Collection[] list(@QueryParam("expand") String expand) {
try {
if(context == null || !context.isValid() ) {
context = new Context();
}
Collection[] collections = Collection.findAll(context);
ArrayList<org.dspace.rest.common.Collection> collectionArrayList = new ArrayList<org.dspace.rest.common.Collection>();
for(Collection collection : collections) {
org.dspace.rest.common.Collection restCollection = new org.dspace.rest.common.Collection(collection, expand);
collectionArrayList.add(restCollection);
}
return collectionArrayList.toArray(new org.dspace.rest.common.Collection[0]);
} catch (SQLException e) {
return null;
}
}
@GET
@Path("/{collection_id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Collection getCollection(@PathParam("collection_id") Integer collection_id, @QueryParam("expand") String expand) {
return new org.dspace.rest.common.Collection(collection_id, expand);
}
}