users@jersey.java.net

WadlResource, stylesheets for Wadl documents, custom marshalling

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 10 Mar 2009 13:36:43 +0100

Hi,

There have been a number of emails on the use of WadlResource,
stylesheets for transforming the the WADL document, and JAXB custom
marshalling of the WADL.

I thought it would be easier to consolidate all the threads into one
new response.


In 1.0.3-SNAPSHOT it is now possible to write your own WADL resource
(see end of email of the Jersey implementation of WadlResource) by
injecting WadlApplicationContext.

 From the WadlApplicationContext you can get an instance of the JAXB
bean com.sun.research.ws.wadl.Application. And then you can serialize
that bean as you wish.

The next steps are to:

1) To incorporate Jame's patch:

      https://jersey.dev.java.net/issues/show_bug.cgi?id=223

      to help with using WADL with implicit views.

2) Allow a developer to declare their own implementation of WadlResource
     at the relative URL "application.wadl"

     @Path("application.wadl")
     public MyWadlResource extends WadlResource { ... }


For setting the style sheet see:

   https://jaxb.dev.java.net/nonav/2.1.10/docs/
vendorProperties.html#xmlheader

You can do this:

         Marshaller m = ...
         m.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml-
stylesheet type='text/xsl' href='foobar.xsl' ?>");

Paul.


@Produces({"application/vnd.sun.wadl+xml", "application/xml"})
@Singleton
public final class WadlResource {

     private static final Logger LOGGER =
Logger.getLogger(WadlResource.class.getName());

     private WadlApplicationContext wadlContext;

     private Application application;

     private byte[] wadlXmlRepresentation;

     public WadlResource(@Context WadlApplicationContext wadlContext) {
         this.wadlContext = wadlContext;
         this.application = wadlContext.getApplication();
     }

     @GET
     public synchronized Response getWadl(@Context UriInfo uriInfo) {
         if (wadlXmlRepresentation == null) {
             if (application.getResources().getBase() == null) {
                  
application.getResources().setBase(uriInfo.getBaseUri().toString());
             }
             try {
                 final Marshaller marshaller =
wadlContext.getJAXBContext().createMarshaller();
                  
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                 final ByteArrayOutputStream os = new
ByteArrayOutputStream();
                 marshaller.marshal(application, os);
                 wadlXmlRepresentation = os.toByteArray();
                 os.close();
             } catch (Exception e) {
                 LOGGER.log(Level.WARNING, "Could not marshal wadl
Application.", e);
                 return Response.ok(application).build();
             }
         }

         return Response.ok(new
ByteArrayInputStream(wadlXmlRepresentation)).build();
     }
}