| By Pramod Jain, Mahaveer Jain | Article Rating: |
|
| June 23, 2006 01:15 PM EDT | Reads: |
43,039 |
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.
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
- Jabber/XMPP: For functionality see www.jabber.org/software/clients.shtml.
- For Jabber server implementations see www.jabber.org/software/servers.shtml.
- For components that can be plugged into a Jabber server see www.jabber.org/software/components.shtml.
- For libraries to develop client, server, and components see www.jabber.org/software/libraries.shtml.
- D.J. Adams. Programming Jabber. O'Reilly. 2002.
- William Wright and Dana Moore. Jabber Developer's Handbook. SAMS. 2003.
- www.jivesoftware.org/
- www.jivesoftware.org/builds/wildfire/docs/ latest/documentation/plugin-dev-guide.html
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.
Published June 23, 2006 Reads 43,039
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Pramod Jain
Pramod 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.
More Stories By Mahaveer Jain
Mahaveer Jain is a lead programmer at INDENT. His expertise is in developing collaboration applications with Java technologies.
![]() |
SYS-CON Australia News Desk 06/23/06 09:05:30 AM EDT | |||
This article will describe our experiences with developing a Java-based instant messenger application using Jabber/XMPP (Extensible Messaging and Presence Protocol) - a free, open and public protocol and technology for instant messaging. According to the Jabber Software Foundation, 'Under the hood, Jabber is a set of streaming XML protocols and technologies that enable any two entities on the Internet to exchange messages, presence, and other structured information in close to real-time.' |
||||
![]() |
SYS-CON Australia News Desk 06/22/06 03:52:45 PM EDT | |||
This article will describe our experiences with developing a Java-based instant messenger application using Jabber/XMPP (Extensible Messaging and Presence Protocol) - a free, open and public protocol and technology for instant messaging. According to the Jabber Software Foundation, 'Under the hood, Jabber is a set of streaming XML protocols and technologies that enable any two entities on the Internet to exchange messages, presence, and other structured information in close to real-time.' |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?










































