Tequila Rendering Made Easier

December 12, 2008 | by Garrett

Recently I’ve been working on our in-house open source project, Tequila. It is a RESTful web service framework, and was lacking some familiar rendering capabilities out of the box. (See http://tequilaframework.org/ for project details). The formats that we thought may be useful were XML, JSON, and RDF, and so I set out to provide these basic abilities.

Our XML and JSON rendering functionality leverages the XStream project, and provides an easy mechanism for pairing aliases and their respective Java objects.
The Spring Framework configuration in the webapp project sets the rendering information for each response type. Response types are used when specifying routes, which are the Tequila representation for URL request patterns.

JSON example:

<bean id="dataPointJsonRenderer" class="org.tequilaframework.core.renderer.JsonRenderer">
<property name="responseType" value="datapoint/json"/>
<property name="contentType" value="text/plain"/>
<property name="aliases">
<map value-type="java.lang.Class">
<entry>
<key><value>dataPoints</value></key>
<value>java.util.List</value>
</entry>
<entry>
<key><value>dataPoint</value></key>
<value>com.twopaths.example.domain.DataPoint</value>
</entry>
<entry>
<key><value>time</value></key>
<value>com.twopaths.example.domain.TimeDimension</value>
</entry>
<entry>
<key><value>spatial</value></key>
<value>com.twopaths.example.domain.SpatialDimension</value>
</entry>
</map>
</property>
<!-- Example omission -->
<!-- to omit a field, you must declare the parent class as the class value, and the key is the would-be rendered value -->
<!-- <property name="omissions">
<map value-type="java.lang.Class">
<entry>
<key><value>spatial</value></key>
<value>com.twopaths.example.domain.DataPoint</value>
</entry>
</map>
</property>
-->
</bean>

XML example:

<bean id="dataPointXmlRenderer" class="org.tequilaframework.core.renderer.XmlRenderer">
<property name="responseType" value="datapoint/xml"/>
<property name="contentType" value="text/plain"/>
<property name="aliases">
<map value-type="java.lang.Class">
<entry>
<key><value>dataPoints</value></key>
<value>java.util.List</value>
</entry>
<entry>
<key><value>dataPoint</value></key>
<value>com.twopaths.example.domain.DataPoint</value>
</entry>
<entry>
<key><value>time</value></key>
<value>com.twopaths.example.domain.TimeDimension</value>
</entry>
<entry>
<key><value>spatial</value></key>
<value>com.twopaths.example.domain.SpatialDimension</value>
</entry>
</map>
</property>
<!-- Example omission -->
<!-- to omit a field, you must declare the parent class as the class value, and the key is the would-be rendered value -->
<!-- <property name="omissions">
<map value-type="java.lang.Class">
<entry>
<key><value>spatial</value></key>
<value>com.twopaths.example.domain.DataPoint</value>
</entry>
</map>
</property>
-->
</bean>

The RDF renderer leverages the Jena project and Jenabean project. Jena supported RDF formats include RDF/XML, RDF/XML-ABBREV, N-TRIPLE, TURTLE, N3, N3-PP, N3-PLAIN, and N3-TRIPLE. Because of Jena, the Tequila RDF renderer gets these formats for free. The only trick with RDF in Tequila is that you will need to use annotations in the Java classes you wish to have RDF support in. If you’re using Hibernate, then you’ll likely already have RDF rendering configured. If no rdfType is specified, the default is RDF/XML.

RDF example:

<bean id="dataDescriptionRdfRenderer" class="org.tequilaframework.core.renderer.RdfRenderer">
<property name="responseType" value="application/rdf"/>
<property name="contentType" value="text/plain"/>
<property name="domain" value="http://www.2paths.com/example"/>
<property name="rdfType" value="TURTLE"/>
</bean>

With multiple rendering capabilities, the GET request to the webapp can alter the rendering output by including the flavor RPC verb descriptor to modify the return format. For example, the URL of

http://localhost:8080/example/app/data/example?flavor=application/rdf

would return an RDF rendered output, and the original response format may have been JSON, or another response format laid out in the configuration file.

Bookmark and Share

Tags:

Leave a Reply