powered by Jive Software

Getting started - extending a stanza

Hi all,

I’m just looking for a way to get me started…

I want to extend a stanza (probably Presence) with my own namespace and some structured information within it.

I’m assuming I just write a packetListener and do something with the packets… but I’m hoping that I don’t need to get down to editing raw XML do I?

Does anyone know (or even better have an example) of the code needed to create a new packet and extend it with my new XML?

Many thanks

You can either modify a packet directly, by manipulating the underlying (dom4j-based) XML representation, or use a org.xmpp.packet.PacketExtension.

The latter is probably the ‘cleaner’ solution, but, as Openfire plugins hardly every generate new packets themselves (typically, they generate responses to stuff they received), the former is more common. Have a look at one of the many implementations of IQHandler, for example.

Thanks very much Guus, I knew you’d save me

I got as far as experimenting with the PacketExtensiok last night but it kept failing for some reason however, I thought that the PacketExtension WAS manipulating the underlying xml because it took an Element as the argument (and Element is a dom4j class).

Do you have a very simple fee lines of code that would show this? I’ve been looking at IQ, but because IQ seems somewhat different (Request Response) it was difficult to understand how to apply it to Presence.

Sorry but thanks.

p.s. Either way, it’s great to know that OpenFire has the capability to support this built right into it!

This is what I have as a starter for 10… but it bombs out at step 5 (apologies for the horrible debugging I’ve had to put in!)

Log.debug(“Step 1”);

Document document = DocumentHelper.createDocument();

Log.debug(“Step 2”);

Element root = document.addElement( “root” );

Log.debug(“Step 3”);

Element author1 = root.addElement( “author” )

.addAttribute( “name”, “James” )

.addAttribute( “location”, “UK” )

.addText( “James Strachan” );

Log.debug(“Step 4”);

PacketExtension pe = new PacketExtension(author1);

Log.debug(“Step 5”);

packet.addExtension(pe);

Log.debug(“Step 6”);

((Message) packet).setBody(“This message was modified”);

Log.debug(“Step 7”);

In case anyone comes across and needs another example, I solved this with the following snippet:

Log.debug(“Packet Interception started”);

setNameSpace(“http://chat.mydomain.com/information/DI”);

if (packet instanceof Message && ((Message) packet).getBody() != null && incoming) {

Log.debug(“Setting up packet extension”);

PacketExtension pe= new PacketExtension(“x”, getNameSpace());

DocumentFactory factory = DocumentFactory.getInstance();

Document doc = factory.createDocument();

Element root = doc.addElement(“DI”);

root.addText(“Test01”);

pe.getElement().add(root);

packet.addExtension(pe);

}

1 Like