mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +01:00
Improved login method.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1780 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
5a8e4b59c7
commit
4e8a84834f
1 changed files with 60 additions and 13 deletions
|
@ -53,6 +53,7 @@
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.*;
|
import org.jivesoftware.smack.packet.*;
|
||||||
|
import org.jivesoftware.smack.packet.Error;
|
||||||
import org.jivesoftware.smack.filter.PacketIDFilter;
|
import org.jivesoftware.smack.filter.PacketIDFilter;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
@ -181,9 +182,10 @@ public class XMPPConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Login to the server using the server's preferred authentication mechanism and set
|
* Login to the server using the strongest authentication mode supported by
|
||||||
* our presence to available. If more than five seconds elapses without a response from
|
* the server, then set our presence to available. If more than five seconds
|
||||||
* the server, or if an error occurs, a XMPPException will be thrown.<p>
|
* elapses in each step of the authentication process without a response from
|
||||||
|
* the server, or if an error occurs, a XMPPException will be thrown.
|
||||||
*
|
*
|
||||||
* @param username the username.
|
* @param username the username.
|
||||||
* @param password the password.
|
* @param password the password.
|
||||||
|
@ -193,15 +195,58 @@ public class XMPPConnection {
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
throw new IllegalStateException("Not connected to server.");
|
throw new IllegalStateException("Not connected to server.");
|
||||||
}
|
}
|
||||||
Authentication auth = new Authentication(username, password, "Smack");
|
// If we send an authentication packet in "get" mode with just the username,
|
||||||
packetWriter.sendPacket(auth);
|
// the server will return the list of authentication protocols it supports.
|
||||||
|
Authentication discoveryAuth = new Authentication();
|
||||||
|
discoveryAuth.setType(IQ.Type.GET);
|
||||||
|
discoveryAuth.setUsername(username);
|
||||||
|
packetWriter.sendPacket(discoveryAuth);
|
||||||
// Wait up to five seconds for a response from the server.
|
// Wait up to five seconds for a response from the server.
|
||||||
PacketCollector collector = packetReader.createPacketCollector(
|
PacketCollector collector = packetReader.createPacketCollector(
|
||||||
|
new PacketIDFilter(discoveryAuth.getPacketID()));
|
||||||
|
Authentication authTypes = (Authentication)collector.nextResult(5000);
|
||||||
|
collector.cancel();
|
||||||
|
if (authTypes == null || authTypes.getType().equals(IQ.Type.ERROR)) {
|
||||||
|
throw new XMPPException("No response from the server.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now, create the authentication packet we'll send to the server.
|
||||||
|
Authentication auth = new Authentication();
|
||||||
|
auth.setUsername(username);
|
||||||
|
|
||||||
|
// Figure out if we should use digest or plain text authentication.
|
||||||
|
if (authTypes.getDigest() != null) {
|
||||||
|
auth.setDigest(connectionID, password);
|
||||||
|
}
|
||||||
|
else if (authTypes.getPassword() != null) {
|
||||||
|
auth.setPassword(password);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new XMPPException("Server does not support compatible authentication mechanism.");
|
||||||
|
}
|
||||||
|
|
||||||
|
auth.setResource("Smack");
|
||||||
|
packetWriter.sendPacket(auth);
|
||||||
|
// Wait up to five seconds for a response from the server.
|
||||||
|
collector = packetReader.createPacketCollector(
|
||||||
new PacketIDFilter(auth.getPacketID()));
|
new PacketIDFilter(auth.getPacketID()));
|
||||||
IQ response = (IQ)collector.nextResult(5000);
|
IQ response = (IQ)collector.nextResult(5000);
|
||||||
if (response == null || response.getType() == IQ.Type.ERROR) {
|
if (response == null) {
|
||||||
throw new XMPPException("Authentication failed.");
|
throw new XMPPException("Authentication failed.");
|
||||||
}
|
}
|
||||||
|
else if (response.getType() == IQ.Type.ERROR) {
|
||||||
|
if (response.getError() == null) {
|
||||||
|
throw new XMPPException("Authentication failed.");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Error error = response.getError();
|
||||||
|
String msg = "Authentication failed -- " + error.getCode();
|
||||||
|
if (error.getMessage() != null) {
|
||||||
|
msg += ": " + error.getMessage();
|
||||||
|
}
|
||||||
|
throw new XMPPException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
// We're done with the collector, so explicitly cancel it.
|
// We're done with the collector, so explicitly cancel it.
|
||||||
collector.cancel();
|
collector.cancel();
|
||||||
// Set presence to online.
|
// Set presence to online.
|
||||||
|
@ -322,7 +367,7 @@ public class XMPPConnection {
|
||||||
allPacketListener.start();
|
allPacketListener.start();
|
||||||
}
|
}
|
||||||
// Start the packet writer. This will open a Jabber stream to the server
|
// Start the packet writer. This will open a Jabber stream to the server
|
||||||
packetWriter.start();
|
packetWriter.startup();
|
||||||
// Start the packet reader. The startup() method will block until we
|
// Start the packet reader. The startup() method will block until we
|
||||||
// get an opening stream packet back from server.
|
// get an opening stream packet back from server.
|
||||||
packetReader.startup();
|
packetReader.startup();
|
||||||
|
@ -403,12 +448,14 @@ public class XMPPConnection {
|
||||||
|
|
||||||
public int read(char cbuf[], int off, int len) throws IOException {
|
public int read(char cbuf[], int off, int len) throws IOException {
|
||||||
int count = myReader.read(cbuf, off, len);
|
int count = myReader.read(cbuf, off, len);
|
||||||
String str = new String(cbuf, off, count);
|
if (count > 0) {
|
||||||
receivedText1.append(str);
|
String str = new String(cbuf, off, count);
|
||||||
receivedText2.append(str);
|
receivedText1.append(str);
|
||||||
if (str.endsWith(">")) {
|
receivedText2.append(str);
|
||||||
receivedText1.append(NEWLINE);
|
if (str.endsWith(">")) {
|
||||||
receivedText2.append(NEWLINE);
|
receivedText1.append(NEWLINE);
|
||||||
|
receivedText2.append(NEWLINE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue