Search in sources :

Example 21 with NotConnectedException

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

the class XMPP method chatXMPP.

/**
     * Sends a message to an XMPP multi user chat.
     * 
     * @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 multi user chat.")
public static boolean chatXMPP(@ParamDoc(name = "message") String message) {
    boolean success = false;
    try {
        MultiUserChat chat = XMPPConnect.getChat();
        try {
            while (message.length() >= 2000) {
                chat.sendMessage(message.substring(0, 2000));
                message = message.substring(2000);
            }
            chat.sendMessage(message);
            logger.debug("Sent message '{}' to multi user chat.", message);
            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 : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPException(org.jivesoftware.smack.XMPPException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 22 with NotConnectedException

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

the class XMPPConnect method establishConnection.

private static void establishConnection() {
    if (servername == null) {
        return;
    }
    ConnectionConfiguration config;
    // Create a connection to the jabber server on the given port
    if (proxy != null) {
        config = new ConnectionConfiguration(servername, port, proxy);
    } else {
        config = new ConnectionConfiguration(servername, port);
    }
    config.setSecurityMode(securityMode);
    if (tlsPin != null) {
        try {
            SSLContext sc = JavaPinning.forPin(tlsPin);
            config.setCustomSSLContext(sc);
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            logger.error("Could not create TLS Pin for XMPP connection", e);
        }
    }
    if (connection != null && connection.isConnected()) {
        try {
            connection.disconnect();
        } catch (NotConnectedException e) {
            logger.debug("Already disconnected", e);
        }
    }
    connection = new XMPPTCPConnection(config);
    try {
        connection.connect();
        connection.login(username, password, null);
        if (consoleUsers.length > 0) {
            ChatManager.getInstanceFor(connection).addChatListener(new XMPPConsole(consoleUsers));
            connection.addConnectionListener(new XMPPConnectionListener());
        }
        logger.info("Connection to XMPP as '{}' has been established. Is secure/encrypted: {}", connection.getUser(), connection.isSecureConnection());
        initialized = true;
    } catch (Exception e) {
        logger.error("Could not establish connection to XMPP server '" + servername + ":" + port + "': {}", e.getMessage());
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) KeyManagementException(java.security.KeyManagementException) ConfigurationException(org.osgi.service.cm.ConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)22 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)11 XMPPException (org.jivesoftware.smack.XMPPException)11 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)9 SmackException (org.jivesoftware.smack.SmackException)6 IOException (java.io.IOException)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)3 KeyManagementException (java.security.KeyManagementException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 LinkedList (java.util.LinkedList)2 ChatManager (org.jivesoftware.smack.ChatManager)2 AlreadyConnectedException (org.jivesoftware.smack.SmackException.AlreadyConnectedException)2 AlreadyLoggedInException (org.jivesoftware.smack.SmackException.AlreadyLoggedInException)2 StreamErrorException (org.jivesoftware.smack.XMPPException.StreamErrorException)2 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)2 NotAMucServiceException (org.jivesoftware.smackx.muc.MultiUserChatException.NotAMucServiceException)2 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)2 InetAddress (java.net.InetAddress)1