users@jersey.java.net

Re: [Jersey] enable xsl on wadl

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 03 May 2010 09:59:23 +0200

Hi Leonardo,

I think the best way to do this is to define your own WadlResource
that overrides that supplied by Jersey (see below).

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

You can modify the JAXB serialization to add the style sheet. See the
xml-stylesheet sample from the JAXB samples:

   https://jaxb.dev.java.net/2.1.12/

(see below for the sample code).

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();
     }
}

public class Main {
     public static void main( String[] args ) throws Exception {

         // create JAXBContext for the primer.xsd
         JAXBContext context = JAXBContext.newInstance("primer");

         // unmarshal a document, just to marshal it back again.
         JAXBElement poe =
(JAXBElement)context.createUnmarshaller().unmarshal(
             new File(args[0]));
         // we don't need to check the return value, because the
unmarshal
         // method should haven thrown an exception if anything went
wrong.
         PurchaseOrderType po = (PurchaseOrderType)poe.getValue();


         // Here's the real meat.
         // we configure marshaller not to print out xml decl,
         // we then print out XML decl plus stylesheet header on our
own,
         // then have the marshaller print the real meat.

         System.out.println("<?xml version='1.0'?>");
         System.out.println("<?xml-stylesheet type='text/xsl'
href='foobar.xsl' ?>");
         // if you need to put DOCTYPE decl, it can be easily done here.

         // create JAXB marshaller.
         Marshaller m = context.createMarshaller();
         // configure it
         m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
         // marshal
         m.marshal(poe,System.out);
     }

}


On Apr 27, 2010, at 4:46 PM, Leonardo wrote:

> hello all,
>
> i'm trying to figure how to enabe those pretty xsl styles on my small
> test project. i got a style[1] for my research purposes but i have no
> idea how to make application.wadl use that xsl.
>
> i'm using jersey 1.1.5.1 here[2],any tip is welcome.
>
> [1]http://github.com/mnot/wadl_stylesheets/raw/master/wadl_documentation-2006-10.xsl
> [2]jersey-core-1.1.5.1.jar,jersey-json-1.1.5.1.jar,jersey-
> server-1.1.5.1.jar,jsr311-api-1.1.jar
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>