XMPP 注册gateway及提取roster

package weibo4j.pens; import java.util.*; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.filter.PacketIDFilter; import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.*; public class XMPPTest { // ++++++++++++++++++ RosterListener ++++++++++++++++++++++++++++ // public static void CreateRosterListener(Roster roster) { roster.addRosterListener(new RosterListener() { public void entriesAdded(Collection addresses) {} public void entriesDeleted(Collection addresses) {} public void entriesUpdated(Collection addresses) {} public void presenceChanged(Presence presence) { System.out.println("Presence changed: " + presence.getFrom() + " " + presence+ "/n"); } }); } // ++++++++++++++++++++ RetreiveRosters +++++++++++++++++++++++++++++++ // public static void RetreiveRosters(Roster roster,Collection entries) { Iterator it = entries.iterator(); while(it.hasNext()) { RosterEntry entry = (RosterEntry) it.next(); Presence prsn = roster.getPresence(entry.getUser()); System.out.println(entry.getName() + " (" + entry.getUser() + ") ["+entry.getStatus()+ "] - [" + entry.getType() + "][" + prsn.getStatus() + "]" ); } } static class GatewayRegisterExtension implements PacketExtension { public String getElementName() { return "x"; } public String getNamespace() { return "jabber:iq:gateway:register"; } public String toXML() { StringBuilder builder = new StringBuilder(); builder.append("<").append(getElementName()).append(" xmlns=/"").append(getNamespace()).append("/"/>"); return builder.toString(); } } public static void XMPPAddRosterEntry(Roster roster, String userIDWithDomainName, String userNickName, String Reserved) { try { roster.createEntry(userIDWithDomainName, userNickName, null); } catch(Exception e) { System.out.println("ADD_ROSTER_ERROR: "+ e); } } public static void register(XMPPConnection conn, String gateway, String username, String password, String nickname) throws XMPPException { if(username == null || username.length() == 0 || password == null || password.length() == 0 || nickname == null || nickname.length() == 0 || gateway == null || gateway.length() == 0) throw new XMPPException("Incorrect paramaters"); Registration registration = new Registration(); registration.setType(IQ.Type.SET); registration.setTo(gateway); registration.addExtension(new GatewayRegisterExtension()); Map attributes = new HashMap(); if (username != null) attributes.put("username", username); if (password != null) attributes.put("password", password); if (nickname != null) attributes.put("nick", nickname); registration.setAttributes(attributes); PacketCollector collector = conn.createPacketCollector(new PacketIDFilter(registration.getPacketID())); conn.sendPacket(registration); IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if(response == null) throw new XMPPException("Server timed out"); if(response.getType() == IQ.Type.ERROR) { System.out.println(response.getXmlns()); System.out.println(response.getChildElementXML()); throw new XMPPException("Error registering user", response.getError()); } if(!conn.getRoster().contains(gateway)) conn.getRoster().createEntry(gateway, gateway, null); Presence subscribe = new Presence(Presence.Type.subscribe); subscribe.setTo(gateway); subscribe.setFrom(conn.getUser()); conn.sendPacket(subscribe); } public static void main(String args[]) { try { XMPPConnection.DEBUG_ENABLED = true; ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1"); //serverIp表示的是你openfire服务器地址 config.setReconnectionAllowed(false); XMPPConnection connection = new XMPPConnection(config); //connection = new XMPPConnection("127.0.0.1"); connection.connect(); Roster roster = null; //if(connection.isConnected()) { connection.login("test", "123456"); roster = connection.getRoster(); Collection entries = roster.getEntries(); RetreiveRosters(roster,entries); CreateRosterListener(roster); } register(connection,"gtalk.pansi.me", "[email protected]","123456","MyGtalk"); XMPPAddRosterEntry(roster, "gtalk.pansi.me", "MyGtalk",null); } catch(Exception e) { System.out.println(e); } } } 

 代码实现了将XMPP用户test与gmail用户[email protected]绑定,以及将已经绑定的其它transport的好友列表列出。需要注意的是,在运行程序之前,必须在Openfire Server的gateway页面,选中对应的网关。如需绑定gmail账号,需选中Goole Talk网关。

你可能感兴趣的:(exception,String,HashMap,null,XMPP,attributes)