Search in sources :

Example 6 with ChatManager

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);
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ChatManager(org.jivesoftware.smack.ChatManager)

Aggregations

ChatManager (org.jivesoftware.smack.ChatManager)6 Chat (org.jivesoftware.smack.Chat)5 XMPPException (org.jivesoftware.smack.XMPPException)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 Message (org.jivesoftware.smack.packet.Message)4 AxisMessage (org.apache.axis2.description.AxisMessage)2 XMPPOutTransportInfo (org.apache.axis2.transport.xmpp.util.XMPPOutTransportInfo)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 IOException (java.io.IOException)1 OMElement (org.apache.axiom.om.OMElement)1 SOAP12Version (org.apache.axiom.soap.SOAP12Version)1 SOAPVersion (org.apache.axiom.soap.SOAPVersion)1 Options (org.apache.axis2.client.Options)1 AxisOperation (org.apache.axis2.description.AxisOperation)1 XMPPConnectionFactory (org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory)1 RuntimeExchangeException (org.apache.camel.RuntimeExchangeException)1 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)1 SmackException (org.jivesoftware.smack.SmackException)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)1