Example #1
0
  /**
   * The packet is a typical 'set' or 'get' update targeted at the server. Notice that the set could
   * be a roster removal in which case we have to generate a local roster removal update as well as
   * a new roster removal to send to the the roster item's owner.
   *
   * @param packet The packet that triggered this update
   * @return Either a response to the roster update or null if the packet is corrupt and the session
   *     was closed down
   */
  private IQ manageRoster(org.xmpp.packet.Roster packet)
      throws UnauthorizedException, UserAlreadyExistsException, SharedGroupException {

    IQ returnPacket = null;
    JID sender = packet.getFrom();
    IQ.Type type = packet.getType();

    try {
      if ((sender.getNode() == null
              || !RosterManager.isRosterServiceEnabled()
              || !userManager.isRegisteredUser(sender.getNode()))
          && IQ.Type.get == type) {
        // If anonymous user asks for his roster or roster service is disabled then
        // return an empty roster
        IQ reply = IQ.createResultIQ(packet);
        reply.setChildElement("query", "jabber:iq:roster");
        return reply;
      }
      if (!localServer.isLocal(sender)) {
        // Sender belongs to a remote server so discard this IQ request
        Log.warn("Discarding IQ roster packet of remote user: " + packet);
        return null;
      }

      Roster cachedRoster = userManager.getUser(sender.getNode()).getRoster();
      if (IQ.Type.get == type) {
        returnPacket = cachedRoster.getReset();
        returnPacket.setType(IQ.Type.result);
        returnPacket.setTo(sender);
        returnPacket.setID(packet.getID());
        // Force delivery of the response because we need to trigger
        // a presence probe from all contacts
        deliverer.deliver(returnPacket);
        returnPacket = null;
      } else if (IQ.Type.set == type) {

        for (org.xmpp.packet.Roster.Item item : packet.getItems()) {
          if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
            removeItem(cachedRoster, packet.getFrom(), item);
          } else {
            if (cachedRoster.isRosterItem(item.getJID())) {
              // existing item
              RosterItem cachedItem = cachedRoster.getRosterItem(item.getJID());
              cachedItem.setAsCopyOf(item);
              cachedRoster.updateRosterItem(cachedItem);
            } else {
              // new item
              cachedRoster.createRosterItem(item);
            }
          }
        }
        returnPacket = IQ.createResultIQ(packet);
      }
    } catch (UserNotFoundException e) {
      throw new UnauthorizedException(e);
    }

    return returnPacket;
  }
 /**
  * Obtain the roster for the given address or null if the address doesn't have a roster.
  *
  * @param address The address to check
  * @return The roster or null if the address is not managed on the server
  */
 private Roster getRoster(JID address) {
   String username;
   Roster roster = null;
   if (localServer.isLocal(address) && userManager.isRegisteredUser(address.getNode())) {
     username = address.getNode();
     try {
       roster = rosterManager.getRoster(username);
     } catch (UserNotFoundException e) {
       // Do nothing
     }
   }
   return roster;
 }
  @Override
  public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ result = IQ.createResultIQ(packet);
    String username = packet.getFrom().getNode();
    if (!serverName.equals(packet.getFrom().getDomain()) || username == null) {
      // Users of remote servers are not allowed to get their "shared groups". Users of
      // remote servers cannot have shared groups in this server.
      // Besides, anonymous users do not belong to shared groups so answer an error
      result.setChildElement(packet.getChildElement().createCopy());
      result.setError(PacketError.Condition.not_allowed);
      return result;
    }

    Collection<Group> groups = rosterManager.getSharedGroups(username);
    Element sharedGroups =
        result.setChildElement("sharedgroup", "http://www.jivesoftware.org/protocol/sharedgroup");
    for (Group sharedGroup : groups) {
      String displayName = sharedGroup.getProperties().get("sharedRoster.displayName");
      if (displayName != null) {
        sharedGroups.addElement("group").setText(displayName);
      }
    }
    return result;
  }