use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MessageTest method testHighestShow.
/**
* User0 is connected from 2 resources. User0 is available in both resources
* but with different show values. User1 sends a message to the
* bare JID of User0. Check that the resource with highest show value will get
* the messages.
*
* @throws Exception if an error occurs.
*/
public void testHighestShow() throws Exception {
// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
conn3.connect();
conn3.login(getUsername(0), getPassword(0), "Home");
// Set this connection as highest priority
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.away);
conn3.sendStanza(presence);
// Set this connection as highest priority
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
getConnection(0).sendStanza(presence);
// Let the server process the change in presences
Thread.sleep(200);
// User0 listen in both connected clients
StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
StanzaCollector coll3 = conn3.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
// User1 sends a message to the bare JID of User0
Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
// Check that messages were sent to resource with highest priority
Message message = (Message) coll3.nextResult(2000);
assertNull("Resource with lowest show value got the message", message);
message = (Message) collector.nextResult(2000);
assertNotNull(message);
assertEquals("Test 1", message.getBody());
message = (Message) collector.nextResult(1000);
assertNotNull(message);
assertEquals("Test 2", message.getBody());
conn3.disconnect();
}
use of org.jivesoftware.smack.packet.Presence in project Openfire by igniterealtime.
the class SmackServiceNode method searchServices.
private static MappedNodes searchServices(final ConcurrentHashMap<String, String> visited, final XMPPConnection xmppConnection, final int maxEntries, final int maxDepth, final int maxSearchNodes, final String protocol, final boolean searchBuddies, final MappedNodes mappedNodes) {
if (xmppConnection == null || !xmppConnection.isConnected()) {
return null;
}
searchDiscoItems(xmppConnection, maxEntries, xmppConnection.getServiceName(), mappedNodes, maxDepth - 1, maxSearchNodes, protocol, visited);
// Request to Server
deepSearch(xmppConnection, maxEntries, xmppConnection.getHost(), mappedNodes, maxDepth - 1, maxSearchNodes, protocol, visited);
// Request to Buddies
if (xmppConnection.getRoster() != null && searchBuddies) {
for (final RosterEntry re : xmppConnection.getRoster().getEntries()) {
for (final Iterator<Presence> i = xmppConnection.getRoster().getPresences(re.getUser()); i.hasNext(); ) {
final Presence presence = i.next();
if (presence.isAvailable()) {
deepSearch(xmppConnection, maxEntries, presence.getFrom(), mappedNodes, maxDepth - 1, maxSearchNodes, protocol, visited);
}
}
}
}
return mappedNodes;
}
use of org.jivesoftware.smack.packet.Presence in project Openfire by igniterealtime.
the class XMPPSession method logIn.
/**
* @see net.sf.kraken.session.TransportSession#logIn(net.sf.kraken.type.PresenceType, String)
*/
@Override
public void logIn(PresenceType presenceType, String verboseStatus) {
final org.jivesoftware.smack.packet.Presence presence = new org.jivesoftware.smack.packet.Presence(org.jivesoftware.smack.packet.Presence.Type.available);
if (JiveGlobals.getBooleanProperty("plugin.gateway." + getTransport().getType() + ".avatars", true) && getAvatar() != null) {
Avatar avatar = getAvatar();
// Same thing in this case, so lets go ahead and set them.
avatar.setLegacyIdentifier(avatar.getXmppHash());
VCardUpdateExtension ext = new VCardUpdateExtension();
ext.setPhotoHash(avatar.getLegacyIdentifier());
presence.addExtension(ext);
}
final Presence.Mode pMode = ((XMPPTransport) getTransport()).convertGatewayStatusToXMPP(presenceType);
if (pMode != null) {
presence.setMode(pMode);
}
if (verboseStatus != null && verboseStatus.trim().length() > 0) {
presence.setStatus(verboseStatus);
}
setPendingPresenceAndStatus(presenceType, verboseStatus);
if (!this.isLoggedIn()) {
listener = new XMPPListener(this);
presenceHandler = new XMPPPresenceHandler(this);
runThread = new Thread() {
@Override
public void run() {
String userName = generateUsername(registration.getUsername());
conn = new XMPPConnection(config);
try {
conn.getSASLAuthentication().registerSASLMechanism("DIGEST-MD5", MySASLDigestMD5Mechanism.class);
if (getTransport().getType().equals(TransportType.facebook) && registration.getUsername().equals("{PLATFORM}")) {
conn.getSASLAuthentication().registerSASLMechanism("X-FACEBOOK-PLATFORM", FacebookConnectSASLMechanism.class);
conn.getSASLAuthentication().supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
}
Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
conn.connect();
conn.addConnectionListener(listener);
try {
conn.addPacketListener(presenceHandler, new PacketTypeFilter(org.jivesoftware.smack.packet.Presence.class));
// Use this to filter out anything we don't care about
conn.addPacketListener(listener, new OrFilter(new PacketTypeFilter(GoogleMailBoxPacket.class), new PacketExtensionFilter(GoogleNewMailExtension.ELEMENT_NAME, GoogleNewMailExtension.NAMESPACE)));
conn.login(userName, registration.getPassword(), xmppResource);
// send initial presence.
conn.sendPacket(presence);
conn.getChatManager().addChatListener(listener);
conn.getRoster().addRosterListener(listener);
if (JiveGlobals.getBooleanProperty("plugin.gateway." + getTransport().getType() + ".avatars", !TransportType.facebook.equals(getTransport().getType())) && getAvatar() != null) {
new Thread() {
@Override
public void run() {
Avatar avatar = getAvatar();
VCard vCard = new VCard();
try {
vCard.load(conn);
vCard.setAvatar(Base64.decode(avatar.getImageData()), avatar.getMimeType());
vCard.save(conn);
} catch (XMPPException e) {
Log.debug("XMPP: Error while updating vcard for avatar change.", e);
} catch (NotFoundException e) {
Log.debug("XMPP: Unable to find avatar while setting initial.", e);
}
}
}.start();
}
setLoginStatus(TransportLoginStatus.LOGGED_IN);
syncUsers();
if (getTransport().getType().equals(TransportType.gtalk) && JiveGlobals.getBooleanProperty("plugin.gateway.gtalk.mailnotifications", true)) {
conn.sendPacket(new IQWithPacketExtension(generateFullJID(getRegistration().getUsername()), new GoogleUserSettingExtension(null, true, null), IQ.Type.SET));
conn.sendPacket(new IQWithPacketExtension(generateFullJID(getRegistration().getUsername()), new GoogleMailNotifyExtension()));
mailCheck = new MailCheck();
timer.schedule(mailCheck, timerInterval, timerInterval);
}
} catch (XMPPException e) {
Log.debug(getTransport().getType() + " user's login/password does not appear to be correct: " + getRegistration().getUsername(), e);
setFailureStatus(ConnectionFailureReason.USERNAME_OR_PASSWORD_INCORRECT);
sessionDisconnectedNoReconnect(LocaleUtils.getLocalizedString("gateway.xmpp.passwordincorrect", "kraken"));
}
} catch (XMPPException e) {
Log.debug(getTransport().getType() + " user is not able to connect: " + getRegistration().getUsername(), e);
setFailureStatus(ConnectionFailureReason.CAN_NOT_CONNECT);
sessionDisconnected(LocaleUtils.getLocalizedString("gateway.xmpp.connectionfailed", "kraken"));
}
}
};
runThread.start();
}
}
use of org.jivesoftware.smack.packet.Presence in project Openfire by igniterealtime.
the class XMPPSession method constructCurrentLegacyPresencePacket.
/**
* Returns a (legacy/Smack-based) Presence stanza that represents the
* current presence of this session. The Presence includes relevant Mode,
* Status and VCardUpdate information.
*
* This method uses the fields {@link TransportSession#presence} and
* {@link TransportSession#verboseStatus} to generate the result.
*
* @return A Presence packet representing the current presence state of this
* session.
*/
public Presence constructCurrentLegacyPresencePacket() {
final org.jivesoftware.smack.packet.Presence presence = new org.jivesoftware.smack.packet.Presence(org.jivesoftware.smack.packet.Presence.Type.available);
final Presence.Mode pMode = ((XMPPTransport) getTransport()).convertGatewayStatusToXMPP(this.presence);
if (pMode != null) {
presence.setMode(pMode);
}
if (verboseStatus != null && verboseStatus.trim().length() > 0) {
presence.setStatus(verboseStatus);
}
final Avatar avatar = getAvatar();
if (avatar != null) {
final VCardUpdateExtension ext = new VCardUpdateExtension();
ext.setPhotoHash(avatar.getLegacyIdentifier());
presence.addExtension(ext);
}
return presence;
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class OfflineMessageManagerTest method testFetchAndPurge.
/**
* While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
* performs some "Flexible Offline Message Retrieval" by fetching all the offline messages
* and then removing all the offline messages.
*/
public void testFetchAndPurge() {
// Make user2 unavailable
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 checks the number of offline messages
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// Get all offline messages
Iterator<Message> messages = offlineManager.getMessages();
assertTrue("No message was found", messages.hasNext());
List<String> stamps = new ArrayList<String>();
while (messages.hasNext()) {
Message message = messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
assertNotNull("No offline information was included in the offline message", info);
assertNotNull("No stamp was found in the message header", info.getNode());
stamps.add(info.getNode());
}
assertEquals("Wrong number of messages", 2, stamps.size());
// Check that the offline messages have not been deleted
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// User2 becomes available again
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user
Message message = (Message) collector.nextResult(2500);
assertNull("An offline message was sent from the server", message);
// Delete all offline messages
offlineManager.deleteMessages();
// Check that there are no offline message for this user
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations