users@jersey.java.net

RDF and jsr311

From: Henry Story <Henry.Story_at_Sun.COM>
Date: Tue, 07 Aug 2007 10:32:45 +0200

Hi, I am working on implementing an Entity Provider for RDF in Jersey.

Using the so(m)mer annotations [ https://sommer.dev.java.net ] it is
easy to map any java bean to rdf. Given that one should then be able
to serialise any such object to any of the RDF formats. So I am
looking at how best to do this with Jersey.

A so(m)mer annotated object looks something like this:


@rdf(atom+"Entry")
@UriTemplate("entry")
class Entry {
   static final String atom = "http://bblfish.net/work/atom-owl/
2006-06-06/#";

   @rdf(atom+"title") String title;
   @rdf(atom+"id") URI id;
   @rdf(atom+"content") Content content;
   ...

   @id URI getID() { ...
    //note this is *NOT* the atom ID. a number atom entries can have
the same atom id,
    //but only one object can have this
    //Identity. This is close to equivalent to a java object pointer.
   }
}


( Usually without the @UriTemplate of course, which is a JSR311
annotation )

As the @rdf anntoations give URIs for the class names and field, so I
was thinking JSR311 is really giving
a way to identify *instances* of objects with URIs. This is missing
in so(m)mer. There, if an object does not have an @id annotation and
has just been created in Java (IE: it does not have a URI from being
read on the net), then the object is thought to be identified by a
blank node - its identity is local to the JVM.

It could be then that JSR311 and so(m)mer complement each other well.
To test this I decided to build a serialiser for Jersey, the
reference implementation of JSR311. To do this I understand that I
need an Entity Provider class, as explained by Paul Sandoz on his blog:
http://blogs.sun.com/sandoz/entry/plugable_types_with_jax_rs

Here is the beginning of my implementation

/**
  *
  * @author hjs
  */
public class RDFProvider extends AbstractTypeEntityProvider<Object> {

     public RDFProvider() {
     }

     /**
      * To start off with we support any class that has an @rdf
annotation.
      * We could make this more general and support any class, full
stop. This
      * would require having being able to create
      * a default URI for every class name and relation, and perhaps
set up some
      * framework to publish an ontology at that URL...
      */
     public boolean supports(Class<?> type) {
         if (type.getAnnotation(rdf.class)!=null) {
             return true;
         }
         return false;
     }

     /** generalising this is not going to be obvious. Neet to think
about it... */
     public Object readFrom(Class<Object> type, String mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException {
         throw new UnsupportedOperationException("Not supported yet.");
     }

     /**
      * this should be quite easy. Essentially we want to deserialise
every
      * object in the graph starting with the given object and up to
any object
      * that does not have a URI. (objects that have URIs will get
deserialised
      * by fetching that URI of course)
      */
     public void writeTo(Object t, MultivaluedMap<String, Object>
httpHeaders, OutputStream entityStream) throws IOException {
         //now how does one get the URI of an object?
     }

}



 From looking at the code in Jersey, it looks like JSR311 is making a
big difference between Resource objects and normal objects, so that
there is a lot of duplication. Every object ends up requiring its own
resource object. In som(m)er I have the @Id annotation that returns
the URI for the object. I suppose I should have my object implement
this. Is there a way of linking this up with the @URITemplate?

The reason is that I want to serialise a graph of objects up until I
have an object with an Id. Indeed there is no need to publish an
object with an URL ID, since that object can be GETed itself at that
id. (Ok One could allow more information to be sent and thereby
reduce bandwidth consumption, by specifying that one should only go X
URIs deep.)

This is called a Concise Bounded Description
http://www.w3.org/Submission/CBD/

Some questions:

- does one really have to have both a POJO and a Resource object?
- Is there a way the id can be made to reliably work with the
@UriTemplate?
- does it make sense to have a Provider that is on an <Object>

Hope this makes sense.


Henry

Home page: http://bblfish.net/
Sun Blog: http://blogs.sun.com/bblfish/
Foaf name: http://bblfish.net/people/henry/card#me