aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Terry2013-06-25 11:04:07 (EDT)
committerDavid Terry2013-06-27 15:59:39 (EDT)
commitc3fcd2d44386397c2cd2fccfef81a5bf21e842a2 (patch)
treeb8081cc333ecab096ceba30d70374d3c9c9af96e
parent40b9061dbfb5267f009f144ad9ac1b4c828f3703 (diff)
downloadorg.eclipse.lyo.rio-c3fcd2d44386397c2cd2fccfef81a5bf21e842a2.zip
org.eclipse.lyo.rio-c3fcd2d44386397c2cd2fccfef81a5bf21e842a2.tar.gz
org.eclipse.lyo.rio-c3fcd2d44386397c2cd2fccfef81a5bf21e842a2.tar.bz2
Bug 411606 - Update TRS implementation to conform to the 2.0refs/changes/99/14099/4
specification (2 of 4) Change-Id: I956a1744e574a7ad480f3a80ae984f347f0446a2 Signed-off-by: David Terry <dgterry@us.ibm.com>
-rw-r--r--org.eclipse.lyo.rio.trs/pom.xml4
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java28
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java62
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java36
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java35
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java32
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java29
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/ResponseUtil.java94
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java52
9 files changed, 293 insertions, 79 deletions
diff --git a/org.eclipse.lyo.rio.trs/pom.xml b/org.eclipse.lyo.rio.trs/pom.xml
index 86acb00..93308a1 100644
--- a/org.eclipse.lyo.rio.trs/pom.xml
+++ b/org.eclipse.lyo.rio.trs/pom.xml
@@ -6,7 +6,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.lyo.rio</groupId>
- <version>0.5.0-SNAPSHOT</version>
+ <version>2.0-SNAPSHOT</version>
<name>TRS Reference Application</name>
<artifactId>org.eclipse.lyo.rio.trs</artifactId>
<description>Reference Application demonstrating a Tracked Resource Set integration utilizing the TRS toolkit.</description>
@@ -28,7 +28,7 @@
<dependency>
<groupId>org.eclipse.lyo</groupId>
<artifactId>oslc-trs</artifactId>
- <version>0.5.0-SNAPSHOT</version>
+ <version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java
index e6a1435..41cb073 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java
@@ -12,6 +12,7 @@
* Contributors:
*
* Ernest Mah - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.resources;
@@ -32,6 +33,7 @@ import javax.ws.rs.core.UriInfo;
import org.apache.wink.common.annotations.Workspace;
import org.eclipse.lyo.core.trs.Base;
+import org.eclipse.lyo.core.trs.Page;
import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.oslc4j.core.annotation.OslcService;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
@@ -74,8 +76,8 @@ public class BaseResource {
* paged Base resources of the Tracked Resource Set
*/
@GET
- @Produces({ OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.TEXT_TURTLE, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- public Base getBase() throws URISyntaxException{
+ @Produces({ OslcMediaType.TEXT_TURTLE, OslcMediaType.APPLICATION_RDF_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+ public Page getBase() throws URISyntaxException{
URI requestURI = uriInfo.getRequestUri();
boolean endsWithSlash = requestURI.getPath().endsWith("/");
@@ -89,10 +91,10 @@ public class BaseResource {
*/
@GET
@Path("{page}")
- @Produces({ OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.TEXT_TURTLE, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- public Base getBasePage(@PathParam("page")Long page) throws URISyntaxException{
+ @Produces({ OslcMediaType.TEXT_TURTLE, OslcMediaType.APPLICATION_RDF_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+ public Page getBasePage(@PathParam("page")Long page) throws URISyntaxException{
init();
-
+
// from uri find out which Inner container to access...
URI requestURI = uriInfo.getRequestUri();
@@ -103,7 +105,21 @@ public class BaseResource {
if (base == null)
throw new WebApplicationException(Status.NOT_FOUND);
- return base;
+ Page nextPage = base.getNextPage();
+
+ if (nextPage == null)
+ throw new WebApplicationException(Status.NOT_FOUND);
+
+ // Return the nextPage Page object, which describes the next base page in terms,
+ // of the current base page we are manipulating. We do not directly
+ // return the base object due to a limitation in OSLC4J. Currently
+ // OSLC4J requires that triples in the RDF graph with different subjects
+ // reference one another. According to the 2.0 spec, the Page object
+ // already references the Base object so we will get the appropriate
+ // output if we return Page. If we force a reference from Base to Page
+ // instead then we get a ldp:nextPage entry which does not conform to the
+ // TRS 2.0 specification.
+ return nextPage;
}
private void init() {
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java
index 3ebd0a0..a1be423 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java
@@ -12,6 +12,7 @@
* Contributors:
*
* Ernest Mah - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.resources;
@@ -20,7 +21,6 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
-import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
@@ -58,9 +58,9 @@ import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResponseInfoArray;
import org.eclipse.lyo.rio.trs.cm.ChangeRequest;
-import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
import org.eclipse.lyo.rio.trs.cm.Constants;
import org.eclipse.lyo.rio.trs.cm.Persistence;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
@OslcService(Constants.CHANGE_MANAGEMENT_DOMAIN)
@@ -69,6 +69,7 @@ import org.eclipse.lyo.rio.trs.util.TRSUtil;
public class ChangeRequestResource
{
@Context ServletContext servletContext;
+ static long currentNumber = -1;
// Handle the HTML request from browser.
@POST
@@ -90,7 +91,7 @@ public class ChangeRequestResource
ChangeRequest changeRequest = Persistence.persistChangeRequest(baseURI, cr);
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
// Send back to the form a small JSON response.
httpServletResponse.setContentType("application/json");
@@ -131,7 +132,7 @@ public class ChangeRequestResource
Persistence.updateChangeRequest(changeRequestId, changeRequest);
// TRS - Insert the modification event to the change log
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_MODIFICATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_MODIFICATION, changeRequest.getAbout(), createRelativeURN());
String eTag = getETagFromChangeRequest(changeRequest);
httpServletResponse.setContentType("application/json");
@@ -332,7 +333,7 @@ public class ChangeRequestResource
if (changeRequest != null)
{
// TRS - Insert the deletion event to the change log
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_DELETION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_DELETION, changeRequest.getAbout(), createRelativeURN());
return Response.ok().build();
@@ -360,39 +361,39 @@ public class ChangeRequestResource
ChangeRequest changeRequest = null;
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Unable to execute Apache Tomcat due to missing Java runtime environment (JRE).",
"Apache Tomcat requires JRE", "Server"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Operating system password required to be alphanumeric with at least eight characters.",
"Password complexity rules", "Server"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Misspelling in error message in XYZZY product.",
"Error message mispelling", "Client"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Unable to execute Apache Tomcat due to missing Java runtime environment (JRE).",
"Integer et elementum est. Maecenas bibendum fermentum pharetra.", "Server"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sed pharetra lectus..",
"Password complexity rules", "Client"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Vestibulum volutpat, est vehicula tincidunt tincidunt, tortor ipsum consequat ante, sit amet ultricies elit dui ac massa. Nulla facilisi..",
"Fusce dapibus imperdiet porta.", "Server"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Nunc aliquet scelerisque leo, eu molestie enim fermentum nec. Nulla lectus dui, dictum non sodales quis, dictum sed ante. Proin quam nulla, euismod ac facilisis eu, rhoncus vel quam. Donec ac ligula ante. Nulla egestas quam sit amet neque placerat quis iaculis risus tincidunt. Mauris a sapien nulla.",
"Suspendisse lobortis nisi nisl, et imperdiet nisl.", "Client"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Ut felis risus, blandit eu tristique sed, vulputate non ligula. Donec interdum mi sed odio bibendum eget placerat mi tempus. Pellentesque id erat mauris.",
"Duis placerat scelerisque purus sed tincidunt.", "Server"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Etiam lacinia cursus risus at iaculis. Nunc metus nisi, lobortis vitae consequat nec, hendrerit nec augue.",
"Donec sit amet felis purus.", "Client"));
- TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), getCurrentTimeStampURN());
+ TRSUtil.insertEventTypeToChangeLog(TRSConstants.TRS_TYPE_CREATION, changeRequest.getAbout(), createRelativeURN());
return "Success!";
}
@@ -423,25 +424,20 @@ public class ChangeRequestResource
}
- // The following two methods help generate a unique URN for a change event.
- // This URN identifies a particular change event. In a production system it
- // is often useful to persist change logs so that the same change event URN
- // can be used on a restart of the application. This allows consumers of the
- // the feed to know which changes events have already been viewed.
- private static URI getCurrentTimeStampURN() {
- URI timestampURI = null;
+ // This method generates a simple unique URN for a change event based on
+ // an in-memory counter. This URN identifies a particular change event while
+ // the server is running. In a production system it is often useful to persist
+ // change logs so that the same change event URN can be used on a restart of
+ // the application. This allows consumers of the the feed to know which
+ // changes events have already been viewed.
+ private static URI createRelativeURN() {
+ // Incrementally track change events since the start of the system.
+ currentNumber++;
try {
- timestampURI = new URI("urn:urn-3:cm1.example.com:" + getCurrentTimeStamp());
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- return timestampURI;
+ return new URI("#" + currentNumber);
+ } catch (URISyntaxException e) {
+ // TODO Auto-generated catch block
+ return null;
+ }
}
-
- private static String getCurrentTimeStamp() {
- Date currDate = new Date();
- SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss.SS");
- String currDateStr = dateFormatGmt.format(currDate);
- return currDateStr;
- }
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java
index 96d9c75..06d127c 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java
@@ -12,14 +12,17 @@
* Contributors:
*
* Ernest Mah - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.resources;
+import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
@@ -42,6 +45,7 @@ import org.eclipse.lyo.core.trs.TrackedResourceSet;
import org.eclipse.lyo.oslc4j.core.annotation.OslcService;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.ResponseUtil;
import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
@@ -77,14 +81,16 @@ public class TRSResource {
@Context
protected UriInfo uriInfo;
@Context private HttpServletResponse httpServletResponse;
+ @Context private HttpServletRequest httpServletRequest;
/**
* getTrackedResourceSet() returns the Tracked Resource Set with the most recent
* page of the Change Log or EmptyChangeLog if no change logs pages exist
+ * @throws IOException
*/
@GET
- @Produces({ OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.TEXT_TURTLE, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- public TrackedResourceSet getTrackedResourceSet() throws URISyntaxException{
+ @Produces({ OslcMediaType.TEXT_TURTLE, OslcMediaType.APPLICATION_RDF_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+ public TrackedResourceSet getTrackedResourceSet() throws URISyntaxException, IOException{
init();
URI requestURI = uriInfo.getRequestUri();
@@ -101,6 +107,12 @@ public class TRSResource {
URI base = requestBase.resolve("trs/"+TRSConstants.TRS_TERM_BASE+"/");
set.setBase(base);
+
+ // Determine the response type and alter the about URIs as appropriate
+ // (turtle prefers relative URIs while RDF/XML requires absolute URIs)
+ String responseType = ResponseUtil.parseAcceptType(httpServletRequest);
+ ResponseUtil.fixRelativeUris(responseType, requestURI.toString(), changeLog);
+
set.setChangeLog(changeLog);
return set;
@@ -108,11 +120,12 @@ public class TRSResource {
/**
* getChangeLog() returns the current changelog...
+ * @throws IOException
*/
@GET
@Path("changelog")
- @Produces({ OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.TEXT_TURTLE, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- public AbstractChangeLog getChangeLog() throws URISyntaxException{
+ @Produces({ OslcMediaType.TEXT_TURTLE, OslcMediaType.APPLICATION_RDF_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+ public AbstractChangeLog getChangeLog() throws URISyntaxException, IOException{
init();
URI requestURI = uriInfo.getRequestUri();
@@ -121,17 +134,23 @@ public class TRSResource {
AbstractChangeLog changeLog = trsObject.getCurrentChangeLog();
+ // Determine the response type and alter the about URIs as appropriate
+ // (turtle prefers relative URIs while RDF/XML requires absolute URIs)
+ String responseType = ResponseUtil.parseAcceptType(httpServletRequest);
+ ResponseUtil.fixRelativeUris(responseType, requestURI.toString(), changeLog);
+
return changeLog;
}
/**
* getChangeLogPage() returns the Change Log at the given page number
+ * @throws IOException
*
*/
@GET
@Path("changelog/{page}")
- @Produces({ OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.TEXT_TURTLE, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- public AbstractChangeLog getChangeLogPage(@PathParam("page") final Long page) throws URISyntaxException{
+ @Produces({ OslcMediaType.TEXT_TURTLE, OslcMediaType.APPLICATION_RDF_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+ public AbstractChangeLog getChangeLogPage(@PathParam("page") final Long page) throws URISyntaxException, IOException{
init();
URI requestURI = uriInfo.getRequestUri();
@@ -142,6 +161,11 @@ public class TRSResource {
if (changeLog == null)
throw new WebApplicationException(Status.NOT_FOUND);
+ // Determine the response type and alter the about URIs as appropriate
+ // (turtle prefers relative URIs while RDF/XML requires absolute URIs)
+ String responseType = ResponseUtil.parseAcceptType(httpServletRequest);
+ ResponseUtil.fixRelativeUris(responseType, requestURI.toString(), changeLog);
+
return changeLog;
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java
index 7c791a3..1e6c1ce 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java
@@ -12,6 +12,7 @@
* Contributors:
*
* Mukherjee Biswarup - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.servlet;
@@ -32,9 +33,12 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.eclipse.lyo.core.trs.Base;
+import org.eclipse.lyo.core.trs.Page;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JContext;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JMarshaller;
+import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.ResponseUtil;
import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
@@ -72,7 +76,9 @@ public class BaseGeneric extends HttpServlet {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("application/rdf+xml");
+ String responseType = ResponseUtil.parseAcceptType(request);
+
+ response.setContentType(responseType);
String path = null;
path = request.getPathInfo();
@@ -104,12 +110,33 @@ public class BaseGeneric extends HttpServlet {
if (base == null)
throw new WebApplicationException(Status.NOT_FOUND);
- List<Base> results = new ArrayList<Base>();
- results.add(base);
+ Page nextPage = base.getNextPage();
+
+ if (nextPage == null)
+ throw new WebApplicationException(Status.NOT_FOUND);
+
+ // Return the nextPage Page object, which describes the next base page in terms,
+ // of the current base page we are manipulating. We do not directly
+ // return the base object due to a limitation in OSLC4J. Currently
+ // OSLC4J requires that triples in the RDF graph with different subjects
+ // reference one another. According to the 2.0 spec, the Page object
+ // already references the Base object so we will get the appropriate
+ // output if we return Page. If we force a reference from Base to Page
+ // instead then we get a ldp:nextPage entry which does not conform to the
+ // TRS 2.0 specification.
+
+ List<Page> results = new ArrayList<Page>();
+ results.add(nextPage);
// return results;
OSLC4JContext context = OSLC4JContext.newInstance();
OSLC4JMarshaller marshaller = context.createMarshaller();
- marshaller.setMediaType(MediaType.APPLICATION_XML_TYPE);
+
+ if (responseType.equals(OslcMediaType.TEXT_TURTLE)) {
+ marshaller.setMediaType(OslcMediaType.TEXT_TURTLE_TYPE);
+ } else if (responseType.equals(OslcMediaType.APPLICATION_RDF_XML)) {
+ marshaller.setMediaType(MediaType.APPLICATION_XML_TYPE);
+ }
+
ServletOutputStream outputStream = response.getOutputStream();
oArray = results.toArray();
marshaller.marshal(oArray, outputStream);
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java
index 1b402f2..f8f0b56 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java
@@ -12,6 +12,7 @@
* Contributors:
*
* Mukherjee Biswarup - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.servlet;
@@ -31,7 +32,9 @@ import javax.ws.rs.core.Response.Status;
import org.eclipse.lyo.core.trs.AbstractChangeLog;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JContext;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JMarshaller;
+import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.ResponseUtil;
import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
@@ -47,7 +50,9 @@ public class ChangeLogGeneric extends HttpServlet {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("application/rdf+xml");
+ String responseType = ResponseUtil.parseAcceptType(request);
+ response.setContentType(responseType);
+
AbstractChangeLog changeLog = null;
Object[] oArray = new Object[1];
@@ -75,16 +80,23 @@ public class ChangeLogGeneric extends HttpServlet {
{
changeLog = trsObject.getCurrentChangeLog();
}
+
+ OSLC4JContext context = OSLC4JContext.newInstance();
+ OSLC4JMarshaller marshaller = context.createMarshaller();
+
+ if (responseType.equals(OslcMediaType.TEXT_TURTLE)) {
+ marshaller.setMediaType(OslcMediaType.TEXT_TURTLE_TYPE);
+ } else if (responseType.equals(OslcMediaType.APPLICATION_RDF_XML)) {
+ marshaller.setMediaType(MediaType.APPLICATION_XML_TYPE);
+ }
+
+ ResponseUtil.fixRelativeUris(responseType, requestBase.toString(), changeLog);
+
+ ServletOutputStream outputStream = response.getOutputStream();
+ oArray[0] = changeLog;
+ marshaller.marshal(oArray, outputStream);
} catch (URISyntaxException e) {
e.printStackTrace();
}
-
- OSLC4JContext context = OSLC4JContext.newInstance();
- OSLC4JMarshaller marshaller = context.createMarshaller();
- marshaller.setMediaType(MediaType.APPLICATION_XML_TYPE);
- ServletOutputStream outputStream = response.getOutputStream();
- oArray[0] = changeLog;
- marshaller.marshal(oArray, outputStream);
- }
-
+ }
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java
index 472d7c6..ba154f8 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java
@@ -12,6 +12,7 @@
* Contributors:
*
* Mukherjee Biswarup - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.servlet;
@@ -32,7 +33,9 @@ import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.core.trs.TrackedResourceSet;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JContext;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JMarshaller;
+import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.ResponseUtil;
import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
/**
@@ -69,8 +72,8 @@ public class TRSGeneric extends HttpServlet {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- response.setContentType("application/rdf+xml");
+ String responseType = ResponseUtil.parseAcceptType(request);
+ response.setContentType(responseType);
Object[] oArray = new Object[1];
TrackedResourceSet set = new TrackedResourceSet();
@@ -93,16 +96,24 @@ public class TRSGeneric extends HttpServlet {
URI base = requestBase.resolve("trs/"+TRSConstants.TRS_TERM_BASE+"/");
set.setBase(base);
set.setChangeLog(changeLog);
+
+ OSLC4JContext context = OSLC4JContext.newInstance();
+ OSLC4JMarshaller marshaller = context.createMarshaller();
+
+ if (responseType.equals(OslcMediaType.TEXT_TURTLE)) {
+ marshaller.setMediaType(OslcMediaType.TEXT_TURTLE_TYPE);
+ } else if (responseType.equals(OslcMediaType.APPLICATION_RDF_XML)) {
+ marshaller.setMediaType(MediaType.APPLICATION_XML_TYPE);
+ }
+
+ ResponseUtil.fixRelativeUris(responseType, requestBase.toString(), changeLog);
+
+ ServletOutputStream outputStream = response.getOutputStream();
+ oArray[0] = set;
+ marshaller.marshal(oArray, outputStream);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
-
- OSLC4JContext context = OSLC4JContext.newInstance();
- OSLC4JMarshaller marshaller = context.createMarshaller();
- marshaller.setMediaType(MediaType.APPLICATION_XML_TYPE);
- ServletOutputStream outputStream = response.getOutputStream();
- oArray[0] = set;
- marshaller.marshal(oArray, outputStream);
}
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/ResponseUtil.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/ResponseUtil.java
new file mode 100644
index 0000000..1467075
--- /dev/null
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/ResponseUtil.java
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * * Contributors:
+ *
+ * David Terry - Initial implementation
+ *******************************************************************************/
+package org.eclipse.lyo.rio.trs.util;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response.Status;
+
+import org.eclipse.lyo.core.trs.AbstractChangeLog;
+import org.eclipse.lyo.core.trs.ChangeEvent;
+import org.eclipse.lyo.core.trs.ChangeLog;
+import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
+
+/**
+ * Class consisting of various methods to assist in the marshalling/unmarshalling
+ * of TRS toolkit resources for the servlet based implementation.
+ */
+public class ResponseUtil {
+
+ /**
+ * This method examines the incoming response type and alters the relative
+ * about URIs stored on the events of the change log. For turtle output the
+ * relative URIs are left alone or restored if necessary. For RDF/XML the
+ * requestBase is added to the relative URI to get the appropriate absolute
+ * URI (since RDF/XML does not allow relative about URIs).
+ *
+ * @param responseType
+ * @param requestBase
+ * @param changeLog
+ * @throws URISyntaxException
+ */
+ public static void fixRelativeUris(String responseType, String requestBase, AbstractChangeLog changeLog) throws URISyntaxException {
+ // If this is a populated change log it has relative uris we need to fix
+ // or the RDF/XML will be illegal
+ if (changeLog instanceof ChangeLog) {
+ List<ChangeEvent> events = ((ChangeLog) changeLog).getChange();
+
+ for (ChangeEvent event : events) {
+ if (!(responseType.equals(OslcMediaType.TEXT_TURTLE))) {
+ event.setAbout(new URI(requestBase + "#" + Integer.toString(event.getOrder())));
+ } else {
+ event.setAbout(new URI("#" + Integer.toString(event.getOrder())));
+ }
+ }
+ }
+ }
+
+ /**
+ * Return a string representing the requested accept type. Right now we
+ * support text/turle and application/rdf+xml with text/turtle
+ * being the default response.
+ */
+ public static String parseAcceptType(HttpServletRequest request) throws IOException {
+
+ String acceptTypes = request.getHeader("Accept");
+
+ if (acceptTypes == null) {
+ // turtle is the default response type if nothing specific is
+ // requested.
+ return OslcMediaType.TEXT_TURTLE;
+ }
+
+ String[] types = acceptTypes.split(",");
+ // Accepts should be answered in priority order. Respond with the first
+ // type we encounter that is supported.
+ for (String type : types) {
+ if (type != null && (type.startsWith(OslcMediaType.TEXT_TURTLE) || type.startsWith("*/*"))) {
+ return OslcMediaType.TEXT_TURTLE;
+ } else if (type != null && (type.startsWith(OslcMediaType.APPLICATION_RDF_XML) || type.startsWith(OslcMediaType.APPLICATION_XML))) {
+ return OslcMediaType.APPLICATION_RDF_XML;
+ }
+ }
+
+ throw new WebApplicationException(Status.UNSUPPORTED_MEDIA_TYPE);
+ }
+}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java
index 892116e..d8524a0 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java
@@ -12,6 +12,7 @@
* Contributors:
*
* Ernest Mah - Initial implementation
+ * David Terry - TRS 2.0 compliant implementation
*******************************************************************************/
package org.eclipse.lyo.rio.trs.util;
@@ -31,6 +32,7 @@ import org.eclipse.lyo.core.trs.Creation;
import org.eclipse.lyo.core.trs.Deletion;
import org.eclipse.lyo.core.trs.EmptyChangeLog;
import org.eclipse.lyo.core.trs.Modification;
+import org.eclipse.lyo.core.trs.Page;
import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
@@ -93,7 +95,7 @@ public class TRSObject {
// if there are no known resources, then create an empty base object
if (uris.size() == 0) {
- currentBase = createNewBase();
+ currentBase = createNewBase(currentPageNumber);
getTrsBaseMapInner().put(currentPageNumber, currentBase);
return;
}
@@ -105,9 +107,17 @@ public class TRSObject {
if (currentPageSize == 0) {
// Point the current Base to the previous Base page by URI before creating
// the new one
- if (currentBase != null)
- currentBase.setNextPage(getBaseResourceURI(currentPageNumber));
- currentBase = createNewBase();
+ if (currentBase != null) {
+ // At this point the currentPageNumber tracks the next
+ // page in the sequence since we are preparing to create
+ // a new base page. Do some trickery so we can properly
+ // set the next page object.
+ URI previousPage = getBaseResourceURI(currentPageNumber - 1);
+ URI nextPage = getBaseResourceURI(currentPageNumber);
+ currentBase.setNextPage(getNextBasePage(currentBase, previousPage, nextPage));
+ }
+
+ currentBase = createNewBase(currentPageNumber);
getTrsBaseMapInner().put(currentPageNumber, currentBase);
}
@@ -220,7 +230,7 @@ public class TRSObject {
}
// add resource into current changelog
- trs_curr_changelog.getChanges().add(0, event);
+ trs_curr_changelog.getChange().add(0, event);
trs_curr_changelog_size++;
last_change_event = event;
@@ -250,7 +260,7 @@ public class TRSObject {
// 1. Iterate change log entries if inCutOffEvent is found
for (long page = trs_curr_changelog_page; page >= 0; page--) {
ChangeLog aChangeLog = getTrsChangelogMapInner().get((long) page);
- List<ChangeEvent> aListofevents = aChangeLog.getChanges();
+ List<ChangeEvent> aListofevents = aChangeLog.getChange();
for (ChangeEvent e : aListofevents) {
if (last_cut_off_event != null) {
if (e.equals(last_cut_off_event)) {
@@ -294,7 +304,7 @@ public class TRSObject {
for (int page = 0; page <= trs_curr_changelog_page; page++) {
ChangeLog aChangeLog = getTrsChangelogMapInner().get((long) page);
- List<ChangeEvent> aListofevents = aChangeLog.getChanges();
+ List<ChangeEvent> aListofevents = aChangeLog.getChange();
for (ChangeEvent e : aListofevents) {
if (uriAbout.equals(e.getAbout().toString())) {
result = e;
@@ -306,12 +316,12 @@ public class TRSObject {
return result;
}
- private Base createNewBase() {
+ private Base createNewBase(long currentPageNumber) {
Base newBase = new Base();
newBase.setAbout(getBaseResourceURI(null));
try {
newBase.setCutoffEvent(new URI(TRSConstants.RDF_NIL));
- newBase.setNextPage(new URI(TRSConstants.RDF_NIL));
+ newBase.setNextPage(getNextBasePage(newBase, getBaseResourceURI(currentPageNumber), new URI(TRSConstants.RDF_NIL)));
} catch (URISyntaxException e) {
e.printStackTrace();
}
@@ -326,6 +336,30 @@ public class TRSObject {
return trs_changelog_map;
}
+ /**
+ * Prepare the LDP object and make it so it points to the current base page
+ * as well as the next base page.
+ *
+ * @param currentBase
+ * Reference to the main base endpoint (the page belongs to this
+ * base resource)
+ * @param currentPage
+ * URI of the current base page being manipulated
+ * @param nextPage
+ * URI to the next page in the sequence
+ */
+ private Page getNextBasePage(Base currentBase, URI currentPage, URI nextPage) {
+ Page ldpNextPage = new Page();
+
+ ldpNextPage.setAbout(currentPage);
+
+ ldpNextPage.setNextPage(nextPage);
+
+ ldpNextPage.setPageOf(currentBase);
+
+ return ldpNextPage;
+ }
+
private URI getBaseResourceURI(Long pageNumber) {
if (pageNumber == null)
return trs_uri.resolve("./" + TRSConstants.TRS_TERM_BASE + "/");