First you need to connect to a server that allows transports. I suggest downloading an openfire server and setting up gateways and just connect to that to test. Then you need to, from your chat client, find out what transports are available. so
public static Hashtable<String, String> getTransports(XMMPConnection conn)
{
ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(conn);
DiscoverItems items;
try
{
items = manager.discoverItems(conn.getServiceName());
}
catch (XMPPException e) {
items = new DiscoverItems();
}
Hashtable<String, String> available = new Hashtable<String, String>();
Iterator<DiscoverItems.Item> iterator = items.getItems();
while(iterator.hasNext())
{
DiscoverItems.Item item = iterator.next();
try
{
DiscoverInfo info = manager.discoverInfo(item.getEntityID());
if(info.containsFeature("jabber:iq:gateway"))
{
Iterator<DiscoverInfo.Identity> identities = info.getIdentities();
while(identities.hasNext())
{
DiscoverInfo.Identity identity = identities.next();
if ("gateway".equals(identity.getCategory())) available.put(identity.getType(), item.getEntityID());
}
}
}
catch(Exception e)
{
} } return available;
}
This will return a list like
msn.myserver
aim.myserver
yahoo.myserver
icq.myserver
Now you can take the types returned and put them in a combo box or the like and set up a form to register the transport, you need a username, password and nickname. Once your user has entered in all those details and they hit the go button, then you register them, you have to add the transport to your roster to get it to load when you start up your client. I havn’t figured it all out yet, there seems to be an issue where you don’t end up subscribed to the transport but it is subsribed to you. Maybe it’s in the order you send the presence packets. You can see it in the openfire admin console. But it will still work regardless of if the transport is “available”.
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<String, String> attributes = new HashMap<String, String>();
if (username != null) attributes.put("username", username);
if (password != null) attributes.put("password", password);
if (nickname != null) attributes.put("nickname", nickname);
registration.setAttributes(attributes); PacketCollector collector = Settings.getConnection().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) throw new XMPPException("Error registering user", response.getError());
if(!conn.getRoster().contains(gateway))
conn.getRoster().createEntry(gateway, gateway, null);
Presence subsrcibe = new Presence(Presence.Type.subscribe);
subscribe.setTo(gateway);
conn.sendPacket(subscribe);
Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(gateway);
conn.sendPacket(subscribed);
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setTo(gateway));
conn.sendPacket(presence);
} 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();
}
}
and then you may want to unregister them
public static void unregister(XMPPConnection conn, String gateway) throws XMPPException
{ Registration registration = new Registration();
registration.setType(IQ.Type.SET);
registration.setTo(gateway);
Map<String,String> map = new HashMap<String,String>();
map.put("remove", "");
registration.setAttributes(map);
PacketCollector collector = Settings.getConnection().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) throw new XMPPException("Error unregistering user", response.getError());
if(conn.getRoster().contains(list.get(box.getSelectedItem())))
conn.getRoster().removeEntry(conn.getRoster().getEntry(gateway))
}
and that’s it. It’s all very simple really.
I hope that helps.
-Mandy