Tuesday, March 24, 2009

Parsing WSDL 2.0 extensions with Woden

Web Services Description Language (WSDL) Version 2.0 Part 2: Adjuncts, specifies predefined extensions for WSDL2.0, Woden implements some of the predefined extensions such as WSDLX, RPC, SOAP, HTTP etc. But it is necessary to have a custom WSDL extension parsing mechanism to supports to other WS specifications such as WS-Security, WS-Policy etc. There are two ways to deal with extensions in Woden.

  • Use traditional Extensions handling mechanism.
I used the word "traditional” because this mechanism was originally developed for WSDL4J and Woden reuse the same concept. This wiki page provides comprehensive details about traditional Extensions handling mechanism architecture in Woden.
  • Use new ComponentExtensionContext based Extensions handling mechanism.
This new Extension handling mechanism was developed according to the feedbacks collected from Woden users and a much more simplified way to deal with WSDL extensions. This wiki page is a good resource about this mechanism.


In this post I'm going to discuss about how we can write set of classes in order to parse custom WSDL extension elements, instead of imaging a hypothetical scenario I will use a real world requirement derived from woden users list. Let’s see how we can use traditional extensions handling mechanism to parse WS-Policy definition available on a WSDL documents and how can retrieve those information as a Apache Neethi Policy or PolicyReference objects.


According to the Woden API we need to develop following classes to achieve our goal.
1. PolicyExtensionSerializer - This implements ExtensionSerializer interface and used to serialize WS-Policy elements.
2. PolicyExtensionDeserializer - This implements ExtensionDeserializer interface and used to deserialize WSDl elemnts in to WS-Policy.
3. PolicyExtension - This implements ExtensionElement and act as wrapper for WS-Policy components.
4. PolicyExtensionsImpl - implementation of above PolicyExtensionsImpl.
5. PolicyExtensionSample - practicality illustrate usage of above classes.


Here I have listed only some of the impotent points; you can download complete source code as a eclipse project from here.

PolicyExtensionSerializer

In the PolicyExtensionSerializer implementation, marshall () is the most significant method, but in our case we don’t need to worry about serialization so keep it as an empty method as below.

public void marshall(Class parentType, QName elementType,
ExtensionElement extension, PrintWriter pw,
DescriptionElement desc, ExtensionRegistry extReg)
throws WSDLException {
}


PolicyExtensionDeserializer
Once we parse WSDL document, WSDlReader query for ExtensionDeserializer on ExtensionRegistry when it encounter a non-WSDL elements or attribute, Here
unmarshall method is significant , in our case it perform following tasks
  1. Read the Policy definition
  2. Store policy elements as a XMLElemen so that we can create DOM element from it.
public ExtensionElement unmarshall(Class parentType, Object parent,
QName extType, XMLElement extEl, DescriptionElement desc,
ExtensionRegistry extReg) throws WSDLException {
PolicyExtension ele = (PolicyExtension) extReg.createExtElement(
parentType, extType);

ele.setExtensionType(extType);
ele.setXMLElement(extEl);
return ele;
}

PolicyExtension
Define following specific methods to deal with WS-Policy objects.

public void setXMLElement(XMLElement source) - to set WS-Policy element as a XML content.
public XMLElement getXMLElement() - to set WS-Policy element as a XML content.
public PolicyComponent getPolicy() - to set WS-Policy element as a Apache Neethi PolicyCompoent.


Our PolicyExtensionSample should perform following set of steps in a sequential manner.


1. Create a WSDLReader instance.

factory = WSDLFactory.newInstance();
WSDLReader reader = factory.newWSDLReader();
reader.setFeature(WSDLReader.FEATURE_VALIDATION, true);

2. retrieve the ExtensionRegistry from WSDLReader instance.

// QName to represent top level Policy Element.
QName type = new QName(
"http://schemas.xmlsoap.org/ws/2004/09/policy", "Policy");

// Retrieve ExtensionRegistry from WSDLReader
ExtensionRegistry reg = reader.getExtensionRegistry();



3. Register our Serializer, Deserializer and ExtElement with ExtensionRegistry.


// register the ExtensionSerializer
reg.registerSerializer(Description.class, type,
new PolicyExtensionSerializer());
// register the ExtensionDeserializer
reg.registerDeserializer(DescriptionElement.class, type,
new PolicyExtensionDeserializer());
// register the ExtElementType
reg.registerExtElementType(DescriptionElement.class, type,
PolicyExtensionsImpl.class);

4. Now we can parse our document and possible to retrive Policy objects as follows

Description descComp = reader
.readWSDL("/work/workspace/axis/wodenExt-eg/wsdl/test.wsdl");

// Retrieve ExtensionElements from DescriptionElement.
DescriptionElement descElem = descComp.toElement();
ExtensionElement[] extEls = descElem
.getExtensionElementsOfType(type);
for (int i = 0; i < extEls.length; i++) {
PolicyExtension policyExt = (PolicyExtension) extEls[0];
// Retrieve policy source as a DOM Element.
Element ele = (Element) policyExt.getXMLElement().getSource();
// Retrieve the policy definition as a Apache Neethi Policy
// instance.
Policy policy = (Policy) policyExt.getPolicy();
System.out.println(" policy " + policy.getName() + " :: "
+ policy.getId());

}


Download source codes from here

Wednesday, March 11, 2009

How I see SL Railway web Site

Two or three days ago I saw a online news paper article about SL Railway Department (SLR) lunching a web site to facilitate commuters to access online time table , in addition to HTTP based services they offer WAP site for mobile based access . This is a good news to those who use public transportation system.

I really happy to see this site not only because I’m a commuter , but also four years ago , in 2005 I did a similar project for my BIT final year project .Those days I really want to do something innovative for my project, there were enough number of payroll, booking, user management systems everywhere. One evening when I was with my friends with our faculty common room some ideas came into my mind about how I can use IT to increase the efficiency and solve some possible problems of our railway system.

My original intention was to provide a dynamic train time table for mobile access through WAP, in my context dynamic means: the arrival time of a specific train at a specific station will dynamically update according to the current running information of that particular train. In each station arrival time to be entered to a web interface and those collected data are used to predict possible arrival times for next coming stations of the route.

As I remember Mr.Samarasinghe one of the main engineer of SLR arranged a visit to observe activities of SLR main control office at Maradana, when I was there I observed officers use pencil and drawing papers to draw train route to identify possible collision among them, so I realized it is possible to develop a graphical monitor so that it will automatically display train route according to the data collected at each station.

I did a prototype for the project and SLR engineers ware happy with the that further they really wanted to implement it , but later with my academic works I couldn’t contact them , but I can see some of the the features that I identified are available on this system.

Anyhow this is not 2005 this is 2009, so I think this system is not perfectly match with today’s requirements , still I’m not sure about the feasibility of dynamic time table concept with our existing infrastructure but I like to see at least following feature with the system because it is not a great deal .

Unlike those days there is no need to implement separate WAP site, because today’s mobiles devices are much more powerful, so we can develop same web site to support to mobile browsing too. Instead, SMS based query service is much more impotent feature for now, basically we have several options.

One possible option is to have partnership with a mobile service provider , Dialog GSM already provide some location based services, may be some other providers also , where I can query about police stations ,ATMs near to me by sending a SMS . So with such a service provider we can implement a SMS based time table query service very easily.

Other option is use a web based two way SMS aggregator service with SMPP support, there are number of such services available now, I have used such service called clickatell for one of our project. SMPP or Short Message Peer-to-Peer is a telecommunications protocol that supports for SMS exchanging among two parties, often in bulk, this protocol can handle very high SMS traffic.

Also I’m wonder why they can’t provide web based booking facilities with this site.

Wednesday, March 04, 2009

Axis2 Maven Module (MAR) plug-in

It is possible to use Axis2’s Mar maven plug-in very easily to package Axis2 module as .mar archive format. First create a Maven project using Maven “archetype:generate " goal or IDE like Eclipse. Then create necessary classes for module such as Module ,IncomingHandler ,OutgoingHandler.........etc.

Second step is creating a directory called "META-INF" in the src/main/resources directory; you can place your module.xml file there, and then add following settings in to the project POM file.


<build>
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-mar-maven-plugin</artifactId>
<version>1.4.1</version>
</plugin>
</plugins>
</build>

Now open a console and run "mvn compile axis2-mar:mar " command to compile source codes and assemble module as a .mar package.

If your module.xml file is available on somewhere else you can use "moduleXmlFile" parameter as follows.

<build>
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-mar-maven-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<moduleXmlFile>src/main/webapp/WEB-INF/modules/counterModule/META-INF/module.xml</moduleXmlFile>
<includeDependencies>false</includeDependencies>
</configuration>
</plugin>
</plugins>
</build>