Sunday, February 10, 2013

WADL-Web Application Description Language


WADL (Web Application Description Language) is a machine-readable description of HTTP-based REST web Services. 

All WADL elements have the following XML namespace name:http://wadl.dev.java.net/2009/02.
Use the following symbol meanings below.  *  => 0 or more, ? => 0 or 1,  + => 1 or more


<application>
forms the root of a WADL description
<doc>   ( + )
·         Each WADL-defined element can have child doc elements that can be used to document that element.
·         Attributes
xml:lang > Defines the language for the title attribute value and the contents of the doc element
title > A short plain text description of the element being documented

<grammars> (?)

·         Acts as a container for definitions of the format of data exchanged during execution of the protocol described by the WADL document.
·         XML grammars used by the service,
·         Currently specify use of W3C XML Schema or RelaxNG

<include> (*)
allows the definitions of one or more data format descriptions to be included by reference

<resources> (?)

acts as a container for the resources provided by the application

<resource> (+)
·         describes a set of resources
·         Identified by a URI template
·         Specify Web resource and which methods are supported
·         Attributes: id, path, type, queryType

<method> (+)
describes the input to and output from an HTTP protocol method that may be applied to a resource

<request> (?)
the input to the method as a collection of parameters

<response> (?)
describe the possible outputs of the method

<representation> (*)
·         Describe the format of a HTTP entity
·         Can refer to grammars


for a complete description visit w3spec

WADL Document Structure


<application>
<doc/>*
<grammars/>?
<resources base='anyURI'>?
<doc/>*
<resource path='template' type='anyURI+'?>+
<doc/>*
<param/>*
( <method/> | <resource/> )+
</resource>
</resources>
( <method/> | <representation/> | <fault/> |
<resource_type/>)*
</application>


WADL Method Structure 


<method name='NMTOKEN'? id='ID'? href='anyURI'?>
<doc/>*
<request>?
<param>*
<representation/>*
</request>
<response>?
( <representation/> | <fault/> )*
</response>
</method>


Example WADL Description


The following is an example of a WADL description for the Yahoo News Search application (link) 


Try a real REST service 


This is a sample .wadl descriptor of a real REST service. To understand how the path for a web resource of a REST service is can be generated according to the information in .wadl you can try invoking some services (this web service is secured so that you can't access all resources without permission, just try some GET services ). eg: try ttp://repo.jfrog.org/artifactory/api/plugins/getPluginInfo

wadl2java


This tool will generate a java client based on the provided WADL that makes use of the Jersey 1.x and JAX-RS 2.0 client API. Visit open source project 
Most of the IDEs support creating REST services and clients(stub). eg: Guide lines for creating rest services and clients with IntellijIDEA can be found here

Yahoo News Search Stub


public class NewsSearch {
public NewsSearch() {...}
public ResultSet getAsResultSet(
String appid, String query) {...}
public DataSource getAsApplicationXml(
String appid, String query) {...}
public DataSource getAsApplicationJson(
String appid, String query) {...}
public DataSource getAsApplicationPhp(
String appid, String query) {...}
...
}


Maping WADL to Java


public class NewsSearch {
public NewsSearch() {...}
public ResultSet getAsResultSet(
String appid, String query) {...}
}

<resource path="newsSearch">
             <param name="appid" style="query"/>
             <method name="GET">
             ...
            </method>
</resource>
public class NewsSearch {
public NewsSearch() {...}
public ResultSet getAsResultSet(
String appid, String query) {...}
}
<resource path="newsSearch">
<param name="appid" style="query"/>
<method name="GET">
...
</method>
</resource>
public class NewsSearch {
public NewsSearch() {...}
public ResultSet getAsResultSet(
String appid, String query) {...}
}

<resource path="newsSearch">
<param name="appid" style="query"/>
<method name="GET">
...
</method>
</resource>

public class NewsSearch {
public NewsSearch() {...}
public ResultSet getAsResultSet(
String appid, String query) {...}
}

<method name="GET">
<request>
<param name="query" style="query"/>
</request>
<response>
<representation element="y:ResultSet"/>
</response>
</method>

public class NewsSearch {
public NewsSearch() {...}
public ResultSet getAsResultSet(
String appid, String query) {...}
}

<method name="GET">
<request>
<param name="query" style="query"/>
</request>
<response>
<representation element="y:ResultSet"/>
</response>
</method>


Client Code


NewsSearch s = new NewsSearch();
ResultSet rs = s.getAsResultSet("some_app_id","java");
for (Result r: rs.getResultList()) {
System.out.printf("%s (%s)\n",
r.getTitle(),
r.getClickUrl());
}









No comments:

Post a Comment