Please find the code block below. We just need to cache all the available transports in a hasmap using service discovery manager. Then we will serach this hassmap to find any gateway name for a gateway id.
We can use the below functions to determine whether the presence comes from a Gateway like yahoo.domain name or comes from a legacy user user1@yahoo.domain
import java.util.Iterator; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverItems; private Hashtable<String, String> _availavleTransports = new Hashtable<String, String>(); public void cacheAllAvailableTransports(XMPPConnection connection) {
System.out.println("Samba Sample: Inside function :xmppGetAvailableTransports()");
ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(connection);
DiscoverItems items;
try
{
System.out.println("Samba Sample: Get items :xmppGetAvailableTransports()");
items = manager.discoverItems(connection.getServiceName());
}
catch (XMPPException e)
{
items = new DiscoverItems();
System.out.println("Samba Sample: Unable to discover transports from XMPP server:"+ e + "xmppGetAvailableTransports()");
}
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()))
{
String gatewayID = item.getEntityID();
String gatewayType = identity.getType(); /*
* We need this check because Kraken is returning
* XMPP for gtalk, so we are converting it to gtalk
* manually.
* */
if(gatewayID.indexOf("gtalk") > -1)
{
gatewayType = "gtalk";
} _availavleTransports.put(gatewayID,gatewayType);
} } }
}
catch(Exception e)
{
System.out.println("Samba Sample: Unable to fetch transports list from XMPP server:"+ e + " :xmppGetAvailableTransports()");
} }
} public String extractGatewayType(String userid,StringBuilder gatewayPresence)
{
Log.sendInfo("Samba Sample: Extract gateway Type for :-"+ userid +" extractGatewayType()");
String gatewayType = null;
if(isGatewayID(userid))
{
Log.sendInfo("Samba Sample: This is a gateway ID :extractGatewayType()");
gatewayType = extractGatewaytype(userid);
gatewayPresence.append("GATEWAY_PRESENCE");
}
else
{
System.out.println("Samba Sample: Not a gateway ID :extractGatewayType()");
gatewayType = extractUserType(userid);
}
System.out.println("Samba Sample: gateway type:-"+gatewayType +" :extractGatewayType()");
return gatewayType;
}
public boolean isGatewayID(String fromUser)
{
ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(connection); try
{
if(manager!=null)
{
DiscoverInfo info = manager.discoverInfo(fromUser); if(info!=null)
{
if(info.containsFeature("jabber:iq:gateway"))
{
return true;
}
}
}
}
catch(Exception e)
{
System.out.println("Samba Sample: Unable to fetch discovery info fromUser:-"+ fromUser + " ,Exception:-" + e);
} return false;
}
public String extractGatewaytype(String gatewayID)
{ if(_availavleTransports.size() > 0 && gatewayID!=null)
{
if(gatewayID.charAt(gatewayID.length()-1) == '/')
{
System.out.println("Samba Sample: Check for / character :extractGatewaytype()");
gatewayID = gatewayID.substring(0, gatewayID.length()-1);
}
String gatewayType = _availavleTransports.get(gatewayID);
if(gatewayType!=null)
{
return gatewayType;
}
System.out.println("Samba Sample: No gateway type found for gateway ID= " + gatewayID + " :extractGatewaytype()");
}
System.out.println("Samba Sample: _availavleTransports size is zero or gatewayID is NULL :extractGatewaytype()");
return null;
} public String extractUserType(String userid)
{
System.out.println("Samba Sample: extract user type for user = " + userid + " :extractUserType()");
String type = null;
String serverAddress = StringUtils.parseServer(userid);
System.out.println("Samba Sample: BARE_SERVER:-" + serverAddress + " :extractUserType()");
if(serverAddress!=null)
{
type = extractGatewaytype(serverAddress);
if(type!=null)
{
System.out.println("Samba Sample: EXTRACTED GATEWAY TYPE:-"+ type+ " :extractUserType()");
return type;
}
}
System.out.println("Samba Sample: Unable to extract gateway type for user:-"+ userid + " :extractUserType()");
return null;
}