Thursday, 22 March 2012

apache axis web service tutorial2


//addition.java



package additionService;

import java.util.Iterator;
import org.apache.axiom.om.*;

public class addition
{

    public addition()
    {
    }

    public OMElement add(OMElement omelement)
    {
        OMFactory omfactory = OMAbstractFactory.getOMFactory();
        org.apache.axiom.om.OMNamespace omnamespace = omfactory.createOMNamespace("http://www.example.org/additionService/", "additionService");
        OMElement omelement1 = omfactory.createOMElement("sum", omnamespace);
        Iterator iterator = omelement.getChildren();
        int i = Integer.parseInt(((OMElement)iterator.next()).getText()) + Integer.parseInt(((OMElement)iterator.next()).getText());
        omelement1.addChild(omfactory.createOMText((new StringBuilder()).append("").append(i).toString()));
        return omelement1;
    }
}


//services.xml


<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="additionService">
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
</messageReceivers>
<parameter name="ServiceClass" locked="false">
additionService.addition
</parameter>
<operation name="add"
mep="http://www.w3.org/2004/08/wsdl/in-out">
<actionMapping>
http://localhost:8080/axis2/services/additionService
</actionMapping>
</operation>
</service>
</serviceGroup>



//client.java


import java.io.StringWriter;
import javax.xml.stream.*;
import org.apache.axiom.om.*;
import org.apache.axis2.*;

import org.apache.axis2.client.*;
import org.apache.axis2.addressing.*;

public class addClient {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/additionService");
public static void main(String[] args) {
try {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://www.example.org/additionService1/", "additionService1");
OMElement add = fac.createOMElement("add", omNs);
OMElement number1 = fac.createOMElement("number", omNs);
number1.addChild(fac.createOMText("1"));
add.addChild(number1);
OMElement number2 = fac.createOMElement("number", omNs);
number2.addChild(fac.createOMText("2"));
add.addChild(number2);

System.out.println(add);


Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setAction("http://localhost:8080/axis2/services/additionService");
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(add);
StringWriter writer = new StringWriter();
result.serialize(XMLOutputFactory.newInstance()
.createXMLStreamWriter(writer));
writer.flush();
System.out.println(writer.toString());
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}

apache axis web service - tutorial 1





//add.java


package addition;
public class add{
public int add(int a,int b){
return a+b;
}
}

//inside your service package place that add.class file
//services dir -addition dir-add.class



//services.xml


<?xml version="1.0" encoding="UTF-8" ?>
<service name="arithmatic">
<description>addition program </description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">addition.add</parameter>
</service>


//services dir--addition dir--META-INF dir- services.xml

//test the your web services
http://localhost:8080/axis2/services/arithmatic

//the previous links show wsdl file means your service working file...

//if it show fault services means your services have some problem..

//the error may be services.xml syntax problem(so careful in writing services.xml)


//client program


import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.om.OMText;


import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.addressing.EndpointReference;


public class client{

public static void main(String args[]) throws Exception{

System.out.println("client program..");


//<add>
//<a>10</a>
//<b>20</b>
//</add>


OMFactory fac= OMAbstractFactory.getOMFactory();

OMNamespace omns=fac.createOMNamespace("http://addition","arith1");

OMElement add=fac.createOMElement("add",omns);


//create a xml request element with child element 'a' and 'b'

OMElement a=fac.createOMElement("a",omns);
a.setText("10");
add.addChild(a);

OMElement b=fac.createOMElement("b",omns);
b.setText("20");
add.addChild(b);

System.out.println("xml request: " + add );


EndpointReference epr=new EndpointReference("http://localhost:8080/axis2/services/arithmatic?wsdl");

Options op= new Options();

op.setTo(epr);

ServiceClient sc=new ServiceClient(); //using this class invoke web service

sc.setOptions(op);

OMElement result=sc.sendReceive(add); //this method for request and response

System.out.println("service response: "+ result); //print the response xml in console

}

}



//complile the java program with apache axis package as class path otherwise you get class definions not found error

ex: javac cp "c:/tomcat/webapps/axis2/services/webinf/lib/*;" client.java

//execute the java program with apache axis package as class path

ex: java cp "c:/tomcat/webapps/axis2/services/webinf/lib/*;" client



OMElement methods();


string getText(); //retrive the element value

setText(string); //set the element value

getFirstElement(); //retrive the first child element



OMFactory methods();


OMNamespace createOMNamespace("URI","namespace");

OMElement createOMElement("element name",namespace);

OMText createOMText("text value");



Options method();


setTo(EndpointReference);// set the target web service address using endpoint reference class


ServiceClient methods();

setOptions(OPTIONS);

OMElement send(OMElement); //for sending oly

 OMElement SendReceive(OMElement); //for sending receving xml request and response