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;
}
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);
}
}
}
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);
}
}
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);
}
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);
}
Aggregations