basetransport.java

来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 1,590 行 · 第 1/5 页

JAVA
1,590 行
字号
        catch (UserNotFoundException e) {            throw new UserNotFoundException("Could not find roster for " + userjid.toString());        }    }    /**     * Removes a roster item from a user's roster based on a legacy contact.     *     * @param userjid JID of user whose roster we will interact with.     * @param contactid Contact to be removed, will be translated to JID.     * @throws UserNotFoundException if userjid not found.     */    void removeFromRoster(JID userjid, String contactid) throws UserNotFoundException {        // Clean up the user's contact list.        try {            removeFromRoster(userjid, convertIDToJID(contactid));        }        catch (UserNotFoundException e) {            // Pass it on through.            throw e;        }    }    /**     * Sync a user's roster with their legacy contact list.     *     * Given a collection of transport buddies, syncs up the user's     * roster by fixing any nicknames, group assignments, adding and removing     * roster items, and generally trying to make the jabber roster list     * assigned to the transport's JID look at much like the legacy buddy     * list as possible.  This is a very extensive operation.  You do not     * want to do this very often.  Typically once right after the person     * has logged into the legacy service.     *     * @param userjid JID of user who's roster we are syncing with.     * @param legacyitems List of TransportBuddy's to be synced.     * @throws UserNotFoundException if userjid not found.     */    public void syncLegacyRoster(JID userjid, List<TransportBuddy> legacyitems) throws UserNotFoundException {        try {            Roster roster = rosterManager.getRoster(userjid.getNode());            boolean hasTransport = false;            // First thing first, we want to build ourselves an easy mapping.            Map<JID,TransportBuddy> legacymap = new HashMap<JID,TransportBuddy>();            for (TransportBuddy buddy : legacyitems) {                //Log.debug("ROSTERSYNC: Mapping "+buddy.getName());                legacymap.put(convertIDToJID(buddy.getName()), buddy);            }            // Now, lets go through the roster and see what matches up.            for (RosterItem ri : roster.getRosterItems()) {                if (!ri.getJid().getDomain().equals(this.jid.getDomain())) {                    // Not our contact to care about.                    continue;                }                if (ri.getJid().getNode() == null) {                    // This is a transport instance, lets leave it alone.                    if (ri.getID() == 0) {                        hasTransport = true;                    }                    else {                        // This is a persistent roster item for the transport, remove.                        this.removeFromRoster(userjid, ri.getJid());                    }                    continue;                }                JID jid = new JID(ri.getJid().toBareJID());                if (legacymap.containsKey(jid)) {                    //Log.debug("ROSTERSYNC: We found, updating " + jid.toString());                    // Ok, matched a legacy to jabber roster item                    // Lets update if there are differences                    TransportBuddy buddy = legacymap.get(jid);                    try {                        this.addOrUpdateRosterItem(userjid, buddy.getName(), buddy.getNickname(), buddy.getGroup());                    }                    catch (UserNotFoundException e) {                        Log.error("Failed updating roster item", e);                    }                    legacymap.remove(jid);                }                else {                    //Log.debug("ROSTERSYNC: We did not find, removing " + jid.toString());                    // This person is apparantly no longer in the legacy roster.                    try {                        this.removeFromRoster(userjid, jid);                    }                    catch (UserNotFoundException e) {                        Log.error("Failed removing roster item", e);                    }                }            }            if (!hasTransport) {                // This person doesn't have the transport in their roster, lets put it there.                this.addOrUpdateRosterItem(userjid, this.getJID(), this.getDescription(), "Transports");            }            // Ok, we should now have only new items from the legacy roster            for (TransportBuddy buddy : legacymap.values()) {                //Log.debug("ROSTERSYNC: We have new, adding " + buddy.getName());                try {                    this.addOrUpdateRosterItem(userjid, buddy.getName(), buddy.getNickname(), buddy.getGroup());                }                catch (UserNotFoundException e) {                    Log.error("Failed adding new roster item", e);                }            }        }        catch (UserNotFoundException e) {            throw new UserNotFoundException("Could not find roster for " + userjid.toString());        }    }    /**     * Adds a registration with this transport.     *     * @param jid JID of user to add registration to.     * @param username Legacy username of registration.     * @param password Legacy password of registration.     * @param nickname Legacy nickname of registration.     * @throws UserNotFoundException if registration or roster not found.     * @throws IllegalAccessException if jid is not from this server.     * @throws IllegalArgumentException if username is not valid for this transport type.     */    public void addNewRegistration(JID jid, String username, String password, String nickname) throws UserNotFoundException, IllegalAccessException {        if (!XMPPServer.getInstance().getServerInfo().getName().equals(jid.getDomain())) {            throw new IllegalAccessException("Domain of jid registering does not match domain of server.");        }        if (!isUsernameValid(username)) {            throw new IllegalArgumentException("Username specified is not valid for this transport type.");        }        Collection<Registration> registrations = registrationManager.getRegistrations(jid, this.transportType);        Boolean foundReg = false;        for (Registration registration : registrations) {            if (!registration.getUsername().equals(username)) {                registrationManager.deleteRegistration(registration);            }            else {                registration.setPassword(password);                foundReg = true;            }        }        if (!foundReg) {            registrationManager.createRegistration(jid, this.transportType, username, password, nickname);        }        // Clean up any leftover roster items from other transports.        try {            cleanUpRoster(jid, true);        }        catch (UserNotFoundException ee) {            throw new UserNotFoundException("Unable to find roster.");        }        try {            addOrUpdateRosterItem(jid, this.getJID(), this.getDescription(), "Transports");        }        catch (UserNotFoundException e) {            throw new UserNotFoundException("User not registered with server.");        }    }    /**     * Removes a registration from this transport.     *     * @param jid JID of user to add registration to.     * @throws UserNotFoundException if registration or roster not found.     */    public void deleteRegistration(JID jid) throws UserNotFoundException {        Collection<Registration> registrations = registrationManager.getRegistrations(jid, this.transportType);        // For now, we're going to have to just nuke all of these.  Sorry.        for (Registration reg : registrations) {            registrationManager.deleteRegistration(reg);        }        // Clean up the user's contact list.        try {            cleanUpRoster(jid, false);        }        catch (UserNotFoundException e) {            throw new UserNotFoundException("Unable to find roster.");        }    }    /**     * Cleans a roster of entries related to this transport that are not shared.     *     * This function will run through the roster of the specified user and clean up any     * entries that share the domain of this transport.  This is primarily used during registration     * to clean up leftovers from other transports.     *     * @param jid JID of user whose roster we want to clean up.     * @param leaveDomain If set, we do not touch the roster item associated with the domain itself.     * @throws UserNotFoundException if the user is not found.     */    public void cleanUpRoster(JID jid, Boolean leaveDomain) throws UserNotFoundException {        try {            Roster roster = rosterManager.getRoster(jid.getNode());            for (RosterItem ri : roster.getRosterItems()) {                if (ri.getJid().getDomain().equals(this.jid.getDomain())) {                    if (ri.isShared()) {                        continue;                    }                    if (ri.getID() == 0) { // Is a non-persistent roster item.                        continue;                    }                    if (leaveDomain && ri.getJid().getNode() == null) {                        continue;                    }                    try {                        Log.debug("Cleaning up roster entry " + ri.getJid().toString());                        roster.deleteRosterItem(ri.getJid(), false);                    }                    catch (Exception e) {                        Log.error("Error removing roster item: " + ri.toString(), e);                    }                }            }        }        catch (UserNotFoundException e) {            throw new UserNotFoundException("Unable to find roster.");        }    }    /**     * Sends offline packets for an entire roster to the target user.     *     * This function will run through the roster of the specified user and send offline     * presence packets for each roster item.   This is typically used when a user logs     * off so that all of the associated roster items appear offline.  This also sends     * the unavailable presence for the transport itself.     *     * @param jid JID of user whose roster we want to clean up.     * @throws UserNotFoundException if the user is not found.     */    public void notifyRosterOffline(JID jid) throws UserNotFoundException {        try {            Roster roster = rosterManager.getRoster(jid.getNode());            for (RosterItem ri : roster.getRosterItems()) {                if (ri.getJid().getDomain().equals(this.jid.getDomain())) {                    Presence p = new Presence(Presence.Type.unavailable);                    p.setTo(jid);                    p.setFrom(ri.getJid());                    sendPacket(p);                }            }        }        catch (UserNotFoundException e) {            throw new UserNotFoundException("Unable to find roster.");        }    }    /**     * Sends a packet through the component manager as the component.     *     * @param packet Packet to be sent.     */    public void sendPacket(Packet packet) {        Log.debug(getType().toString()+": Sending packet: "+packet.toXML());        try {            this.componentManager.sendPacket(this, packet);        }        catch (Exception e) {            Log.error("Failed to deliver packet: " + packet.toString());        }    }    /**     * Intercepts roster additions related to the gateway and flags them as non-persistent.     *     * @see org.jivesoftware.wildfire.roster.RosterEventListener#addingContact(org.jivesoftware.wildfire.roster.Roster, org.jivesoftware.wildfire.roster.RosterItem, boolean)     */    public boolean addingContact(Roster roster, RosterItem item, boolean persistent) {        Log.debug(getType().toString()+": addingContact "+roster.getUsername()+":"+item.getJid()+" is "+persistent);        return !(item.getJid().getDomain().equals(this.getJID()) &&                item.getJid().getNode() != null) && persistent;//        if (item.getJid().getDomain().equals(this.getJID()) && item.getJid().getNode() != null) {//            return false;//        }//        return persistent;    }    /**     * Handles updates to a roster item that are not normally forwarded to the transport.     *     * @see org.jivesoftware.wildfire.roster.RosterEventListener#contactUpdated(org.jivesoftware.wildfire.roster.Roster, org.jivesoftware.wildfire.roster.RosterItem)     */    public void contactUpdated(Roster roster, RosterItem item) {        Log.debug(getType().toString()+": contactUpdated "+roster.getUsername()+":"+item.getJid());        if (!item.getJid().getDomain().equals(this.getJID().getDomain())) {            // Not ours, not our problem.            return;        }        if (item.getJid().getNode() == null) {            // Gateway itself, don't care.            return;        }        try {            TransportSession session = sessionManager.getSession(roster.getUsername());            session.updateContact(item);        }        catch (NotFoundException e) {            // Well we just don't care then.        }    }    /**     * Handles additions to a roster.  We don't really care because we hear about these via subscribes.     *     * @see org.jivesoftware.wildfire.roster.RosterEventListener#contactAdded(org.jivesoftware.wildfire.roster.Roster, org.jivesoftware.wildfire.roster.RosterItem)     */    public void contactAdded(Roster roster, RosterItem item) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?