Search in sources :

Example 16 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector 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());
    }
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) Chat(org.jivesoftware.smack.Chat) ArrayList(java.util.ArrayList) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Example 17 with StanzaCollector

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

the class MultiUserChatTest method testPrivateChat.

public void testPrivateChat() {
    try {
        // User2 joins the new room
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        muc2.join("testbot2");
        getConnection(0).getChatManager().addChatListener(new ChatManagerListener() {

            public void chatCreated(Chat chat2, boolean createdLocally) {
                assertEquals("Sender of chat is incorrect", room + "/testbot2", chat2.getParticipant());
                try {
                    chat2.sendMessage("ACK");
                } catch (XMPPException e) {
                    fail(e.getMessage());
                }
            }
        });
        // Start a private chat with another participant            
        Chat chat = muc2.createPrivateChat(room + "/testbot", null);
        StanzaCollector collector = chat.createCollector();
        chat.sendMessage("Hello there");
        Message response = (Message) collector.nextResult(2000);
        assertNotNull("No response", response);
        assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
        assertEquals("Body of response is incorrect", "ACK", response.getBody());
        // User2 leaves the room
        muc2.leave();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) XMPPException(org.jivesoftware.smack.XMPPException) StanzaCollector(org.jivesoftware.smack.StanzaCollector) ChatManagerListener(org.jivesoftware.smack.ChatManagerListener) XMPPException(org.jivesoftware.smack.XMPPException)

Example 18 with StanzaCollector

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

the class XHTMLExtensionTest method testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage.

/**
     * Low level API test.
     * 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 testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
    // 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()));
    // User1 creates a message to send to user2
    Message msg = new Message();
    msg.setSubject("Any subject you want");
    msg.setBody("Hey John, this is my new green!!!!");
    // Create a XHTMLExtension Package and add it to the message
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    xhtmlExtension.addBody("<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
    msg.addExtension(xhtmlExtension);
    // User1 sends the message that contains the XHTML to user2
    try {
        chat1.sendMessage(msg);
    } catch (Exception e) {
        fail("An error occured sending the message with XHTML");
    }
    Packet packet = chat2.nextResult(2000);
    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(); ) {
            String body = it.next();
            System.out.println(body);
        }
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) Chat(org.jivesoftware.smack.Chat) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 19 with StanzaCollector

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

the class OfflineMessageManager method getMessages.

/**
     * Returns a List of Messages with all the offline <tt>Messages</tt> of the user. The returned offline
     * messages will not be deleted from the server. Use {@link #deleteMessages(java.util.List)}
     * to delete the messages.
     *
     * @return a List with all the offline <tt>Messages</tt> of the user.
     * @throws XMPPErrorException If the user is not allowed to make this request or the server does
     *                       not support offline message retrieval.
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public List<Message> getMessages() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    OfflineMessageRequest request = new OfflineMessageRequest();
    request.setFetch(true);
    StanzaCollector resultCollector = connection.createStanzaCollectorAndSend(request);
    StanzaCollector.Configuration messageCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(PACKET_FILTER).setCollectorToReset(resultCollector);
    StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration);
    List<Message> messages = null;
    try {
        resultCollector.nextResultOrThrow();
        // Be extra safe, cancel the message collector right here so that it does not collector
        // other messages that eventually match (although I've no idea how this could happen in
        // case of XEP-13).
        messageCollector.cancel();
        messages = new ArrayList<>(messageCollector.getCollectedCount());
        Message message;
        while ((message = messageCollector.pollResult()) != null) {
            messages.add(message);
        }
    } finally {
        // Ensure that the message collector is canceled even if nextResultOrThrow threw. It
        // doesn't matter if we cancel the message collector twice
        messageCollector.cancel();
    }
    return messages;
}
Also used : Message(org.jivesoftware.smack.packet.Message) OfflineMessageRequest(org.jivesoftware.smackx.offline.packet.OfflineMessageRequest) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 20 with StanzaCollector

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

the class MultiUserChat method changeSubject.

/**
     * Changes the subject within the room. As a default, only users with a role of "moderator"
     * are allowed to change the subject in a room. Although some rooms may be configured to
     * allow a mere participant or even a visitor to change the subject.
     *
     * @param subject the new room's subject to set.
     * @throws XMPPErrorException if someone without appropriate privileges attempts to change the
     *          room subject will throw an error with code 403 (i.e. Forbidden)
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void changeSubject(final String subject) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Message message = createMessage();
    message.setSubject(subject);
    // Wait for an error or confirmation message back from the server.
    StanzaFilter responseFilter = new AndFilter(fromRoomGroupchatFilter, new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            Message msg = (Message) packet;
            return subject.equals(msg.getSubject());
        }
    });
    StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, message);
    // Wait up to a certain number of seconds for a reply.
    response.nextResultOrThrow();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) 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