*
* @param msgAttr a map of attributes as described
* @return an intance of SendResponse
*/
public static SendResponse send(XMPPService xmppService, @SuppressWarnings("rawtypes") Map msgAttr) {
MessageBuilder msgBuilder = new MessageBuilder();
if (msgAttr.containsKey(XML_BODY_ATTR) && msgAttr.containsKey(TEXT_BODY_ATTR)) {
throw new RuntimeException("You have to choose between XML and text bodies, you can't have both!");
}
// sets the body of the message
if (msgAttr.containsKey(XML_BODY_ATTR)) {
msgBuilder.asXml(true);
Object xml = new StreamingMarkupBuilder().bind(msgAttr.get(XML_BODY_ATTR));
msgBuilder.withBody(String.valueOf(xml));
} else if (msgAttr.containsKey(TEXT_BODY_ATTR)) {
msgBuilder.withBody(String.valueOf(msgAttr.get(TEXT_BODY_ATTR)));
}
// sets the recepients of the message
if (msgAttr.containsKey(TO_ATTR)) {
Object to = msgAttr.get(TO_ATTR);
if (to instanceof String) {
msgBuilder.withRecipientJids(new JID((String) to));
} else if (to instanceof List<?>) {
List<?> toList = (List<?>)to;
JID[] jids = new JID[toList.size()];
for (int i = 0; i < toList.size(); i++) {
jids[i] = new JID(String.valueOf(toList.get(i)));
}
msgBuilder.withRecipientJids(jids);
}
}
// sets the sender of the message
if (msgAttr.containsKey(FROM_ATTR)) {
msgBuilder.withFromJid(new JID(String.valueOf(msgAttr.get(FROM_ATTR))));
}
// sets the type of the message
if (msgAttr.containsKey(TYPE_ATTR)) {
Object type = msgAttr.get(TYPE_ATTR);
if (type instanceof MessageType) {
msgBuilder.withMessageType((MessageType) type);
} else if (type instanceof String) {
msgBuilder.withMessageType(MessageType.valueOf((String) type));
}
}
return xmppService.sendMessage(msgBuilder.build());
}