Smack 4.1 anonymous registration to Openfire - android

I have tried enough but getting error when trying to register a user through smack 4.1 on openfire server without any login (anonymous). With a user login, the registration works fine.
I have already checked Smack 4.1 Android anonymous registration to Openfire link but still getting the same error (Bad Request - Modify).
<iq type="error" id="R6rBt-3" from="192.168.0.105" to="desktop-ebr9dof/a96febd1"><query xmlns="jabber:iq:register"><username>user4</username><password>uhifg123</password></query><error code="400" type="modify"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
below is the code snippet I've applied in my code:
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setServiceName("192.168.0.105");
configBuilder.setHost("192.168.0.105");
configBuilder.setPort(5222);
configBuilder.setDebuggerEnabled(true);
configBuilder.setCompressionEnabled(false);
configBuilder.setSendPresence(true);
configBuilder.setUsernameAndPassword("user4", "uhifg123");
XMPPTCPConnection connection = new XMPPTCPConnection(configBuilder.build());
try {
connection.connect();
AccountManager accountManager = AccountManager.getInstance(connection);
accountManager.sensitiveOperationOverInsecureConnection(true);
Map<String, String> map = new HashMap<String, String>();
map.put("username", "user4");
map.put("password", "uhifg123");
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
try {
accountManager.createAccount("user4", "uhifg123", map);
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}

Related

XMPP error reply received from myroom#conference.****/myroom#conference.****: XMPPError: item-not-found - cancel

I am not able to join the room. I have gone through the documentation XMPP Instant Room Error
Here is how i am creating instant room:
try {
UserSearchManager usm = new UserSearchManager(Utils.connection);
List<DomainBareJid> services = usm.getSearchServices();
String roomjid = "myroom#" + services.get(0);
mucJid = JidCreate.entityBareFrom(roomjid);
Log.d(TAG, mucJid.toString());
// Create the nickname.
nickname = Resourcepart.from(roomjid);
Log.d(TAG, nickname.toString());
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
}
// Get the MultiUserChatManager
manager = MultiUserChatManager.getInstanceFor(Utils.connection);
// Get a MultiUserChat using MultiUserChatManager
MultiUserChat muc = manager.getMultiUserChat(mucJid);
// Create the room and send an empty configuration form to make this an instant room
try {
// Prepare a list of owners of the new room
Set<Jid> owners = JidUtil.jidSetFrom(new String[] { "54321#rahul", "12345#rahul" });
muc.create(nickname)
.getConfigFormManager()
.setRoomOwners(owners)
.submitConfigurationForm();
muc.sendConfigurationForm(new Form(DataForm.Type.submit));
muc.join(nickname);
EntityBareJid invitemucJid = JidCreate.entityBareFrom("12345#rahul");
muc.invite(invitemucJid, "testing");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucAlreadyJoinedException e) {
e.printStackTrace();
} catch (MultiUserChatException.MissingMucCreationAcknowledgeException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucConfigurationNotSupportedException e) {
e.printStackTrace();
}
The MUC room is getting created successfully. But when i am checking through Invitation Listener and trying to join the room. It is giving error.
The Invitation listener code is as follow:
try {
Resourcepart nickname = Resourcepart.from(room.getRoom().toString());
room.join(nickname);
Log.d(TAG, "room status---> " + room.isJoined());
runOnUiThread(new Runnable() {
#Override
public void run() {
if (room.isJoined()) {
Toast.makeText(FriendListActivity.this, "Joined", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FriendListActivity.this, "Not joined", Toast.LENGTH_SHORT).show();
}
}
});
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
}
Below is screen shot of the stack Trace
As per the documentation it says that the MUC room is locked. But I have created instant room along with default configuration.
Maybe it's a network error, I do somethings wrong if I use Wifi but get corrcet result with cellular network.

not getting offline messages with smack

I am trying a chat application with openfire,smack and android where offline messages are not working. if both the users are online, able to send and receive the messages correctly . But if user A is offline and user B sends a message, User A is not getting the sent message of B once he is online.Tried possible solutions from stackoverflow but none of them working. using the below code to retrieve the offline messages.
new Thread(){
public void run(){
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setSocketFactory(SocketFactory.getDefault())
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName("123.456.0.854")//service name
.setHost("123.456.0.854") // host name
.setPort(5222) //port
.setUsernameAndPassword("phone", "admin")
.setConnectTimeout(40000)
.setCompressionEnabled(false).build();
connection = new XMPPTCPConnection(config);
try {
connection.connect();
connection.login("phone", "admin");
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Available");
try {
connection.sendStanza(presence);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection);
try {
System.out.println("Count is " +offlineMessageManager.getMessageCount());
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
}.start();
After lot struggle, I have resolved the issue. In your openfire admin page, go to "client settings" and reduce the idle time from 360sec (by default) to 1 sec(may be). Only then when you disconnected from Internet, it can detect that you are offline and preserve rest of the messages as OFFLINE.
#Override public void onNetworkConnectionChanged(boolean isConnected) {
if(isConnected){
new Thread() {
public void run() {
try {
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
builder.setUsernameAndPassword("phone", "admin");
builder.setSendPresence(true);
builder.setServiceName(<Service name>);
builder.setHost(<Host name>);
builder.setResource("Test");
builder.setDebuggerEnabled(true);
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Available");
connection = new XMPPTCPConnection(builder.build());
connection.connect();
connection.login();
Presence presence123 = new Presence(Presence.Type.available);
presence123.setStatus("Available");
try {
connection.sendStanza(presence123);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
StanzaFilter filter = new AndFilter(new StanzaTypeFilter(Message.class));
PacketListener myListener = new PacketListener()
{
public void processPacket(Stanza stanza)
{
retrieveMessage(stanza,userType);
}
};
connection.addPacketListener(myListener, filter);
try {
connection.sendStanza(presence);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
} catch (SmackException | XMPPException | IOException e) {
e.printStackTrace();
}
//return connection.isConnected();
}
}.start();
The above is working fine and able to retrieve the offline messages. The method "retrieveMessage(stanza,userType);" is used to process the incoming message and update the Adapter. Make sure to send the Presence as "Available" when you reconnect. Please let me know if there are still any issues.

How to re-join a group/room after login using smack 4.1

This is what i am doing:-
Trying to join a group after login.
connection.login(userDate.getUserId(), userDate.getUserPassword());
****after login ****
String groupName =
XmppUtils.buildMUCName(results.get(i).getGroupName().trim());
MultiUserChat multiUserChat = managerMuc.getMultiUserChat(groupName);
try {
if (!multiUserChat.isJoined()) {
multiUserChat.join(userId);
} } catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
public void joinMultiUserChatRoom(String userName, String roomName) {
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
// Create a MultiUserChat using an XMPPConnection for a room
MultiUserChat multiUserChat = manager.getMultiUserChat(roomName + "servername");
DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(-1);
try {
multiUserChat.join(userName+"servername`enter code here`",userName, history, connection.getPacketReplyTimeout());
} catch (Exception e) {
e.printStackTrace();
}
}

Smack 4.2.0-beta1 Error in creating group chat 'MultiUserChat. Cannot be accessed from outside package

I am developing Chat application. I have done one to one chat. But I cannot create Group chat because it shows an below error
'MultiUserChat(org.jivesoftware.smack.XMPPConnection, org.jxmpp.jid.EntityBareJid, org.jivesoftware.smackx.muc.MultiUserChatManager)' is not public in 'org.jivesoftware.smackx.muc.MultiUserChat'. 
Cannot be accessed from outside package
I have seen some answers about creating groups everyone using multiUserChat = new MultiUserChat(connection,room_name); to create a Group chat.
But in 4.2.0-beta1 version is not allowing us to access this class.
I have added my code below
public void createGroupChat() {
// Create a MultiUserChat using a Connection for a room
// (room name as the second parameter)
try {
Resourcepart nikname = Resourcepart.from("admin");
MultiUserChat mMultiUserChat = new MultiUserChat(connection, "room#conference.myserver");
mMultiUserChat.create(nikname);
Form form = mMultiUserChat.getConfigurationForm().createAnswerForm();
form.setAnswer("muc#roomconfig_publicroom", true);
form.setAnswer("muc#roomconfig_roomname", "room");
form.setAnswer("muc#roomconfig_roomowners", "owner");
form.setAnswer("muc#roomconfig_persistentroom", true);
mMultiUserChat.sendConfigurationForm(form);
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucAlreadyJoinedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (MultiUserChatException.MissingMucCreationAcknowledgeException e) {
e.printStackTrace();
} catch (NotConnectedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
}
}
Can anyone tell me how to create group in Latest smack 4.2.0-beta1 version?
If I asked anything wrong sorry for that.
I Found an answer to create group chat from this below link
https://github.com/igniterealtime/Smack/blob/master/documentation/extensions/muc.md
I have added my new code also. It can be helpful for some of other developers
public void createGroupChat() {
// Create a MultiUserChat using a Connection for a room
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
try {
EntityBareJid jid = JidCreate.entityBareFrom("myroom" + "#"
+ context.getString(R.string.serviceNameOld));
// Create a MultiUserChat using an XMPPConnection for a room
MultiUserChat muc = manager.getMultiUserChat(jid);
// Prepare a list of owners of the new room
Set<Jid> owners = JidUtil.jidSetFrom(new String[]{"mathan#example.org", "kumar#example.org"});
// Create the room
Resourcepart nickname = Resourcepart.from("mathan");
muc.create(nickname).getConfigFormManager().setRoomOwners(owners).submitConfigurationForm();
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucAlreadyJoinedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (MultiUserChatException.MissingMucCreationAcknowledgeException e) {
e.printStackTrace();
} catch (NotConnectedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucConfigurationNotSupportedException e) {
e.printStackTrace();
}
}

Error while trying to connect openfire using smack on android

XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("test", "test");
configBuilder.setResource("test");
configBuilder.setServiceName("37.139.26.142");
configBuilder.setHost("37.139.26.142");
configBuilder.setPort(5222);
configBuilder.setSendPresence(true);
configBuilder.setDebuggerEnabled(true);
configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.required );
SASLMechanism mechanism = new SASLDigestMD5Mechanism();
SASLAuthentication.registerSASLMechanism(mechanism);
SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
try {
connection.connect();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
try {
connection.login();
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I'm currently trying to handshake my android application and my openfire server(working on ubuntu). But I couldnt. I dont get any fail or something. Just nothing happens. And that feels bad.
Did you try to send a message? Are you sure that you are not connected?
Did you check on Openfire admin that your test user is not connected?
First I suggest you to try to send a message:
ChatManager chatmanager = ChatManager.getInstanceFor(connection);
Chat newChat = chatmanager.createChat("anotheruser#yourdomain", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
try {
newChat.sendMessage("Howdy!");
}
catch (XMPPException e) {
System.out.println("Error Delivering block");
}
I got this code from : http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html
Another suggestion is to disable the SecurityMode, just for a test.
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
If nothing of this works, try to use the configuration below, which works for me.
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setServiceName(serverAddress);
config.setHost(serverAddress);
config.setPort(5222);
config.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(config.build());
try {
connection.connect();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
try {
connection.login(loginUser, passwordUser);
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
First I find out this is not about Android part, this is about Openfire part. Because I couldnt connect it with Spark and I saw this in Logcat;
W/System.err: org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '37.139.26.142:5222' failed because java.net.SocketTimeoutException: failed to connect to /37.139.26.142 (port 5222) after 30000ms
Then I did some research and tried somethings and I saw it is about Ubuntu(at least for me). Then I moved my openfire server to Centos. Then I could be able to connect with Spark to it. Then I got another problem.
org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
And I solved this problem with this code below. I hope this can help others.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SmackConfiguration.DEBUG = true;
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("test", "test");
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setResource("test");
configBuilder.setServiceName("enter your server ip here");
configBuilder.setHost("eneter your server ip here");
configBuilder.setPort(5222);
configBuilder.setSendPresence(true);
configBuilder.setDebuggerEnabled(true);
SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
XMPPTCPConnection connection;
connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
try {
connection.connect();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
// Log into the server
try {
connection.login();
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Categories

Resources