How to connect to gateway using Smack API

Dear All

I have installed OPenfire on my local machine along with gateway plugins, i need to use yahoo, aim msn gateway from my application. Following link shows the main steps invloved in using gateway i.e. discovering the gateway using Service discovery, registeration and including the transport in the roster.

[http://www.igniterealtime.org/forum/thread.jspa?messageID=107687&#107687]

This link also provides two other links which explain gateway discovery using service discovery nad regiseration but both links are not available.

Can anyone please help me in finding how this can be done?.

So far i tried the code provided in smack documentation which uses DiscoverInf, the code is shown below

ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(xmppConnection);
DiscoverInfo discoInfo = new DiscoverInfo();
try
{

discoInfo = discoManager.discoverInfo(“msn.tcmjabberserver.com”);

} catch (XMPPException ex) {

ex.printStackTrace();

} // Get the discovered identities of the remote XMPP entity
Iterator it = discoInfo.getIdentities();
// Display the identities of the remote XMPP entity
while (it.hasNext())
{

DiscoverInfo.Identity identity = (DiscoverInfo.Identity) it.next();

System.out.println(identity.getName());

System.out.println(identity.getType());

System.out.println(identity.getCategory());

}

But when i run this code it gives me the following exception

bad-request(400)

at org.jivesoftware.smackx.ServiceDiscoveryManager.discoverInfo(ServiceDiscoveryMa nager.java:413)

at org.jivesoftware.smackx.ServiceDiscoveryManager.discoverInfo(ServiceDiscoveryMa nager.java:379)

at com.tcm.im.JabberConnection.main(JabberConnection.java:82)

In debugger window, i noticed that the following XML request was sent to jabber server which matches with XEP100’'s description. but the replies back in an error. <iq id=“DlHym-0” to=“msn.tcmjabberserver.com” type=“get”>

<query xmlns=“http://jabber.org/protocol/disco#info”/>

</iq>

Error returned from the server

<iq id=“DlHym-0” to=“tcmjabberserver.com/beebf601” from=“msn.tcmjabberserver.com” type=“error”>
<query xmlns=“http://jabber.org/protocol/disco#info”/>
<error code=“400” type=“MODIFY”>
<bad-request xmlns=“urn:ietf:params:xml:ns:xmpp-stanzas”/>
</error>
</iq>

Please help me with this and also guide me on the rest of the steps. Thanks alot in advance

Here is the implementation I use- it was assembled from a number of examples and may not be the most efficient or elegant approach, but it works.

FYI- the Yahoo Gateway barely works at all- if you need Yahoo, you probably need a different solution.


logger.fine("starting for gateway " + gwPrefix);

ServiceDiscoveryManager discoveryManager = new ServiceDiscoveryManager(conn);

String connHost = conn.getHost();

String gatewayJid = gwPrefix + “.” + connHost;

logger.fine("discovering items on " + connHost);

DiscoverItems items = discoveryManager.discoverItems(connHost);

Iterator itemIter = items.getItems();

boolean gatewaySupported = false;

while (itemIter.hasNext()) {

DiscoverItems.Item item = (DiscoverItems.Item) itemIter.next();

if (item.getEntityID().startsWith(gwPrefix)) {

gatewaySupported = true;

}

}

if (gatewaySupported == false) {

logger.info("gateway not supported: " + gwPrefix);

return;

}

logger.fine(“verifying register capable”);

DiscoverInfo info = discoveryManager.discoverInfo(gatewayJid);

if (info.containsFeature(“jabber:iq:register”) == false) {

logger.info("register not supported: " + gwPrefix);

return;

}

logger.fine(“register”);

Registration registration = new Registration();

registration.setType(IQ.Type.SET);

registration.setTo(gatewayJid);

registration.setFrom(conn.getUser());

registration.addExtension(new PacketExtension() {

public String getElementName()

{

return “x”;

}

public String getNamespace()

{

// return “jabber:iq:gateway:register”;

return “jabber:iq:register”;

}

public String toXML()

{

StringBuilder builder = new StringBuilder();

builder.append("<").append(getElementName()).append(" xmlns="").append(getNamespace()).append(""/>");

return builder.toString();

}

});

Map<String, String> attributes = new HashMap<String, String>();

attributes.put(“username”, username);

attributes.put(“password”, password);

registration.setAttributes(attributes);

logger.finer(“create packetCollector and send message”);

PacketCollector collector = conn.createPacketCollector(new PacketIDFilter(registration.getPacketID()));

conn.sendPacket(registration);

logger.finer(“message sent; waiting”);

IQ response = (IQ) collector.nextResult(10000);

collector.cancel();

if (response == null) {

logger.warning("no response from server for register: " + gwPrefix);

gatewayActiveStatus.put(gwPrefix, new Boolean(false));

return;

}

if (response.getType() == IQ.Type.ERROR) {

errorMessage = response.getError().getMessage();

logger.warning(“error registering with gateway: " + gwPrefix + " error=” + response.getError().toXML());

gatewayActiveStatus.put(gwPrefix, new Boolean(false));

return;

}

gatewayActiveStatus.put(gwPrefix, new Boolean(true));

logger.fine(“sending presence to gateway”);

Presence p = new Presence(Presence.Type.available);

p.setTo(gatewayJid);

p.setFrom(conn.getUser());

conn.sendPacket§;