Search in sources :

Example 46 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

the class ChatConnectionTest method chatMatchedOnJIDWhenNoThreadJidMode.

@Test
public void chatMatchedOnJIDWhenNoThreadJidMode() {
    TestMessageListener msgListener = new TestMessageListener();
    TestChatManagerListener listener = new TestChatManagerListener(msgListener);
    cm.setMatchMode(ChatManager.MatchMode.SUPPLIED_JID);
    cm.addChatListener(listener);
    Stanza incomingChat = createChatMessage(null, true);
    processServerMessage(incomingChat);
    Chat newChat = listener.getNewChat();
    assertNotNull(newChat);
    cm.removeChatListener(listener);
    // Should match on chat with full jid
    incomingChat = createChatMessage(null, true);
    processServerMessage(incomingChat);
    assertEquals(2, msgListener.getNumMessages());
    // Should not match on chat with bare jid
    TestChatManagerListener listener2 = new TestChatManagerListener();
    cm.addChatListener(listener2);
    incomingChat = createChatMessage(null, false);
    processServerMessage(incomingChat);
    assertEquals(2, msgListener.getNumMessages());
    assertNotNull(listener2.getNewChat());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 47 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

the class RosterVersioningTest method answerWithEmptyRosterResult.

private void answerWithEmptyRosterResult() {
    // We expect that the roster request is the only packet sent. This is not part of the specification,
    // but a shortcut in the test implementation.
    Stanza sentPacket = connection.getSentPacket();
    if (sentPacket instanceof RosterPacket) {
        final IQ emptyIQ = IQ.createResultIQ((RosterPacket) sentPacket);
        connection.processStanza(emptyIQ);
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ)

Example 48 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

the class MamIntegrationTest method mamPageTest.

@SmackIntegrationTest
public void mamPageTest() throws TimeoutException, Exception {
    final int messagesPerPage = 10;
    final int numPages = 3;
    final int totalMessages = messagesPerPage * numPages;
    final List<Message> outgoingMessages = new ArrayList<>(totalMessages);
    final EntityBareJid userOne = conOne.getUser().asEntityBareJid();
    final EntityBareJid userTwo = conTwo.getUser().asEntityBareJid();
    final SimpleResultSyncPoint allMessagesReceived = new SimpleResultSyncPoint();
    final String lastMessageArchiveUid = mamManagerConTwo.getMessageUidOfLatestMessage();
    for (int i = 0; i < totalMessages; i++) {
        String messageBody = "MAM Page Test " + testRunId + ' ' + (i + 1);
        Message message = StanzaBuilder.buildMessage().to(userTwo).setBody(messageBody).build();
        outgoingMessages.add(message);
    }
    final String lastBody = outgoingMessages.get(outgoingMessages.size() - 1).getBody();
    final StanzaListener stanzaListener = new StanzaListener() {

        @Override
        public void processStanza(Stanza stanza) {
            Message message = (Message) stanza;
            if (message.getBody().equals(lastBody)) {
                allMessagesReceived.signal();
            }
        }
    };
    conTwo.addAsyncStanzaListener(stanzaListener, MessageWithBodiesFilter.INSTANCE);
    try {
        for (Message message : outgoingMessages) {
            conOne.sendStanza(message);
        }
        allMessagesReceived.waitForResult(timeout);
    } finally {
        conTwo.removeAsyncStanzaListener(stanzaListener);
    }
    MamQueryArgs mamQueryArgs = MamQueryArgs.builder().setResultPageSize(messagesPerPage).limitResultsToJid(userOne).afterUid(lastMessageArchiveUid).build();
    MamQuery mamQuery = mamManagerConTwo.queryArchive(mamQueryArgs);
    assertFalse(mamQuery.isComplete());
    assertEquals(messagesPerPage, mamQuery.getMessageCount());
    List<List<Message>> pages = new ArrayList<>(numPages);
    pages.add(mamQuery.getMessages());
    for (int additionalPageRequestNum = 0; additionalPageRequestNum < numPages - 1; additionalPageRequestNum++) {
        List<Message> page = mamQuery.pageNext(messagesPerPage);
        boolean isLastQuery = additionalPageRequestNum == numPages - 2;
        if (isLastQuery) {
            assertTrue(mamQuery.isComplete());
        } else {
            assertFalse(mamQuery.isComplete());
        }
        assertEquals(messagesPerPage, page.size());
        pages.add(page);
    }
    List<Message> queriedMessages = new ArrayList<>(totalMessages);
    for (List<Message> messages : pages) {
        queriedMessages.addAll(messages);
    }
    assertEquals(outgoingMessages.size(), queriedMessages.size());
    for (int i = 0; i < outgoingMessages.size(); i++) {
        Message outgoingMessage = outgoingMessages.get(i);
        Message queriedMessage = queriedMessages.get(i);
        assertEquals(outgoingMessage.getBody(), queriedMessage.getBody());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) StanzaListener(org.jivesoftware.smack.StanzaListener) MamQuery(org.jivesoftware.smackx.mam.MamManager.MamQuery) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) ArrayList(java.util.ArrayList) List(java.util.List) EntityBareJid(org.jxmpp.jid.EntityBareJid) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) MamQueryArgs(org.jivesoftware.smackx.mam.MamManager.MamQueryArgs) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)

Example 49 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

the class EntityCapsTest method testPreventDiscoInfo.

/**
 * Test if entity caps actually prevent a disco info request and reply.
 *
 * @throws Exception if exception.
 */
@SmackIntegrationTest
public void testPreventDiscoInfo() throws Exception {
    final String dummyFeature = getNewDummyFeature();
    final AtomicBoolean discoInfoSend = new AtomicBoolean();
    conOne.addStanzaSendingListener(new StanzaListener() {

        @Override
        public void processStanza(Stanza stanza) {
            discoInfoSend.set(true);
        }
    }, new AndFilter(new StanzaTypeFilter(DiscoverInfo.class), IQTypeFilter.GET));
    addFeatureAndWaitForPresence(conOne, conTwo, dummyFeature);
    dropCapsCache();
    // discover that
    DiscoverInfo info = sdmOne.discoverInfo(conTwo.getUser());
    // that discovery should cause a disco#info
    assertTrue(discoInfoSend.get());
    assertTrue(info.containsFeature(dummyFeature), "The info response '" + info + "' does not contain the expected feature '" + dummyFeature + '\'');
    discoInfoSend.set(false);
    // discover that
    info = sdmOne.discoverInfo(conTwo.getUser());
    // that discovery shouldn't cause a disco#info
    assertFalse(discoInfoSend.get());
    assertTrue(info.containsFeature(dummyFeature));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)

Example 50 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

the class JingleManager method initJingleSessionRequestListeners.

/**
 * Register the listenerJingles, waiting for a Jingle stanza that tries to
 * establish a new session.
 */
private void initJingleSessionRequestListeners() {
    StanzaFilter initRequestFilter = new StanzaFilter() {

        // Return true if we accept this packet
        @Override
        public boolean accept(Stanza pin) {
            if (pin instanceof IQ) {
                IQ iq = (IQ) pin;
                if (iq.getType().equals(IQ.Type.set)) {
                    if (iq instanceof Jingle) {
                        Jingle jin = (Jingle) pin;
                        if (jin.getAction().equals(JingleActionEnum.SESSION_INITIATE)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    };
    jingleSessionRequestListeners = new ArrayList<>();
    // Start a packet listener for session initiation requests
    connection.addAsyncStanzaListener(new StanzaListener() {

        @Override
        public void processStanza(Stanza packet) {
            triggerSessionRequested((Jingle) packet);
        }
    }, initRequestFilter);
}
Also used : Jingle(org.jivesoftware.smackx.jingleold.packet.Jingle) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener)

Aggregations

Stanza (org.jivesoftware.smack.packet.Stanza)101 StanzaListener (org.jivesoftware.smack.StanzaListener)24 Test (org.junit.Test)22 IQ (org.jivesoftware.smack.packet.IQ)20 Test (org.junit.jupiter.api.Test)18 XMPPConnection (org.jivesoftware.smack.XMPPConnection)14 Message (org.jivesoftware.smack.packet.Message)14 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)9 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)8 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)8 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)7 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 Protocol (org.jivesoftware.util.Protocol)6 EntityFullJid (org.jxmpp.jid.EntityFullJid)6 LinkedList (java.util.LinkedList)5