Search in sources :

Example 1 with RosterItem

use of org.jivesoftware.openfire.roster.RosterItem 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)

Example 2 with RosterItem

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

the class Xep227Exporter method importUser.

/**
   * @param user
   * @param previousDomain
   * @param isUserProviderReadOnly
   * @param invalidUsers
   */
@SuppressWarnings("unchecked")
private void importUser(Element user, String previousDomain, boolean isUserProviderReadOnly, List<String> invalidUsers) {
    Log.debug("importUser");
    List<RosterItem> rosterItems = new ArrayList<RosterItem>();
    List<OfflineMessage> offlineMessages = new ArrayList<OfflineMessage>();
    Element vCardElement = null;
    String userName = user.attributeValue(NAME_NAME);
    String password = user.attributeValue(PASSWORD_NAME);
    Iterator<Element> userElements = user.elementIterator();
    while (userElements.hasNext()) {
        Element userElement = userElements.next();
        String nameElement = userElement.getName();
        if (OFFLINE_MESSAGES_ELEMENT_NAME.equals(nameElement)) {
            importOffLineMessages(userElement, offlineMessages);
        } else if (QUERY_ELEMENT_NAME.equals(nameElement) && JABBER_IQ_ROSTER_NS.equals(userElement.getNamespaceURI())) {
            importUserRoster(userElement, rosterItems, previousDomain);
        } else if (V_CARD_NAME.equals(nameElement) && VCARD_TEMP_NS.equals(userElement.getNamespaceURI())) {
            vCardElement = userElement;
        }
    }
    if (userName != null) {
        try {
            userName = Stringprep.nodeprep(userName);
            if (!isUserProviderReadOnly && (password != null)) {
                userManager.createUser(userName, password, userName, null);
            }
            if (!isUserProviderReadOnly && vCardElement != null) {
                try {
                    vCardManager.setVCard(userName, vCardElement);
                } catch (Exception e) {
                    Log.warn("Error updating VCard:" + userName + ":" + e.getMessage());
                    Log.debug("", e);
                }
            }
            // 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);
            }
            for (OfflineMessage offlineMessage : offlineMessages) {
                offlineMessagesStore.addMessage(offlineMessage);
            }
        } 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);
        } catch (Exception e) {
            Log.warn("Error updating User:" + userName + ":" + e.getLocalizedMessage());
            invalidUsers.add(userName);
        }
    }
}
Also used : StringprepException(gnu.inet.encoding.StringprepException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) OfflineMessage(org.jivesoftware.openfire.OfflineMessage) StringprepException(gnu.inet.encoding.StringprepException) ParseException(java.text.ParseException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 3 with RosterItem

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

the class UserServicePlugin method addRosterItem.

/**
	 * Add new roster item for specified user
	 *
	 * @param username
	 *            the username of the local user to add roster item to.
	 * @param itemJID
	 *            the JID of the roster item to be added.
	 * @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 to place contact into.
	 * @throws UserNotFoundException
	 *             if the user does not exist in the local server.
	 * @throws UserAlreadyExistsException
	 *             if roster item with the same JID already exists.
	 * @throws SharedGroupException
	 *             if roster item cannot be added to a shared group.
	 */
public void addRosterItem(String username, String itemJID, String itemName, String subscription, String groupNames) throws UserNotFoundException, UserAlreadyExistsException, SharedGroupException {
    getUser(username);
    Roster r = rosterManager.getRoster(username);
    JID j = new JID(itemJID);
    try {
        r.getRosterItem(j);
        throw new UserAlreadyExistsException(j.toBareJID());
    } catch (UserNotFoundException e) {
    // Roster item does not exist. Try to add it.
    }
    if (r != null) {
        List<String> groups = new ArrayList<String>();
        if (groupNames != null) {
            StringTokenizer tkn = new StringTokenizer(groupNames, ",");
            while (tkn.hasMoreTokens()) {
                groups.add(tkn.nextToken());
            }
        }
        RosterItem ri = r.createRosterItem(j, itemName, groups, false, true);
        if (subscription == null) {
            subscription = "0";
        }
        ri.setSubStatus(RosterItem.SubType.getTypeFromInt(Integer.parseInt(subscription)));
        r.updateRosterItem(ri);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) 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) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 4 with RosterItem

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

the class UserServicePluginNG method getRosterEntities.

/**
	 * Gets the roster entities.
	 *
	 * @param username
	 *            the username
	 * @return the roster entities
	 * @throws ServiceException
	 *             the service exception
	 */
public RosterEntities getRosterEntities(String username) throws ServiceException {
    Roster roster = getUserRoster(username);
    List<RosterItemEntity> rosterEntities = new ArrayList<RosterItemEntity>();
    for (RosterItem rosterItem : roster.getRosterItems()) {
        RosterItemEntity rosterItemEntity = new RosterItemEntity(rosterItem.getJid().toBareJID(), rosterItem.getNickname(), rosterItem.getSubStatus().getValue());
        rosterItemEntity.setGroups(rosterItem.getGroups());
        rosterEntities.add(rosterItemEntity);
    }
    return new RosterEntities(rosterEntities);
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) RosterEntities(org.jivesoftware.openfire.entity.RosterEntities) ArrayList(java.util.ArrayList) RosterItemEntity(org.jivesoftware.openfire.entity.RosterItemEntity)

Example 5 with RosterItem

use of org.jivesoftware.openfire.roster.RosterItem 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)

Aggregations

RosterItem (org.jivesoftware.openfire.roster.RosterItem)37 Roster (org.jivesoftware.openfire.roster.Roster)27 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)26 JID (org.xmpp.packet.JID)18 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)11 ArrayList (java.util.ArrayList)10 Element (org.dom4j.Element)8 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)6 User (org.jivesoftware.openfire.user.User)5 StringTokenizer (java.util.StringTokenizer)4 IQ (org.xmpp.packet.IQ)4 StringprepException (gnu.inet.encoding.StringprepException)3 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)3 ParseException (java.text.ParseException)2 Email (net.sf.jml.Email)2 DefaultElement (org.dom4j.tree.DefaultElement)2 XMPPServer (org.jivesoftware.openfire.XMPPServer)2 Group (org.jivesoftware.openfire.group.Group)2 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)2 UserManager (org.jivesoftware.openfire.user.UserManager)2