Search in sources :

Example 11 with NotConnectedException

use of org.jivesoftware.smack.SmackException.NotConnectedException 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 12 with NotConnectedException

use of org.jivesoftware.smack.SmackException.NotConnectedException in project opennms by OpenNMS.

the class XMPPNotificationManager method sendGroupChat.

/**
	 * send an xmpp message to a specified Chat Room.
	 *
	 * @param xmppChatRoom
	 *            room to send message to.
	 * @param xmppMessage
	 *            text to be sent in the body of the message
	 * @return true if message is sent, false otherwise
	 */
public boolean sendGroupChat(String xmppChatRoom, String xmppMessage) {
    MultiUserChat groupChat;
    if (rooms.containsKey(xmppChatRoom)) {
        groupChat = rooms.get(xmppChatRoom);
    } else {
        LOG.debug("Adding room: {}", xmppChatRoom);
        groupChat = new MultiUserChat(xmpp, xmppChatRoom);
        rooms.put(xmppChatRoom, groupChat);
    }
    if (!groupChat.isJoined()) {
        LOG.debug("Joining room: {}", xmppChatRoom);
        try {
            groupChat.join(xmppUser);
        } catch (XMPPException | NoResponseException | NotConnectedException e) {
            LOG.error("XMPP Exception joining chat room ", e);
            return false;
        }
    }
    try {
        groupChat.sendMessage(xmppMessage);
        LOG.debug("XMPP Manager sent message to: {}", xmppChatRoom);
    } catch (XMPPException | NotConnectedException e) {
        LOG.error("XMPP Exception sending message to Chat room", e);
        return false;
    }
    return true;
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 13 with NotConnectedException

use of org.jivesoftware.smack.SmackException.NotConnectedException 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 14 with NotConnectedException

use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.

the class PingManager method ping.

/**
     * Pings the given jid. This method will return false if an error occurs.  The exception 
     * to this, is a server ping, which will always return true if the server is reachable, 
     * event if there is an error on the ping itself (i.e. ping not supported).
     * <p>
     * Use {@link #isPingSupported(Jid)} to determine if XMPP Ping is supported 
     * by the entity.
     * 
     * @param jid The id of the entity the ping is being sent to
     * @param pingTimeout The time to wait for a reply in milliseconds
     * @return true if a reply was received from the entity, false otherwise.
     * @throws NoResponseException if there was no response from the jid.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public boolean ping(Jid jid, long pingTimeout) throws NotConnectedException, NoResponseException, InterruptedException {
    final XMPPConnection connection = connection();
    // otherwise the client JID will be null causing an NPE
    if (!connection.isAuthenticated()) {
        throw new NotConnectedException();
    }
    Ping ping = new Ping(jid);
    try {
        connection.createStanzaCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
    } catch (XMPPException exc) {
        return jid.equals(connection.getXMPPServiceDomain());
    }
    return true;
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Ping(org.jivesoftware.smackx.ping.packet.Ping) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 15 with NotConnectedException

use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.

the class EntityCapsManager method updateLocalEntityCaps.

/**
     * Updates the local user Entity Caps information with the data provided
     *
     * If we are connected and there was already a presence send, another
     * presence is send to inform others about your new Entity Caps node string.
     *
     */
public void updateLocalEntityCaps() {
    XMPPConnection connection = connection();
    DiscoverInfo discoverInfo = new DiscoverInfo();
    discoverInfo.setType(IQ.Type.result);
    sdm.addDiscoverInfoTo(discoverInfo);
    // getLocalNodeVer() will return a result only after currentCapsVersion is set. Therefore
    // set it first and then call getLocalNodeVer()
    currentCapsVersion = generateVerificationString(discoverInfo);
    final String localNodeVer = getLocalNodeVer();
    discoverInfo.setNode(localNodeVer);
    addDiscoverInfoByNode(localNodeVer, discoverInfo);
    if (lastLocalCapsVersions.size() > 10) {
        CapsVersionAndHash oldCapsVersion = lastLocalCapsVersions.poll();
        sdm.removeNodeInformationProvider(entityNode + '#' + oldCapsVersion.version);
    }
    lastLocalCapsVersions.add(currentCapsVersion);
    if (connection != null)
        JID_TO_NODEVER_CACHE.put(connection.getUser(), new NodeVerHash(entityNode, currentCapsVersion));
    final List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
    sdm.setNodeInformationProvider(localNodeVer, new AbstractNodeInformationProvider() {

        List<String> features = sdm.getFeatures();

        List<ExtensionElement> packetExtensions = sdm.getExtendedInfoAsList();

        @Override
        public List<String> getNodeFeatures() {
            return features;
        }

        @Override
        public List<Identity> getNodeIdentities() {
            return identities;
        }

        @Override
        public List<ExtensionElement> getNodePacketExtensions() {
            return packetExtensions;
        }
    });
    // to respect ConnectionConfiguration.isSendPresence()
    if (connection != null && connection.isAuthenticated() && presenceSend != null) {
        try {
            connection.sendStanza(presenceSend.cloneWithNewId());
        } catch (InterruptedException | NotConnectedException e) {
            LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e);
        }
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) AbstractNodeInformationProvider(org.jivesoftware.smackx.disco.AbstractNodeInformationProvider) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XMPPConnection(org.jivesoftware.smack.XMPPConnection) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)

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