users@jersey.java.net

Re: [Jersey] Wadl generator bug ? - not using ContextResolver<JAXBContext> ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 19 May 2010 16:58:21 +0200

On May 12, 2010, at 10:59 PM, Morten wrote:

> I have setup a in jersey ContextResolver<JAXBContext> which works
> fine for normal jersey operations except the build-in WADL generator
> which seems to ignore my ContextResolver. Is this a bug in Jersey ?
>

The ContextResolver is not utilized for WADL. Generation of WADL has
some special requirements like being generated independent of the
running application and components like ContextResolver will not be
available. If we remove the support for JavaDoc, use Java annotations
and property files for docs (better for I18N as well) this would go
away.


> It is important for me to use my own JAXBContext, so I would like to
> configure wadl for this. I noticed that there is something called a
> WadlApplicationContext that has a getJAXBContext method, but could
> not exactly find out how to install my own implementation
>


One way you can do this is to define your own WADL resource (see end
of email for the default one that Jersey uses) obtain the default JAXB
context path and merge if with your own to create a JAXBContext. Or
alternatively:

The default JAXB context path is:

   import com.sun.research.ws.wadl.Application;
   ...

   public class WadlGeneratorImpl implements WadlGenerator {

       public String getRequiredJaxbContextPath() {
           final String name = Application.class.getName();
           return name.substring(0, name.lastIndexOf('.'));
       }


The other is to define your own WadlGeneratorConfig and WadlGenerator
that merges the JAXB context path in the getRequiredJaxbContextPath
from the delegated WadlGenerator.

     public String getRequiredJaxbContextPath() {
         String name = ...
         return _delegate.getRequiredJaxbContextPath() + ":" + name;
     }

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