Tuesday, July 26, 2011

W3C Document support in Axis2



Recently we have implemented W3C Document  support for Axis2 ADB data binding. Now it is possible to use W3C Dom Document  as a input parameter , return type or as a field of a java bean. 
Let’s consider following web service method as an example. 
 public Document simpleDomService(Document doc) {  
    printInCommingDocument(doc);  
    return doc;  
  }  
It takes W3C DOM Document  as a parameter and return the same value. In order to support this method Axis2 run time should construct DOM Document from incoming payload and need to be serialized into out going payload in both server and client sides. 
In server side  based on Java refection Axis2 run-time identify parameters and return type of DOM Document and perform required conventions. But in client side this become bit of a complex task because they is no standard  way  to represent DOM Document in a WSDL. Axis2  generate xs:any type for DOM Document and this is represented as a OMElement  in generated code.
With above limitation if you have a Dom Document first you need to convert into a OMElement , you could use following code segment to convert DOM to OM. 

 OMFactory fac = OMAbstractFactory.getOMFactory();  
 OMElement domDoc = BeanUtil.convertDOMtoOM(fac, getDomDocument()); 
 
The other option is without constructing a DOM Document, directly construct a OMElement and pass it to the server side. If the server side method parameter type is DOM Document  Axis2 run-time can construct  a DOM Document for that particular service. As an example following code segment use String to construct OMElement. Refer Axiom user guide to find out other options.  
 public static OMElement getOmDocument() throws XMLStreamException {  
    return AXIOMUtil  
        .stringToOM("sample content using Axiom");  
  }  
After a successful invocation you receive a OMElement as the return value , sometimes you may want to convert this incoming value in to a DOM back . In such scenarios following code segment can be used. 

 public static org.w3c.dom.Document getDomDocument(OMElement element){  
    OMFactory doomFactory = DOOMAbstractFactory.getOMFactory();  
    StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory,  
        element.getXMLStreamReader());  
    return (org.w3c.dom.Document) doomBuilder.getDocument();  
  }  

Here is the complete  client side code 

     // create a OMElement using Axiom tools.  
     OMFactory fac = OMAbstractFactory.getOMFactory();  
     OMElement domDoc = BeanUtil.convertDOMtoOM(fac, getDomDocument());  
     // create a pure Axiom based document  
     OMElement omDoc = getOmDocument();      
     SimpleDomServiceStub stub = new SimpleDomServiceStub();  
     SimpleDomService req = new SimpleDomService();  
     Document doc = new Document();  
     req.setDoc(doc);  
     //1st call  
     doc.setExtraElement(domDoc);  
     SimpleDomServiceResponse res = stub.simpleDomService(req);  
     res.get_return().getExtraElement().serialize(System.out);  
     //2nd call  
     System.out.println();  
     doc.setExtraElement(omDoc);  
     res = stub.simpleDomService(req);  
     res.get_return().getExtraElement().serialize(System.out);  

Note : 
1.) Some of above features based on Axiom based DOM API implementation called DOOM. But DOOM is not a complete implementation of W3C DOM and you may find some limitation. 
2.) Above features available with  upcoming   Axis2 1.7.0 release and right now you can try with 1.7.0-SNAPSHOT.
3.) Download sample project from here.  You can run “mvn jetty:run “ to start embedded Jetty server and WSDL content can be found here http://localhost:8080/services/SimpleDomService?wsdl. 

0 comments: