powered by Jive Software

Bug: packet.getExtension() failing after iterating extensions

Or maybe I’m just missing something.

If I itereate over the extensions on a packet, a subsequent call to packet.getExtension() fails to find the extension. If I reverse the order of the calls, both work. In the snippet below (I’ve already registered the extension, etc.)

public void processPacket(Packet packet) {

if (packet instanceof Message) {

// some syntactic sugar because parenthesis are endangered and casting wastes them

Message m = (Message)packet;

boolean contentHandled = false;

/* straight get */

PacketExtension pe2 = m.getExtension(“content”, XMLConstants.AV_CORTEXT_NAMESPACE);

if(pe2 != null){

log.debug("found a cortext message with content element in namespace: " + pe2.getNamespace());

// connection.sendPacket(cmf.handleContent(m, botDirectory));

}

/* itereate to find it and use it */

Iterator i = m.getExtensions().iterator();

while(i.hasNext()){

PacketExtension pe = i.next();

log.debug("extension: " + pe.getClass().getCanonicalName() + " " + pe.getElementName() + " " + pe.getNamespace());

if(pe.getElementName().equals(“content”)){

log.debug("found content element: ");

log.debug("it is a: " + ((ContentExtension)pe).getMimeType());

// connection.sendPacket(cmf.handleContent(m, botDirectory));

contentHandled = true; // set flag so that we don’t handle it again

break;

}

}

… other code elided …

}

In that approach I get the output and results I expect.

[DEBUG 19 Dec 2012 10:06:26] - [processPacket ] …

[DEBUG 19 Dec 2012 10:06:26] - found a cortext message with content element in namespace: http://imprivata.com/protocol/cortext

[DEBUG 19 Dec 2012 10:06:26] - extension: org.jivesoftware.smack.packet.DefaultPacketExtension domain http://imprivata.com/protocol/cortext

[DEBUG 19 Dec 2012 10:06:26] - extension: org.jivesoftware.smack.packet.DefaultPacketExtension request urn:xmpp:receipts

[DEBUG 19 Dec 2012 10:06:26] - extension: com.imprivata.mpgqa.test.xmpp.bot.smack.extensions.ContentExtension content http://imprivata.com/protocol/cortext

[DEBUG 19 Dec 2012 10:06:26] - found content element:

[DEBUG 19 Dec 2012 10:06:26] - it is a: application/x-cortext-callback

If I swap things around and iterate first, then use packet.getExtension() the PacketExtension (pe2) comes back as null so my output looks like:

[DEBUG 19 Dec 2012 10:21:33] - [processPacket ] …

[DEBUG 19 Dec 2012 10:21:33] - extension: org.jivesoftware.smack.packet.DefaultPacketExtension domain http://imprivata.com/protocol/cortext

[DEBUG 19 Dec 2012 10:21:33] - extension: org.jivesoftware.smack.packet.DefaultPacketExtension request urn:xmpp:receipts

[DEBUG 19 Dec 2012 10:21:33] - extension: com.imprivata.mpgqa.test.xmpp.bot.smack.extensions.ContentExtension content http://imprivata.com/protocol/cortext

[DEBUG 19 Dec 2012 10:21:33] - found content element:

[DEBUG 19 Dec 2012 10:21:33] - it is a: application/x-cortext-callback

The “found a cortext message” output that is gated by the null check is never fired.

-Rich