RegistrationHelper.java (1653B)
1 package eu.siacs; 2 3 import rocks.xmpp.addr.Jid; 4 import rocks.xmpp.core.XmppException; 5 import rocks.xmpp.core.session.XmppClient; 6 import rocks.xmpp.extensions.register.RegistrationManager; 7 import rocks.xmpp.extensions.register.model.Registration; 8 9 import java.math.BigInteger; 10 import java.security.SecureRandom; 11 12 class RegistrationHelper { 13 static String register(Jid jid) throws RegistrationFailed, RegistrationNotSupported { 14 XmppClient client = XmppClient.create(jid.getDomain()); 15 try { 16 client.connect(jid); 17 } catch(XmppException e) { 18 throw new RegistrationFailed("unable to connect to server"); 19 } 20 try { 21 RegistrationManager registrationManager = client.getManager(RegistrationManager.class); 22 if (registrationManager.isRegistrationSupported().getResult()) { 23 String password = new BigInteger(64, new SecureRandom()).toString(36); 24 Registration registration = Registration.builder() 25 .username(jid.getLocal()) 26 .password(password) 27 .build(); 28 registrationManager.register(registration).getResult(); 29 return password; 30 } else { 31 throw new RegistrationNotSupported(); 32 } 33 } catch (XmppException e) { 34 throw new RegistrationFailed(e.getMessage()); 35 } 36 } 37 38 39 static class RegistrationNotSupported extends Exception { 40 41 } 42 43 static class RegistrationFailed extends Exception { 44 RegistrationFailed(String message) { 45 super(message); 46 } 47 } 48 }