users@jersey.java.net

RE: [Jersey] wadl to html

From: Rabick, Mark A (IS) <"Rabick,>
Date: Fri, 15 May 2009 17:24:15 -0500

Felipe,

I modified my implementation slightly to take advantage of Arul's use of the @Context WadlApplicationContext injection.

The constructor initializes the transformer factory to use the saxon factory implementation. I just added the saxon9.jar to my classpath

package com.rabick.rs.resources;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.sun.jersey.server.impl.wadl.WadlResource;
import com.sun.jersey.server.wadl.WadlApplicationContext;
import com.sun.jersey.spi.resource.Singleton;
import com.sun.research.ws.wadl.Application;

@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_XML})
@Singleton
@Path("wadl")
public class WadlHtmlResource {

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

        private WadlApplicationContext wadlContext;

        private Application application;

        private byte[] wadlHtmlRepresentation;

        public WadlHtmlResource(@Context WadlApplicationContext wadlContext) {
                this.wadlContext = wadlContext;
                this.application = wadlContext.getApplication();
                System.setProperty("javax.xml.transform.TransformerFactory",
                                                        "net.sf.saxon.TransformerFactoryImpl");
        }

        @GET
        public synchronized Response getWadl(@Context UriInfo uriInfo) {
                if (wadlHtmlRepresentation == null) {
                        if (application.getResources().getBase() == null) {
                                application.getResources().setBase(uriInfo.getBaseUri().toString());
                        }
                        try {
                                /*
                                 * Get the current XML WADL into a byte array.
                                 */
                                final Marshaller marshaller = wadlContext.getJAXBContext().createMarshaller();
                                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                                marshaller.marshal(application, os);
                                byte [] wadlXmlRepresentation = os.toByteArray();
                                os.close();

                                /*
                                 * Create a StreamSource for the wadl stylesheet.
                                 */
                                String xslResource = "/wadl_documentation.xsl";
                                URL xslUrl = this.getClass().getResource(xslResource);
                                InputStream xslStream = xslUrl.openStream();
                                StreamSource xslSource = new StreamSource(xslStream);

                                /*
                                 * Get a Transformer using the stylesheet StreamSource
                                 */
                                TransformerFactory tFactory = TransformerFactory.newInstance();
                                Transformer transformer = tFactory.newTransformer(xslSource);

                                /*
                                 * Create a StreamSource for the WADL XML byte array
                                 */
                                InputStream xmlStream = new BufferedInputStream(
                                                new ByteArrayInputStream(wadlXmlRepresentation)
                                );
                                StreamSource xmlSource = new StreamSource(xmlStream);

                                /*
                                 * Perform the transform to an output stream
                                 */
                                ByteArrayOutputStream htmlOs = new ByteArrayOutputStream();
                                transformer.transform(xmlSource, new StreamResult(htmlOs));

                                wadlHtmlRepresentation = htmlOs.toByteArray();

                        } catch (Exception e) {
                                LOGGER.log(Level.WARN, "Could not marshal wadl Application as HTML.", e);
                                return javax.ws.rs.core.Response.ok(application).build();
                        }
                }
                return Response.ok(new ByteArrayInputStream(wadlHtmlRepresentation)).build();
        }

}

_______________________________________________
Mark A. Rabick - Software Engineer
Em: mark.rabick_at_ngc.com

 

> -----Original Message-----
> From: Felipe Gaścho [mailto:fgaucho_at_gmail.com]
> Sent: Wednesday, May 13, 2009 11:02 AM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] wadl to html
>
> Hi Arul,
>
> I already checked your blog.. but you claim it only works on Chrome..
> is it true ?
>
> if yes, two problems: Chrome is only 5% of the browsers out
> there, and the worst: it don't work on Linux :(
>
>
>
> On Wed, May 13, 2009 at 5:52 PM, Arul Dhesiaseelan
> <arul_at_fluxcorp.com> wrote:
> > You may want to look into my recent blog entry [1].
> > <http://aruld.info/jersey-103-improves-wadl-support/>It
> shows how you
> > can apply Mark's XSL to your WADL resource.
> >
> > -Arul
> >
> > [1] http://aruld.info/jersey-103-improves-wadl-support/
> >
> > Felipe Gaścho wrote:
> >>
> >> some time ago I got some references of WSDL -> HTML
> converters, using
> >> xslt or other techniques..
> >>
> >> I started to use those suggestions, but I never got it
> properly working:
> >>
> >>
> >>
> http://fgaucho.dyndns.org:8080/footprint-service/application.wadl?for
> >> mat=html
> >>
> >>
> >> so, if you have an update reference on how to convert WADL
> to HTML on
> >> the fly, please let me know.........
> >>
> >>
> >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
> >
>
>
>
> --
>
> Please help to test this application:
> http://fgaucho.dyndns.org:8080/cejug-classifieds-richfaces
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>