Thursday, July 28, 2011

Sample client for Axis2 XMPP transport

Following is a sample service client that demonstrates the use of Apache Axis2's XMPP transport.

1) Add the following to your axis2.xml

i) Register the tranport listener.
<transportReceiver name="xmpp" class="org.apache.axis2.transport.xmpp.XMPPListener">
<!-- Account details for google talk -->
<parameter name="GoogleServer">
<parameter name="transport.xmpp.ServerUrl">talk.google.com</parameter>
<parameter name="transport.xmpp.ServerAccountUserName">axis2.xmpp.account1</parameter>
<parameter name="transport.xmpp.ServerAccountPassword">apacheaxis2</parameter>
<parameter name="transport.xmpp.ServerType">transport.xmpp.ServerType.GoogleTalk</parameter>
</parameter>
</transportReceiver>

ii) Register the transport sender.
<transportSender name="xmpp" class="org.apache.axis2.transport.xmpp.XMPPSender">
</transportSender>

2) Sample service client.
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 org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.transport.xmpp.util.XMPPConstants;
import org.apache.axis2.util.XMLPrettyPrinter;

public class XMPPSampleClient {
public static void main(String[] args) {
invokeTimeService();
}

private static void invokeTimeService() {
String endPointUrl = "xmpp://synapse.demo.0@gmail.com/" + "TimeService";

EndpointReference targetEPR = new EndpointReference(endPointUrl);
try {
ConfigurationContext ctx =
ConfigurationContextFactory.createConfigurationContextFromURIs(
XMPPSampleClient.class.getResource("axis2.xml"), null);

OMElement payload = getPayload();
Options options = new Options();
options.setProperty(XMPPConstants.XMPP_SERVER_TYPE, XMPPConstants.XMPP_SERVER_TYPE_GOOGLETALK);
options.setProperty(XMPPConstants.XMPP_SERVER_URL, XMPPConstants.GOOGLETALK_URL);
options.setProperty(XMPPConstants.XMPP_SERVER_USERNAME, "synapse.demo.0");
options.setProperty(XMPPConstants.XMPP_SERVER_PASSWORD, "mailpassword");

options.setTo(targetEPR);
options.setAction("urn:getServerTime");
ServiceClient sender = new ServiceClient(ctx,null);

sender.setOptions(options);
OMElement result = sender.sendReceive(payload);
XMLPrettyPrinter.prettify(result, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}

private static OMElement getPayload() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example.xmpp.transports.axis2.org/example1", "example1");
return fac.createOMElement("getServerTime", omNs);
}
}

3 comments:

nico said...

My current plan is to write web services using Axis2 which will connect to XMPP Server(Openfire) and that web services will provide all the functions (sendmessage, login etc.. )
But now it seems it is not working... Currently I am using smack.jar
Could you please tell me how can I implement it ? Or isn't it possible to do?
Thanks!

Heshan Suriyaarachchi said...

Hi Nico,

I did not check out the XMPP transport recently. Can you drop an email to axis2-transports mailing list and discuss it there?

nico said...

Thank you very much Heshan Suriyaarchchi !