UserNameManager.java 5.04 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Gaston Dombiak's avatar
Gaston Dombiak committed
19 20
 */

21
package org.jivesoftware.openfire.user;
Gaston Dombiak's avatar
Gaston Dombiak committed
22

23
import org.jivesoftware.openfire.XMPPServer;
Gaston Dombiak's avatar
Gaston Dombiak committed
24 25 26 27 28 29 30 31 32 33
import org.xmpp.packet.JID;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Main responsibility of this class is to return the correct name of XMPP entities. For local
 * entities (i.e. users) the {@link User} name is used. For remote entities the following logic
 * is used:
 * <ol>
34 35 36 37 38
 * <li>Check if a {@link UserNameProvider} is registered for the entity's domain. If a provider
 * was found then use it to get the entity's name</li>
 * <li>If no provider was found then retrieve the vCard of the entity and return the name as
 * defined in the vCard. <i>This is not implemented yet.</i></li>
 * <li>If no vCard was found then return the string representation of the entity's JID.</li>
Gaston Dombiak's avatar
Gaston Dombiak committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * </ol>
 *
 * @author Gaston Dombiak
 */
public class UserNameManager {

    private static XMPPServer server = XMPPServer.getInstance();
    /**
     * Map that keeps the UserNameProvider to use for each specific domain.
     */
    private static Map<String, UserNameProvider> providersByDomain =
            new ConcurrentHashMap<String, UserNameProvider>();

    private UserNameManager() {
    }

    /**
     * Adds the specified {@link UserNameProvider} as the provider of users of the specified domain.
     *
     * @param domain   the domain hosted by the UserNameProvider.
     * @param provider the provider that will provide the name of users in the specified domain.
     */
    public static void addUserNameProvider(String domain, UserNameProvider provider) {
        providersByDomain.put(domain, provider);
    }

    /**
     * Removes any {@link UserNameProvider} that was associated with the specified domain.
     *
     * @param domain the domain hosted by a UserNameProvider.
     */
    public static void removeUserNameProvider(String domain) {
        providersByDomain.remove(domain);
    }

    /**
     * Returns the name of the XMPP entity. If the entity is a local user then the User's name
     * will be returned. However, if the user is not a local user then check if there exists a
     * UserNameProvider that provides name for the specified domain. If none was found then
     * the vCard of the entity might be requested and if none was found then a string
     * representation of the entity's JID will be returned.
     *
     * @param entity the JID of the entity to get its name.
     * @return the name of the XMPP entity.
     * @throws UserNotFoundException if the jid belongs to the local server but no user was
84
     *                               found for that jid.
Gaston Dombiak's avatar
Gaston Dombiak committed
85 86
     */
    public static String getUserName(JID entity) throws UserNotFoundException {
87 88 89 90 91 92 93 94 95 96
        return getUserName(entity, entity.toString());
    }

    /**
     * Returns the name of the XMPP entity. If the entity is a local user then the User's name
     * will be returned. However, if the user is not a local user then check if there exists a
     * UserNameProvider that provides name for the specified domain. If none was found then
     * the vCard of the entity might be requested and if none was found then a string
     * representation of the entity's JID will be returned.
     *
97
     * @param entity      the JID of the entity to get its name.
98 99 100
     * @param defaultName default name to return when no name was found.
     * @return the name of the XMPP entity.
     * @throws UserNotFoundException if the jid belongs to the local server but no user was
101
     *                               found for that jid.
102 103
     */
    public static String getUserName(JID entity, String defaultName) throws UserNotFoundException {
Gaston Dombiak's avatar
Gaston Dombiak committed
104 105 106
        if (server.isLocal(entity)) {
            // Contact is a local entity so search for his user name
            User localUser = UserManager.getInstance().getUser(entity.getNode());
107 108
            return !localUser.isNameVisible() || "".equals(localUser.getName()) ? entity.getNode() : localUser.getName();
        } else {
Gaston Dombiak's avatar
Gaston Dombiak committed
109 110 111 112 113 114 115 116 117
            UserNameProvider provider = providersByDomain.get(entity.getDomain());
            if (provider != null) {
                return provider.getUserName(entity);
            }
            // TODO Request vCard to the remote server/component and return the name as
            // TODO defined in the vCard. We might need to cache this information to avoid
            // TODO high traffic.

            // Return the jid itself as the username
118
            return defaultName;
Gaston Dombiak's avatar
Gaston Dombiak committed
119 120 121
        }
    }
}