use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.
the class PresenceUpdateHandler method directedPresenceSent.
/**
* Notification method sent to this handler when a user has sent a directed
* presence to an entity. If the sender of the presence is local (to this server)
* and the target entity does not belong to the user's roster then update the
* registry of sent directed presences by the user.
*
* @param update the directed Presence sent by the user to an entity.
* @param handlerJID the JID of the handler that will receive/handle/process the sent packet.
* @param jid the receipient specified in the packet to handle.
*/
public void directedPresenceSent(Presence update, JID handlerJID, String jid) {
if (update.getFrom() == null) {
return;
}
if (localServer.isLocal(update.getFrom())) {
boolean keepTrack = false;
String name = update.getFrom().getNode();
if (name != null && !"".equals(name)) {
// Keep track of all directed presences if roster service is disabled
if (!RosterManager.isRosterServiceEnabled()) {
keepTrack = true;
} else {
try {
Roster roster = rosterManager.getRoster(name);
// If the directed presence was sent to an entity that is not in the user's
// roster, keep a registry of this so that when the user goes offline we
// will be able to send the unavailable presence to the entity
RosterItem rosterItem = null;
try {
rosterItem = roster.getRosterItem(update.getTo());
} catch (UserNotFoundException e) {
// Ignore
}
if (rosterItem == null || RosterItem.SUB_NONE == rosterItem.getSubStatus() || RosterItem.SUB_TO == rosterItem.getSubStatus()) {
keepTrack = true;
}
} catch (UserNotFoundException e) {
Log.warn("Presence being sent from unknown user " + name, e);
} catch (PacketException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
} else if (update.getFrom().getResource() != null) {
// Keep always track of anonymous users directed presences
keepTrack = true;
}
if (keepTrack) {
String sender = update.getFrom().toString();
Lock lock = CacheFactory.getLock(sender, directedPresencesCache);
try {
lock.lock();
Collection<DirectedPresence> directedPresences = directedPresencesCache.get(sender);
if (Presence.Type.unavailable.equals(update.getType())) {
if (directedPresences != null) {
// It's a directed unavailable presence
if (routingTable.hasClientRoute(handlerJID)) {
// address of the session) so remove the handler from the map
for (DirectedPresence directedPresence : directedPresences) {
if (directedPresence.getHandler().equals(handlerJID)) {
directedPresences.remove(directedPresence);
break;
}
}
} else {
// unavailable presence
for (DirectedPresence directedPresence : directedPresences) {
if (directedPresence.getHandler().equals(handlerJID)) {
directedPresence.removeReceiver(jid);
if (directedPresence.isEmpty()) {
directedPresences.remove(directedPresence);
}
break;
}
}
}
if (directedPresences.isEmpty()) {
// Remove the user from the registry since the list of directed
// presences is empty
directedPresencesCache.remove(sender);
localDirectedPresences.remove(sender);
} else {
directedPresencesCache.put(sender, directedPresences);
localDirectedPresences.put(sender, directedPresences);
}
}
} else {
if (directedPresences == null) {
// We are using a set to avoid duplicate jids in case the user
// sends several directed presences to the same handler. The Map also
// ensures that if the user sends several presences to the same handler
// we will have only one entry in the Map
directedPresences = new ConcurrentLinkedQueue<>();
}
// Add the handler to the list of handler that processed the directed
// presence sent by the user. This handler will be used to send
// the unavailable presence when the user goes offline
DirectedPresence affectedDirectedPresence = null;
for (DirectedPresence directedPresence : directedPresences) {
if (directedPresence.getHandler().equals(handlerJID)) {
affectedDirectedPresence = directedPresence;
break;
}
}
if (affectedDirectedPresence == null) {
affectedDirectedPresence = new DirectedPresence(handlerJID);
directedPresences.add(affectedDirectedPresence);
}
affectedDirectedPresence.addReceiver(jid);
directedPresencesCache.put(sender, directedPresences);
localDirectedPresences.put(sender, directedPresences);
}
} finally {
lock.unlock();
}
}
}
}
use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.
the class IQDiscoInfoHandler method getServerInfoProvider.
/**
* Returns the DiscoInfoProvider responsible for providing information at the server level. This
* means that this DiscoInfoProvider will provide information whenever a disco request whose
* recipient JID is the server (e.g. localhost) is made.
*
* @return the DiscoInfoProvider responsible for providing information at the server level.
*/
private DiscoInfoProvider getServerInfoProvider() {
return new DiscoInfoProvider() {
final ArrayList<Element> identities = new ArrayList<>();
@Override
public Iterator<Element> getIdentities(String name, String node, JID senderJID) {
if (node != null && serverNodeProviders.get(node) != null) {
// Redirect the request to the disco info provider of the specified node
return serverNodeProviders.get(node).getIdentities(name, node, senderJID);
}
if (name != null && name.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
// Answer identity of the server
synchronized (identities) {
if (identities.isEmpty()) {
Element identity = DocumentHelper.createElement("identity");
identity.addAttribute("category", "server");
identity.addAttribute("name", JiveGlobals.getProperty("xmpp.server.name", "Openfire Server"));
identity.addAttribute("type", "im");
identities.add(identity);
// Include identities from modules that implement ServerIdentitiesProvider
for (Element identityElement : serverIdentities) {
identities.add(identityElement);
}
}
}
return identities.iterator();
} else {
if (SessionManager.getInstance().isAnonymousRoute(name)) {
// Answer identity of an anonymous user.
return anonymousUserIdentities.iterator();
} else {
// Note: We know that this user exists because #hasInfo returned true
return registeredUserIdentities.iterator();
}
}
}
@Override
public Iterator<String> getFeatures(String name, String node, JID senderJID) {
if (node != null && serverNodeProviders.get(node) != null) {
// Redirect the request to the disco info provider of the specified node
return serverNodeProviders.get(node).getFeatures(name, node, senderJID);
}
// Answer features of the server
return new HashSet<>(serverFeatures.keySet()).iterator();
}
@Override
public boolean hasInfo(String name, String node, JID senderJID) {
if (node != null) {
if (serverNodeProviders.get(node) != null) {
// Redirect the request to the disco info provider of the specified node
return serverNodeProviders.get(node).hasInfo(name, node, senderJID);
}
// Unknown node
return false;
}
try {
// anonymous user. We now support disco of user's bare JIDs
return name == null || UserManager.getInstance().getUser(name) != null || SessionManager.getInstance().isAnonymousRoute(name);
} catch (UserNotFoundException e) {
return false;
}
}
@Override
public DataForm getExtendedInfo(String name, String node, JID senderJID) {
if (node != null && serverNodeProviders.get(node) != null) {
// Redirect the request to the disco info provider of the specified node
return serverNodeProviders.get(node).getExtendedInfo(name, node, senderJID);
}
return null;
}
};
}
use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.
the class IQDiscoItemsHandler method getUserItems.
@Override
public Iterator<Element> getUserItems(String name, JID senderJID) {
List<Element> answer = new ArrayList<>();
try {
User user = UserManager.getInstance().getUser(name);
RosterItem item = user.getRoster().getRosterItem(senderJID);
// answer the user's "available resources"
if (item.getSubStatus() == RosterItem.SUB_FROM || item.getSubStatus() == RosterItem.SUB_BOTH) {
for (Session session : SessionManager.getInstance().getSessions(name)) {
Element element = DocumentHelper.createElement("item");
element.addAttribute("jid", session.getAddress().toString());
answer.add(element);
}
}
return answer.iterator();
} catch (UserNotFoundException e) {
return answer.iterator();
}
}
use of org.jivesoftware.openfire.user.UserNotFoundException 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.");
}
use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.
the class OpenfireExporter method exportUsers.
/* (non-Javadoc)
* @see org.jivesoftware.openfire.plugin.Exporter#exportUsers()
*/
@Override
public Document exportUsers() {
Log.debug("exportUsers");
Document document = DocumentHelper.createDocument();
Element root = document.addElement("Openfire");
Collection<User> users = userManager.getUsers();
for (User user : users) {
Element userElement = root.addElement("User");
String userName = user.getUsername();
userElement.addElement("Username").addText(userName);
try {
String pw = AuthFactory.getPassword(user.getUsername());
userElement.addElement("Password").addText(pw);
} catch (UserNotFoundException e) {
Log.info("User " + userName + " not found, setting their password to their username");
userElement.addElement("Password").addText(userName);
} catch (UnsupportedOperationException e) {
Log.info("Unable to retrieve " + userName + " password, setting their password to their username");
userElement.addElement("Password").addText(userName);
}
userElement.addElement("Email").addText(user.getEmail() == null ? "" : user.getEmail());
String name = user.getName();
userElement.addElement("Name").addText(name == null ? "" : name);
//creation and modified date are not used as part of the import process but are exported
//for historical purposes, should they be formatted differently?
userElement.addElement("CreationDate").addText(String.valueOf(user.getCreationDate().getTime()));
userElement.addElement("ModifiedDate").addText(String.valueOf(user.getModificationDate().getTime()));
Element rosterElement = userElement.addElement("Roster");
Collection<RosterItem> roster = user.getRoster().getRosterItems();
for (RosterItem ri : roster) {
Element itemElement = rosterElement.addElement("Item");
itemElement.addAttribute("jid", ri.getJid().toBareJID());
itemElement.addAttribute("askstatus", String.valueOf(ri.getAskStatus().getValue()));
itemElement.addAttribute("recvstatus", String.valueOf(ri.getRecvStatus().getValue()));
itemElement.addAttribute("substatus", String.valueOf(ri.getSubStatus().getValue()));
itemElement.addAttribute("name", ri.getNickname());
List<String> groups = ri.getGroups();
for (String group : groups) {
if (group != null && group.trim().length() > 0) {
itemElement.addElement("Group").addText(group);
}
}
}
}
return document;
}
Aggregations