Search in sources :

Example 1 with ChatManager

use of org.jivesoftware.smack.ChatManager in project openhab1-addons by openhab.

the class XMPP method sendXMPP.

// provide public static methods here
/**
     * Sends a message to an XMPP user.
     * 
     * @param to the XMPP address to send the message to
     * @param message the message to send
     * 
     * @return <code>true</code>, if sending the message has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends a message to an XMPP user.")
public static boolean sendXMPP(@ParamDoc(name = "to") String to, @ParamDoc(name = "message") String message) {
    boolean success = false;
    try {
        XMPPConnection conn = XMPPConnect.getConnection();
        ChatManager chatmanager = ChatManager.getInstanceFor(conn);
        Chat newChat = chatmanager.createChat(to, null);
        try {
            while (message.length() >= 2000) {
                newChat.sendMessage(message.substring(0, 2000));
                message = message.substring(2000);
            }
            newChat.sendMessage(message);
            logger.debug("Sent message '{}' to '{}'.", message, to);
            success = true;
        } catch (XMPPException e) {
            logger.warn("Error Delivering block", e);
        } catch (NotConnectedException e) {
            logger.warn("Error Delivering block", e);
        }
    } catch (NotInitializedException e) {
        logger.warn("Could not send XMPP message as connection is not correctly initialized!");
    }
    return success;
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) Chat(org.jivesoftware.smack.Chat) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.smack.ChatManager) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 2 with ChatManager

use of org.jivesoftware.smack.ChatManager in project camel by apache.

the class XmppPrivateChatProducer method process.

public void process(Exchange exchange) {
    // make sure we are connected
    try {
        if (connection == null) {
            connection = endpoint.createConnection();
        }
        if (!connection.isConnected()) {
            this.reconnect();
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not connect to XMPP server.", e);
    }
    String participant = endpoint.getParticipant();
    String thread = endpoint.getChatId();
    if (participant == null) {
        participant = getParticipant();
    } else {
        thread = "Chat:" + participant + ":" + endpoint.getUser();
    }
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    Chat chat = getOrCreateChat(chatManager, participant, thread);
    Message message = null;
    try {
        message = new Message();
        message.setTo(participant);
        message.setThread(thread);
        message.setType(Message.Type.normal);
        endpoint.getBinding().populateXmppMessage(message, exchange);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending XMPP message to {} from {} : {}", new Object[] { participant, endpoint.getUser(), message.getBody() });
        }
        chat.sendMessage(message);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Could not send XMPP message to " + participant + " from " + endpoint.getUser() + " : " + message + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) Chat(org.jivesoftware.smack.Chat) ChatManager(org.jivesoftware.smack.ChatManager) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 3 with ChatManager

use of org.jivesoftware.smack.ChatManager in project opennms by OpenNMS.

the class XMPPNotificationManager method sendMessage.

/**
 * <p>sendMessage</p>
 *
 * @param xmppTo a {@link java.lang.String} object.
 * @param xmppMessage a {@link java.lang.String} object.
 * @return a boolean.
 */
public boolean sendMessage(String xmppTo, String xmppMessage) {
    if (!isLoggedIn()) {
        connectToServer();
    }
    try {
        ChatManager cm = ChatManager.getInstanceFor(xmpp);
        cm.createChat(xmppTo, new NullMessageListener()).sendMessage(xmppMessage);
        LOG.debug("XMPP Manager sent message to: {}", xmppTo);
    } catch (XMPPException | NotConnectedException e) {
        LOG.error("XMPP Exception Sending message ", e);
        return false;
    }
    return true;
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.smack.ChatManager)

Example 4 with ChatManager

use of org.jivesoftware.smack.ChatManager in project wso2-axis2-transports by wso2.

the class XMPPSender method sendChatMessage.

/**
 * Replies to IM clients via a chat message. The reply contains the invocation response as a string.
 * @param msgCtx
 * @param responseMsg
 * @throws AxisFault
 */
private static void sendChatMessage(MessageContext msgCtx, String responseMsg) throws AxisFault {
    XMPPConnection xmppConnection = null;
    XMPPOutTransportInfo xmppOutTransportInfo = null;
    Message message = new Message();
    xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
    if (xmppOutTransportInfo != null) {
        message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
        xmppConnection = xmppOutTransportInfo.getConnectionFactory().getXmppConnection();
        if (xmppConnection == null) {
            handleException("Connection to XMPP Server is not established.");
        }
    } else {
        handleException("Could not find message sender details.");
    }
    // initialize the chat manager using connection
    ChatManager chatManager = xmppConnection.getChatManager();
    Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);
    try {
        message.setProperty(XMPPConstants.SEQUENCE_ID, xmppOutTransportInfo.getSequenceID());
        message.setBody(responseMsg);
        chat.sendMessage(message);
        log.debug("Sent message :" + message.toXML());
    } catch (XMPPException e) {
        XMPPSender.handleException("Error occurred while sending the message : " + message.toXML(), e);
    }
}
Also used : XMPPOutTransportInfo(org.apache.axis2.transport.xmpp.util.XMPPOutTransportInfo) AxisMessage(org.apache.axis2.description.AxisMessage) Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.smack.ChatManager)

Example 5 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