001: /**
002: * $RCSfile$
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2006 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.user;
011:
012: import org.jivesoftware.openfire.XMPPServer;
013: import org.xmpp.packet.JID;
014:
015: import java.util.Map;
016: import java.util.concurrent.ConcurrentHashMap;
017:
018: /**
019: * Main responsibility of this class is to return the correct name of XMPP entities. For local
020: * entities (i.e. users) the {@link User} name is used. For remote entities the following logic
021: * is used:
022: * <ol>
023: * <li>Check if a {@link UserNameProvider} is registered for the entity's domain. If a provider
024: * was found then use it to get the entity's name</li>
025: * <li>If no provider was found then retrieve the vCard of the entity and return the name as
026: * defined in the vCard. <i>This is not implemented yet.</i></li>
027: * <li>If no vCard was found then return the string representation of the entity's JID.</li>
028: * </ol>
029: *
030: * @author Gaston Dombiak
031: */
032: public class UserNameManager {
033:
034: private static XMPPServer server = XMPPServer.getInstance();
035: /**
036: * Map that keeps the UserNameProvider to use for each specific domain.
037: */
038: private static Map<String, UserNameProvider> providersByDomain = new ConcurrentHashMap<String, UserNameProvider>();
039:
040: private UserNameManager() {
041: }
042:
043: /**
044: * Adds the specified {@link UserNameProvider} as the provider of users of the specified domain.
045: *
046: * @param domain the domain hosted by the UserNameProvider.
047: * @param provider the provider that will provide the name of users in the specified domain.
048: */
049: public static void addUserNameProvider(String domain,
050: UserNameProvider provider) {
051: providersByDomain.put(domain, provider);
052: }
053:
054: /**
055: * Removes any {@link UserNameProvider} that was associated with the specified domain.
056: *
057: * @param domain the domain hosted by a UserNameProvider.
058: */
059: public static void removeUserNameProvider(String domain) {
060: providersByDomain.remove(domain);
061: }
062:
063: /**
064: * Returns the name of the XMPP entity. If the entity is a local user then the User's name
065: * will be returned. However, if the user is not a local user then check if there exists a
066: * UserNameProvider that provides name for the specified domain. If none was found then
067: * the vCard of the entity might be requested and if none was found then a string
068: * representation of the entity's JID will be returned.
069: *
070: * @param entity the JID of the entity to get its name.
071: * @return the name of the XMPP entity.
072: * @throws UserNotFoundException if the jid belongs to the local server but no user was
073: * found for that jid.
074: */
075: public static String getUserName(JID entity)
076: throws UserNotFoundException {
077: return getUserName(entity, entity.toString());
078: }
079:
080: /**
081: * Returns the name of the XMPP entity. If the entity is a local user then the User's name
082: * will be returned. However, if the user is not a local user then check if there exists a
083: * UserNameProvider that provides name for the specified domain. If none was found then
084: * the vCard of the entity might be requested and if none was found then a string
085: * representation of the entity's JID will be returned.
086: *
087: * @param entity the JID of the entity to get its name.
088: * @param defaultName default name to return when no name was found.
089: * @return the name of the XMPP entity.
090: * @throws UserNotFoundException if the jid belongs to the local server but no user was
091: * found for that jid.
092: */
093: public static String getUserName(JID entity, String defaultName)
094: throws UserNotFoundException {
095: if (server.isLocal(entity)) {
096: // Contact is a local entity so search for his user name
097: User localUser = UserManager.getInstance().getUser(
098: entity.getNode());
099: return "".equals(localUser.getName()) ? entity.getNode()
100: : localUser.getName();
101: } else {
102: UserNameProvider provider = providersByDomain.get(entity
103: .getDomain());
104: if (provider != null) {
105: return provider.getUserName(entity);
106: }
107: // TODO Request vCard to the remote server/component and return the name as
108: // TODO defined in the vCard. We might need to cache this information to avoid
109: // TODO high traffic.
110:
111: // Return the jid itself as the username
112: return defaultName;
113: }
114: }
115: }
|