I have a very simple Java application that sends messages to an openfire server using a custom account called "thesis.dimfccs.sensors", who is seding a message to another custom account called "thesis.dimfccs".
I am using openfire 3.9.1 and my Java application is able to connect to the server, but it is unable to login into it. When I try to login, I get the following error:
Initializing connection to server localhost, port 5222, service gmail.com
Connected: true
Logging in: thesis.dimfccs.sensors, thesis.dimfccs.sensors.pwd
Exception in thread "main" No response from the server.:
at org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:73)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:357)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:243)
at org.jivesoftware.smack.Connection.login(Connection.java:366)
at javacodegeeks.XmppManager.performLogin(XmppManager.java:76)
at javacodegeeks.XmppLocalTest.main(XmppLocalTest.java:44)
I know that the password and name for the test account are correct because I can enter the Spark IM client using them.
However, what really blows my mind, is that if I do the exact same thing using the "admin" account, everything works!
I have double checked the thesis.dimfccs.sensors password and gave it admin access, but it is still not enough.
Here is the code:
Main.java:
public class XmppLocalTest {
//Total number of messages sent during one minute.
public static final int MESSAGES_NUMBER = 120;
//Each message will have a random number between 0 and MAX_RANDOM.
public static final int MAX_RANDOM = 100;
private static final String SENDER_USERNAME = "thesis.dimfccs.sensors";
private static final String SENDER_PASSWORD = "thesis.dimfccs.sensors.pwd";
public static void main(String[] args) throws Exception {
String username = SENDER_USERNAME;
String password = SENDER_PASSWORD;
XmppManager xmppManager = new XmppManager("localhost", 5222,
"gmail.com");
xmppManager.init();
xmppManager.performLogin(username, password);
System.out.println("Login performed with success, setting status.");
xmppManager.setStatus(true, "Let the pain begin!");
System.out.println("Setting up receivers");
xmppManager.createEntry("thesis.dimfccs", "thesis.dimfccs");
//go message!
xmppManager.sendMessage("Hello world!", "[email protected]");
xmppManager.destroy();
}
}
XmppManager.java:
package javacodegeeks;
/**
* Main class with all the logic of the connection. Followed from the tutorials:
* http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html
* */
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
public class XmppManager {
private static final int packetReplyTimeout = 500; // millis
private String server;
private int port;
private String service;
private ConnectionConfiguration config;
private XMPPConnection connection;
private ChatManager chatManager;
private MessageListener messageListener;
public XmppManager(String aServer, int aPort, String aService) {
server = aServer;
port = aPort;
service = aService;
}
public XmppManager(String server, int port) {
this(server, port, null);
}
public void init() throws XMPPException {
System.out.println(String.format("Initializing connection to server " + server + ", port " + port
+ ", service " + service));
SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
if(service != null)
config = new ConnectionConfiguration(server, port, service);
else
config = new ConnectionConfiguration(server, port);
// config.setSASLAuthenticationEnabled(false);
// config.setSecurityMode(SecurityMode.disabled);
// SASLAuthentication.supportSASLMechanism("PLAIN", 0);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
connection = new XMPPConnection(config);
connection.connect();
System.out.println("Connected: " + connection.isConnected());
chatManager = connection.getChatManager();
messageListener = new MyMessageListener();
}
public void performLogin(String username, String password) throws XMPPException {
if (connection!=null && connection.isConnected()) {
System.out.println("Logging in: " + username + ", " + password);
connection.login(username, password);
}
}
public void setStatus(boolean available, String status) {
Presence.Type type = available? Type.available: Type.unavailable;
Presence presence = new Presence(type);
presence.setStatus(status);
connection.sendPacket(presence);
}
public void destroy() {
if (connection!=null && connection.isConnected()) {
connection.disconnect();
}
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
System.out.println(String.format("Sending message " + message + " to user " + buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);
chat.sendMessage(message);
}
public void createEntry(String user, String name) throws Exception {
System.out.println(String.format("Creating entry for buddy " + user + " with name " + name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);
}
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));
}
}
}
What am I doing wrong? How can I fix this?
NOTE: If you believe this question is poor, please leave a comment and suggest an edit instead of down-voting it. This way I can actually improve the question and everyone gets happy!
The problem was that my server seemed to be inconsistent somehow. After deleting and re.creating the users, the problem got fixed !