use of org.jivesoftware.smack.ChatManager in project jbpm-work-items by kiegroup.
the class JabberWorkItemHandler method executeWorkItem.
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
this.user = (String) workItem.getParameter("User");
this.password = (String) workItem.getParameter("Password");
this.server = (String) workItem.getParameter("Server");
String portString = (String) workItem.getParameter("Port");
if (portString != null && !portString.equals("")) {
this.port = Integer.valueOf((String) workItem.getParameter("Port"));
}
this.service = (String) workItem.getParameter("Service");
this.text = (String) workItem.getParameter("Text");
String to = (String) workItem.getParameter("To");
if (to == null || to.trim().length() == 0) {
throw new RuntimeException("IM must have one or more to adresses");
}
for (String s : to.split(";")) {
if (s != null && !"".equals(s)) {
this.toUsers.add(s);
}
}
if (conf == null) {
conf = new ConnectionConfiguration(server, port, service);
}
try {
if (server != null && !server.equals("") && port != 0) {
if (connection == null) {
connection = new XMPPConnection(conf);
}
} else {
if (connection == null) {
connection = new XMPPConnection(service);
}
}
connection.connect();
logger.info("Connected to {}", connection.getHost());
connection.login(user, password);
logger.info("Logged in as {}", connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
for (String toUser : toUsers) {
ChatManager chatmanager = connection.getChatManager();
Chat chat = chatmanager.createChat(toUser, null);
// google bounces back the default message types, you must use chat
Message msg = new Message(toUser, Message.Type.chat);
msg.setBody(text);
chat.sendMessage(msg);
logger.info("Message Sent {}", msg);
}
connection.disconnect();
manager.completeWorkItem(workItem.getId(), null);
} catch (Exception e) {
handleException(e);
}
}
Aggregations