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;
}
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;
}
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;
}
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;
}
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);
}
}
}
Aggregations