basetransport.java
来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 1,590 行 · 第 1/5 页
JAVA
1,590 行
Element query = DocumentHelper.createElement(QName.get("query", IQ_VERSION)); query.addElement("name").addText("Wildfire " + this.getDescription()); query.addElement("version").addText(XMPPServer.getInstance().getServerInfo().getVersion().getVersionString() + " - " + this.getVersionString()); query.addElement("os").addText(System.getProperty("os.name")); result.setChildElement(query); reply.add(result); } return reply; } /** * Converts a legacy username to a JID. * * @param username Username to be converted to a JID. * @return The legacy username as a JID. */ public JID convertIDToJID(String username) { return new JID(username.replace('@', '%').replace(" ", ""), this.jid.getDomain(), null); } /** * Converts a JID to a legacy username. * * @param jid JID to be converted to a legacy username. * @return THe legacy username as a String. */ public String convertJIDToID(JID jid) { return jid.getNode().replace('%', '@'); } /** * Gets an easy to use presence type from a presence packet. * * @param packet A presence packet from which the type will be pulled. * @return the presence type of the specified packet. */ public PresenceType getPresenceType(Presence packet) { Presence.Type ptype = packet.getType(); Presence.Show stype = packet.getShow(); if (stype == Presence.Show.chat) { return PresenceType.chat; } else if (stype == Presence.Show.away) { return PresenceType.away; } else if (stype == Presence.Show.xa) { return PresenceType.xa; } else if (stype == Presence.Show.dnd) { return PresenceType.dnd; } else if (ptype == Presence.Type.unavailable) { return PresenceType.unavailable; } else if (packet.isAvailable()) { return PresenceType.available; } else { return PresenceType.unknown; } } /** * Handles startup of the transport. */ public void start() { RosterEventDispatcher.addListener(this); PresenceEventDispatcher.addListener(this); SessionManager sessionManager = SessionManager.getInstance(); // Check all registered users to auto-log them in for (Registration registration : registrationManager.getRegistrations()) { String user = registration.getJID().getNode(); if (sessionManager.getSessionCount(user) > 0) { for (ClientSession session : sessionManager.getSessions(user)) { availableSession(session, session.getPresence()); } } } } /** * Handles shutdown of the transport. * * Cleans up all active sessions. */ public void shutdown() { RosterEventDispatcher.removeListener(this); PresenceEventDispatcher.removeListener(this); // Disconnect everyone's session for (TransportSession session : sessionManager.getSessions()) { registrationLoggedOut(session); session.removeAllResources(); } sessionManager.shutdown(); } /** * Returns the jid of the transport. * * @return the jid of the transport. */ public JID getJID() { return this.jid; } /** * Returns the roster manager for the transport. * * @return the roster manager for the transport. */ public RosterManager getRosterManager() { return this.rosterManager; } /** * Returns the name (type) of the transport. */ public String getName() { return transportType.toString(); } /** * @return the type of the transport. */ public TransportType getType() { return transportType; } /** * @return the description of the transport. */ public String getDescription() { return description; } /** * @return the component manager of the transport. */ public ComponentManager getComponentManager() { return componentManager; } /** * @return the registration manager of the transport. */ public RegistrationManager getRegistrationManager() { return registrationManager; } /** * @return the session manager of the transport. */ public TransportSessionManager getSessionManager() { return sessionManager; } /** * Retains the version string for later requests. */ private String versionString = null; /** * @return the version string of the gateway. */ public String getVersionString() { if (versionString == null) { PluginManager pluginManager = XMPPServer.getInstance().getPluginManager(); versionString = pluginManager.getVersion(pluginManager.getPlugin("gateway")); } return versionString; } /** * Either updates or adds a JID to a user's roster. * * Tries to only edit the roster if it has to. * * @param userjid JID of user to have item added to their roster. * @param contactjid JID to add to roster. * @param nickname Nickname of item. (can be null) * @param groups List of group the item is to be placed in. (can be null) * @throws UserNotFoundException if userjid not found. */ public void addOrUpdateRosterItem(JID userjid, JID contactjid, String nickname, ArrayList<String> groups) throws UserNotFoundException { try { Roster roster = rosterManager.getRoster(userjid.getNode()); try { RosterItem gwitem = roster.getRosterItem(contactjid); boolean changed = false; if (gwitem.getSubStatus() != RosterItem.SUB_BOTH) { gwitem.setSubStatus(RosterItem.SUB_BOTH); changed = true; } if (gwitem.getAskStatus() != RosterItem.ASK_NONE) { gwitem.setAskStatus(RosterItem.ASK_NONE); changed = true; } // This could probably be simplified, for not I'm going with brute force logic. // gnickname is null, nickname is null, leave // gnickname is not null, nickname is null, set gnickname to null // gnickname is null, nickname is not null, set gnickname to nickname // gnickname is not null, nickname is not null, if different, set gnickname to nickname if ( (gwitem.getNickname() != null && nickname == null) || (gwitem.getNickname() == null && nickname != null) || (gwitem.getNickname() != null && nickname != null && !gwitem.getNickname().equals(nickname))) { gwitem.setNickname(nickname); changed = true; } List<String> curgroups = gwitem.getGroups(); if (curgroups != groups) { try { gwitem.setGroups(groups); changed = true; } catch (Exception ee) { // Oooookay, ignore then. } } if (changed) { roster.updateRosterItem(gwitem); } } catch (UserNotFoundException e) { try { // Create new roster item for the gateway service or legacy contact. Only // roster items related to the gateway service will be persistent. Roster // items of legacy users are never persisted in the DB.// RosterItem gwitem =// roster.createRosterItem(contactjid, true, contactjid.getNode() == null); RosterItem gwitem = roster.createRosterItem(contactjid, true, false); gwitem.setSubStatus(RosterItem.SUB_BOTH); gwitem.setAskStatus(RosterItem.ASK_NONE); gwitem.setNickname(nickname); try { gwitem.setGroups(groups); } catch (Exception ee) { // Oooookay, ignore then. } roster.updateRosterItem(gwitem); } catch (UserAlreadyExistsException ee) { Log.error("getRosterItem claims user exists, but couldn't find via getRosterItem?", ee); } catch (Exception ee) { Log.error("Exception while creating roster item:", ee); } } } catch (UserNotFoundException e) { throw new UserNotFoundException("Could not find roster for " + userjid.toString()); } } /** * Either updates or adds a JID to a user's roster. * * Tries to only edit the roster if it has to. * * @param userjid JID of user to have item added to their roster. * @param contactjid JID to add to roster. * @param nickname Nickname of item. (can be null) * @param group Group item is to be placed in. (can be null) * @throws UserNotFoundException if userjid not found. */ public void addOrUpdateRosterItem(JID userjid, JID contactjid, String nickname, String group) throws UserNotFoundException { ArrayList<String> groups = null; if (group != null) { groups = new ArrayList<String>(); groups.add(group); } addOrUpdateRosterItem(userjid, contactjid, nickname, groups); } /** * Either updates or adds a contact to a user's roster. * * @param userjid JID of user to have item added to their roster. * @param contactid String contact name, will be translated to JID. * @param nickname Nickname of item. (can be null) * @param group Group item is to be placed in. (can be null) * @throws UserNotFoundException if userjid not found. */ public void addOrUpdateRosterItem(JID userjid, String contactid, String nickname, String group) throws UserNotFoundException { try { addOrUpdateRosterItem(userjid, convertIDToJID(contactid), nickname, group); } catch (UserNotFoundException e) { // Pass it on down. throw e; } } /** * Removes a roster item from a user's roster. * * @param userjid JID of user whose roster we will interact with. * @param contactjid JID to be removed from roster. * @throws UserNotFoundException if userjid not found. */ public void removeFromRoster(JID userjid, JID contactjid) throws UserNotFoundException { // Clean up the user's contact list. try { Roster roster = rosterManager.getRoster(userjid.getNode()); for (RosterItem ri : roster.getRosterItems()) { if (ri.getJid().toBareJID().equals(contactjid.toBareJID())) { try { roster.deleteRosterItem(ri.getJid(), false); } catch (Exception e) { Log.error("Error removing roster item: " + ri.toString(), e); } } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?