Java Code Examples for org.jivesoftware.smack.roster.packet.RosterPacket#Item

The following examples show how to use org.jivesoftware.smack.roster.packet.RosterPacket#Item . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RosterTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Remove all roster entries by iterating trough {@link Roster#getEntries()}
 * and simulating receiving roster pushes from the server.
 *
 * @param connection the dummy connection of which the provided roster belongs to.
 * @param roster the roster (or buddy list) which should be initialized.
 */
public static void removeAllRosterEntries(DummyConnection connection, Roster roster) {
    for (RosterEntry entry : roster.getEntries()) {
        // prepare the roster push packet
        final RosterPacket rosterPush = new RosterPacket();
        rosterPush.setType(Type.set);
        rosterPush.setTo(connection.getUser());

        // prepare the buddy's item entry which should be removed
        final RosterPacket.Item item = new RosterPacket.Item(entry.getJid(), entry.getName());
        item.setItemType(ItemType.remove);
        rosterPush.addRosterItem(item);

        // simulate receiving the roster push
        connection.processStanza(rosterPush);
    }
}
 
Example 2
Source File: RosterGroup.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a roster entry from this group. If the entry does not belong to any other group
 * then it will be considered as unfiled, therefore it will be added to the list of unfiled
 * entries.
 * Note that this is a synchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPErrorException if an error occurred while trying to remove the entry from the group.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void removeEntry(RosterEntry entry) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    // Only remove the entry if it's in the entry list.
    // Remove the entry locally, if we wait for RosterPacketListenerProcess>>Packet(Packet)
    // to take place the entry will exist in the group until a packet is received from the
    // server.
    synchronized (entries) {
        if (entries.contains(entry)) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.set);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.removeGroupName(this.getName());
            packet.addRosterItem(item);
            // Wait up to a certain number of seconds for a reply from the server.
            connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
        }
    }
}
 
Example 3
Source File: Roster.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a roster entry from the roster. The roster entry will also be removed from the
 * unfiled entries or from any roster group where it could belong and will no longer be part
 * of the roster. Note that this is a synchronous call -- Smack must wait for the server
 * to send an updated subscription status.
 *
 * @param entry a roster entry.
 * @throws XMPPErrorException if an XMPP error occurs.
 * @throws NotLoggedInException if not logged in.
 * @throws NoResponseException SmackException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();

    // Only remove the entry if it's in the entry list.
    // The actual removal logic takes place in RosterPacketListenerProcess>>Packet(Packet)
    if (!entries.containsKey(entry.getJid())) {
        return;
    }
    RosterPacket packet = new RosterPacket();
    packet.setType(IQ.Type.set);
    RosterPacket.Item item = RosterEntry.toRosterItem(entry);
    // Set the item type as REMOVE so that the server will delete the entry
    item.setItemType(RosterPacket.ItemType.remove);
    packet.addRosterItem(item);
    connection.createStanzaCollectorAndSend(packet).nextResultOrThrow();
}
 
Example 4
Source File: RosterGroup.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a roster entry to this group. If the entry was unfiled then it will be removed from
 * the unfiled list and will be added to this group.
 * Note that this is a synchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPErrorException if an error occurred while trying to add the entry to the group.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void addEntry(RosterEntry entry) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    // Only add the entry if it isn't already in the list.
    synchronized (entries) {
        if (!entries.contains(entry)) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.set);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.addGroupName(getName());
            packet.addRosterItem(item);
            // Wait up to a certain number of seconds for a reply from the server.
            connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
        }
    }
}
 
Example 5
Source File: RosterVersioningTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static void populateStore(RosterStore store) throws IOException {
    store.addEntry(new RosterPacket.Item(JidCreate.entityBareFrom("[email protected]"), "geoff hurley"), "");

    RosterPacket.Item item = new RosterPacket.Item(JidCreate.entityBareFrom("[email protected]"), "joe stevens");
    item.addGroupName("friends");
    item.addGroupName("partners");
    store.addEntry(item, "");

    item = new RosterPacket.Item(JidCreate.entityBareFrom("[email protected]"), "higgins mcmann");
    item.addGroupName("all");
    item.addGroupName("friends");
    store.addEntry(item, "v96");
}
 
Example 6
Source File: RosterPacketProvider.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public RosterPacket parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    RosterPacket roster = new RosterPacket();
    String version = parser.getAttributeValue("", "ver");
    roster.setVersion(version);

    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String startTag = parser.getName();
            switch (startTag) {
            case "item":
                RosterPacket.Item item = parseItem(parser);
                roster.addRosterItem(item);
                break;
            }
            break;
        case END_ELEMENT:
            String endTag = parser.getName();
            switch (endTag) {
            case IQ.QUERY_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }
    return roster;
}
 
Example 7
Source File: RosterGroup.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the name of the group. Changing the group's name is like moving all the group entries
 * of the group to a new group specified by the new name. Since this group won't have entries
 * it will be removed from the roster. This means that all the references to this object will
 * be invalid and will need to be updated to the new group specified by the new name.
 *
 * @param name the name of the group.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException {
    synchronized (entries) {
        for (RosterEntry entry : entries) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.set);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.removeGroupName(this.name);
            item.addGroupName(name);
            packet.addRosterItem(item);
            connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
        }
    }
}
 
Example 8
Source File: Roster.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Ignore ItemTypes as of RFC 6121, 2.1.2.5.
 *
 * This is used by {@link RosterPushListener} and {@link RosterResultListener}.
 * */
private static boolean hasValidSubscriptionType(RosterPacket.Item item) {
    switch (item.getItemType()) {
        case none:
        case from:
        case to:
        case both:
            return true;
        default:
            return false;
    }
}
 
Example 9
Source File: RosterEntry.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a roster entry with the given name to a roster item. As per RFC 6121 ยง 2.1.2.2., clients MUST NOT include
 * the 'ask' attribute, thus set {@code includeAskAttribute} to {@code false}.
 *
 * @param entry the roster entry.
 * @param name the name of the roster item.
 * @param includeAskAttribute whether or not to include the 'ask' attribute.
 * @return the roster item.
 */
private static RosterPacket.Item toRosterItem(RosterEntry entry, String name, boolean includeAskAttribute) {
    RosterPacket.Item item = new RosterPacket.Item(entry.getJid(), name);
    item.setItemType(entry.getType());
    if (includeAskAttribute) {
        item.setSubscriptionPending(entry.isSubscriptionPending());
    }
    item.setApproved(entry.isApproved());
    // Set the correct group names for the item.
    for (RosterGroup group : entry.getGroups()) {
        item.addGroupName(group.getName());
    }
    return item;
}
 
Example 10
Source File: Roster.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new roster item. The server will asynchronously update the roster with the subscription status.
 * <p>
 * There will be no presence subscription request. Consider using
 * {@link #createItemAndRequestSubscription(BareJid, String, String[])} if you also want to request a presence
 * subscription from the contact.
 * </p>
 *
 * @param jid the XMPP address of the contact (e.g. [email protected])
 * @param name the nickname of the user.
 * @param groups the list of group names the entry will belong to, or <code>null</code> if the the roster entry won't
 *        belong to a group.
 * @throws NoResponseException if there was no response from the server.
 * @throws XMPPErrorException if an XMPP exception occurs.
 * @throws NotLoggedInException If not logged in.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @since 4.4.0
 */
public void createItem(BareJid jid, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();

    // Create and send roster entry creation packet.
    RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.set);
    RosterPacket.Item item = new RosterPacket.Item(jid, name);
    if (groups != null) {
        for (String group : groups) {
            if (group != null && group.trim().length() > 0) {
                item.addGroupName(group);
            }
        }
    }
    rosterPacket.addRosterItem(item);
    connection.createStanzaCollectorAndSend(rosterPacket).nextResultOrThrow();
}
 
Example 11
Source File: RosterEntry.java    From Smack with Apache License 2.0 4 votes vote down vote up
static RosterPacket.Item toRosterItem(RosterEntry entry, boolean includeAskAttribute) {
    return toRosterItem(entry, entry.getName(), includeAskAttribute);
}
 
Example 12
Source File: RosterPacketProvider.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static RosterPacket.Item parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
    ParserUtils.assertAtStartTag(parser, RosterPacket.Item.ELEMENT);
    final int initialDepth = parser.getDepth();
    String jidString = parser.getAttributeValue("", "jid");
    String itemName = parser.getAttributeValue("", "name");
    BareJid jid = JidCreate.bareFrom(jidString);

    // Create item.
    RosterPacket.Item item = new RosterPacket.Item(jid, itemName);
    // Set status.
    String ask = parser.getAttributeValue("", "ask");
    item.setSubscriptionPending("subscribe".equals(ask));
    // Set type.
    String subscription = parser.getAttributeValue("", "subscription");
    RosterPacket.ItemType type = RosterPacket.ItemType.fromString(subscription);
    item.setItemType(type);
    // Set approval status.
    boolean approved = ParserUtils.getBooleanAttribute(parser, "approved", false);
    item.setApproved(approved);

    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String name = parser.getName();
            switch (name) {
            case RosterPacket.Item.GROUP:
                final String groupName = parser.nextText();
                if (groupName != null && groupName.trim().length() > 0) {
                    item.addGroupName(groupName);
                }
                break;
            }
            break;
        case END_ELEMENT:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }
    ParserUtils.assertAtEndTag(parser);
    assert item != null;
    return item;
}
 
Example 13
Source File: RosterEntry.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new roster entry.
 *
 * @param item the Roster Stanza's Item entry.
 * @param roster The Roster managing this entry.
 * @param connection a connection to the XMPP server.
 */
RosterEntry(RosterPacket.Item item, Roster roster, XMPPConnection connection) {
    super(connection);
    this.item = item;
    this.roster = roster;
}
 
Example 14
Source File: RosterStore.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * This method returns a list of all roster items contained in this store. If there was an error while loading the store, then <code>null</code> is returned.
 *
 * @return List of {@link org.jivesoftware.smack.roster.RosterEntry} or <code>null</code>.
 */
List<RosterPacket.Item> getEntries();
 
Example 15
Source File: RosterStore.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * This method returns the roster item in this store for the given JID.
 *
 * @param bareJid The bare JID of the RosterEntry
 * @return The {@link org.jivesoftware.smack.roster.RosterEntry} which belongs to that user
 */
RosterPacket.Item getEntry(Jid bareJid);
 
Example 16
Source File: RosterStore.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * This method stores a new roster entry in this store or updates an existing one.
 *
 * @param item the entry to store
 * @param version the new roster version
 * @return True if successful
 */
boolean addEntry(RosterPacket.Item item, String version);
 
Example 17
Source File: RosterStore.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * This method updates the store so that it contains only the given entries.
 *
 * @param items the entries to store
 * @param version the new roster version
 * @return True if successful
 */
boolean resetEntries(Collection<RosterPacket.Item> items, String version);
 
Example 18
Source File: RosterEntry.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Convert the RosterEntry to a Roster stanza &lt;item/&gt; element.
 *
 * @param entry the roster entry
 * @param name the name of the roster item.
 * @return the roster item.
 */
static RosterPacket.Item toRosterItem(RosterEntry entry, String name) {
    return toRosterItem(entry, name, false);
}
 
Example 19
Source File: RosterEntry.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Convert the RosterEntry to a Roster stanza &lt;item/&gt; element.
 *
 * @param entry the roster entry.
 * @return the roster item.
 */
static RosterPacket.Item toRosterItem(RosterEntry entry) {
    return toRosterItem(entry, entry.getName(), false);
}
 
Example 20
Source File: RosterEntry.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Updates this entries item.
 *
 * @param item new item
 */
void updateItem(RosterPacket.Item item) {
    assert item != null;
    this.item = item;
}