use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.
the class MessageEventManager method sendDisplayedNotification.
/**
* Sends the notification that the message was displayed to the sender of the original message.
*
* @param to the recipient of the notification.
* @param packetID the id of the message to send.
* @throws NotConnectedException
* @throws InterruptedException
*/
public void sendDisplayedNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
// Create the message to send
Message msg = new Message(to);
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setDisplayed(true);
messageEvent.setStanzaId(packetID);
msg.addExtension(messageEvent);
// Send the packet
connection().sendStanza(msg);
}
use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.
the class RosterExchangeManager method send.
/**
* Sends a roster entry to userID.
*
* @param rosterEntry the roster entry to send
* @param targetUserID the user that will receive the roster entries
* @throws NotConnectedException
* @throws InterruptedException
*/
public void send(RosterEntry rosterEntry, Jid targetUserID) throws NotConnectedException, InterruptedException {
// Create a new message to send the roster
Message msg = new Message(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange();
rosterExchange.addRosterEntry(rosterEntry);
msg.addExtension(rosterExchange);
XMPPConnection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendStanza(msg);
}
use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.
the class MultiUserChatTest method testInvitationWithMessage.
public void testInvitationWithMessage() {
final String[] answer = new String[2];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User3 is listening to MUC invitations
MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
public void invitationReceived(XMPPConnection conn, String room, String inviter, String reason, String password, Message message) {
// Indicate that the invitation was received
answer[0] = reason;
XHTMLExtension extension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
assertNotNull("An extension was not found in the invitation", extension);
answer[1] = (String) extension.getBodies().next();
}
});
// User2 invites user3 to join to the room
Message msg = new Message();
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody("<body>Meet me in this excellent room</body>");
msg.addExtension(xhtmlExtension);
muc2.invite(msg, getFullJID(2), "Meet me in this excellent room");
Thread.sleep(350);
assertEquals("Invitation was not received", "Meet me in this excellent room", answer[0]);
assertEquals("Rejection was not received", "<body>Meet me in this excellent room</body>", answer[1]);
// User2 leaves the room
muc2.leave();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.
the class XHTMLExtensionTest method testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage.
/**
* Low level API test. Test a message with two XHTML bodies and several XHTML tags.
* 1. User_1 will send a message with XHTML to user_2
* 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
* is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final StanzaCollector chat2 = getConnection(1).createStanzaCollector(new ThreadFilter(chat1.getThreadID()));
// Create a Listener that listens for Messages with the extension
//"http://jabber.org/protocol/xhtml-im"
// This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter = new StanzaExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
PacketListener packetListener = new PacketListener() {
@Override
public void processStanza(Packet packet) {
}
};
getConnection(1).addAsyncPacketListener(packetListener, packetFilter);
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
// Create an XHTMLExtension and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody("<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridicula es el espantajo de mentes pequenas.</p></blockquote></body>");
xhtmlExtension.addBody("<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
bodiesSent = xhtmlExtension.getBodiesCount();
bodiesReceived = 0;
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
Packet packet = chat2.nextResult(2000);
int received = 0;
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
xhtmlExtension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
assertNotNull("Message without extension \"http://jabber.org/protocol/xhtml-im\"", xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator<String> it = xhtmlExtension.getBodies(); it.hasNext(); ) {
received++;
System.out.println(it.next());
}
bodiesReceived = received;
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is " + "misconfigured");
}
// Wait half second so that the complete test can run
assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
}
use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.
the class LastActivityManagerTest method testOnline.
/**
* This is a test to check if a LastActivity request for idle time is
* answered and correct.
*/
public void testOnline() {
TCPConnection conn0 = getConnection(0);
TCPConnection conn1 = getConnection(1);
// Send a message as the last activity action from connection 1 to
// connection 0
conn1.sendStanza(new Message(getBareJID(0)));
// Wait 1 seconds to have some idle time
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
fail("Thread sleep interrupted");
}
LastActivity lastActivity = null;
try {
lastActivity = LastActivityManager.getLastActivity(conn0, getFullJID(1));
} catch (XMPPException e) {
e.printStackTrace();
fail("An error occurred requesting the Last Activity");
}
// Asserts that the last activity packet was received
assertNotNull("No last activity packet", lastActivity);
// Asserts that there is at least a 1 second of idle time
assertTrue("The last activity idle time is less than expected: " + lastActivity.getIdleTime(), lastActivity.getIdleTime() >= 1);
}
Aggregations