protected List handleSet()

in server/core/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java [134:204]


    protected List<Stanza> handleSet(IQStanza stanza, ServerRuntimeContext serverRuntimeContext,
            SessionContext sessionContext, StanzaBroker stanzaBroker) {
        RosterManager rosterManager = serverRuntimeContext.getStorageProvider(RosterManager.class);

        if (rosterManager == null) {
            return handleCannotRetrieveRoster(stanza, sessionContext);
        }

        Entity user = extractUniqueSenderJID(stanza, sessionContext);
        if (user == null || !user.isResourceSet()) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.UNKNOWN_SENDER,
                    stanza, StanzaErrorType.MODIFY,
                    "sender info insufficient: " + ((user == null) ? "no from" : user.getFullQualifiedName()), null,
                    null));
        }

        RosterItem setRosterItem;
        try {
            setRosterItem = RosterUtils.parseRosterItem(stanza);
        } catch (RosterBadRequestException e) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST,
                    stanza, StanzaErrorType.MODIFY, e.getMessage(), null, null));
        } catch (RosterNotAcceptableException e) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.NOT_ACCEPTABLE,
                    stanza, StanzaErrorType.MODIFY, e.getMessage(), null, null));
        }

        Entity contactJid = setRosterItem.getJid().getBareJID();

        RosterItem existingItem;
        try {
            existingItem = rosterManager.getContact(user.getBareJID(), contactJid);
        } catch (RosterException e) {
            existingItem = null;
        }

        if (setRosterItem.getSubscriptionType() == REMOVE) {
            // remove is handled in separate method, return afterwards
            return rosterItemRemove(stanza, sessionContext, rosterManager, user, contactJid, existingItem,
                    stanzaBroker);
        } /*
           * else: all other subscription types are ignored in a roster set and have been
           * filtered out by RosterUtils.parseRosterItem()
           */

        // proper set (update, not a remove)
        if (existingItem == null) {
            existingItem = new RosterItem(contactJid, NONE);
        }

        if (setRosterItem.getName() != null) {
            existingItem.setName(setRosterItem.getName());
            LOG.debug(user.getBareJID() + " roster: set roster item name to " + setRosterItem.getName());
        }
        existingItem.setGroups(setRosterItem.getGroups());
        LOG.debug(user.getBareJID() + " roster: roster item groups set to " + setRosterItem.getGroups());

        try {
            // update contact persistently
            rosterManager.addContact(user.getBareJID(), existingItem);
        } catch (RosterException e) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST,
                    stanza, StanzaErrorType.CANCEL, "roster item contact not (yet) in roster: " + contactJid, null,
                    null));
        }

        pushRosterItemToInterestedResources(sessionContext, user, existingItem, stanzaBroker);

        return Collections.singletonList(
                RosterStanzaUtils.createRosterItemIQ(user, stanza.getID(), IQStanzaType.RESULT, existingItem));
    }