Java Code Examples for org.jivesoftware.smack.packet.RosterPacket#addRosterItem()

The following examples show how to use org.jivesoftware.smack.packet.RosterPacket#addRosterItem() . 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: InstantMessagingClient.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a subscription request to the username answers are handled by the method
 * 
 * @param uname
 * @param groupname
 */
protected void subscribeToUser(final String uname, final String groupname) {
    final Presence presence = new Presence(Presence.Type.subscribe);
    presence.setTo(uname + "@" + jabberServer);
    try {
        connection.sendPacket(presence);

        final RosterPacket rosterPacket = new RosterPacket();
        rosterPacket.setType(IQ.Type.SET);
        final RosterPacket.Item item = new RosterPacket.Item(uname + "@" + jabberServer, uname);
        item.addGroupName(groupname);
        item.setItemType(RosterPacket.ItemType.both);
        rosterPacket.addRosterItem(item);
        connection.sendPacket(rosterPacket);
    } catch (final RuntimeException e) {
        log.warn("Error while trying to send Instant Messaging packet.", e);
    }
}
 
Example 2
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a subscription request to the username answers are handled by the method
 * 
 * @param uname
 * @param groupname
 */
protected void subscribeToUser(final String uname, final String groupname) {
    final Presence presence = new Presence(Presence.Type.subscribe);
    presence.setTo(uname + "@" + jabberServer);
    try {
        connection.sendPacket(presence);

        final RosterPacket rosterPacket = new RosterPacket();
        rosterPacket.setType(IQ.Type.SET);
        final RosterPacket.Item item = new RosterPacket.Item(uname + "@" + jabberServer, uname);
        item.addGroupName(groupname);
        item.setItemType(RosterPacket.ItemType.both);
        rosterPacket.addRosterItem(item);
        connection.sendPacket(rosterPacket);
    } catch (final RuntimeException e) {
        log.warn("Error while trying to send Instant Messaging packet.", e);
    }
}
 
Example 3
Source File: Roster.java    From AndroidPNClient 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 an asynchronous call -- Smack must wait for the server
 * to send an updated subscription status.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an XMPP error occurs.
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
    // 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.getUser())) {
        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);
    PacketCollector collector = connection.createPacketCollector(
            new PacketIDFilter(packet.getPacketID()));
    connection.sendPacket(packet);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
}
 
Example 4
Source File: RosterEntry.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets the name associated with this entry.
 *
 * @param name the name.
 */
public void setName(String name) {
  // Do nothing if the name hasn't changed.
  if (name != null && name.equals(this.name)) {
    return;
  }

  // Create a RosterEntry-copy with the new name to create an
  // update-RosterPacket
  RosterEntry updatedRosterEntry =
      new RosterEntry(this.user, name, this.type, this.status, this.roster, this.connection);
  RosterPacket packet = new RosterPacket();
  packet.setType(IQ.Type.SET);
  packet.addRosterItem(toRosterItem(updatedRosterEntry));
  connection.sendPacket(packet);
}
 
Example 5
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * For unsubscription we have to create a packet like: <iq type="set" id="ab7ba" > <query xmlns="jabber:iq:roster"> <item subscription="remove" jid="guido@localhost"
 * /> </query> </iq>
 * 
 * @param uname
 *            a valid username
 */
protected void removeSubscription(final String uname) {
    final RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.SET);
    final RosterPacket.Item item = new RosterPacket.Item(uname + "@" + jabberServer, uname);
    item.setItemType(RosterPacket.ItemType.remove);
    rosterPacket.addRosterItem(item);
    try {
        connection.sendPacket(rosterPacket);
    } catch (final RuntimeException e) {
        log.warn("Error while trying to send Instant Messaging packet.", e);
    }
}
 
Example 6
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * For unsubscription we have to create a packet like: <iq type="set" id="ab7ba" > <query xmlns="jabber:iq:roster"> <item subscription="remove" jid="guido@localhost"
 * /> </query> </iq>
 * 
 * @param uname
 *            a valid username
 */
protected void removeSubscription(final String uname) {
    final RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.SET);
    final RosterPacket.Item item = new RosterPacket.Item(uname + "@" + jabberServer, uname);
    item.setItemType(RosterPacket.ItemType.remove);
    rosterPacket.addRosterItem(item);
    try {
        connection.sendPacket(rosterPacket);
    } catch (final RuntimeException e) {
        log.warn("Error while trying to send Instant Messaging packet.", e);
    }
}
 
Example 7
Source File: RosterEntry.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the name associated with this entry.
 *
 * @param name the name.
 */
public void setName(String name) {
    // Do nothing if the name hasn't changed.
    if (name != null && name.equals(this.name)) {
        return;
    }
    this.name = name;
    RosterPacket packet = new RosterPacket();
    packet.setType(IQ.Type.SET);
    packet.addRosterItem(toRosterItem(this));
    connection.sendPacket(packet);
}
 
Example 8
Source File: Roster.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new roster entry and presence subscription. The server will asynchronously
 * update the roster with the subscription status.
 *
 * @param user   the user. (e.g. [email protected])
 * @param name   the nickname of the user.
 * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
 *               the roster entry won't belong to a group.
 * @throws XMPPException if an XMPP exception occurs.
 */
public void createEntry(String user, String name, String[] groups) throws XMPPException {
    // Create and send roster entry creation packet.
    RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.SET);
    RosterPacket.Item item = new RosterPacket.Item(user, name);
    if (groups != null) {
        for (String group : groups) {
            if (group != null && group.trim().length() > 0) {
                item.addGroupName(group);
            }
        }
    }
    rosterPacket.addRosterItem(item);
    // Wait up to a certain number of seconds for a reply from the server.
    PacketCollector collector = connection.createPacketCollector(
            new PacketIDFilter(rosterPacket.getPacketID()));
    connection.sendPacket(rosterPacket);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }

    // Create a presence subscription packet and send.
    Presence presencePacket = new Presence(Presence.Type.subscribe);
    presencePacket.setTo(user);
    connection.sendPacket(presencePacket);
}
 
Example 9
Source File: RosterGroup.java    From AndroidPNClient 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.
 */
public void setName(String name) {
    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.sendPacket(packet);
        }
    }
}
 
Example 10
Source File: RosterGroup.java    From AndroidPNClient 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 an asynchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an error occured while trying to add the entry to the group.
 */
public void addEntry(RosterEntry entry) throws XMPPException {
    PacketCollector collector = null;
    // 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.
            collector = connection
                    .createPacketCollector(new PacketIDFilter(packet.getPacketID()));
            connection.sendPacket(packet);
        }
    }
    if (collector != null) {
        IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (response == null) {
            throw new XMPPException("No response from the server.");
        }
        // If the server replied with an error, throw an exception.
        else if (response.getType() == IQ.Type.ERROR) {
            throw new XMPPException(response.getError());
        }
    }
}
 
Example 11
Source File: RosterGroup.java    From AndroidPNClient with Apache License 2.0 5 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 an asynchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an error occured while trying to remove the entry from the group. 
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
    PacketCollector collector = null;
    // 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.
            collector = connection
                    .createPacketCollector(new PacketIDFilter(packet.getPacketID()));
            connection.sendPacket(packet);
        }
    }
    if (collector != null) {
        IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (response == null) {
            throw new XMPPException("No response from the server.");
        }
        // If the server replied with an error, throw an exception.
        else if (response.getType() == IQ.Type.ERROR) {
            throw new XMPPException(response.getError());
        }
    }
}
 
Example 12
Source File: Roster.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new roster entry and presence subscription. The server will asynchronously update the
 * roster with the subscription status.
 *
 * @param user the user. (e.g. [email protected])
 * @param name the nickname of the user.
 * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the the
 *     roster entry won't belong to a group.
 * @throws XMPPException if an XMPP exception occurs.
 * @throws IllegalStateException if connection is not logged in or logged in anonymously
 */
public void createEntry(String user, String name, String[] groups) throws XMPPException {
  if (!connection.isAuthenticated()) {
    throw new IllegalStateException("Not logged in to server.");
  }
  if (connection.isAnonymous()) {
    throw new IllegalStateException("Anonymous users can't have a roster.");
  }

  // Create and send roster entry creation packet.
  RosterPacket rosterPacket = new RosterPacket();
  rosterPacket.setType(IQ.Type.SET);
  RosterPacket.Item item = new RosterPacket.Item(user, name);
  if (groups != null) {
    for (String group : groups) {
      if (group != null && group.trim().length() > 0) {
        item.addGroupName(group);
      }
    }
  }
  rosterPacket.addRosterItem(item);
  // Wait up to a certain number of seconds for a reply from the server.
  PacketCollector collector =
      connection.createPacketCollector(new PacketIDFilter(rosterPacket.getPacketID()));
  connection.sendPacket(rosterPacket);
  IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
  collector.cancel();
  if (response == null) {
    throw new XMPPException("No response from the server.");
  }
  // If the server replied with an error, throw an exception.
  else if (response.getType() == IQ.Type.ERROR) {
    throw new XMPPException(response.getError());
  }

  // Create a presence subscription packet and send.
  Presence presencePacket = new Presence(Presence.Type.subscribe);
  presencePacket.setTo(user);
  connection.sendPacket(presencePacket);
}
 
Example 13
Source File: Roster.java    From saros with GNU General Public License v2.0 5 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 an asynchronous call -- Smack must wait for the server to send an
 * updated subscription status.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an XMPP error occurs.
 * @throws IllegalStateException if connection is not logged in or logged in anonymously
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
  if (!connection.isAuthenticated()) {
    throw new IllegalStateException("Not logged in to server.");
  }
  if (connection.isAnonymous()) {
    throw new IllegalStateException("Anonymous users can't have a roster.");
  }

  // 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.getUser())) {
    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);
  PacketCollector collector =
      connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
  connection.sendPacket(packet);
  IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
  collector.cancel();
  if (response == null) {
    throw new XMPPException("No response from the server.");
  }
  // If the server replied with an error, throw an exception.
  else if (response.getType() == IQ.Type.ERROR) {
    throw new XMPPException(response.getError());
  }
}