private Resource generateResource(AbstractResource r, String path) {
return generateResource(r, path, Collections.<Class<?>>emptySet());
}
private Resource generateResource(AbstractResource r, String path, Set<Class<?>> visitedClasses) {
Resource wadlResource = _wadlGenerator.createResource(r, path);
// prevent infinite recursion
if (visitedClasses.contains(r.getResourceClass())) {
return wadlResource;
} else {
visitedClasses = new HashSet<Class<?>>(visitedClasses);
visitedClasses.add(r.getResourceClass());
}
Map<String, Param> wadlResourceParams = new HashMap<String, Param>();
// add resource field/setter parameters that are associated with the resource PATH template
List<Parameterized> fieldsOrSetters = new LinkedList<Parameterized>();
if (r.getFields() != null) {
fieldsOrSetters.addAll(r.getFields());
}
if (r.getSetterMethods() != null) {
fieldsOrSetters.addAll(r.getSetterMethods());
}
for (Parameterized f : fieldsOrSetters) {
for (Parameter fp : f.getParameters()) {
Param wadlParam = generateParam(r, null, fp);
if (wadlParam != null) {
wadlResource.getParam().add(wadlParam);
}
}
}
// for each resource method
for (AbstractResourceMethod m : r.getResourceMethods()) {
com.sun.research.ws.wadl.Method wadlMethod = generateMethod(r, wadlResourceParams, m);
wadlResource.getMethodOrResource().add(wadlMethod);
}
// add method parameters that are associated with the resource PATH template
for (Param wadlParam : wadlResourceParams.values()) {
wadlResource.getParam().add(wadlParam);
}
// for each sub-resource method
Map<String, Resource> wadlSubResources = new HashMap<String, Resource>();
Map<String, Map<String, Param>> wadlSubResourcesParams =
new HashMap<String, Map<String, Param>>();
for (AbstractSubResourceMethod m : r.getSubResourceMethods()) {
// find or create sub resource for uri template
String template = m.getPath().getValue();
Resource wadlSubResource = wadlSubResources.get(template);
Map<String, Param> wadlSubResourceParams = wadlSubResourcesParams.get(template);
if (wadlSubResource == null) {
wadlSubResource = new Resource();
wadlSubResource.setPath(template);
wadlSubResources.put(template, wadlSubResource);
wadlSubResourceParams = new HashMap<String, Param>();
wadlSubResourcesParams.put(template, wadlSubResourceParams);
wadlResource.getMethodOrResource().add(wadlSubResource);
}
com.sun.research.ws.wadl.Method wadlMethod = generateMethod(r, wadlSubResourceParams, m);
wadlSubResource.getMethodOrResource().add(wadlMethod);
}
// add parameters that are associated with each sub-resource method PATH template
for (Map.Entry<String, Resource> e : wadlSubResources.entrySet()) {
String template = e.getKey();
Resource wadlSubResource = e.getValue();
Map<String, Param> wadlSubResourceParams = wadlSubResourcesParams.get(template);
for (Param wadlParam : wadlSubResourceParams.values()) {
wadlSubResource.getParam().add(wadlParam);
}
}
// for each sub resource locator
for (AbstractSubResourceLocator l : r.getSubResourceLocators()) {
AbstractResource subResource = IntrospectionModeller.createResource(
l.getMethod().getReturnType());
Resource wadlSubResource = generateResource(subResource,
l.getPath().getValue(), visitedClasses);
wadlResource.getMethodOrResource().add(wadlSubResource);
for (Parameter p : l.getParameters()) {
Param wadlParam = generateParam(r, l, p);
if (wadlParam != null && wadlParam.getStyle() == ParamStyle.TEMPLATE) {
wadlSubResource.getParam().add(wadlParam);
}
}
}
return wadlResource;
}