HOME | J2ME | Struts | AJAX | SOAP | SOA MEDIA STREAMING AXIS |
AXIS STEP BY STEP
Web Services
Axis Introduction
Installations
Hello World
Using Eclipse
POJO Service
POJO Client
AXIOM Service
AXIOM Client

 

 

AXIOM Based Axis Web Service End Points

Back | Tutorial Home | Next

AXIOM stands for AXIs Object Model and is a based on StAX(Streaming API for XML) and was developed to improve performance in XML parsing mechanism. AXIOM uses light weight XML with smaller hierarchy of XML elements than traditional XML technologies. It was initially developed for Axis but is now an Apache Commons project. AXIOM uses a technology called pull parsing where required parsing is done by the AXIOM engine rather than depending on externals parsers. Axiom comprises two layers

  • Object Model(OM) Layer which takes care of the XML processing
  • SOAP Layer sits over the Object Model layer and takes care of the SOAP modeling.

Let’s create an Axiom Document and later use in our Phone Book example. This example has following request

  • Set phone number to a particular employee id
  • Get the phone number of the user based on employee id

Below code uses AXIOM to read and create XML to get phone number

Reading through the AXIOM XML Tree

Object element in the below code is the XML request for phone number.

Build the xml tree using AXIOM API for the request element
element.build();

Detach the node from its parent container
element.detach();

Get First element which is employee id
OMElement empIDElement = element.getFirstElement();
String empid = empIDElement.getText();


Get the phone number from the local hash map
String phoneNumber = (String) map.get(empid);


Writing to an AXIOM XML tree

Build the response using factory method
OMFactory fac = OMAbstractFactory.getOMFactory();

Create name space definition
OMNamespace omNs = fac.createOMNamespace("http://axiom.demo/xsd", "ns");

Create response element
OMElement method = fac.createOMElement("getPhoneNumberResponse", omNs);

Create element called return for return value
OMElement value = fac.createOMElement("return", omNs);

Add elements according to hierarchy
value.addChild(fac.createOMText(value, phoneNumber));
method.addChild(value);

The final generated XML will be as shown below

<ns:getPhoneNumberResponse xmlns:ns="http://axiom.demo/xsd">
<ns:return>53453456</ns:return>
</ns:getPhoneNumberResponse>

Download Phone Book source code discussed in this lesson.

Now Lets have a look at the complete code for Phone Book Service which is derived from the above code segments

package demo.axiom.service;

import javax.xml.stream.XMLStreamException;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

import java.util.HashMap;
public class PhoneBookService {
  private HashMap map = new HashMap();

  private String namespace = "http://axiom.demo/xsd";

  public OMElement getPhoneNumber(OMElement element) throws XMLStreamException {
    	
    // build the xml tree using AXIOM
    element.build();
    // detach the node from its parent container
    element.detach();
        
    // Get First element which is employee id
    OMElement empIDElement = element.getFirstElement();
    String empid = empIDElement.getText();
        
    // Get the phone number from hash map
    String phoneNumber = (String) map.get(empid);

    // Build the response 
    OMFactory fac = OMAbstractFactory.getOMFactory();
        
    // Create name space definition 
    OMNamespace omNs =
      fac.createOMNamespace(namespace, "ns");
    // Create response element 
    OMElement method = fac.createOMElement("getPhoneNumberResponse", omNs);
        
    // Create element called return    
        OMElement value = fac.createOMElement("return", omNs);
        
    // add elements according to hierarchy
    value.addChild(fac.createOMText(value, phoneNumber));
    method.addChild(value);

    /*Final AXIOM XML: 
        
    <ns:getPhoneNumberResponse xmlns:ns="http://axiom.demo/xsd">
    <ns:return>53453456</ns:return>
    </ns:getPhoneNumberResponse> */    
        
    return method;
  }

  public void setPhone(OMElement element) throws XMLStreamException {
    	
    /*Received AXIOM XML: 
        
    <tns:setPhone xmlns:tns="http://axiom.demo/xsd">
    <tns:empid>Tom</tns:empid><
    <tns:phoneno>53453456</tns:phoneno>
    </tns:setPhone>*/
    	
    element.build();
    element.detach();
        
    // Get employee id from request XML 
    OMElement empIDElement = 
	element.getFirstChildWithName(new QName(namespace, "empid"));
    String empid = empIDElement.getText();

    OMElement phoneElement = 
	element.getFirstChildWithName(new QName(namespace, "phoneno"));
    String phone = phoneElement.getText();
        
    map.put(empid, phone);
  }
}

Code for getPhoneNumber() method is already described above. setPhone() method simply gets the request XML with phone number and employee id and sets the values to hash map.

The service definition should be described in services.xml

	  
<service name="PhoneBookService" scope="application">
	<description>
	Phone Book Service
	</description>
	<operation name="getPhoneNumber">
		<messageReceiver class="
		org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
	</operation>
	<operation name="setPhone">
		<messageReceiver class="
		org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
	</operation>
	<parameter name="ServiceClass">demo.axiom.service.PhoneBookService
	</parameter>
</service>

Only difference in this file compared to one for POJO is the Message Receiver description required for AXIOM data binding -

org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver

Back | Tutorial Home | Next

site comments powered by Disqus
Download our free toolbar

toolbar powered by Conduit

| Copyright © 2009. All rights reserved | Terms and Conditions | About | Contact | Feed Back |