private static RosterItem parseRosterItem()

in server/core/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterUtils.java [105:180]


    private static RosterItem parseRosterItem(IQStanza stanza, boolean parseSubscriptionTypes)
            throws RosterBadRequestException, RosterNotAcceptableException {
        XMLElement queryElement;
        try {
            queryElement = stanza.getSingleInnerElementsNamed("query");
            if (queryElement == null)
                throw new XMLSemanticError("missing query node");
        } catch (XMLSemanticError xmlSemanticError) {
            throw new RosterBadRequestException("roster set needs a single query node.");
        }

        XMLElement itemElement;
        try {
            itemElement = queryElement.getSingleInnerElementsNamed("item");
            if (itemElement == null)
                throw new XMLSemanticError("missing item node");
        } catch (XMLSemanticError xmlSemanticError) {
            throw new RosterBadRequestException("roster set needs a single item node.");
        }

        Attribute attributeJID = itemElement.getAttribute("jid");
        if (attributeJID == null || attributeJID.getValue() == null)
            throw new RosterBadRequestException("missing 'jid' attribute on item node");

        XMLElementVerifier verifier = itemElement.getVerifier();
        String name = verifier.attributePresent("name") ? itemElement.getAttribute("name").getValue() : null;
        if (name != null && name.length() > RosterConfiguration.ROSTER_ITEM_NAME_MAX_LENGTH) {
            throw new RosterNotAcceptableException("roster name too long: " + name.length());
        }

        SubscriptionType subscription = verifier.attributePresent("subscription") ? SubscriptionType
                .valueOf(itemElement.getAttribute("subscription").getValue().toUpperCase()) : SubscriptionType.NONE;
        if (!parseSubscriptionTypes && subscription != SubscriptionType.REMOVE)
            subscription = SubscriptionType.NONE; // roster remove is always tolerated

        AskSubscriptionType askSubscriptionType = AskSubscriptionType.NOT_SET;
        if (parseSubscriptionTypes) {
            askSubscriptionType = verifier.attributePresent("ask") ? AskSubscriptionType.valueOf("ASK_"
                    + itemElement.getAttribute("ask").getValue().toUpperCase()) : AskSubscriptionType.NOT_SET;
        }

        String contactJid = attributeJID.getValue();
        Entity contact;
        try {
            contact = EntityImpl.parse(contactJid);
        } catch (EntityFormatException e) {
            throw new RosterNotAcceptableException("jid cannot be parsed: " + contactJid);
        }

        List<RosterGroup> groups = new ArrayList<RosterGroup>();
        List<XMLElement> groupElements = itemElement.getInnerElementsNamed("group");
        if (groupElements != null) {
            for (XMLElement groupElement : groupElements) {
                String groupName = null;
                try {
                    groupName = groupElement.getSingleInnerText().getText();
                } catch (XMLSemanticError xmlSemanticError) {
                    throw new RosterBadRequestException("roster item group node is malformed");
                }
                if (StringUtils.isEmpty(groupName)) {
                    throw new RosterNotAcceptableException("roster item group name of zero length");
                } else if (groupName.length() > RosterConfiguration.ROSTER_GROUP_NAME_MAX_LENGTH) {
                    throw new RosterNotAcceptableException("roster item group name too long: " + groupName.length());
                }
                RosterGroup group = new RosterGroup(groupName);
                if (groups.contains(group) && !RosterConfiguration.ROSTER_ITEM_GROUP_ALLOW_DUPLICATES) {
                    throw new RosterNotAcceptableException("duplicate roster group name: " + groupName);
                } else {
                    groups.add(group);
                }
            }
        }

        RosterItem rosterItem = new RosterItem(contact, name, subscription, askSubscriptionType, groups);
        return rosterItem;
    }