Search in sources :

Example 16 with Roster

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

the class UserServicePluginNG method updateRosterItem.

/**
	 * Update roster item.
	 *
	 * @param username
	 *            the username
	 * @param rosterJid
	 *            the roster jid
	 * @param rosterItemEntity
	 *            the roster item entity
	 * @throws ServiceException
	 *             the service exception
	 * @throws UserNotFoundException
	 *             the user not found exception
	 * @throws UserAlreadyExistsException
	 *             the user already exists exception
	 * @throws SharedGroupException
	 *             the shared group exception
	 */
public void updateRosterItem(String username, String rosterJid, RosterItemEntity rosterItemEntity) throws ServiceException, UserNotFoundException, UserAlreadyExistsException, SharedGroupException {
    getAndCheckUser(username);
    Roster roster = getUserRoster(username);
    JID jid = new JID(rosterJid);
    RosterItem rosterItem = roster.getRosterItem(jid);
    if (rosterItemEntity.getNickname() != null) {
        rosterItem.setNickname(rosterItemEntity.getNickname());
    }
    if (rosterItemEntity.getGroups() != null) {
        rosterItem.setGroups(rosterItemEntity.getGroups());
    }
    UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
    rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
    roster.updateRosterItem(rosterItem);
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) JID(org.xmpp.packet.JID)

Example 17 with Roster

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

the class BaseTransport method syncLegacyRoster.

/**
     * Sync a user's roster with their legacy contact list.
     *
     * Given a collection of transport buddies, syncs up the user's
     * roster by fixing any nicknames, group assignments, adding and removing
     * roster items, and generally trying to make the jabber roster list
     * assigned to the transport's JID look at much like the legacy buddy
     * list as possible.  This is a very extensive operation.  You do not
     * want to do this very often.  Typically once right after the person
     * has logged into the legacy service.
     *
     * @param userjid JID of user who's roster we are syncing with.
     * @param legacyitems List of TransportBuddy's to be synced.
     * @throws UserNotFoundException if userjid not found.
     */
public void syncLegacyRoster(JID userjid, Collection<B> legacyitems) throws UserNotFoundException {
    Log.debug("Syncing Legacy Roster: " + legacyitems);
    try {
        Roster roster = rosterManager.getRoster(userjid.getNode());
        // Lets lock down the roster from update notifications if there's an active session.
        try {
            TransportSession<B> session = sessionManager.getSession(userjid.getNode());
            session.lockRoster();
        } catch (NotFoundException e) {
        // No active session?  Then no problem.
        }
        // First thing first, we want to build ourselves an easy mapping.
        Map<JID, TransportBuddy> legacymap = new HashMap<JID, TransportBuddy>();
        for (TransportBuddy buddy : legacyitems) {
            //                Log.debug("ROSTERSYNC: Mapping "+buddy.getName());
            legacymap.put(buddy.getJID(), buddy);
        }
        // Now, lets go through the roster and see what matches up.
        for (RosterItem ri : roster.getRosterItems()) {
            if (!ri.getJid().getDomain().equals(this.jid.getDomain())) {
                // Not our contact to care about.
                continue;
            }
            if (ri.getJid().getNode() == null) {
                // This is a transport instance, lets leave it alone.
                continue;
            }
            JID jid = new JID(ri.getJid().toBareJID());
            if (legacymap.containsKey(jid)) {
                Log.debug("ROSTERSYNC: We found, updating " + jid.toString());
                // Ok, matched a legacy to jabber roster item
                // Lets update if there are differences
                TransportBuddy buddy = legacymap.get(jid);
                try {
                    if (buddy.getAskType() != null && buddy.getSubType() != null) {
                        this.addOrUpdateRosterItem(userjid, buddy.getName(), buddy.getNickname(), buddy.getGroups(), buddy.getSubType(), buddy.getAskType());
                    } else {
                        this.addOrUpdateRosterItem(userjid, buddy.getName(), buddy.getNickname(), buddy.getGroups());
                    }
                } catch (UserNotFoundException e) {
                    Log.debug("Failed updating roster item", e);
                }
                legacymap.remove(jid);
            } else {
                Log.debug("ROSTERSYNC: We did not find, removing " + jid.toString());
                // This person is apparantly no longer in the legacy roster.
                try {
                    this.removeFromRoster(userjid, jid);
                } catch (UserNotFoundException e) {
                    Log.debug("Failed removing roster item", e);
                }
            }
        }
        // Ok, we should now have only new items from the legacy roster
        for (TransportBuddy buddy : legacymap.values()) {
            Log.debug("ROSTERSYNC: We have new, adding " + buddy.getName());
            try {
                this.addOrUpdateRosterItem(userjid, buddy.getName(), buddy.getNickname(), buddy.getGroups());
            } catch (UserNotFoundException e) {
                Log.debug("Failed adding new roster item", e);
            }
        }
        // All done, lets unlock the roster.
        try {
            TransportSession<B> session = sessionManager.getSession(userjid.getNode());
            session.unlockRoster();
        } catch (NotFoundException e) {
        // No active session?  Then no problem.
        }
    } catch (UserNotFoundException e) {
        throw new UserNotFoundException("Could not find roster for " + userjid.toString());
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Roster(org.jivesoftware.openfire.roster.Roster) TransportBuddy(net.sf.kraken.roster.TransportBuddy) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 18 with Roster

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

the class JustMarriedPlugin method addNewUserToOthersRoster.

private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) {
    otherItem.getJid();
    UserManager userManager = UserManager.getInstance();
    // Is this user registered with our OF server?
    String username = otherItem.getJid().getNode();
    if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
        try {
            User otherUser = userManager.getUser(username);
            Roster otherRoster = otherUser.getRoster();
            RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
            try {
                if (!oldUserOnOthersRoster.isOnlyShared()) {
                    RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
                    justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
                    justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
                    justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
                    otherRoster.updateRosterItem(justCreated);
                }
            } catch (UserAlreadyExistsException e) {
                Log.error("Could not create roster item for user " + newUser.getUsername(), e);
            } catch (SharedGroupException e) {
                Log.error(e);
            }
        } catch (UserNotFoundException e) {
            Log.error("Could not create roster item for user " + newUser.getUsername() + " because it is a contact from a shared group", e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) User(org.jivesoftware.openfire.user.User) Roster(org.jivesoftware.openfire.roster.Roster) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 19 with Roster

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

the class BaseTransport method notifyRosterOffline.

/**
     * Sends offline packets for an entire roster to the target user.
     *
     * This function will run through the roster of the specified user and send offline
     * presence packets for each roster item.   This is typically used when a user logs
     * off so that all of the associated roster items appear offline.  This does not send
     * the unavailable presence for the transport itself.
     *
     * @param jid JID of user whose roster we want to clean up.
     * @throws UserNotFoundException if the user is not found.
     * @deprecated Use net.sf.kraken.roster.TransportBuddyManager#sendOfflineForAllAvailablePresences(JID)
     */
@Deprecated
public void notifyRosterOffline(JID jid) throws UserNotFoundException {
    try {
        Roster roster = rosterManager.getRoster(jid.getNode());
        for (RosterItem ri : roster.getRosterItems()) {
            if (ri.getJid().getNode() != null && ri.getJid().getDomain().equals(this.jid.getDomain())) {
                Presence p = new Presence(Presence.Type.unavailable);
                p.setTo(jid);
                p.setFrom(ri.getJid());
                sendPacket(p);
            }
        }
    } catch (UserNotFoundException e) {
        throw new UserNotFoundException("Unable to find roster.");
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Roster(org.jivesoftware.openfire.roster.Roster)

Example 20 with Roster

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

the class IQPEPHandler method addSubscriptionForRosterItems.

/**
 * Populates the PEPService instance with subscriptions. The subscriptions that
 * are added to the PEPService are based on the roster of the owner of the PEPService:
 * every entity that's subscribed to the presence of the owner, is added as a
 * subscriber of the PEPService.
 *
 * This method adds, but does not remove of update existing subscriptions.
 *
 * @param pepService The PEPService to be populated with subscriptions.
 */
public void addSubscriptionForRosterItems(final PEPService pepService) {
    try {
        final RosterManager rm = XMPPServer.getInstance().getRosterManager();
        final Roster roster = rm.getRoster(pepService.getAddress().getNode());
        for (final RosterItem item : roster.getRosterItems()) {
            if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_FROM) {
                createSubscriptionToPEPService(pepService, item.getJid(), pepService.getAddress());
            }
        }
    } catch (UserNotFoundException e) {
        Log.warn("Attempting to manage subscriptions for a PEP node that is associated to an unrecognized user: {}", pepService.getAddress(), e);
    }
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) RosterManager(org.jivesoftware.openfire.roster.RosterManager)

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