Search in sources :

Example 31 with Roster

use of org.jivesoftware.openfire.roster.Roster in project Openfire by igniterealtime.

the class UserCreationPlugin method populateRosters.

public void populateRosters(String userPrefix, int from, int total, int usersPerRoster) {
    XMPPServer server = XMPPServer.getInstance();
    RosterManager rosterManager = server.getRosterManager();
    int batchTotal = total / usersPerRoster;
    System.out.println("Total batches of users: " + batchTotal);
    for (int batchNumber = 0; batchNumber < batchTotal; batchNumber++) {
        System.out.println("Current batch: " + batchNumber + ". Users: " + batchNumber * usersPerRoster + " - " + ((batchNumber * usersPerRoster) + usersPerRoster));
        // Add rosters items between connected users
        for (int i = (batchNumber * usersPerRoster) + from; i < (batchNumber * usersPerRoster) + usersPerRoster + from; i++) {
            String username = userPrefix + i;
            Roster roster;
            try {
                roster = rosterManager.getRoster(username);
            } catch (UserNotFoundException e) {
                continue;
            }
            if (roster.getRosterItems().size() >= usersPerRoster) {
                // Roster already populated. Skip it.
                continue;
            }
            for (int j = (batchNumber * usersPerRoster) + from; j < (batchNumber * usersPerRoster) + usersPerRoster + from; j++) {
                if (i == j) {
                    continue;
                }
                try {
                    Roster recipientRoster = rosterManager.getRoster(userPrefix + j);
                    manageSub(server.createJID(userPrefix + j, null), true, Presence.Type.subscribe, roster);
                    manageSub(server.createJID(username, null), false, Presence.Type.subscribe, recipientRoster);
                    manageSub(server.createJID(userPrefix + j, null), true, Presence.Type.subscribed, roster);
                    manageSub(server.createJID(username, null), false, Presence.Type.subscribed, recipientRoster);
                } catch (UserNotFoundException e) {
                // Ignore
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    System.out.println("Rosters populated with " + usersPerRoster + " contacts.");
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) XMPPServer(org.jivesoftware.openfire.XMPPServer) Roster(org.jivesoftware.openfire.roster.Roster) RosterManager(org.jivesoftware.openfire.roster.RosterManager) DocumentException(org.dom4j.DocumentException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 32 with Roster

use of org.jivesoftware.openfire.roster.Roster in project Openfire by igniterealtime.

the class UserServicePlugin method updateRosterItem.

/**
	 * Update roster item for specified user
	 *
	 * @param username
	 *            the username of the local user to update roster item for.
	 * @param itemJID
	 *            the JID of the roster item to be updated.
	 * @param itemName
	 *            the nickname of the roster item.
	 * @param subscription
	 *            the type of subscription of the roster item. Possible values
	 *            are: -1(remove), 0(none), 1(to), 2(from), 3(both).
	 * @param groupNames
	 *            the name of a group.
	 * @throws UserNotFoundException
	 *             if the user does not exist in the local server or roster item
	 *             does not exist.
	 * @throws SharedGroupException
	 *             if roster item cannot be added to a shared group.
	 */
public void updateRosterItem(String username, String itemJID, String itemName, String subscription, String groupNames) throws UserNotFoundException, SharedGroupException {
    getUser(username);
    Roster r = rosterManager.getRoster(username);
    JID j = new JID(itemJID);
    RosterItem ri = r.getRosterItem(j);
    List<String> groups = new ArrayList<String>();
    if (groupNames != null) {
        StringTokenizer tkn = new StringTokenizer(groupNames, ",");
        while (tkn.hasMoreTokens()) {
            groups.add(tkn.nextToken());
        }
    }
    ri.setGroups(groups);
    ri.setNickname(itemName);
    if (subscription == null) {
        subscription = "0";
    }
    ri.setSubStatus(RosterItem.SubType.getTypeFromInt(Integer.parseInt(subscription)));
    r.updateRosterItem(ri);
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) StringTokenizer(java.util.StringTokenizer) Roster(org.jivesoftware.openfire.roster.Roster) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList)

Example 33 with Roster

use of org.jivesoftware.openfire.roster.Roster in project Openfire by igniterealtime.

the class UserServicePluginNG method addRosterItem.

/**
	 * Adds the roster item.
	 *
	 * @param username
	 *            the username
	 * @param rosterItemEntity
	 *            the roster item entity
	 * @throws ServiceException
	 *             the service exception
	 * @throws UserAlreadyExistsException
	 *             the user already exists exception
	 * @throws SharedGroupException
	 *             the shared group exception
	 * @throws UserNotFoundException
	 *             the user not found exception
	 */
public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException, UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
    Roster roster = getUserRoster(username);
    if (rosterItemEntity.getJid() == null) {
        throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
    }
    JID jid = new JID(rosterItemEntity.getJid());
    try {
        roster.getRosterItem(jid);
        throw new UserAlreadyExistsException(jid.toBareJID());
    } catch (UserNotFoundException e) {
    // Roster item does not exist. Try to add it.
    }
    if (roster != null) {
        RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(), rosterItemEntity.getGroups(), false, true);
        UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
        rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
        roster.updateRosterItem(rosterItem);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) ServiceException(org.jivesoftware.openfire.exceptions.ServiceException) JID(org.xmpp.packet.JID) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 34 with Roster

use of org.jivesoftware.openfire.roster.Roster in project Openfire by igniterealtime.

the class JustMarriedPlugin method copyRoster.

private static void copyRoster(User currentUser, User newUser, String currentUserName) {
    Roster newRoster = newUser.getRoster();
    Roster currentRoster = currentUser.getRoster();
    for (RosterItem item : currentRoster.getRosterItems()) {
        try {
            List<String> groups = item.getGroups();
            RosterItem justCreated = newRoster.createRosterItem(item.getJid(), item.getNickname(), groups, true, true);
            justCreated.setAskStatus(item.getAskStatus());
            justCreated.setRecvStatus(item.getRecvStatus());
            justCreated.setSubStatus(item.getSubStatus());
            for (Group gr : item.getSharedGroups()) {
                justCreated.addSharedGroup(gr);
            }
            for (Group gr : item.getInvisibleSharedGroups()) {
                justCreated.addInvisibleSharedGroup(gr);
            }
            newRoster.updateRosterItem(justCreated);
            addNewUserToOthersRoster(newUser, item, currentUserName);
        } catch (UserAlreadyExistsException e) {
            Log.error("Could not create roster item for user " + item.getJid(), e);
        } catch (SharedGroupException e) {
            Log.error("Could not create roster item for user " + item.getJid() + " because it is a contact from a shared group", e);
        } catch (UserNotFoundException e) {
            Log.error("Could not update Roster item for user " + newUser.getName() + " because it was not properly created.", e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Group(org.jivesoftware.openfire.group.Group) Roster(org.jivesoftware.openfire.roster.Roster) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 35 with Roster

use of org.jivesoftware.openfire.roster.Roster in project Openfire by igniterealtime.

the class BaseTransport method cleanUpRoster.

/**
     * Cleans a roster of entries related to this transport.
     *
     * This function will run through the roster of the specified user and clean up any
     * entries that share the domain of this transport.  Depending on the removeNonPersistent
     * option, it will either leave or keep the non-persistent 'contact' entries.
     *
     * @param jid JID of the user whose roster we want to clean up.
     * @param leaveDomain If set, we do not touch the roster item associated with the domain itself.
     * @param removeNonPersistent If set, we will also remove non-persistent items.
     * @throws UserNotFoundException if the user is not found.
     */
public void cleanUpRoster(JID jid, Boolean leaveDomain, Boolean removeNonPersistent) throws UserNotFoundException {
    try {
        Roster roster = rosterManager.getRoster(jid.getNode());
        // Lets lock down the roster from update notifications if there's an active session.
        try {
            TransportSession<B> session = sessionManager.getSession(jid.getNode());
            session.lockRoster();
        } catch (NotFoundException e) {
        // No active session?  Then no problem.
        }
        for (RosterItem ri : roster.getRosterItems()) {
            if (ri.getJid().getDomain().equals(this.jid.getDomain())) {
                if (ri.isShared()) {
                    // Is a shared item we can't really touch.
                    continue;
                }
                if (!removeNonPersistent && ri.getID() == 0) {
                    // Is a non-persistent roster item.
                    continue;
                }
                if (leaveDomain && ri.getJid().getNode() == null) {
                    // The actual transport domain item.
                    continue;
                }
                try {
                    Log.debug("Cleaning up roster entry " + ri.getJid().toString());
                    roster.deleteRosterItem(ri.getJid(), false);
                } catch (Exception e) {
                    Log.debug("Error removing roster item: " + ri.toString(), e);
                }
            }
        }
        // All done, lets unlock the roster.
        try {
            TransportSession<B> session = sessionManager.getSession(jid.getNode());
            session.unlockRoster();
        } catch (NotFoundException e) {
        // No active session?  Then no problem.
        }
    } catch (UserNotFoundException e) {
        throw new UserNotFoundException("Unable to find roster.");
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Roster(org.jivesoftware.openfire.roster.Roster) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) NotFoundException(org.jivesoftware.util.NotFoundException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Aggregations

Roster (org.jivesoftware.openfire.roster.Roster)42 RosterItem (org.jivesoftware.openfire.roster.RosterItem)29 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)27 JID (org.xmpp.packet.JID)20 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)13 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)9 ArrayList (java.util.ArrayList)7 StringTokenizer (java.util.StringTokenizer)4 PacketException (org.jivesoftware.openfire.PacketException)4 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)4 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)4 Presence (org.xmpp.packet.Presence)4 NotFoundException (org.jivesoftware.util.NotFoundException)3 IQ (org.xmpp.packet.IQ)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Email (net.sf.jml.Email)2 XMPPServer (org.jivesoftware.openfire.XMPPServer)2 ServiceException (org.jivesoftware.openfire.exceptions.ServiceException)2 Group (org.jivesoftware.openfire.group.Group)2 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)2