Project

General

Profile

Example codes

A very simple client in Groovy

import tigase.jaxmpp.j2se.Jaxmpp
import tigase.jaxmpp.j2se.connectors.bosh.BoshConnector
import tigase.jaxmpp.core.client.SessionObject
import tigase.jaxmpp.core.client.JID
import tigase.jaxmpp.core.client.BareJID
import tigase.jaxmpp.j2se.ConnectionConfiguration

Jaxmpp jaxmpp = new Jaxmpp()
jaxmpp.with {
  getConnectionConfiguration().setConnectionType(ConnectionConfiguration.ConnectionType.bosh)
  getConnectionConfiguration().setBoshService("http://tigase.im:5280/")
  getConnectionConfiguration().setUserJID("some-user@tigase.im")
  getConnectionConfiguration().setUserPassword("some-password")
}

println("Loging in...")
jaxmpp.login(true)

println("Sending message...")
jaxmpp.sendMessage(JID.jidInstance("a-friend@some-server.com"), "Test", "This is a test")
println("Sleeping...")
Thread.sleep(2000)

println("Disconnecting...")
jaxmpp.disconnect()
println("END.")

Configuration for SASL-ANONYMOUS

import tigase.jaxmpp.j2se.Jaxmpp

Jaxmpp jaxmpp = new Jaxmpp()
jaxmpp.getConnectionConfiguration().setDomain("tigase.org")
jaxmpp.login(true)

:)

A very simple client in Java outputing presence

import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.SessionObject;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.observer.Listener;
import tigase.jaxmpp.core.client.xmpp.modules.presence.PresenceModule;
import tigase.jaxmpp.core.client.xmpp.modules.presence.PresenceModule.PresenceEvent;
import tigase.jaxmpp.j2se.Jaxmpp;

public class JaxmppExample {

    public static void main( String[] args ) throws JaxmppException, InterruptedException {

        final Jaxmpp contact = new Jaxmpp();

        contact.getModulesManager().getModule( PresenceModule.class ).addListener( PresenceModule.ContactChangedPresence, new Listener<PresenceModule.PresenceEvent>() {

            @Override
            public void handleEvent( PresenceEvent be ) throws JaxmppException {
                System.out.println( String.format( "Presence received:\t %1$s is now %2$s (%3$s)", be.getJid(), be.getShow(), be.getStatus() != null ? be.getStatus() : "none" ) );
            }
        } );

        contact.getProperties().setUserProperty( SessionObject.USER_BARE_JID, BareJID.bareJIDInstance( "user@domain" ) );
        contact.getProperties().setUserProperty( SessionObject.PASSWORD, "pass" );

        System.out.println( "Loging in..." );

        contact.login();

        System.out.println( "Waiting for the presence for 10 minutes" );

        Thread.sleep( 10 * 60 * 1000 );

        contact.sendMessage(JID.jidInstance("user@test.domain"), "Test", "This is a test");

        Thread.sleep( 2 * 60 * 1000 );

        contact.disconnect();

    }
}

A very simple client in Groovy outputing presence

import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.SessionObject;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.observer.Listener;
import tigase.jaxmpp.core.client.xmpp.modules.presence.PresenceModule;
import tigase.jaxmpp.core.client.xmpp.modules.presence.PresenceModule.PresenceEvent;
import tigase.jaxmpp.j2se.Jaxmpp;

final Jaxmpp contact = new Jaxmpp()

contact.getProperties().setUserProperty( SessionObject.USER_BARE_JID, BareJID.bareJIDInstance( "admin@firefly" ) )
contact.getProperties().setUserProperty( SessionObject.PASSWORD, "pass" )

contact.getModulesManager().getModule( PresenceModule.class ).addListener( PresenceModule.ContactChangedPresence,  new Listener<PresenceModule.PresenceEvent>() {

        public void handleEvent( PresenceEvent be ) {
            def msg = (be.getStatus() != null) ? be.getStatus() : "none"
            println( "Presence received:\t" + be.getJid() + " is now " + be.getShow() + " (" + msg + ")" );
        }
    } )

println( "Loging in..." );

contact.login()

println( "Waiting for the presence for 10 minutes" )

Thread.sleep( 10 * 60 * 1000 )

disconnect()

PubSub

PubSubModule pubsub = jaxmpp.getModule(PubSubModule.class);

pubsub.createNode(BareJID.bareJIDInstance("pubsub.somewhere.com"), "nodeName", new PubSubAsyncCallback() {

    @Override
    public void onTimeout() throws JaxmppException {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSuccess(Stanza responseStanza) throws JaxmppException {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onEror(IQ response, ErrorCondition errorCondition, PubSubErrorCondition pubSubErrorCondition) {
        // TODO Auto-generated method stub

    }
});


pubsub.deleteNode(BareJID.bareJIDInstance("pubsub.somewhere.com"), "nodeName", new PubSubAsyncCallback() {

    @Override
    public void onTimeout() throws JaxmppException {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSuccess(Stanza responseStanza) throws JaxmppException {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onEror(IQ response, ErrorCondition errorCondition, PubSubErrorCondition pubSubErrorCondition) {
        // TODO Auto-generated method stub

    }
});

There are also methods: subscribe(), unsubscribe(), publish(), deleteItem(), retrieveItem()

How to run examples

Jingle

// Create Jingle module and register it - should be done before connecting to server
JingleModule jingle = new JingleModule(null, jaxmpp.getSessionObject(), new PacketWriter() {

    @Override
    public void write(final Element stanza) throws JaxmppException {
        jaxmpp.send(Stanza.create(stanza);
    }

    @Override
    public void write(Element stanza, AsyncCallback asyncCallback) throws JaxmppException {
        jaxmpp.send(Stanza.create(stanza), asyncCallback);
    }

    @Override
    public void write(Element stanza, Long timeout, AsyncCallback asyncCallback) throws JaxmppException {
        writer.write(Stanza.create(stanza), timeout, asyncCallback);
    }
});
jaxmpp.getModulesManager().register(jingle);

// Get registered Jingle module
jingle = jaxmpp.getModule(JingleModule.class);

// Register event listeners - all notifications from Jingle module are events
jingle.addListener(JingleSessionInitiation, new Listener<JingleSessionInitiationEvent>() { /* implement me */ });
jingle.addListener(JingleSessionAccept, new Listener<JingleSessionAcceptEvent>() { /* implement me */ });
jingle.addListener(JingleSessionInfo, new Listener<JingleSessionInfoEvent>() { /* implement me */ });
jingle.addListener(JingleSessionTerminate, new Listener<JingleSessionTerminateEvent>() { /* implement me */ });

// Start session initiation 
Element description = new DefaultElement(); // element needs to be created and filled as in XEP-0166
List<Element> transports = new ArrayList<Element>(); // list of transport elements needs to be created and filled as in XEP
// send session initiation request
jingle.initiateSession(JID.jidInstance("user@domain/test"), sid, name, description, transports);

// Accept session
jingle.acceptSession(JID.jidInstance("user@domain/test"), sid, name, description, transport);

// To terminate accepted session
jingle.terminateSession(JID.jidInstance("user@domain/test"), sid, JID.jidInstance("user@domain/test"));

Example implementation of file transfer using Jingle

Sender class

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import tigase.jaxmpp.core.client.AsyncCallback;
import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.JID;
import tigase.jaxmpp.core.client.JaxmppCore;
import tigase.jaxmpp.core.client.PacketWriter;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.observer.Listener;
import tigase.jaxmpp.core.client.xml.DefaultElement;
import tigase.jaxmpp.core.client.xml.Element;
import tigase.jaxmpp.core.client.xmpp.modules.jingle.JingleModule;
import tigase.jaxmpp.core.client.xmpp.modules.jingle.JingleModule.JingleSessionAcceptEvent;
import tigase.jaxmpp.core.client.xmpp.modules.jingle.JingleModule.JingleTransportInfoEvent;
import tigase.jaxmpp.core.client.xmpp.stanzas.Stanza;
import tigase.jaxmpp.j2se.Jaxmpp;

public class Sender {

        private static final Logger log = Logger.getLogger(Sender.class.getCanonicalName());

        private final Jaxmpp jaxmpp;

        public Sender(JID jid, String password) {
                jaxmpp = new Jaxmpp();
                jaxmpp.getSessionObject().setUserProperty(JaxmppCore.AUTOADD_STANZA_ID_KEY, true);

                // create and register JingleModule
                jaxmpp.getModulesManager().register(new JingleModule(null, jaxmpp.getSessionObject(), new PacketWriter() {

                        public void write(Element stanza) throws JaxmppException {
                                jaxmpp.send(Stanza.create(stanza));
                        }

                        public void write(Element stanza, AsyncCallback asyncCallback) throws JaxmppException {
                                jaxmpp.send(Stanza.create(stanza), asyncCallback);
                        }

                        public void write(Element stanza, Long timeout, AsyncCallback asyncCallback) throws JaxmppException {
                                jaxmpp.send(Stanza.create(stanza), timeout, asyncCallback);
                        }

                }));

                // add listeners for jingle session events
                jaxmpp.getModulesManager().getModule(JingleModule.class).addListener(JingleModule.JingleSessionAccept, new JingleSessionAcceptListener(this, jaxmpp));
                jaxmpp.getModulesManager().getModule(JingleModule.class).addListener(JingleModule.JingleTransportInfo, new JingleTransportInfoListener(this, jaxmpp));

                jaxmpp.getConnectionConfiguration().setUserJID(jid.getBareJid());
                jaxmpp.getConnectionConfiguration().setResource(jid.getResource());
                jaxmpp.getConnectionConfiguration().setUserPassword(password);                
        }

        public void login() throws JaxmppException {
                jaxmpp.login(true);
        }

        public void createSession(JID recipient, Element description, String sid) {
                JingleModule jingle = jaxmpp.getModulesManager().getModule(JingleModule.class);

                try {
                        List<Element> transports = new ArrayList<Element>();

                        // creation of example description of transport
                        Element transport = new DefaultElement("transport");
                        transport.setXMLNS("urn:xmpp:jingle:transports:s5b:1");
                        transport.setAttribute("sid", "sdjhdskf");
                        transport.setAttribute("mode", "tcp");                        
                        transports.add(transport);

                        // creation of example transport candidate
                        Element candidate = new DefaultElement("candidate");
                        candidate.setAttribute("cid", "dsfkjlsdjf");
                        candidate.setAttribute("host", "127.0.0.1");
                        candidate.setAttribute("jid", jaxmpp.getSessionObject().getBindedJid().toString());
                        candidate.setAttribute("port", String.valueOf(8134));
                        candidate.setAttribute("priority", String.valueOf(7659));
                        candidate.setAttribute("type","direct");
                        transport.addChild(candidate);

                        jingle.initiateSession(recipient, sid, "ex", description, transports);
                } catch (JaxmppException ex) {
                        log.log(Level.SEVERE, null, ex);
                }
        }

        /**
         * notify recipient that transfer finished and close session (sid)
         * @param recipient
         * @param sid 
         */
        public void closeSession(JID recipient, String sid) {
                JingleModule jingle = jaxmpp.getModulesManager().getModule(JingleModule.class);
                try {
                        jingle.terminateSession(recipient, sid, jaxmpp.getSessionObject().getBindedJid());
                } catch (JaxmppException ex) {
                        log.log(Level.SEVERE, null, ex);
                }
        }

        /**
         * recipient accepted session (sid)
         * @param recipient
         * @param sid 
         */
        protected void sessionAccepted(JID recipient, String sid) {
                log.log(Level.INFO, "session with sid = {0} accepted by {1}", new String[] { sid, recipient.toString() });
        }

        /**
         * recipient notifies sender that it established connection for session (sid) using transport (transportSid)
         * and candidate (cid)
         * @param recipient
         * @param sid
         * @param transportSid
         * @param cid 
         */
        protected void startSendingData(JID recipient, String sid, String transportSid, String cid) {
                // simulated time for sending data
                log.log(Level.INFO, "session with sid = {0} started transfer using cid = {1} to = {2}", new String[] { sid, cid, recipient.toString() });
                try {
                        Thread.sleep(10000);
                } catch (InterruptedException ex) {
                        log.log(Level.SEVERE, null, ex);
                }

                closeSession(recipient, sid);
        }

        private class JingleSessionAcceptListener implements Listener<JingleModule.JingleSessionAcceptEvent> {

                private final Sender sender;
                private final Jaxmpp jaxmpp;

                public JingleSessionAcceptListener(final Sender sender, final Jaxmpp jaxmpp) {
                        this.sender = sender;
                        this.jaxmpp = jaxmpp;
                }

                public void handleEvent(JingleSessionAcceptEvent be) throws JaxmppException {
                        Element description = be.getDescription();
                        JID sender = be.getSender();
                        String sid = be.getSid();

                        this.sender.sessionAccepted(sender, sid);
                }

        }

        private class JingleTransportInfoListener implements Listener<JingleModule.JingleTransportInfoEvent> {

                private final Sender sender;
                private final Jaxmpp jaxmpp;

                public JingleTransportInfoListener(final Sender sender, final Jaxmpp jaxmpp) {
                        this.sender = sender;
                        this.jaxmpp = jaxmpp;
                }

                public void handleEvent(JingleTransportInfoEvent be) throws JaxmppException {
                        JID sender = be.getSender();
                        String sid = be.getSid();
                        Element content = be.getContent();
                        if (content != null) {
                                List<Element> transports = content.getChildren("transport");
                                if (transports == null || transports.isEmpty()) return;

                                Element transport = transports.get(0);                                
                                String transportSid = transport.getAttribute("sid");

                                List<Element> candidatesUsed = transport.getChildren("candidate-used");
                                if (candidatesUsed == null || candidatesUsed.isEmpty()) return;

                                Element candidateUsed = candidatesUsed.get(0);
                                String cid = candidateUsed.getAttribute("cid");

                                this.sender.startSendingData(sender, sid, transportSid, cid);
                        }
                }

        }
}

Receiver class

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import tigase.jaxmpp.core.client.AsyncCallback;
import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.Connector;
import tigase.jaxmpp.core.client.Connector.ConnectorEvent;
import tigase.jaxmpp.core.client.JID;
import tigase.jaxmpp.core.client.JaxmppCore;
import tigase.jaxmpp.core.client.PacketWriter;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.observer.Listener;
import tigase.jaxmpp.core.client.xml.DefaultElement;
import tigase.jaxmpp.core.client.xml.Element;
import tigase.jaxmpp.core.client.xmpp.modules.jingle.JingleModule;
import tigase.jaxmpp.core.client.xmpp.modules.jingle.JingleModule.JingleSessionInitiationEvent;
import tigase.jaxmpp.core.client.xmpp.modules.jingle.JingleModule.JingleSessionTerminateEvent;
import tigase.jaxmpp.core.client.xmpp.stanzas.Stanza;
import tigase.jaxmpp.j2se.Jaxmpp;
import tigase.jaxmpp.j2se.connectors.socket.SocketConnector;

public class Receiver {

        private static final Logger log = Logger.getLogger(Receiver.class.getCanonicalName());

        private final Jaxmpp jaxmpp;

        public Receiver(JID jid, String password) {
                jaxmpp = new Jaxmpp();
                jaxmpp.getSessionObject().setUserProperty(JaxmppCore.AUTOADD_STANZA_ID_KEY, true);

                jaxmpp.addListener(SocketConnector.StanzaReceived, new Listener<SocketConnector.ConnectorEvent>() {

                        public void handleEvent(ConnectorEvent be) throws JaxmppException {
                                log.info("RECEIVED: " + be.getStanza().getAsString());
                        }

                });

                jaxmpp.addListener(SocketConnector.StanzaSending, new Listener<SocketConnector.ConnectorEvent>() {

                        public void handleEvent(ConnectorEvent be) throws JaxmppException {
                                log.info("SENDING: " + be.getStanza().getAsString());
                        }

                });

                // create and register JingleModule
                jaxmpp.getModulesManager().register(new JingleModule(null, jaxmpp.getSessionObject(), new PacketWriter() {

                        public void write(Element stanza) throws JaxmppException {
                                jaxmpp.send(Stanza.create(stanza));
                        }

                        public void write(Element stanza, AsyncCallback asyncCallback) throws JaxmppException {
                                jaxmpp.send(Stanza.create(stanza), asyncCallback);
                        }

                        public void write(Element stanza, Long timeout, AsyncCallback asyncCallback) throws JaxmppException {
                                jaxmpp.send(Stanza.create(stanza), timeout, asyncCallback);
                        }

                }));

                // add listeners for jingle session events
                jaxmpp.getModulesManager().getModule(JingleModule.class).addListener(JingleModule.JingleSessionInitiation, new JingleSessionInitiateListener(this, jaxmpp));
                jaxmpp.getModulesManager().getModule(JingleModule.class).addListener(JingleModule.JingleSessionTerminate, new JingleSessionTerminateListener(this, jaxmpp));

                jaxmpp.getConnectionConfiguration().setUserJID(jid.getBareJid());
                jaxmpp.getConnectionConfiguration().setResource(jid.getResource());
                jaxmpp.getConnectionConfiguration().setUserPassword(password);
        }

        public void login() throws JaxmppException {
                jaxmpp.login(true);
        }

        /**
         * in this method we try to connect to endpoints and if we succeed we call connectedToEndpoint
         * @param jid
         * @param sid
         * @param transports 
         */
        protected void tryToConnectToEndpoint(JID jid, String sid, List<Element> transports) {
                try {
                        Element transport = transports.get(0);

                        // get candidate as it contains informations needed for connection (can be more than 1)
                        Element candidate = transport.getChildren("candidate").get(0);
                        String host = candidate.getAttribute("host");
                        int port = Integer.parseInt(candidate.getAttribute("port"));
                        String cid = candidate.getAttribute("cid");
                        log.log(Level.INFO, "connecting to endpoint with cid = {0} and address = {1}", new Object[] { cid, host + ":" + port });

                        // simulation of connection delay
                        try {                
                                Thread.sleep(4000);
                        } catch (InterruptedException ex) {
                                Logger.getLogger(Receiver.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        connectedToEndpoint(jid, sid, transport, cid);
                }
                catch (JaxmppException ex) {
                        log.log(Level.SEVERE, null, ex);
                }
        }

        protected void connectedToEndpoint(JID jid, String sid, Element transport, String cid) {
                JingleModule jingle = jaxmpp.getModule(JingleModule.class);

                try {
                        // @todo - implement content creation
                        Element content = new DefaultElement("content");
                        content.setAttribute("creator", "initiator");
                        content.setAttribute("name", "ex");

                        Element selectedTransport = new DefaultElement("transport");
                        selectedTransport.setXMLNS(transport.getXMLNS());
                        selectedTransport.setAttribute("sid", transport.getAttribute("sid"));

                        content.addChild(selectedTransport);
                        Element candidateUsed = new DefaultElement("candidate-used");
                        candidateUsed.setAttribute("cid", cid);                        
                        selectedTransport.addChild(candidateUsed);

                        // start receiving data
                        startReceivingData();

                        // notify initiator that we connected and we are ready for file transfer
                        jingle.transportInfo(jid, jid, sid, content);               
                } catch (JaxmppException ex) {
                        log.log(Level.SEVERE, null, ex);
                }
        }

        /**
         * method will start receiving data - propably in another thread
         */
        private void startReceivingData() {
                log.log(Level.INFO, "starting receiving data in separate thread");
        }

        private class JingleSessionInitiateListener implements Listener<JingleSessionInitiationEvent> {

                private final Receiver receiver;
                private final Jaxmpp jaxmpp;

                public JingleSessionInitiateListener(final Receiver receiver, final Jaxmpp jaxmpp) {
                        this.receiver = receiver;
                        this.jaxmpp = jaxmpp;
                }

                public void handleEvent(JingleSessionInitiationEvent be) throws JaxmppException {
                        JID sender = be.getSender();
                        String sid = be.getSid();
                        String name = "file-transfer";
                        Element description = be.getDescription();

                        // here we create list of our endpoints to try to connect (can be ommited)
                        List<Element> transports = null;

                        // send session-accept as we accept file transfer
                        jaxmpp.getModulesManager().getModule(JingleModule.class).acceptSession(sender, sid, name, DefaultElement.create(description), transports);                        

                        receiver.tryToConnectToEndpoint(sender, sid, be.getTransports());
                }

        }

        private class JingleSessionTerminateListener implements Listener<JingleSessionTerminateEvent> {

                private final Receiver receiver;
                private final Jaxmpp jaxmpp;

                public JingleSessionTerminateListener(final Receiver receiver, final Jaxmpp jaxmpp) {
                        this.receiver = receiver;
                        this.jaxmpp = jaxmpp;
                }

                public void handleEvent(JingleSessionTerminateEvent be) throws JaxmppException {
                        log.log(Level.INFO, "session sid = {0} terminated", be.getSid());
                }

        }
}

Example class for running Sender and Receiver

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.JID;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.xml.DefaultElement;
import tigase.jaxmpp.core.client.xml.Element;
import tigase.jaxmpp.core.client.xml.XMLException;

/**
 * Hello world!
 *
 */
public class Example {

        public static void main(String[] args) {
                Logger.getGlobal().setLevel(Level.ALL);

                JID recipientJid = JID.jidInstance("test-jingle1@tigase.im/receiver");
                JID senderJid = JID.jidInstance("test-jingle2@tigase.im/sender");

                Receiver receiver = new Receiver(recipientJid, "test-jingle");
                Sender sender = new Sender(senderJid, "test-jingle");
                try {
                        receiver.login();
                        sender.login();
                } catch (JaxmppException ex) {
                        Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
                }

                // create example description of file to transfer to recipient
                try {
                        Element description = new DefaultElement("description");
                        description.setXMLNS("urn:xmpp:jingle:apps:file-transfer:3");

                        Element offer = new DefaultElement("offer");
                        description.addChild(offer);

                        Element file = new DefaultElement("file");
                        file.addChild(new DefaultElement("name", "file.txt", null));
                        file.addChild(new DefaultElement("date", "1969-07-21T02:56:15Z", null));
                        file.addChild(new DefaultElement("size", "2469", null));
                        offer.addChild(file);

                        Element hash = new DefaultElement("hash", "552da749930852c69ae5d2141d3766b1", "urn:xmpp:hashes:1");
                        hash.setAttribute("algo", "sha-1");                        
                        file.addChild(hash);

                        sender.createSession(recipientJid, description, "9742dfsf" /* random generated jingle session id */);
                } catch (XMLException ex) {
                        Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                        Thread.sleep(90000);
                } catch (InterruptedException ex) {
                        Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
                }
        }

}

Client using custom TrustManager

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import javax.net.ssl.X509TrustManager;

import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.SessionObject;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.observer.Listener;
import tigase.jaxmpp.j2se.Jaxmpp;
import tigase.jaxmpp.j2se.connectors.socket.SocketConnector;
import tigase.jaxmpp.j2se.connectors.socket.SocketConnector.SocketConnectorEvent;

public class Jaxmpp2Demo {
    public static void main(String[] args) throws JaxmppException, InterruptedException {
        final Jaxmpp jaxmpp = new Jaxmpp();

        X509TrustManager trustManager = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                // TODO Auto-generated method stub
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                // TODO Auto-generated method stub
                System.out.println("SERVER CERTIFICATE: " + Arrays.toString(chain));
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                // TODO Auto-generated method stub
                return null;
            }
        };
        jaxmpp.getProperties().setUserProperty(SocketConnector.TRUST_MANAGERS_KEY, new X509TrustManager[] { trustManager });

        jaxmpp.addListener(SocketConnector.EncryptionEstablished, new Listener<SocketConnectorEvent>() {

            @Override
            public void handleEvent(SocketConnectorEvent be) throws JaxmppException {
                System.out.println("Stream is now encrypted!");
            }
        });

        jaxmpp.getProperties().setUserProperty(SessionObject.USER_BARE_JID, BareJID.bareJIDInstance("user@domain"));
        jaxmpp.getProperties().setUserProperty(SessionObject.PASSWORD, "pass");

        System.out.println("Loging in...");

        jaxmpp.login();

        Thread.sleep(10 * 60 * 1000);

        jaxmpp.disconnect();
    }
}