Feature
Building an Instant Messaging Application Using Jabber/XMPP
An adventure with Smack and Wildfire
Jun. 23, 2006 01:15 PM
Client-Side Processing of Reply Packets
At the
client end, you'll have to define a class to handles the IQ message.
This is defined in a configuration file called smack.
provider:
<iqProvider>
<elementName>query</elementName>
<namespace>jabber:iq:token</namespace>
<className>org.jivesoftware.smack.packet.ClientID$Provider</className>
</iqProvider>
The iqProvider defines the class and methods for both
generating custom iq messages with the namespace jabber:iq:token for
sending to server or other clients and for processing incoming iq
messages with the namespace jabber:iq:token.
The first section of the code is for generating an iq packet, as
described in the section "Client to Server" above. The provider
contains a parseIQ() method to parse the incoming message and extract
useful data and the tokenNum in the reply message above and stores them
in object c in Listing 2. This ends the lifecycle of the iq packet.
Use Case 2: The Custom Message
During
collaboration, userA wants to launch a browser with a URL in userB's
machine. This will be implemented as a custom <message>. Usually
<message> is used to carry chat messages, but in this use case it
will be used to carry a custom message (<xsow>) like:
<message xmlns="" id="WHFl2-7" to="userB@sow" from="userA@sow/1139322562328">
<xsow xmlns="http://www.indent.org">
<conf Url="http://my.shareonWeb.com/jetspeed/indent/sowWCLogin.jsp?p=2079810"/>
</xsow>
</message>
The steps are:
- At the client SOWExtension, a custom PacketExtension, is
implemented to create the message and the provider to parse the
incoming custom message
- At the receiving client, a packet filter and listener are implemented to trap the custom message and process it.
This is illustrated in
Figure 2.
Creating a Custom Message at the Client In the use case, the first step
for userA to send a custom message to userB is to do the following in
the client GUI: userA chooses "Open Document" from a menu or
right-clicks on userB in the roster list and chooses "Open Document."
The client GUI in our case uses Smack and JFace; "Open Document"
triggers a call to the run() method associated with the selection. The
run() method creates a message and sends to server, which it then
routes to userB.
public void run() {
Message iceMessage = new Message();
SOWExtension icePacket = new SOWExtension();
String urlToSend = this.appData.aspireRequest + "&p=" + ClientID.token;
icePacket.setUrl(urlToSend);
iceMessage.addExtension(icePacket);
iceMessage.setType(Message.Type.NORMAL);
for (Iterator i=selection.iterator(); i.hasNext();) {
Object user = i.next();
RosterEntry entry = (RosterEntry) user;
iceMessage.setTo(entry.getUser());
Session.getInstance().getConnection().sendPacket(iceMessage);
}
}
A standard message/packet is created first; an extension
is created on the message using the new SOWExtension() - this is going
to carry the custom packet; the custom packet is added to the message
using addExtension(icePacket). The for loop then sends the message to
each selected user (userB, userC, ...). The sendPacket() method calls
toXML() in Listing 3 to create an XML string.
The SOWExtension class implements PacketExtension; one of the methods
of interest is the class is toXML() and the class of interest is the
Provider class. The Provider class provides the parse method for
incoming custom messages with the extension <xsow>. The provider
class will be discussed in the next section.
Processing the Custom Message at the Client
Note
that the server doesn't do any operation on the message other than
route it to userB. userB gets the message first by parsing it using the
parseExtension() in Listing 3, and then passing the message through a
filter described below. filterExt below is a filter that looks for packets with the extension "xsow." When the filter recognizes such a message, a listener
then processes the message. The processPacket() method below first
extracts the extension in the custom message by calling
message.getExtension(). getExtension() returns an SOWExtension object
defined in Listing 3. Since the custom message above has already been
parsed, this object contains all the relevant information contained in
the packet extension. sowExt.getURL() returns the desired value. The
last two lines in listener below correspond to the desired action, in
this case, opening a browse with a URL that was contained in the
extension of the custom message above.
PacketListener listener = new PacketListener() {
public void processPacket(Packet packet) {
final Message message = (Message) packet;
SOWExtension sowExt = (SOWExtension) message.getExtension(
SOWExtension.elementname, SOWExtension.namespace);
String url = sowExt.getUrl() + "&uname=" +
Session.getInstance().getConnectionDetails().getUserId();
Program.launch(url);
}
};
PacketExtensionFilter filterExt = new PacketExtensionFilter("xsow", "http://www.indent.org");
connection.addPacketListener(listener, filterExt);
The message extension and the SOWExtension's Provider are defined in smack.provider.
<extensionProvider>
<elementName>xsow</elementName>
<namespace>http://www.indent.org</namespace>
<className>org.jivesoftware.smackx.packet.SOWExtension$Provider</className>
</extensionProvider>
Note: iqProvider to handle custom IQ messages
was defined in reply packets code above. extensionProvider to handle
custom message extension is defined in the code immediately above.
Summary
There are several Open Source and
commercial implementations of XMPP server and client to choose from
that provide basic chat, presence, and roster functionality. Here we
discussed how to extend the functionality of your XMPP-based IM
applications through two mechanisms: defining custom queries in the
<iq> element and defining extensions in the <message>
element. Client-side extensions and server-side plug-ins are described
to accomplish the custom functionality.
Some of the specific uses of the <iq> extension will be to create
rich IM applications that interact with a backend application server
and/or database; examples of uses of the <message> extension
include doing things like opening browsers and sending files from one
user's client IM application to another.
References
Acknowledgements We would like to acknowledge
partial funding from NIMH of NIH, and the help of Gaurav Mantro for
coding some pieces of the client application and thank Ryan Graham,
Gaston Dombiak, and a few others for answering questions we posted on
www.jivesoftware.org's discussion forum for developers.
About Pramod JainPramod Jain is president of Innovative Decision Technologies, Inc. (INDENT, www.indent.org), in Jacksonville, FL. Their clients include Recruitmax, NASA, and NIH. Pramod has a PhD from the University of California, Berkeley.
About Mahaveer JainMahaveer Jain is a lead programmer at INDENT. His expertise is in developing collaboration applications with Java technologies.