You can use a packet extension for this: unfortunately there is no good documentation or examples for using packet extensions. I've previously looked at this unresolved question which has example code but I was unable to get it working: I got no exceptions but it simply didn't work as my extension wasn't called and I moved on to just encoding my data in the body of a Message.
EDIT: for posterity, I managed to get the following code working. It uses the DOM4J classes DocumentHelper
and Element
.
Presence np, packet = new Presence();
packet.setID(sessionManager.nextStreamID().toString());
packet.setFrom(server.createJID(operator, null));
if(!available) packet.setType(Presence.Type.unavailable);
else packet.setType(null);
// add the custom XML
Element xml = DocumentHelper.createElement(QName.get("custom", "http://www.custom.com/xmpp"));
xml.addAttribute("type", "presenceupdate");
packet.addExtension(new PacketExtension(xml));
Mildly humorous: I ran into my own answer a year later while actually trying to solve this problem for a real project (as opposed to tinkering like I did before) and since I couldn't just abandon it I had to figure it out. I figure I'll need this answer again so here it is. SO: my memory in the sky.
EDIT: found an even simpler way of doing this:
Element xml = packet.addChildElement("custom", "http://www.custom.com/xmpp");
xml.addAttribute("type", "presenceupdate");
Thing to note: trying to add certain things (in my case, trying to add a delay element) resulted in the packet not being routed. Something in Openfire swallowed it, so this is something to watch for.