resp.sendError(404);
return;
}
String jidStr = req.getParameter("jid");
JID jid = null;
if (jidStr == null || jidStr.equals("")) {
jidStr = req.getParameter("jid_other");
}
if (jidStr != null) {
jidStr = jidStr.trim();
jid = new JID(jidStr);
}
String command = req.getParameter("command");
String adminMessage;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
if (jid != null && command.equals("chat")) {
Message message = new MessageBuilder()
.withMessageType(MessageType.CHAT)
.withRecipientJids(jid)
.withBody(req.getParameter("chat_message"))
.build();
SendResponse sendResponse = xmpp.sendMessage(message);
if (sendResponse.getStatusMap().get(jid) == SendResponse.Status.SUCCESS) {
adminMessage = "Chat message sent to JID " + jidStr + ".";
} else if (sendResponse.getStatusMap().get(jid) == SendResponse.Status.INVALID_ID) {
adminMessage = "Message not sent: invalid JID " + jidStr + ".";
} else {
adminMessage = "Message not sent to " + jidStr + ": internal service error.";
}
} else if (jid != null && command.equals("invite")) {
xmpp.sendInvitation(jid);
adminMessage = "Chat message sent to JID " + jidStr + ".";
} else if (jid != null && command.equals("probe")) {
xmpp.sendPresence(jid, PresenceType.PROBE, null, null);
adminMessage = "A presence probe has been sent to JID " + jidStr + ".";
} else if (command.equals("presence")) {
// Store the app's presence.
Entity statusEntity = MainPageServlet.getStatusEntity();
if (req.getParameter("presence_available").equals("true")) {
statusEntity.setProperty("presence_available", true);
} else if (req.getParameter("presence_available").equals("false")) {
statusEntity.setProperty("presence_available", false);
}
if (req.getParameter("presence_show").equals("chat") ||
req.getParameter("presence_show").equals("away") ||
req.getParameter("presence_show").equals("dnd") ||
req.getParameter("presence_show").equals("xa")) {
statusEntity.setProperty("presence_show",
req.getParameter("presence_show"));
}
statusEntity.setProperty("status_message",
req.getParameter("status_message"));
datastore.put(statusEntity);
// Send presence messages to all subscribed users. As
// written, this could be slow or broken for a large number
// of users. A more robust solution would use a task queue
// and a query cursor. (Unlike sendMessage(),
// sendPresence() only accepts one JID at a time.)
Query q = new Query("ChatUser").addFilter("is_subscribed",
Query.FilterOperator.EQUAL,
true);
PreparedQuery pq = datastore.prepare(q);
for (Entity e : pq.asIterable()) {
String recipJidStr = (String)(e.getProperty("jid"));
JID recipJid = new JID(recipJidStr);
xmpp.sendPresence(recipJid,
((Boolean)statusEntity.getProperty("presence_available")) ?
PresenceType.AVAILABLE : PresenceType.UNAVAILABLE,
PresenceShow.valueOf(((String)statusEntity.getProperty("presence_show")).toUpperCase()),
(String)statusEntity.getProperty("status_message"));