Search in sources :

Example 76 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class RosterIntegrationTest method subscribeRequestListenerTest.

@SmackIntegrationTest
public void subscribeRequestListenerTest() throws TimeoutException, Exception {
    ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(conOne.getUser().asBareJid())) {
                return SubscribeAnswer.Approve;
            }
            return SubscribeAnswer.Deny;
        }
    };
    rosterTwo.addSubscribeListener(subscribeListener);
    final String conTwosRosterName = "ConTwo " + testRunId;
    final SimpleResultSyncPoint addedAndSubscribed = new SimpleResultSyncPoint();
    rosterOne.addRosterListener(new AbstractRosterListener() {

        @Override
        public void entriesAdded(Collection<Jid> addresses) {
            checkIfAddedAndSubscribed(addresses);
        }

        @Override
        public void entriesUpdated(Collection<Jid> addresses) {
            checkIfAddedAndSubscribed(addresses);
        }

        private void checkIfAddedAndSubscribed(Collection<Jid> addresses) {
            for (Jid jid : addresses) {
                if (!jid.equals(conTwo.getUser().asBareJid())) {
                    continue;
                }
                BareJid bareJid = conTwo.getUser().asBareJid();
                RosterEntry rosterEntry = rosterOne.getEntry(bareJid);
                if (rosterEntry == null) {
                    addedAndSubscribed.signalFailure("No roster entry for " + bareJid);
                    return;
                }
                String name = rosterEntry.getName();
                if (StringUtils.isNullOrEmpty(name)) {
                    addedAndSubscribed.signalFailure("Roster entry without name");
                    return;
                }
                if (!rosterEntry.getName().equals(conTwosRosterName)) {
                    addedAndSubscribed.signalFailure("Roster name does not match");
                    return;
                }
                if (!rosterEntry.getType().equals(ItemType.to)) {
                    return;
                }
                addedAndSubscribed.signal();
            }
        }
    });
    try {
        rosterOne.createEntry(conTwo.getUser().asBareJid(), conTwosRosterName, null);
        assertTrue(addedAndSubscribed.waitForResult(2 * connection.getReplyTimeout()));
    } finally {
        rosterTwo.removeSubscribeListener(subscribeListener);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) BareJid(org.jxmpp.jid.BareJid) Presence(org.jivesoftware.smack.packet.Presence) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 77 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class JingleSession method sendFormattedJingle.

/**
     * Complete and send a packet. Complete all the null fields in a Jingle
     * reponse, using the session information we have or some info from the
     * incoming packet.
     * 
     * @param iq
     *            The Jingle stanza(/packet) we are responing to
     * @param jout
     *            the Jingle stanza(/packet) we want to complete and send
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public Jingle sendFormattedJingle(IQ iq, Jingle jout) throws NotConnectedException, InterruptedException {
    if (jout != null) {
        if (jout.getInitiator() == null) {
            jout.setInitiator(getInitiator());
        }
        if (jout.getResponder() == null) {
            jout.setResponder(getResponder());
        }
        if (jout.getSid() == null) {
            jout.setSid(getSid());
        }
        Jid me = getConnection().getUser();
        Jid other = getResponder().equals(me) ? getInitiator() : getResponder();
        if (jout.getTo() == null) {
            if (iq != null) {
                jout.setTo(iq.getFrom());
            } else {
                jout.setTo(other);
            }
        }
        if (jout.getFrom() == null) {
            if (iq != null) {
                jout.setFrom(iq.getTo());
            } else {
                jout.setFrom(me);
            }
        }
        // CHECKSTYLE:OFF
        if ((getConnection() != null) && (getConnection().isConnected()))
            getConnection().sendStanza(jout);
    // CHECKSTYLE:ON
    }
    return jout;
}
Also used : Jid(org.jxmpp.jid.Jid)

Example 78 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class JingleSession method updatePacketListener.

/**
     * Install the stanza(/packet) listener. The listener is responsible for responding
     * to any stanza(/packet) that we receive...
     */
protected void updatePacketListener() {
    removeAsyncPacketListener();
    LOGGER.fine("UpdatePacketListener");
    packetListener = new StanzaListener() {

        @Override
        public void processStanza(Stanza packet) {
            try {
                receivePacketAndRespond((IQ) packet);
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "exception", e);
            }
        }
    };
    packetFilter = new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            if (packet instanceof IQ) {
                IQ iq = (IQ) packet;
                Jid me = getConnection().getUser();
                if (!iq.getTo().equals(me)) {
                    return false;
                }
                Jid other = getResponder().equals(me) ? getInitiator() : getResponder();
                if (iq.getFrom() == null || !iq.getFrom().equals(other == null ? "" : other)) {
                    return false;
                }
                if (iq instanceof Jingle) {
                    Jingle jin = (Jingle) iq;
                    String sid = jin.getSid();
                    if (sid == null || !sid.equals(getSid())) {
                        LOGGER.fine("Ignored Jingle(SID) " + sid + "|" + getSid() + " :" + iq.toXML());
                        return false;
                    }
                    Jid ini = jin.getInitiator();
                    if (!ini.equals(getInitiator())) {
                        LOGGER.fine("Ignored Jingle(INI): " + iq.toXML());
                        return false;
                    }
                } else {
                    // We accept some non-Jingle IQ packets: ERRORs and ACKs
                    if (iq.getType().equals(IQ.Type.set)) {
                        LOGGER.fine("Ignored Jingle(TYPE): " + iq.toXML());
                        return false;
                    } else if (iq.getType().equals(IQ.Type.get)) {
                        LOGGER.fine("Ignored Jingle(TYPE): " + iq.toXML());
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
    };
    getConnection().addAsyncStanzaListener(packetListener, packetFilter);
}
Also used : Jingle(org.jivesoftware.smackx.jingleold.packet.Jingle) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Jid(org.jxmpp.jid.Jid) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

Jid (org.jxmpp.jid.Jid)78 EntityBareJid (org.jxmpp.jid.EntityBareJid)18 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 DomainBareJid (org.jxmpp.jid.DomainBareJid)14 EntityFullJid (org.jxmpp.jid.EntityFullJid)12 BareJid (org.jxmpp.jid.BareJid)11 IQ (org.jivesoftware.smack.packet.IQ)7 Presence (org.jivesoftware.smack.packet.Presence)7 MUCLightBlockingIQ (org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ)7 SmackException (org.jivesoftware.smack.SmackException)6 Message (org.jivesoftware.smack.packet.Message)6 WeakHashMap (java.util.WeakHashMap)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 Map (java.util.Map)3 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)3 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)3 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)3 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)3