Re-add the login(String, String, String) method

This commit is contained in:
Florian Schmaus 2014-12-18 14:01:31 +01:00
parent d081055312
commit 4b77d00e91
5 changed files with 58 additions and 51 deletions

View File

@ -223,11 +223,8 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
} }
@Override @Override
protected void loginNonAnonymously() protected void loginNonAnonymously(String username, String password, String resource)
throws XMPPException, SmackException, IOException { throws XMPPException, SmackException, IOException {
String password = config.getPassword();
String resource = config.getResource();
String username = config.getUsername();
if (saslAuthentication.hasNonAnonymousAuthentication()) { if (saslAuthentication.hasNonAnonymousAuthentication()) {
// Authenticate using SASL // Authenticate using SASL
if (password != null) { if (password != null) {

View File

@ -73,6 +73,7 @@ import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.rosterstore.RosterStore; import org.jivesoftware.smack.rosterstore.RosterStore;
import org.jivesoftware.smack.util.DNSUtil; import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.dns.HostAddress; import org.jivesoftware.smack.util.dns.HostAddress;
import org.jxmpp.util.XmppStringUtils; import org.jxmpp.util.XmppStringUtils;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -359,6 +360,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/ */
protected abstract void connectInternal() throws SmackException, IOException, XMPPException; protected abstract void connectInternal() throws SmackException, IOException, XMPPException;
private String usedUsername, usedPassword, usedResource;
/** /**
* Logs in to the server using the strongest authentication mode supported by * Logs in to the server using the strongest authentication mode supported by
* the server, then sets presence to available. If the server supports SASL authentication * the server, then sets presence to available. If the server supports SASL authentication
@ -388,11 +391,56 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
if (isAnonymous()) { if (isAnonymous()) {
loginAnonymously(); loginAnonymously();
} else { } else {
loginNonAnonymously(); // The previously used username, password and resource take over precedence over the
// ones from the connection configuration
String username = usedUsername != null ? usedUsername : config.getUsername();
String password = usedPassword != null ? usedPassword : config.getPassword();
String resource = usedResource != null ? usedResource : config.getResource();
login(username, password, resource);
} }
} }
protected abstract void loginNonAnonymously() throws XMPPException, SmackException, IOException; /**
* Same as {@link #login(String, String, String)}, but takes the resource from the connection
* configuration.
*
* @param username
* @param password
* @throws XMPPException
* @throws SmackException
* @throws IOException
* @see #login
*/
public void login(String username, String password) throws XMPPException, SmackException,
IOException {
login(username, password, config.getResource());
}
/**
* Login with the given username. You may omit the password if a callback handler is used. If
* resource is null, then the server will generate one.
*
* @param username
* @param password
* @param resource
* @throws XMPPException
* @throws SmackException
* @throws IOException
* @see #login
*/
public void login(String username, String password, String resource) throws XMPPException,
SmackException, IOException {
if (StringUtils.isNullOrEmpty(username)) {
throw new IllegalArgumentException("Username must not be null or empty");
}
usedUsername = username;
usedPassword = password;
usedResource = resource;
loginNonAnonymously(username, password, resource);
}
protected abstract void loginNonAnonymously(String username, String password, String resource)
throws XMPPException, SmackException, IOException;
protected abstract void loginAnonymously() throws XMPPException, SmackException, IOException; protected abstract void loginAnonymously() throws XMPPException, SmackException, IOException;
@ -471,8 +519,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
} }
@Override @Override
public boolean isAnonymous() { public final boolean isAnonymous() {
return config.isAnonymous(); return config.getUsername() == null && usedUsername == null;
} }
private String serviceName; private String serviceName;

View File

@ -102,10 +102,6 @@ public abstract class ConnectionConfiguration {
} }
password = builder.password; password = builder.password;
callbackHandler = builder.callbackHandler; callbackHandler = builder.callbackHandler;
if (callbackHandler == null && (password == null || username == null) && !builder.anonymous) {
throw new IllegalArgumentException(
"Must provide either a username and password, a callback handler or set the connection configuration anonymous");
}
// Resource can be null, this means that the server must provide one // Resource can be null, this means that the server must provide one
resource = builder.resource; resource = builder.resource;
@ -142,10 +138,6 @@ public abstract class ConnectionConfiguration {
debuggerEnabled = builder.debuggerEnabled; debuggerEnabled = builder.debuggerEnabled;
} }
public boolean isAnonymous() {
return username == null && callbackHandler == null;
}
/** /**
* Returns the server name of the target server. * Returns the server name of the target server.
* *
@ -401,7 +393,6 @@ public abstract class ConnectionConfiguration {
private HostnameVerifier hostnameVerifier; private HostnameVerifier hostnameVerifier;
private String username; private String username;
private String password; private String password;
private boolean anonymous;
private String resource = "Smack"; private String resource = "Smack";
private boolean sendPresence = true; private boolean sendPresence = true;
private boolean rosterLoadedAtLogin = true; private boolean rosterLoadedAtLogin = true;
@ -421,8 +412,7 @@ public abstract class ConnectionConfiguration {
/** /**
* Set the XMPP entities username and password. * Set the XMPP entities username and password.
* <p> * <p>
* The username is the localpart of the entities JID, e.g. <code>localpart@example.org</code>. In order to * The username is the localpart of the entities JID, e.g. <code>localpart@example.org</code>.
* create an anonymous connection, call {@link #makeAnonymous} instead.
* </p> * </p>
* *
* @param username * @param username
@ -435,22 +425,6 @@ public abstract class ConnectionConfiguration {
return getThis(); return getThis();
} }
/**
* Create a configuration for a anonymous XMPP connection.
* <p>
* Anonyous connections don't provide a username or other authentification credentials like a password. Instead
* the XMPP server, if supporting anonymous connections, will assign a username to the client.
* </p>
*
* @return a reference to this builder.
*/
public B makeAnonymous() {
this.username = null;
this.password = null;
anonymous = true;
return getThis();
}
/** /**
* Set the service name of this XMPP service (i.e., the XMPP domain). * Set the service name of this XMPP service (i.e., the XMPP domain).
* *

View File

@ -45,7 +45,6 @@ import org.jivesoftware.smack.packet.TopLevelStreamElement;
*/ */
public class DummyConnection extends AbstractXMPPConnection { public class DummyConnection extends AbstractXMPPConnection {
private boolean anonymous = false;
private boolean reconnect = false; private boolean reconnect = false;
private String connectionID; private String connectionID;
@ -93,7 +92,6 @@ public class DummyConnection extends AbstractXMPPConnection {
connectionID = null; connectionID = null;
roster = null; roster = null;
authenticated = false; authenticated = false;
anonymous = false;
for (ConnectionListener listener : getConnectionListeners()) { for (ConnectionListener listener : getConnectionListeners()) {
listener.connectionClosed(); listener.connectionClosed();
@ -123,11 +121,6 @@ public class DummyConnection extends AbstractXMPPConnection {
return roster; return roster;
} }
@Override
public boolean isAnonymous() {
return anonymous;
}
@Override @Override
public boolean isSecureConnection() { public boolean isSecureConnection() {
return false; return false;
@ -139,15 +132,14 @@ public class DummyConnection extends AbstractXMPPConnection {
} }
@Override @Override
protected void loginNonAnonymously() protected void loginNonAnonymously(String username, String password, String resource)
throws XMPPException { throws XMPPException {
user = config.getUsername() user = username
+ "@" + "@"
+ config.getServiceName() + config.getServiceName()
+ "/" + "/"
+ (config.getResource() != null ? config.getResource() : "Test"); + (resource != null ? resource : "Test");
roster = new Roster(this); roster = new Roster(this);
anonymous = false;
authenticated = true; authenticated = true;
} }
@ -159,7 +151,6 @@ public class DummyConnection extends AbstractXMPPConnection {
if (isAuthenticated()) { if (isAuthenticated()) {
throw new IllegalStateException("Already logged in to server."); throw new IllegalStateException("Already logged in to server.");
} }
anonymous = true;
authenticated = true; authenticated = true;
} }

View File

@ -339,10 +339,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
} }
@Override @Override
protected synchronized void loginNonAnonymously() throws XMPPException, SmackException, IOException { protected synchronized void loginNonAnonymously(String username, String password, String resource) throws XMPPException, SmackException, IOException {
String password = config.getPassword();
String resource = config.getResource();
String username = config.getUsername();
if (saslAuthentication.hasNonAnonymousAuthentication()) { if (saslAuthentication.hasNonAnonymousAuthentication()) {
// Authenticate using SASL // Authenticate using SASL
if (password != null) { if (password != null) {