use of org.jivesoftware.smack.XMPPConnection in project xabber-android by redsolution.
the class MUCManager method requestRoomInfo.
public static void requestRoomInfo(final String account, final String roomJid, final RoomInfoListener listener) {
final XMPPConnection xmppConnection = AccountManager.getInstance().getAccount(account).getConnectionThread().getXMPPConnection();
final Thread thread = new Thread("Get room " + roomJid + " info for account " + account) {
@Override
public void run() {
RoomInfo roomInfo = null;
try {
LogManager.i(MUCManager.class, "Requesting room info " + roomJid);
roomInfo = MultiUserChatManager.getInstanceFor(xmppConnection).getRoomInfo(roomJid);
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
e.printStackTrace();
}
final RoomInfo finalRoomInfo = roomInfo;
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onRoomInfoReceived(finalRoomInfo);
}
});
}
};
thread.start();
}
use of org.jivesoftware.smack.XMPPConnection in project xabber-android by redsolution.
the class AttentionManager method onSettingsChanged.
public void onSettingsChanged() {
synchronized (enabledLock) {
for (String account : AccountManager.getInstance().getAccounts()) {
ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
if (connectionThread == null)
continue;
XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
if (xmppConnection == null)
continue;
ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(xmppConnection);
if (manager == null)
continue;
boolean contains = false;
for (String feature : manager.getFeatures()) {
if (Attention.NAMESPACE.equals(feature)) {
contains = true;
}
}
if (SettingsManager.chatsAttention() == contains)
continue;
if (SettingsManager.chatsAttention())
manager.addFeature(Attention.NAMESPACE);
else
manager.removeFeature(Attention.NAMESPACE);
}
AccountManager.getInstance().resendPresence();
}
}
use of org.jivesoftware.smack.XMPPConnection in project camel by apache.
the class XmppEndpoint method createConnection.
public synchronized XMPPConnection createConnection() throws XMPPException, SmackException, IOException {
if (connection != null && connection.isConnected()) {
// use existing working connection
return connection;
}
// prepare for creating new connection
connection = null;
LOG.trace("Creating new connection ...");
XMPPConnection newConnection = createConnectionInternal();
newConnection.connect();
newConnection.addPacketListener(new XmppLogger("INBOUND"), new PacketFilter() {
public boolean accept(Packet packet) {
return true;
}
});
newConnection.addPacketSendingListener(new XmppLogger("OUTBOUND"), new PacketFilter() {
public boolean accept(Packet packet) {
return true;
}
});
if (!newConnection.isAuthenticated()) {
if (user != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Logging in to XMPP as user: {} on connection: {}", user, getConnectionMessage(newConnection));
}
if (password == null) {
LOG.warn("No password configured for user: {} on connection: {}", user, getConnectionMessage(newConnection));
}
if (createAccount) {
AccountManager accountManager = AccountManager.getInstance(newConnection);
accountManager.createAccount(user, password);
}
if (login) {
if (resource != null) {
newConnection.login(user, password, resource);
} else {
newConnection.login(user, password);
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Logging in anonymously to XMPP on connection: {}", getConnectionMessage(newConnection));
}
newConnection.loginAnonymously();
}
// presence is not needed to be sent after login
}
// okay new connection was created successfully so assign it as the connection
LOG.debug("Created new connection successfully: {}", newConnection);
connection = newConnection;
return connection;
}
use of org.jivesoftware.smack.XMPPConnection 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.XMPPConnection in project perun by CESNET.
the class PerunNotifJabberSender method send.
@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
Set<Integer> usedPools = new HashSet<Integer>();
try {
ConnectionConfiguration config = new ConnectionConfiguration(jabberServer, port, serviceName);
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.login(username, password);
for (PerunNotifMessageDto messageDto : dtosToSend) {
PerunNotifReceiver receiver = messageDto.getReceiver();
PoolMessage dto = messageDto.getPoolMessage();
Message message = new Message();
message.setSubject(messageDto.getSubject());
message.setBody(messageDto.getMessageToSend());
message.setType(Message.Type.headline);
String myReceiverId = dto.getKeyAttributes().get(receiver.getTarget());
if (myReceiverId == null || myReceiverId.isEmpty()) {
//Can be set one static account
message.setTo(receiver.getTarget());
} else {
//We try to resolve id
Integer id = null;
try {
id = Integer.valueOf(myReceiverId);
} catch (NumberFormatException ex) {
logger.error("Cannot resolve id: {}, error: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
}
if (id != null) {
try {
User user = perun.getUsersManagerBl().getUserById(session, id);
Attribute emailAttribute = perun.getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:jabber");
if (emailAttribute != null && StringUtils.hasText(emailAttribute.toString())) {
message.setTo((String) emailAttribute.getValue());
}
} catch (UserNotExistsException ex) {
logger.error("Cannot found user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
} catch (AttributeNotExistsException ex) {
logger.warn("Cannot found email for user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
} catch (Exception ex) {
logger.error("Error during user email recognition, ex: {}", ex.getMessage());
logger.debug("ST:", ex);
}
}
}
connection.sendPacket(message);
usedPools.addAll(messageDto.getUsedPoolIds());
}
connection.disconnect();
} catch (XMPPException ex) {
logger.error("Error during jabber establish connection.", ex);
}
return null;
}
Aggregations