protected List handleSet()

in server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0077_inbandreg/InBandRegistrationHandler.java [96:158]


    protected List<Stanza> handleSet(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, SessionContext sessionContext, StanzaBroker stanzaBroker) {
        //        <iq type='set' id='reg2'>
        //        <query xmlns='jabber:iq:register'>
        //          <username>bill</username>
        //          <password>Calliope</password>
        //          <email>bard@shakespeare.lit</email>
        //        </query>
        //      </iq>
        
        if(sessionContext.getState().equals(SessionState.STARTED)
                || sessionContext.getState().equals(SessionState.ENCRYPTED)
                || sessionContext.getState().equals(SessionState.AUTHENTICATED)) {

            try {
                XMLElement query = stanza.getSingleInnerElementsNamed("query", NamespaceURIs.JABBER_IQ_REGISTER);
                XMLElement usernameElm = query.getSingleInnerElementsNamed("username", NamespaceURIs.JABBER_IQ_REGISTER);
                if(usernameElm == null || usernameElm.getInnerText() == null) throw new XMLSemanticError("Invalid or missing username");
                String username = usernameElm.getInnerText().getText();
                
                XMLElement passwordElm = query.getSingleInnerElementsNamed("password", NamespaceURIs.JABBER_IQ_REGISTER);
                if(passwordElm == null ||  passwordElm.getInnerText() == null) throw new XMLSemanticError("Invalid or missing password");
                String password = passwordElm.getInnerText().getText();
                if(password.trim().length() == 0) throw new XMLSemanticError("Invalid password");
    
                AccountManagement accountManagement = serverRuntimeContext.getStorageProvider(AccountManagement.class);
                Entity user;
                if(username.contains("@")) {
                    user = EntityImpl.parse(username);
                    if(!EntityUtils.isAddressingServer(serverRuntimeContext.getServerEntity(), user)) {
                        throw new XMLSemanticError("Username must be in the same domain as the server");
                    }
                } else {
                    user = EntityImpl.parse(username + "@" + serverRuntimeContext.getServerEntity());
                }
                
                if(sessionContext.getState().equals(SessionState.AUTHENTICATED)) {
                    if(accountManagement.verifyAccountExists(user)) {
                        // account exists
                        accountManagement.changePassword(user, password);
                    } else {
                        throw new AccountCreationException("Account does not exist");
                    }
                } else {
                    if(accountManagement.verifyAccountExists(user)) {
                        // account exists
                        throw new AccountCreationException("Account already exists");
                    } else {
                        accountManagement.addUser(user, password);
                    }
                }
                return Collections.singletonList(StanzaBuilder.createDirectReply(stanza, true, IQStanzaType.RESULT).build());
                
            } catch (XMLSemanticError e) {
                return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.NOT_ACCEPTABLE, stanza, StanzaErrorType.MODIFY, 406, null, null, null));
            } catch (EntityFormatException e) {
                return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.NOT_ACCEPTABLE, stanza, StanzaErrorType.MODIFY, 406, null, null, null));
            } catch (AccountCreationException e) {
                return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.CONFLICT, stanza, StanzaErrorType.CANCEL, 409, e.getMessage(), null, null));
            }
        } else {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.SERVICE_UNAVAILABLE, stanza, StanzaErrorType.CANCEL, null, null, null));
        }
    }