Sending XMPP messages from Google App Engine with html

Since this took me a while to figure out I’d like to share with you. I wanted to extend Feederator to send incoming news items to the users XMPP client. What worked pretty fast was sending plain text messages:

XMPPService xmpp = XMPPServiceFactory.getXMPPService();
JID jid = new JID(xmppAddress);
Message msg = new MessageBuilder()
    .withRecipientJids(jid)
    .withMessageType(MessageType.CHAT)
    .withBody(“New post arrived”)
    .build();
if (xmpp.getPresence(jid).isAvailable()) {
    SendResponse status = xmpp.sendMessage(msg);
    messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
    if (messageSent) {
        Log.info(“message sent”);
    }
}

But to send parts of the message bold and maybe even displaying pictures was trickier. But I finally figured it out. Change the part with the message:
String message = “<body>Plain text message</body>”
   + “<html xmlns=’http://jabber.org/protocol/xhtml-im’>”
   + “<body xmlns=’http://www.w3.org/1999/xhtml’>”
   + “<B>bold text</B> and normal text”
   + “</body>”
   + “</html>”;
Message msg = new MessageBuilder()
   .withRecipientJids(jid)
   .withMessageType(MessageType.CHAT)
   .withBody(message)
   .asXml(true);
   .build();
By adding the .asXml(true) parameter you tell the XMPP service that you take care yourself of the content of the message. Pay attention to the fact that you always should send a normal elment for the plain text. Not all XMPP/ Jabber clients unterstand the xhtml-im extension. The specification (http://xmpp.org/extensions/xep-0071.html) says that the plain text should always tell the same as the element with markup and that clients are free to ignore the xhtml-im tag. Also note that clients are free to display the text defined by the alt attribute of a <img> tag and not the picture.

Now a beer! Did you like this post? It often takes me several hours of my free time to write one. If I was your neighbour would you offer me a beer for the hard work I did for you? The beauty is: beers can be teletransported with a painless donation using Paypal. A beer in a Swiss bar costs about USD $4.80. Or use this affiliate link if you order something on Banggood and I'll get a small kickback. Thank you!
Related:  Google App Engine (Java) ChannelService with AngularJS