Search in sources :

Example 41 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class Roster method addSharedUser.

/**
     * Adds a new contact that belongs to a certain list of groups to the roster. Depending on
     * the contact's groups and this user's groups, the presence subscription of the roster item may
     * vary.
     *
     * @param addedUser the new contact to add to the roster
     * @param groups    the groups where the contact is a member
     */
void addSharedUser(JID addedUser, Collection<Group> groups, Group addedGroup) {
    boolean newItem = false;
    RosterItem item = null;
    try {
        // Get the RosterItem for the *local* user to add
        item = getRosterItem(addedUser);
        newItem = false;
    } catch (UserNotFoundException e) {
        try {
            // Create a new RosterItem for this new user
            String nickname = UserNameManager.getUserName(addedUser);
            item = new RosterItem(addedUser, RosterItem.SUB_BOTH, RosterItem.ASK_NONE, RosterItem.RECV_NONE, nickname, null);
            // Add the new item to the list of items
            rosterItems.put(item.getJid().toBareJID(), item);
            newItem = true;
        } catch (UserNotFoundException ex) {
            Log.error("Couldn't find a user with username (" + addedUser + ")");
        }
    }
    // Update the subscription of the item **based on the item groups**
    Collection<Group> userGroups = GroupManager.getInstance().getGroups(getUserJID());
    // that is mutually visible with a shared group of the new roster item
    if (rosterManager.hasMutualVisibility(getUsername(), userGroups, addedUser, groups)) {
        item.setSubStatus(RosterItem.SUB_BOTH);
        for (Group group : groups) {
            if (rosterManager.isGroupVisible(group, getUserJID())) {
                // Add the shared group to the list of shared groups
                item.addSharedGroup(group);
            }
        }
        // BOTH subscription
        for (Group group : userGroups) {
            if (!group.isUser(addedUser) && rosterManager.isGroupVisible(group, addedUser)) {
                // Add the shared group to the list of invisible shared groups
                item.addInvisibleSharedGroup(group);
            }
        }
    } else {
        // If an item already exists then take note of the old subscription status
        RosterItem.SubType prevSubscription = null;
        if (!newItem) {
            prevSubscription = item.getSubStatus();
        }
        // Assume by default that the contact has subscribed from the presence of
        // this user
        item.setSubStatus(RosterItem.SUB_FROM);
        // Check if the user may see the new contact in a shared group
        for (Group group : groups) {
            if (rosterManager.isGroupVisible(group, getUserJID())) {
                // Add the shared group to the list of shared groups
                item.addSharedGroup(group);
                item.setSubStatus(RosterItem.SUB_TO);
            }
        }
        if (item.getSubStatus() == RosterItem.SUB_FROM) {
            item.addInvisibleSharedGroup(addedGroup);
        }
        // changed to BOTH based on the old and new subscription status
        if (prevSubscription != null) {
            if (prevSubscription == RosterItem.SUB_TO && item.getSubStatus() == RosterItem.SUB_FROM) {
                item.setSubStatus(RosterItem.SUB_BOTH);
            } else if (prevSubscription == RosterItem.SUB_FROM && item.getSubStatus() == RosterItem.SUB_TO) {
                item.setSubStatus(RosterItem.SUB_BOTH);
            }
        }
    }
    // Optimization: Check if we do not need to keep the item in memory
    if (item.isOnlyShared() && item.getSubStatus() == RosterItem.SUB_FROM) {
        // Remove from memory and do nothing else
        rosterItems.remove(item.getJid().toBareJID());
        // Cache information about shared contacts with subscription status FROM
        implicitFrom.put(item.getJid().toBareJID(), item.getInvisibleSharedGroupsNames());
    } else {
        // Remove from list of shared contacts with status FROM (if any)
        implicitFrom.remove(item.getJid().toBareJID());
        // Ensure that the item is an explicit roster item
        rosterItems.put(item.getJid().toBareJID(), item);
        // Brodcast to all the user resources of the updated roster item
        broadcast(item, true);
        // Probe the presence of the new group user
        if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_TO) {
            probePresence(item.getJid());
        }
    }
    if (newItem) {
        // Fire event indicating that a roster item has been added
        RosterEventDispatcher.contactAdded(this, item);
    } else {
        // Fire event indicating that a roster item has been updated
        RosterEventDispatcher.contactUpdated(this, item);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Group(org.jivesoftware.openfire.group.Group)

Example 42 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class RosterManager method userDeleting.

@Override
public void userDeleting(User user, Map<String, Object> params) {
    // Shared public groups that have a presence subscription of type FROM
    // for the deleted user should no longer have a reference to the deleted user
    JID userJID = server.createJID(user.getUsername(), null);
    // of type FROM for the new user
    for (Group group : getPublicSharedGroups()) {
        // Get group members of public group
        Collection<JID> users = new HashSet<>(group.getMembers());
        users.addAll(group.getAdmins());
        // Update the roster of each group member to include a subscription of type FROM
        for (JID userToUpdate : users) {
            // Get the roster to update
            Roster roster = null;
            if (server.isLocal(userToUpdate)) {
                // Check that the user exists, if not then continue with the next user
                try {
                    UserManager.getInstance().getUser(userToUpdate.getNode());
                } catch (UserNotFoundException e) {
                    continue;
                }
                roster = rosterCache.get(userToUpdate.getNode());
            }
            // Only update rosters in memory
            if (roster != null) {
                roster.deleteSharedUser(group, userJID);
            }
            if (!server.isLocal(userToUpdate)) {
                // Unsusbcribe from the presence of the remote user. This is only necessary for
                // remote users and may only work with remote users that **automatically**
                // accept presence subscription requests
                sendSubscribeRequest(userJID, userToUpdate, false);
            }
        }
    }
    deleteRoster(userJID);
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Group(org.jivesoftware.openfire.group.Group) JID(org.xmpp.packet.JID)

Example 43 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class WorkgroupManager method processPacket.

public void processPacket(Packet packet) {
    try {
        // Check if the packet is a disco request
        if (packet instanceof IQ) {
            if (process((IQ) packet)) {
                return;
            }
        }
        // was found then let the workgroup process the packet
        try {
            Workgroup workgroup = getWorkgroup(packet.getTo());
            workgroup.process(packet);
        } catch (UserNotFoundException e) {
            // above situations
            if (packet instanceof Presence) {
                if (((Presence) packet).getType() == Presence.Type.error) {
                    // Skip Presence packets of type error
                    return;
                }
                Presence reply = new Presence();
                reply.setID(packet.getID());
                reply.setTo(packet.getFrom());
                reply.setFrom(packet.getTo());
                reply.setError(PacketError.Condition.not_authorized);
                send(reply);
            } else if (packet instanceof IQ) {
                if (((IQ) packet).getType() == IQ.Type.error) {
                    // Skip IQ packets of type error
                    return;
                }
                IQ reply = IQ.createResultIQ((IQ) packet);
                reply.setChildElement(((IQ) packet).getChildElement().createCopy());
                reply.setError(PacketError.Condition.not_authorized);
                send(reply);
            } else {
                if (((Message) packet).getType() == Message.Type.error) {
                    // Skip Message packets of type error
                    return;
                }
                Message reply = new Message();
                reply.setID(packet.getID());
                reply.setTo(packet.getFrom());
                reply.setFrom(packet.getTo());
                reply.setError(PacketError.Condition.not_authorized);
                send(reply);
            }
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Message(org.xmpp.packet.Message) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence) CreateWorkgroup(org.jivesoftware.openfire.fastpath.commands.CreateWorkgroup) DeleteWorkgroup(org.jivesoftware.openfire.fastpath.commands.DeleteWorkgroup) ComponentException(org.xmpp.component.ComponentException) SQLException(java.sql.SQLException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 44 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class WorkgroupUtils method updateWorkgroup.

public static String updateWorkgroup(String workgroupName, String displayName, String description, int maxSize, int minSize, long requestTimeout, long offerTimeout) {
    final WorkgroupManager workgroupManager = WorkgroupManager.getInstance();
    Workgroup workgroup;
    try {
        workgroup = workgroupManager.getWorkgroup(new JID(workgroupName));
    } catch (UserNotFoundException e) {
        return getUpdateMessage(false, "The JID specified is invalid.");
    }
    workgroup.setDisplayName(displayName);
    workgroup.setDescription(description);
    if (maxSize < minSize) {
        return getUpdateMessage(false, "Max size must be greater or equal to min size.");
    }
    workgroup.setMaxChats(maxSize);
    workgroup.setMinChats(minSize);
    workgroup.setRequestTimeout(requestTimeout);
    workgroup.setOfferTimeout(offerTimeout);
    return getUpdateMessage(true, "Workgroup has been updated");
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Workgroup(org.jivesoftware.xmpp.workgroup.Workgroup) WorkgroupManager(org.jivesoftware.xmpp.workgroup.WorkgroupManager)

Example 45 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class OpenfireExporter method importUsers.

/*
   * (non-Javadoc)
   * 
   * @see
   * org.jivesoftware.openfire.plugin.InExporter#importUsers(org.dom4j.Document,
   * java.lang.String)
   */
@SuppressWarnings("unchecked")
private List<String> importUsers(Document document, String previousDomain, boolean isUserProviderReadOnly) {
    Log.debug("importUsers");
    List<String> invalidUsers = new ArrayList<String>();
    Element users = document.getRootElement();
    Iterator<Element> usersIter = users.elementIterator("User");
    while (usersIter.hasNext()) {
        Element user = usersIter.next();
        String userName = null;
        String password = null;
        String email = null;
        String name = null;
        List<RosterItem> rosterItems = new ArrayList<RosterItem>();
        Iterator<Element> userElements = user.elementIterator();
        while (userElements.hasNext()) {
            Element userElement = userElements.next();
            String nameElement = userElement.getName();
            if ("Username".equals(nameElement)) {
                userName = userElement.getText();
            } else if ("Password".equals(nameElement)) {
                password = userElement.getText();
            } else if ("Name".equals(nameElement)) {
                name = userElement.getText();
            } else if ("Email".equals(nameElement)) {
                email = userElement.getText();
            } else if ("Roster".equals(nameElement)) {
                Iterator<Element> rosterIter = userElement.elementIterator("Item");
                while (rosterIter.hasNext()) {
                    Element rosterElement = rosterIter.next();
                    String jid = rosterElement.attributeValue("jid");
                    String askstatus = rosterElement.attributeValue("askstatus");
                    String recvstatus = rosterElement.attributeValue("recvstatus");
                    String substatus = rosterElement.attributeValue("substatus");
                    String nickname = rosterElement.attributeValue("name");
                    List<String> groups = new ArrayList<String>();
                    Iterator<Element> groupIter = rosterElement.elementIterator("Group");
                    while (groupIter.hasNext()) {
                        Element group = groupIter.next();
                        String groupName = group.getText();
                        if (groupName != null && groupName.trim().length() > 0) {
                            groups.add(groupName);
                        }
                    }
                    // used for migration
                    if (previousDomain != null) {
                        jid = jid.replace(previousDomain, serverName);
                    }
                    rosterItems.add(new RosterItem(new JID(jid), RosterItem.SubType.getTypeFromInt(Integer.parseInt(substatus)), RosterItem.AskType.getTypeFromInt(Integer.parseInt(askstatus)), RosterItem.RecvType.getTypeFromInt(Integer.parseInt(recvstatus)), nickname, groups));
                }
            }
        }
        if ((userName != null) && (password != null)) {
            try {
                userName = Stringprep.nodeprep(userName);
                if (isUserProviderReadOnly) {
                    userManager.createUser(userName, password, name, email);
                }
                // Check to see user exists before adding their roster, this is for
                // read-only user providers.
                userManager.getUser(userName);
                for (RosterItem ri : rosterItems) {
                    rosterItemProvider.createItem(userName, ri);
                }
            } catch (StringprepException se) {
                Log.info("Invalid username " + userName);
                invalidUsers.add(userName);
            } catch (UserAlreadyExistsException e) {
                Log.info("User already exists " + userName);
                invalidUsers.add(userName);
            } catch (UserNotFoundException e) {
                Log.info("User not found " + userName);
                invalidUsers.add(userName);
            }
        }
    }
    return invalidUsers;
}
Also used : StringprepException(gnu.inet.encoding.StringprepException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Aggregations

UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)118 JID (org.xmpp.packet.JID)50 Element (org.dom4j.Element)28 Roster (org.jivesoftware.openfire.roster.Roster)27 RosterItem (org.jivesoftware.openfire.roster.RosterItem)26 User (org.jivesoftware.openfire.user.User)25 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)23 IQ (org.xmpp.packet.IQ)15 ArrayList (java.util.ArrayList)14 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)12 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)11 Group (org.jivesoftware.openfire.group.Group)10 UserManager (org.jivesoftware.openfire.user.UserManager)10 Workgroup (org.jivesoftware.xmpp.workgroup.Workgroup)10 Presence (org.xmpp.packet.Presence)10 NotFoundException (org.jivesoftware.util.NotFoundException)9 SQLException (java.sql.SQLException)8 List (java.util.List)8 IOException (java.io.IOException)7 Connection (java.sql.Connection)7