Esempio n. 1
0
  /**
   * Set the current groups for the item.
   *
   * @param groups The new lists of groups the item belongs to.
   * @throws org.jivesoftware.openfire.SharedGroupException if trying to remove shared group.
   */
  public void setGroups(List<String> groups) throws SharedGroupException {
    if (groups == null) {
      this.groups = new LinkedList<String>();
    } else {
      // Raise an error if the user is trying to remove the item from a shared group
      for (Group group : getSharedGroups()) {
        // Get the display name of the group
        String groupName = group.getProperties().get("sharedRoster.displayName");
        // Check if the group has been removed from the new groups list
        if (!groups.contains(groupName)) {
          throw new SharedGroupException("Cannot remove item from shared group");
        }
      }

      // Remove shared groups from the param
      Collection<Group> existingGroups = GroupManager.getInstance().getSharedGroups();
      for (Iterator<String> it = groups.iterator(); it.hasNext(); ) {
        String groupName = it.next();
        try {
          // Optimistic approach for performance reasons. Assume first that the shared
          // group name is the same as the display name for the shared roster

          // Check if exists a shared group with this name
          Group group = GroupManager.getInstance().getGroup(groupName);
          // Get the display name of the group
          String displayName = group.getProperties().get("sharedRoster.displayName");
          if (displayName != null && displayName.equals(groupName)) {
            // Remove the shared group from the list (since it exists)
            try {
              it.remove();
            } catch (IllegalStateException e) {
              // Do nothing
            }
          }
        } catch (GroupNotFoundException e) {
          // Check now if there is a group whose display name matches the requested group
          for (Group group : existingGroups) {
            // Get the display name of the group
            String displayName = group.getProperties().get("sharedRoster.displayName");
            if (displayName != null && displayName.equals(groupName)) {
              // Remove the shared group from the list (since it exists)
              try {
                it.remove();
              } catch (IllegalStateException ise) {
                // Do nothing
              }
            }
          }
        }
      }
      this.groups = groups;
    }
  }
  @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;
  }