Use Localpart in AccountManager

This commit is contained in:
Florian Schmaus 2015-08-20 19:01:33 +02:00
parent ef8fa1fa46
commit 7b1b20a13c
2 changed files with 22 additions and 4 deletions

View File

@ -34,7 +34,9 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.StanzaIdFilter; import org.jivesoftware.smack.filter.StanzaIdFilter;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.iqregister.packet.Registration; import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.jxmpp.jid.parts.Localpart;
/** /**
* Allows creation and management of accounts on an XMPP server. * Allows creation and management of accounts on an XMPP server.
@ -239,7 +241,7 @@ public final class AccountManager extends Manager {
* @throws NotConnectedException * @throws NotConnectedException
* @throws InterruptedException * @throws InterruptedException
*/ */
public void createAccount(String username, String password) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { public void createAccount(Localpart username, String password) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
// Create a map for all the required attributes, but give them blank values. // Create a map for all the required attributes, but give them blank values.
Map<String, String> attributes = new HashMap<String, String>(); Map<String, String> attributes = new HashMap<String, String>();
for (String attributeName : getAccountAttributes()) { for (String attributeName : getAccountAttributes()) {
@ -262,14 +264,21 @@ public final class AccountManager extends Manager {
* @throws InterruptedException * @throws InterruptedException
* @see #getAccountAttributes() * @see #getAccountAttributes()
*/ */
public void createAccount(String username, String password, Map<String, String> attributes) public void createAccount(Localpart username, String password, Map<String, String> attributes)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
if (!connection().isSecureConnection() && !allowSensitiveOperationOverInsecureConnection) { if (!connection().isSecureConnection() && !allowSensitiveOperationOverInsecureConnection) {
// TODO throw exception in newer Smack versions // TODO throw exception in newer Smack versions
LOGGER.warning("Creating account over insecure connection. " LOGGER.warning("Creating account over insecure connection. "
+ "This will throw an exception in future versions of Smack if AccountManager.sensitiveOperationOverInsecureConnection(true) is not set"); + "This will throw an exception in future versions of Smack if AccountManager.sensitiveOperationOverInsecureConnection(true) is not set");
} }
attributes.put("username", username); if (username == null) {
throw new IllegalArgumentException("Username must not be null");
}
if (StringUtils.isNullOrEmpty(password)) {
throw new IllegalArgumentException("Password must not be null");
}
attributes.put("username", username.toString());
attributes.put("password", password); attributes.put("password", password);
Registration reg = new Registration(attributes); Registration reg = new Registration(attributes);
reg.setType(IQ.Type.set); reg.setType(IQ.Type.set);

View File

@ -32,6 +32,8 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.iqregister.AccountManager; import org.jivesoftware.smackx.iqregister.AccountManager;
import org.jxmpp.jid.parts.Localpart;
import org.jxmpp.stringprep.XmppStringprepException;
public class IntTestUtil { public class IntTestUtil {
@ -58,7 +60,14 @@ public class IntTestUtil {
Map<String, String> additionalAttributes = new HashMap<>(); Map<String, String> additionalAttributes = new HashMap<>();
additionalAttributes.put("name", "Smack Integration Test"); additionalAttributes.put("name", "Smack Integration Test");
additionalAttributes.put("email", "flow@igniterealtime.org"); additionalAttributes.put("email", "flow@igniterealtime.org");
accountManager.createAccount(username, password, additionalAttributes); Localpart usernameLocalpart;
try {
usernameLocalpart = Localpart.from(username);
}
catch (XmppStringprepException e) {
throw new IllegalArgumentException("Invalid username: " + username, e);
}
accountManager.createAccount(usernameLocalpart, password, additionalAttributes);
return new UsernameAndPassword(username, password); return new UsernameAndPassword(username, password);
} }