Search in sources :

Example 1 with BareJid

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

the class Roster method isSubscribedToMyPresence.

/**
     * Check if the given JID is subscribed to the user's presence.
     * <p>
     * If the JID is subscribed to the user's presence then it is allowed to see the presence and
     * will get notified about presence changes. Also returns true, if the JID is the service
     * name of the XMPP connection (the "XMPP domain"), i.e. the XMPP service is treated like
     * having an implicit subscription to the users presence.
     * </p>
     * Note that if the roster is not loaded, then this method will always return false.
     * 
     * @param jid
     * @return true if the given JID is allowed to see the users presence.
     * @since 4.1
     */
public boolean isSubscribedToMyPresence(Jid jid) {
    if (jid == null) {
        return false;
    }
    BareJid bareJid = jid.asBareJid();
    if (connection().getXMPPServiceDomain().equals(bareJid)) {
        return true;
    }
    RosterEntry entry = getEntry(bareJid);
    if (entry == null) {
        return false;
    }
    return entry.canSeeMyPresence();
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid)

Example 2 with BareJid

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

the class RosterTest method testDeleteRosterItem.

/**
     * Test deleting a roster item according to the example in
     * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-delete"
     *     >RFC3921: Deleting a Roster Item</a>.
     */
@Test
public void testDeleteRosterItem() throws Throwable {
    // The contact which should be deleted
    final BareJid contactJID = JidCreate.entityBareFrom("romeo@example.net");
    // Setup
    assertNotNull("Can't get the roster from the provided connection!", roster);
    initRoster();
    rosterListener.reset();
    // Delete a roster item
    final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {

        @Override
        void verifyUpdateRequest(final RosterPacket updateRequest) {
            final Item item = updateRequest.getRosterItems().iterator().next();
            assertEquals("The provided JID doesn't match the requested!", contactJID, item.getJid());
        }
    };
    serverSimulator.start();
    roster.removeEntry(roster.getEntry(contactJID));
    serverSimulator.join();
    // Check if an error occurred within the simulator
    final Throwable exception = serverSimulator.getException();
    if (exception != null) {
        throw exception;
    }
    rosterListener.waitUntilInvocationOrTimeout();
    // Verify
    final RosterEntry deletedEntry = roster.getEntry(contactJID);
    assertNull("The contact wasn't deleted from the roster!", deletedEntry);
    assertTrue("The roster listener wasn't invoked for the deleted contact!", rosterListener.getDeletedAddresses().contains(contactJID));
    verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
    verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
    assertSame("Wrong number of roster entries (" + roster.getEntries() + ").", 2, roster.getEntries().size());
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Test(org.junit.Test)

Example 3 with BareJid

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

the class RosterTest method testAddEmptyGroupEntry.

/**
     * Test if adding an user with an empty group is equivalent with providing
     * no group.
     * 
     * @see <a href="http://www.igniterealtime.org/issues/browse/SMACK-294">SMACK-294</a>
     */
@Test(timeout = 5000)
public void testAddEmptyGroupEntry() throws Throwable {
    // Constants for the new contact
    final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
    final String contactName = "Nurse";
    final String[] contactGroup = { "" };
    // Setup
    assertNotNull("Can't get the roster from the provided connection!", roster);
    initRoster();
    rosterListener.reset();
    // Adding the new roster item
    final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {

        @Override
        void verifyUpdateRequest(final RosterPacket updateRequest) {
            final Item item = updateRequest.getRosterItems().iterator().next();
            assertSame("The provided JID doesn't match the requested!", contactJID, item.getJid());
            assertSame("The provided name doesn't match the requested!", contactName, item.getName());
            assertSame("Shouldn't provide an empty group element!", 0, item.getGroupNames().size());
        }
    };
    serverSimulator.start();
    roster.createEntry(contactJID, contactName, contactGroup);
    serverSimulator.join();
    // Check if an error occurred within the simulator
    final Throwable exception = serverSimulator.getException();
    if (exception != null) {
        throw exception;
    }
    rosterListener.waitUntilInvocationOrTimeout();
    // Verify the roster entry of the new contact
    final RosterEntry addedEntry = roster.getEntry(contactJID);
    assertNotNull("The new contact wasn't added to the roster!", addedEntry);
    assertTrue("The roster listener wasn't invoked for the new contact!", rosterListener.getAddedAddresses().contains(contactJID));
    assertSame("Setup wrong name for the new contact!", contactName, addedEntry.getName());
    assertSame("Setup wrong default subscription status!", ItemType.none, addedEntry.getType());
    assertSame("The new contact shouldn't be member of any group!", 0, addedEntry.getGroups().size());
    // Verify the unchanged roster items
    verifyRomeosEntry(roster.getEntry(JidCreate.entityBareFrom("romeo@example.net")));
    verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
    verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
    assertSame("Wrong number of roster entries.", 4, roster.getEntries().size());
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Test(org.junit.Test)

Example 4 with BareJid

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

the class SubscriptionPreApprovalTest method testPreApproveAndCreate.

@Test
public void testPreApproveAndCreate() throws Throwable {
    final BareJid contactJID = JidCreate.bareFrom("preapproval@example.com");
    final String contactName = "PreApproval";
    final String[] contactGroup = {};
    connection.enableStreamFeature(SubscriptionPreApproval.INSTANCE);
    final PreApproveAndCreateEntryResponder serverSimulator = new PreApproveAndCreateEntryResponder() {

        @Override
        void verifyRosterUpdateRequest(final RosterPacket updateRequest) {
            final Item item = updateRequest.getRosterItems().iterator().next();
            assertSame("The provided JID doesn't match the requested!", contactJID, item.getJid());
            assertSame("The provided name doesn't match the requested!", contactName, item.getName());
            assertSame("The provided group number doesn't match the requested!", 0, item.getGroupNames().size());
        }

        @Override
        void verifyPreApprovalRequest(Presence preApproval) {
            assertSame("The provided name doesn't match the requested!", contactJID, preApproval.getTo());
            assertSame("The provided presence type is incorrect!", Presence.Type.subscribed, preApproval.getType());
        }
    };
    serverSimulator.start();
    roster.preApproveAndCreateEntry(contactJID, contactName, contactGroup);
    serverSimulator.join();
    // Check if an error occurred within the simulator
    final Throwable exception = serverSimulator.getException();
    if (exception != null) {
        throw exception;
    }
    rosterListener.waitUntilInvocationOrTimeout();
    // Verify the roster entry of the new contact
    final RosterEntry addedEntry = roster.getEntry(contactJID);
    assertNotNull("The new contact wasn't added to the roster!", addedEntry);
    assertTrue("The roster listener wasn't invoked for the new contact!", rosterListener.getAddedAddresses().contains(contactJID));
    assertSame("Setup wrong name for the new contact!", contactName, addedEntry.getName());
    assertSame("Setup wrong default subscription status!", ItemType.none, addedEntry.getType());
    assertSame("The new contact should be member of exactly one group!", 0, addedEntry.getGroups().size());
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Presence(org.jivesoftware.smack.packet.Presence) Test(org.junit.Test)

Example 5 with BareJid

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

the class Roster method iAmSubscribedTo.

/**
     * Check if the XMPP entity this roster belongs to is subscribed to the presence of the given JID.
     *
     * @param jid the jid to check.
     * @return <code>true</code> if we are subscribed to the presence of the given jid.
     * @since 4.2
     */
public boolean iAmSubscribedTo(Jid jid) {
    if (jid == null) {
        return false;
    }
    BareJid bareJid = jid.asBareJid();
    RosterEntry entry = getEntry(bareJid);
    if (entry == null) {
        return false;
    }
    return entry.canSeeHisPresence();
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid)

Aggregations

BareJid (org.jxmpp.jid.BareJid)26 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)9 Test (org.junit.Test)9 Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)8 Jid (org.jxmpp.jid.Jid)7 EntityBareJid (org.jxmpp.jid.EntityBareJid)6 Presence (org.jivesoftware.smack.packet.Presence)5 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 ArrayList (java.util.ArrayList)2 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)2 SmackException (org.jivesoftware.smack.SmackException)2 IQ (org.jivesoftware.smack.packet.IQ)2 IoTIsFriendResponse (org.jivesoftware.smackx.iot.provisioning.element.IoTIsFriendResponse)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 File (java.io.File)1 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)1 IoTRemove (org.jivesoftware.smackx.iot.discovery.element.IoTRemove)1