Search code examples
javaxmppopenfiresmacksasl

Can't connect to XMPP(Openfire) server in Smack. SASLAuthentication exception


I am getting the following error for connecting Smack libraries to Openfire server. I googled and searched in stack-overflow but couldn't solve my problem

The Exception is as follows

Exception in thread "main" org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:352)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.login(XMPPTCPConnection.java:244)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:442)
at JabberSmackAPI.login(JabberSmackAPI.java:64)
at JabberSmackAPI.main(JabberSmackAPI.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

 Process finished with exit code 1

My Code is as follows

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.io.*;

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils;
import sun.rmi.runtime.Log;

import javax.net.ssl.*;
import javax.security.sasl.SaslException;

public class JabberSmackAPI implements MessageListener {

private XMPPConnection connection;
private final String mHost = "192.168.2.250";//if fails use "192.168.2.250" // server IP address or the
// host

public void login(String userName, String password) throws XMPPException, IOException, SmackException, NoSuchAlgorithmException, KeyManagementException {
    String service = StringUtils.parseServer(userName);
    final String user_name = StringUtils.parseName(userName);

    TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
                public void checkClientTrusted(
                        X509Certificate[] certs,
                        String authType) {
                }
                public void checkServerTrusted(
                        X509Certificate[] certs,
                        String authType) {
                }
            }
    };
    HostnameVerifier verifier = new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    ConnectionConfiguration config = new ConnectionConfiguration(mHost, 5222);
    SSLContext context= SSLContext.getInstance("TLS");
    context.init(null, trustAllCerts, new java.security.SecureRandom());
    config.setCustomSSLContext(context);
    config.setHostnameVerifier(verifier);

    config.setSendPresence(true);
    config.setDebuggerEnabled(false);
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    connection = new XMPPTCPConnection(config);

    SASLAuthentication.supportSASLMechanism("PLAIN", 0);
    connection.connect();
    System.out.println("JabberSmackAPI.login");
    connection.login(user_name, password);

}

public void sendMessage(String msg, String message) throws XMPPException, SmackException.NotConnectedException {
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    Chat chat = chatManager.createChat("ali", new MyMessageListener());
    chat.sendMessage(message);
}

public void displayBuddyList() {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();

    System.out.println("\n\n" + entries.size() + " buddy(ies):");
    for (RosterEntry r : entries) {
        System.out.println(r.getUser());
    }
}

public void disconnect() throws SmackException.NotConnectedException {
    connection.disconnect();
}

public void processMessage(Chat chat, Message message) {
    System.out.println("Received something: " + message.getBody());
    if (message.getType() == Message.Type.chat)
        System.out.println(chat.getParticipant() + " says: "
                + message.getBody());
}

public static void main(String args[]) throws XMPPException, IOException, SmackException, KeyManagementException, NoSuchAlgorithmException {
    // declare variables
    JabberSmackAPI c = new JabberSmackAPI();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String msg;

    // turn on the enhanced debugger
   // XMPPConnection.DEBUG_ENABLED = true;

    // Enter your login information here
    System.out.println("-----");
    System.out.println("Login information:");

    System.out.print("username: ");
    String login_username = br.readLine();

    System.out.print("password: ");
    String login_pass = br.readLine();

    c.login(login_username, login_pass);

    c.displayBuddyList();

    System.out.println("-----");

    System.out
            .println("Who do you want to talk to? - Type contacts full email address:");
    String talkTo = br.readLine();

    System.out.println("-----");
    System.out.println("All messages will be sent to " + talkTo);
    System.out.println("Enter your message in the console:");
    System.out.println("-----\n");

    while (!(msg = br.readLine()).equals("bye")) {
        c.sendMessage(msg, talkTo);
    }

    c.disconnect();
    System.exit(0);
}

private class MyMessageListener implements MessageListener {
    @Override
    public void processMessage(Chat chat, Message message) {
        String from = message.getFrom();
        String body = message.getBody();
        System.out.println(String.format("Received message " + body + " from " + from));
    }
}

}

any help is appriciated


Solution

  • You have this row:

    connection.login(user_name, password);
    

    it throws a NoResponseException:

    public static class SmackException.NoResponseException extends SmackException

    Exception thrown always when there was no response to an IQ request within the packet reply timeout of the used connection instance.

    This essentially means that you do not get a response, so you must check whether:

    • the receiver exists and is able to get and parse messages
    • the receiver is able to send responses
    • the sender is able to send messages
    • the connection settings are correct and up to date