powered by Jive Software

Trying to register with Gtalk using smack

Hi all,

I am a newbie to openfire and smack.

I am trying to do the registration of a gtalk user in my xmpp server using smack api through code.

I am not sure whether i am doing right or wrong.

please tell me whether i am doing right or whether i missing some steps to get this done , i am not able to find the problem.

Request and response packet through smack debugger is as follows :-

*******for service discovery

******for registration

rajiv.chaudhary.it@gmail.com rajiv xyz rajiv.chaudhary.it@gmail.com rajiv xyz

My Code is as follows :-

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverItems;

//con is connection with connection configuration of my XMPP server not the google server

//gatewaydomain is gmail.com i.e service name.

public class TransportRegistration
{
private DiscoverItems discoverItems;
public void registerUser(XMPPConnection con, String gatewayDomain,
String username, String password, String nickname)
{
try
{
boolean flag = true;

                try
                {
                    con.connect();
                   
                }
                catch(Exception ae)
                {
                    ae.printStackTrace();
                }
               //******************** For Service Discovery

ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(con);
try
{
System.out.println("service name is " + con.getServiceName());
discoverItems = disco.discoverItems(gatewayDomain);
}
catch (XMPPException e) {
System.out.println("Exception occurred " + e.getMessage());
discoverItems = new DiscoverItems();

}

                //************************For Service Discovery

Registration registration = new Registration();
registration.setType(IQ.Type.SET);
registration.setTo(gatewayDomain);
registration.addExtension(new GatewayRegisterExtension());
System.out.println("USer details:: " );
System.out.println(gatewayDomain);
System.out.println(username);
System.out.println(password);
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(“nick”, “rajiv”);
}
registration.setAttributes(attributes);

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

IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

collector.cancel();

if (response == null) {
flag = false;

                    System.out.println("Server Timed Out");
                }
                else if (response.getType() == IQ.Type.ERROR) {
                    System.out.println("Error is "+response.getError().toString());
                    flag = false;
                    System.out.println("Error registering user::"+ response.getError());
                }

// System.out.println(response.getType().RESULT);
if(flag)
{
Presence presence = new Presence(Presence.Type.available);
presence.setTo(“gmail.com”);
con.sendPacket(presence);
}
}
catch(Exception ae )
{
ae.printStackTrace();
}
}

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();
}
}
}

You need to be authenticated as well.

e.g. con.login( “username”, “password” );