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