Search in sources :

Example 6 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class InBandBytestreamTest method testRespondWithErrorOnInBandBytestreamRequest.

/**
     * Target should respond with not-acceptable error if no listeners for incoming In-Band
     * Bytestream requests are registered.
     * 
     * @throws XMPPException should not happen
     */
public void testRespondWithErrorOnInBandBytestreamRequest() throws XMPPException {
    XMPPConnection targetConnection = getConnection(0);
    XMPPConnection initiatorConnection = getConnection(1);
    Open open = new Open("sessionID", 1024);
    open.setFrom(initiatorConnection.getUser());
    open.setTo(targetConnection.getUser());
    StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(open.getStanzaId()));
    initiatorConnection.sendStanza(open);
    Packet result = collector.nextResult();
    assertNotNull(result.getError());
    assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) Open(org.jivesoftware.smackx.bytestreams.ibb.packet.Open)

Example 7 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testRespondWithErrorOnSocks5BytestreamRequest.

/**
     * Target should respond with not-acceptable error if no listeners for incoming Socks5
     * bytestream requests are registered.
     * 
     * @throws XMPPException should not happen
     */
public void testRespondWithErrorOnSocks5BytestreamRequest() throws XMPPException {
    XMPPConnection targetConnection = getConnection(0);
    XMPPConnection initiatorConnection = getConnection(1);
    Bytestream bytestreamInitiation = Socks5PacketUtils.createBytestreamInitiation(initiatorConnection.getUser(), targetConnection.getUser(), "session_id");
    bytestreamInitiation.addStreamHost("proxy.localhost", "127.0.0.1", 7777);
    StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(bytestreamInitiation.getStanzaId()));
    initiatorConnection.sendStanza(bytestreamInitiation);
    Packet result = collector.nextResult();
    assertNotNull(result.getError());
    assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 8 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector 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);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) StanzaExtensionFilter(org.jivesoftware.smack.filter.StanzaExtensionFilter) PacketListener(org.jivesoftware.smack.PacketListener) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) Chat(org.jivesoftware.smack.Chat) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 9 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class MultipleRecipientManagerTest method testReplying.

/**
     * Ensures that replying to packets is ok.
     */
public void testReplying() throws XMPPException {
    StanzaCollector collector0 = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    // Send the intial message with multiple recipients
    Message message = new Message();
    message.setBody("Hola");
    List<String> to = Arrays.asList(new String[] { getBareJID(1) });
    List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
    List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
    MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
    // Get the message and ensure it's ok
    Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the message", message1);
    MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
    assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
    assertFalse("Message 1 should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Prepare and send the reply
    Message reply1 = new Message();
    reply1.setBody("This is my reply");
    MultipleRecipientManager.reply(getConnection(1), message1, reply1);
    // Get the reply and ensure it's ok
    reply1 = (Message) collector0.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 0 never received the reply", reply1);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply1);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Send a reply to the reply
    Message reply2 = new Message();
    reply2.setBody("This is my reply to your reply");
    reply2.setFrom(getBareJID(0));
    MultipleRecipientManager.reply(getConnection(0), reply1, reply2);
    // Get the reply and ensure it's ok
    reply2 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the reply", reply2);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply2);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Check that connection2 recevied 3 messages
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 2 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 3 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 4 messages", message1);
    // Check that connection3 recevied only 1 message (was BCC in the first message)
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 2 messages", message1);
    collector0.cancel();
    collector1.cancel();
    collector2.cancel();
    collector3.cancel();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 10 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class OfflineMessageManagerTest method testReadAndDelete.

/**
     * While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
     * performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
     * retriving the headers, then the real messages of the headers and finally removing the
     * loaded messages.
     */
public void testReadAndDelete() {
    // 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());
        // Check the message headers
        Iterator<OfflineMessageHeader> headers = offlineManager.getHeaders();
        assertTrue("No message header was found", headers.hasNext());
        List<String> stamps = new ArrayList<String>();
        while (headers.hasNext()) {
            OfflineMessageHeader header = headers.next();
            assertEquals("Incorrect sender", getFullJID(0), header.getJid());
            assertNotNull("No stamp was found in the message header", header.getStamp());
            stamps.add(header.getStamp());
        }
        assertEquals("Wrong number of headers", 2, stamps.size());
        // Get the offline messages
        Iterator<Message> messages = offlineManager.getMessages(stamps);
        assertTrue("No message was found", messages.hasNext());
        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 the retrieved offline messages
        offlineManager.deleteMessages(stamps);
        // 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());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) ArrayList(java.util.ArrayList) XMPPException(org.jivesoftware.smack.XMPPException) MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Chat(org.jivesoftware.smack.Chat) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

StanzaCollector (org.jivesoftware.smack.StanzaCollector)28 Message (org.jivesoftware.smack.packet.Message)14 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 Presence (org.jivesoftware.smack.packet.Presence)7 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 ArrayList (java.util.ArrayList)5 Chat (org.jivesoftware.smack.Chat)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)5 Packet (org.jivesoftware.smack.packet.Packet)5 XMPPException (org.jivesoftware.smack.XMPPException)4 ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)3 Stanza (org.jivesoftware.smack.packet.Stanza)3 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)2 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)2 IQ (org.jivesoftware.smack.packet.IQ)2 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)2 OfflineMessageRequest (org.jivesoftware.smackx.offline.packet.OfflineMessageRequest)2 OfflineMessageInfo (org.jivesoftware.smackx.packet.OfflineMessageInfo)2