IQ packets (error and result)

I’‘m trying to implement one of the JEPs as an IQ extension. Smack contains some good examples of how to do this, but I can’'t find any uses of how to create an error or result message.

IQ is an abstract class so it’'s not possible to create one (as I saw in an example in this forum) using:

IQ iq = new IQ();

iq.setType(IQ.Type.RESULT);

I suppose I could just use an empty IQ of my extension type, but I run into more confusion when I need to send an error.

XMPPError seems like the place to start, but it doesn’‘t include the type attribute or child stanza that seem to be required by the XMPP core. If I create an empty IQ as above and set the type to ERROR, how do I add the XMPPError as a child element? There doesn’'t appear to be a setChild or addChild method on IQ.

Thanks

doug

Doug,

I’'m trying to implement one of the JEPs as an IQ

extension. Smack contains some good examples of how

to do this, but I can’'t find any uses of how to

create an error or result message.

You’'d normally need to create an IQ packet of the same class of the IQ packet that you need to respond. In the following example the variable “discoverInfo” is a DiscoverInfo that Smack received and is responding to.

DiscoverInfo response = new DiscoverInfo();
    response.setType(IQ.Type.RESULT);
    response.setTo(discoverInfo.getFrom());
    response.setPacketID(discoverInfo.getPacketID());
    ...
    connection.sendPacket(response);

In order to respond with an error you will need to set the packet’'s type and the specific error.

response.setType(IQ.Type.ERROR);
    response.setError(new XMPPError(501, "feature-not-implemented"));

Regards,

– Gato

Thanks for the info. That’'s pretty much what I ended up doing.

I noticed that you set the packetID on the response packet. Is that necessary? I was under the impression that the server would assign the packetID (and also the from field).

Thanks

doug

The reason why you need to set the packetID is that when you are responding to a request you need to maintain the original ID. That will make life easier to the other client to figure out which packet is responding to the original packet that the client sent.

Regards,

– Gato