Skip to content

Commit

Permalink
Add dspace-rest, using Jersey, light support for Collection / Community
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdietz committed Sep 19, 2013
1 parent 84e9f8e commit 6ac5842
Show file tree
Hide file tree
Showing 11 changed files with 544 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dspace-rest/LICENSE
@@ -0,0 +1,5 @@
The contents of this file are subject to the license and copyright
detailed in the LICENSE and NOTICE files at the root of the source
tree and available online at

http://www.dspace.org/license/
16 changes: 16 additions & 0 deletions dspace-rest/README
@@ -0,0 +1,16 @@
A RESTful web services API for DSpace.

Built on JERSEY

mvn clean package
Deploy /target/rest.war to tomcat.

Endpoints:
- http://localhost:8080/dspace-rest/collections
-- http://localhost:8080/dspace-rest/collections/{collectionID}
-- http://localhost:8080/dspace-rest/collections/{collectionID}?expand=parentCommunityID,parentCommunityIDList,itemIDList
-- http://localhost:8080/dspace-rest/collections/123

- http://localhost:8080/dspace-rest/communities

There is an ?expand= query parameter for expensive operations. You can tack it on the end of /collections endpoints. It is optional, all, some or none.
61 changes: 61 additions & 0 deletions dspace-rest/pom.xml
@@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.dspace.rest</groupId>
<artifactId>dspace-rest</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>DSpace RESTful web services API</name>
<url>http://demo.dspace.org</url>
<dependencies>
<!-- Jersey, for RESTful web services -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
</dependency>
<!-- JSON serialization, should I use jackson?-->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>

<!-- Use DSpace, for now, an older version to minimize spring generated dependency on Discovery -->
<dependency>
<groupId>org.dspace</groupId>
<artifactId>dspace-api</artifactId>
<version>1.8.2</version>
</dependency>

<!-- Connecting to DSpace datasource sets a dependency on Postgres DB-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<finalName>${artifactId}</finalName>
</build>
</project>
77 changes: 77 additions & 0 deletions dspace-rest/src/main/java/org/dspace/rest/CollectionsResource.java
@@ -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);
}
}
46 changes: 46 additions & 0 deletions dspace-rest/src/main/java/org/dspace/rest/CommunitiesResource.java
@@ -0,0 +1,46 @@
package org.dspace.rest;

import org.dspace.content.Community;
import org.dspace.core.Context;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.sql.SQLException;

/*
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>/communities
*/
@Path("/communities")
public class CommunitiesResource {
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
@Produces(MediaType.TEXT_HTML)
public String list() {
StringBuilder everything = new StringBuilder();
try {
if(context == null || !context.isValid() ) {
context = new Context();
}
Community[] communities = Community.findAllTop(context);
for(Community community : communities) {
everything.append(community.getName() + "<br/>\n");
}

} catch (SQLException e) {
return "ERROR: " + e.getMessage();
}

return "<html><title>Hello!</title><body>Communities:<br/>" + everything.toString() + ".</body></html> ";
}
}
27 changes: 27 additions & 0 deletions dspace-rest/src/main/java/org/dspace/rest/HelloWorld.java
@@ -0,0 +1,27 @@
package org.dspace.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/*
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>/helloworld
*/
@Path("/helloworld")
public class HelloWorld {

/*
The "GET" annotation indicates this method will respond to HTTP Get requests.
The "Produces" annotation indicates the MIME response the method will return.
*/
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html><title>Hello!</title><body>Hello, world.</body></html> ";
}
}

0 comments on commit 6ac5842

Please sign in to comment.