use of org.jivesoftware.smack.filter.MessageTypeFilter in project camel by apache.
the class XmppConsumer method doStart.
@Override
protected void doStart() throws Exception {
try {
connection = endpoint.createConnection();
} catch (SmackException e) {
if (endpoint.isTestConnectionOnStartup()) {
throw new RuntimeException("Could not connect to XMPP server.", e);
} else {
LOG.warn(e.getMessage());
if (getExceptionHandler() != null) {
getExceptionHandler().handleException(e.getMessage(), e);
}
scheduleDelayedStart();
return;
}
}
chatManager = ChatManager.getInstanceFor(connection);
chatManager.addChatListener(this);
OrFilter pubsubPacketFilter = new OrFilter();
if (endpoint.isPubsub()) {
//xep-0060: pubsub#notification_type can be 'headline' or 'normal'
pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.headline));
pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.normal));
connection.addPacketListener(this, pubsubPacketFilter);
}
if (endpoint.getRoom() == null) {
privateChat = chatManager.getThreadChat(endpoint.getChatId());
if (privateChat != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding listener to existing chat opened to " + privateChat.getParticipant());
}
privateChat.addMessageListener(this);
} else {
privateChat = ChatManager.getInstanceFor(connection).createChat(endpoint.getParticipant(), endpoint.getChatId(), this);
if (LOG.isDebugEnabled()) {
LOG.debug("Opening private chat to " + privateChat.getParticipant());
}
}
} else {
// add the presence packet listener to the connection so we only get packets that concerns us
// we must add the listener before creating the muc
final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class));
connection.addPacketListener(this, packetFilter);
muc = new MultiUserChat(connection, endpoint.resolveRoom(connection));
muc.addMessageListener(this);
DiscussionHistory history = new DiscussionHistory();
// we do not want any historical messages
history.setMaxChars(0);
muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getDefaultPacketReplyTimeout());
if (LOG.isInfoEnabled()) {
LOG.info("Joined room: {} as: {}", muc.getRoom(), endpoint.getNickname());
}
}
this.startRobustConnectionMonitor();
super.doStart();
}
use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.
the class MessageTest method testHighestPriority.
/**
* User0 is connected from 2 resources. User0 is available in both resources
* but with different priority presence values. User1 sends a message to the
* bare JID of User0. Check that the resource with highest priority will get
* the messages.
*
* @throws Exception if an error occurs.
*/
public void testHighestPriority() 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.setPriority(10);
conn3.sendStanza(presence);
// Set this connection as highest priority
presence = new Presence(Presence.Type.available);
presence.setPriority(5);
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) collector.nextResult(2000);
assertNull("Resource with lowest priority got the message", message);
message = (Message) coll3.nextResult(2000);
assertNotNull(message);
assertEquals("Test 1", message.getBody());
message = (Message) coll3.nextResult(1000);
assertNotNull(message);
assertEquals("Test 2", message.getBody());
conn3.disconnect();
}
use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.
the class MessageTest method testOfflineMessage.
/**
* Check that when a client becomes unavailable all messages sent to the client are stored offline. So that when
* the client becomes available again the offline messages are received.
*/
public void testOfflineMessage() {
getConnection(0).sendStanza(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// 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);
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 becomes available again
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available
Message message = (Message) collector.nextResult(2500);
assertNotNull(message);
message = (Message) collector.nextResult(2000);
assertNotNull(message);
message = (Message) collector.nextResult(1000);
assertNull(message);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.
the class MessageTest method testMostRecentActive.
/**
* User0 is connected from 2 resources. User0 is available in both resources
* with same priority presence values and same show values. User1 sends a message to the
* bare JID of User0. Check that the resource with most recent activity will get
* the messages.
*
* @throws Exception if an error occurs.
*/
public void testMostRecentActive() 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.available);
presence.setPriority(10);
conn3.sendStanza(presence);
// Set this connection as highest priority
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(10);
getConnection(0).sendStanza(presence);
connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
conn4.connect();
conn4.login(getUsername(0), getPassword(0), "Home2");
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(4);
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));
StanzaCollector coll4 = conn4.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
// Send a message from this resource to indicate most recent activity
conn3.sendStanza(new Message("admin@" + getXMPPServiceDomain()));
// 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) collector.nextResult(2000);
assertNull("Resource with oldest activity got the message", message);
message = (Message) coll4.nextResult(2000);
assertNull(message);
message = (Message) coll3.nextResult(2000);
assertNotNull(message);
assertEquals("Test 1", message.getBody());
message = (Message) coll3.nextResult(1000);
assertNotNull(message);
assertEquals("Test 2", message.getBody());
conn3.disconnect();
conn4.disconnect();
}
use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.
the class MessageTest method testHugeMessage.
/**
* Send messages with invalid XML characters to offline users. Check that offline users
* are receiving them from the server.<p>
*
* Test case commented out since some servers may just close the connection while others
* are more tolerant and accept stanzas with invalid XML characters.
*/
/*public void testOfflineMessageInvalidXML() {
// 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);
StanzaCollector collector = getConnection(1).createStanzaCollector(
new MessageTypeFilter(Message.Type.chat));
chat.sendMessage("Test \f 1");
chat.sendMessage("Test \r 1");
Thread.sleep(500);
// User2 becomes available again
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available
Message message = (Message) collector.nextResult(2500);
assertNotNull(message);
message = (Message) collector.nextResult(2000);
assertNotNull(message);
message = (Message) collector.nextResult(1000);
assertNull(message);
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}*/
/**
* Check that two clients are able to send messages with a body of 4K characters and their
* connections are not being closed.
*/
public void testHugeMessage() {
getConnection(0).sendStanza(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// User2 becomes available again
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
// Create message with a body of 4K characters
Message msg = new Message(getFullJID(1), Message.Type.chat);
StringBuilder sb = new StringBuilder(5000);
for (int i = 0; i <= 4000; i++) {
sb.append('X');
}
msg.setBody(sb.toString());
// Send the first message
getConnection(0).sendStanza(msg);
// Check that the connection that sent the message is still connected
assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
// Check that the message was received
Message rcv = (Message) collector.nextResult(1000);
assertNotNull("No Message was received", rcv);
// Send the second message
getConnection(0).sendStanza(msg);
// Check that the connection that sent the message is still connected
assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
// Check that the second message was received
rcv = (Message) collector.nextResult(1000);
assertNotNull("No Message was received", rcv);
}
Aggregations