protected List handleGet()

in server/core/src/main/java/org/apache/vysper/xmpp/modules/servicediscovery/handler/DiscoItemIQHandler.java [69:135]


    protected List<Stanza> handleGet(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, SessionContext sessionContext, StanzaBroker stanzaBroker) {
        ServiceCollector serviceCollector = null;

        // retrieve the service collector
        try {
            serviceCollector = (ServiceCollector) serverRuntimeContext
                    .getServerRuntimeContextService(ServiceDiscoveryRequestListenerRegistry.SERVICE_DISCOVERY_REQUEST_LISTENER_REGISTRY);
        } catch (Exception e) {
            logger.error("error retrieving ServiceCollector service {}", e);
            serviceCollector = null;
        }

        if (serviceCollector == null) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.INTERNAL_SERVER_ERROR,
                    stanza, StanzaErrorType.CANCEL, "cannot retrieve IQ-get-items result from internal components",
                    getErrorLanguage(serverRuntimeContext, sessionContext), null));
        }

        Entity to = stanza.getTo();
        boolean isServerInfoRequest = false;
        boolean isComponentInfoRequest = false;
        if (to == null) {
            isServerInfoRequest = true; // this can only be meant to query the server
        } else if (!to.isNodeSet()) {
            isServerInfoRequest = serverRuntimeContext.getServerEntity().equals(to);
            isComponentInfoRequest = serverRuntimeContext.hasComponentStanzaProcessor(to);
            if (!isServerInfoRequest && !isComponentInfoRequest) {
                return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.ITEM_NOT_FOUND, stanza,
                        StanzaErrorType.CANCEL,
                        "server does not handle items query requests for " + to.getFullQualifiedName(),
                        getErrorLanguage(serverRuntimeContext, sessionContext), null));
            }
        }

        XMLElement queryElement = stanza.getFirstInnerElement();
        String node = queryElement != null ? queryElement.getAttributeValue("node") : null;

        // collect all the item response elements
        List<Item> items;
        try {
            Entity from = stanza.getFrom();
            if (from == null) from = sessionContext.getInitiatingEntity(); 
            items = serviceCollector.processItemRequest(new InfoRequest(from, stanza.getTo(), node, stanza
                    .getID()), stanzaBroker);
        } catch (ServiceDiscoveryRequestException e) {
            // the request yields an error
            StanzaErrorCondition stanzaErrorCondition = e.getErrorCondition();
            if (stanzaErrorCondition == null)
                stanzaErrorCondition = StanzaErrorCondition.INTERNAL_SERVER_ERROR;
            return Collections.singletonList(ServerErrorResponses.getStanzaError(stanzaErrorCondition, stanza,
                    StanzaErrorType.CANCEL, "disco info request failed.",
                    getErrorLanguage(serverRuntimeContext, sessionContext), null));
        }

        // render the stanza with information collected
        StanzaBuilder stanzaBuilder = StanzaBuilder.createIQStanza(to, stanza.getFrom(), IQStanzaType.RESULT,
                stanza.getID()).startInnerElement("query", NamespaceURIs.XEP0030_SERVICE_DISCOVERY_ITEMS);
        if (node != null) {
            stanzaBuilder.addAttribute("node", node);
        }
        for (Item item : items) {
            item.insertElement(stanzaBuilder);
        }
        stanzaBuilder.endInnerElement();

        return Collections.singletonList(stanzaBuilder.build());
    }