Search code examples
javaxmppsmack

How to connect XMPP bosh server using java smack library?


I have working xmpp client on webapp using strophe.js ,as per my use case scenario i have to switch to different pages rapidly

Current approach is not secure as jid and password is visible in java script ,I was finding work around to implement security in strophe client an trying to make connection time(with bosh) more shorter ,while going through the book "XMPP Programming with JavaScript and jQuery"by jake moffitt i came across one solution which element both of my above problems is to implement session mechanism.which says that we can use strophe attach(jid,sid,rid) to connect to existing connection,so i need SID and RID ,which i can get from application server!!!

book has given an example of automated connection to bosh server when user logged in the web application,author has implement it using an Django project in python,As I am using java as server side language i tried to implement same example using java smcak-4.0.3 and smack-bosh-4.0.3 but unable to connect to bosh server(i am using ejabberd as xmpp server)

my code is as below

 BOSHConfiguration config = new BOSHConfiguration(false,"192.168.0.106",5280,"/http-bind/","192.168.0.106");
                XMPPBOSHConnection xbc=new XMPPBOSHConnection(config); 
                xbc.connect();
                xbc.login("admin", "admin");
                System.out.println(xbc.getConnectionID());

stack trace

java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:352)
    at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:347)
    at org.jivesoftware.smack.SmackConfiguration.<clinit>(SmackConfiguration.java:155)
    at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:67)

When i tried to login to bosh server it fails every time,i am not sure what is wrong here can some one explain me?

One more thing i have find is one can get session identifier(SID) using "xbc.getConnectionID()" but how to find request identifier?

Any help on above problem will be appriciable!!!!

thanks in advance!


Solution

  • I had a similar problem. I donwload all the smack github I import smack.jar from /lib/ and add the 3 java files from /src/main/java/org/jivesoftware/smack/

    I tried to fix it by importing smack-bosh-3.2.2-jar-with-dependencies.jar from /target/. I don't know why this diddn't work.

    Finally, I read here that you need to download all the dependencies libraries : jbosh-0.6.0.jar xlightweb-2.5.jar xSocket-2.4.6.jar xpp3-1.1.3.3.jar dom4j-1.6.1.jar

    So I used smack.jar from /lib/ with all thoses libraries and this problem was solved.

    As i said in comments, you need after to retreive RID. I used jbosh sources and add following lines :

    In com.kenai.jbosh.BOSHClient class
    //I introduced a new property
    private Long rid;
    
    //commented the following code
    //long rid = requestIDSeq.getNextRID();
    //and at that place added
    this.rid = requestIDSeq.getNextRID();
    
    //and finally added a new getter for rid
    public Long getRid() {
        return rid;}
    

    Then in smack-bosh :

    In BOSHConnection.java
    public Long getRid() {
        return client.getRid();}
    public String getSid() {
        return sessionID;}
    

    Now I'm blocked cause my session is disconected. According to my openFire logs, it's because of overactivity. So I'm looking for a solution to reduce the number of presence messages.