Dynamic Wadl
The WADL (Web Application Definition Language) is used to describe HTTP-based applications which means that JAXRS applications can and are described by that. Typically it is generated (static or dynamic) with the jersey maven plugin (detailed informations can be found here). But for some reason i could not use either maven nor the jersey plugin. So i had to do it on my own and used it as a little finger exercise. The main goal was to achieve dynamic wadl generation for a JAXRS application.
The working steps were relativly easy:
- Create a JAXB-Model from the current WADL-Specification
- Create a CDI-Based design for JAXRS resources
1 @Path("/")
2 public class MyResource
3 {
4 @Inject
5 @Wadl
6 @WadlApplication
7 Instance<Application> applicationInstance;
8
9 @GET
10 @Produces(MediaType.APPLICATION_XML)
11 public Application getRoot(@Context UriInfo uriInfo)
12 {
13 return getWadl(uriInfo);
14 }
15 }
As a simple example assume that you have a TinkerForge MasterBrick connected to a RaspberryPi. Addionally you want to provide a JAXRS-Resource for every connected sensor. With the sample code below you are now able to extend the WADL dynmically.
1 public class WadlClassProducer
2 {
3 @Inject
4 @SensorAnno
5 @Any
6 Instance<SensorResource> sensorInstances;
7
8 @Produces
9 @Wadl
10 public Class[] create()
11 {
12 List<Class<?>> clazzList = new ArrayList<Class<?>>();
13 for (SensorResource sensor : sensorInstances)
14 {
15 clazzList.add(sensor.getResourceClass());
16 }
17 return clazzList.toArray(new Class[0]);
18 }
19 }
The sources can be found here.