users@jersey.java.net

Re: [Jersey] WadlGenerator does not generate resource methods for subresources?

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Tue, 10 Jun 2008 15:27:28 +0200

Hi Paul,

On Tue, 2008-06-10 at 14:05 +0200, Paul Sandoz wrote:
> Hi Martin,
>
> The main issue we do not know at compile/deploy time what concrete
> resources are returned by sub-resource locater methods.
though IMHO there's an issue with subresources referenced by
subresource-locators, as they are not added to the wadl.

I changed the WadlGenerator as follows, unfortunately the wadl does not
yet contain the expected stuff below /users/{username}/...

Perhaps you see what's the issue here?

Thx && cheers,
Martin

Index: jersey/src/impl/com/sun/jersey/impl/wadl/WadlGenerator.java
===================================================================
--- jersey/src/impl/com/sun/jersey/impl/wadl/WadlGenerator.java (revision 1081)
+++ jersey/src/impl/com/sun/jersey/impl/wadl/WadlGenerator.java (working copy)
@@ -37,6 +37,22 @@

 package com.sun.jersey.impl.wadl;

+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.ws.rs.core.MediaType;
+import javax.xml.bind.JAXBElement;
+import javax.xml.namespace.QName;
+
+import com.sun.jersey.api.model.AbstractResource;
+import com.sun.jersey.api.model.AbstractResourceMethod;
+import com.sun.jersey.api.model.AbstractSubResourceLocator;
+import com.sun.jersey.api.model.AbstractSubResourceMethod;
+import com.sun.jersey.api.model.Parameter;
+import com.sun.jersey.impl.modelapi.annotation.IntrospectionModeller;
 import com.sun.research.ws.wadl.Application;
 import com.sun.research.ws.wadl.Param;
 import com.sun.research.ws.wadl.ParamStyle;
@@ -45,17 +61,6 @@
 import com.sun.research.ws.wadl.Resource;
 import com.sun.research.ws.wadl.Resources;
 import com.sun.research.ws.wadl.Response;
-import com.sun.jersey.api.model.AbstractResource;
-import com.sun.jersey.api.model.AbstractResourceMethod;
-import com.sun.jersey.api.model.AbstractSubResourceLocator;
-import com.sun.jersey.api.model.AbstractSubResourceMethod;
-import com.sun.jersey.api.model.Parameter;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import javax.ws.rs.core.MediaType;
-import javax.xml.bind.JAXBElement;
-import javax.xml.namespace.QName;

 /**
  *
@@ -249,17 +254,28 @@
             }
         }

- // for each sub resource locator
- for (AbstractSubResourceLocator l : r.getSubResourceLocators()) {
- Resource wadlSubResource = new Resource();
- wadlSubResource.setPath(l.getUriPath().getValue());
- for (Parameter p : l.getParameters()) {
- Param wadlParam = generateParam(p);
- wadlSubResource.getParam().add(wadlParam);
+ // add resources for sub resource locators
+ final List<Resource> subResourceLocators = generateSubResourceLocators( r );
+ wadlResource.getMethodOrResource().addAll( subResourceLocators );
+
+ return wadlResource;
+ }
+
+ private static List<Resource> generateSubResourceLocators( AbstractResource r ) {
+ final List<Resource> result = new ArrayList<Resource>();
+ if ( r.getSubResourceLocators() != null ) {
+ for (AbstractSubResourceLocator l : r.getSubResourceLocators()) {
+ final AbstractResource subResource = IntrospectionModeller.createResource( l.getMethod().getReturnType() );
+ Resource wadlSubResource = generateSubResource( subResource, l.getUriPath().getValue() );
+ wadlSubResource.setPath(l.getUriPath().getValue());
+ for (Parameter p : l.getParameters()) {
+ Param wadlParam = generateParam(p);
+ wadlSubResource.getParam().add(wadlParam);
+ }
+ result.add( wadlSubResource );
             }
- wadlResource.getMethodOrResource().add(wadlSubResource);
         }
- return wadlResource;
+ return result;
     }

     private static Resource generateSubResource(AbstractResource r, String path) {
@@ -286,6 +302,10 @@
             wadlResource.getParam().add(wadlParam);
         }

+ // add resources for sub resource locators
+ final List<Resource> subResourceLocators = generateSubResourceLocators( r );
+ wadlResource.getMethodOrResource().addAll( subResourceLocators );
+
         return wadlResource;
     }


>
> This is why we support the serving of WADL via the HTTP OPTIONS method
> for each resource.
>
> Paul.
>
> Martin Grotzke wrote:
> > Hi,
> >
> > we have a root resource UsersResource with a subresource locator that
> > returns a UserResource that provides a resource method getUser().
> >
> > The generated application.wadl only contains the UserResource, but not
> > the resource methods that are provided by the UserResource.
> >
> > Here comes the UsersResource:
> >
> > @PerRequest
> > @Path("/"+ PathParams.USERS +"/")
> > public class UsersResource {
> >
> > @Path("{username}/")
> > public UserResource getUser( @PathParam("username")
> > final String username ) {
> > final UserResource user = _facade.getUserByUsername( username );
> > if ( user == null ) {
> > throw new NotFoundException("username " + username + " does
> > not exist!");
> > }
> > return new UserResource( user, getUriInfo(), _facade,
> > _dataTypeFactory, _securityContext );
> > }
> >
> > ...
> >
> > }
> >
> > This is a part of the UserResource:
> >
> > public class UserResource {
> >
> > ...
> >
> > @GET
> > @ProduceMime({ "application/xml" })
> > public User getUser() {
> > return new User( _user ); // just for
> > }
> >
> > @PUT
> > @Path( "{version}" )
> > @ConsumeMime( { "application/xml" } )
> > @ProduceMime({ "application/xml" })
> > public Response updateUser( @PathParam( "version" )
> > final int version,
> > UpdateUserRequest updateUserRequest ) {
> > ...
> > }
> >
> > ...
> >
> > }
> >
> > The relevant part of the generated application.wadl does not show the
> > methods provided by the UserResource:
> >
> > <resource path="/users/">
> > <method name="POST">
> > <request>
> > <representation mediaType="application/xml" />
> > </request>
> > <response>
> > <representation mediaType="application/xml" />
> > </response>
> > </method>
> > <resource path="{username}/">
> > <param xmlns:xs="http://www.w3.org/2001/XMLSchema"
> > type="xs:string"
> > style="template" name="username" />
> > </resource>
> > </resource>
> >
> > Can please s.o. confirm that this is an issue of the WadlGenerator and
> > not of our resource classes?
> >
> > Cheers,
> > Martin
> >
> >