protected List handleSet()

in server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0050_adhoc_commands/AdhocCommandIQHandler.java [68:130]


    protected List<Stanza> handleSet(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, SessionContext sessionContext, StanzaBroker stanzaBroker) {
        Entity from = stanza.getFrom();
        if (from == null) {
            from = sessionContext.getInitiatingEntity();
        }

        AdhocCommandHandler commandHandler = null;
        String commandNode;
        String requestedSessionId;
        String action;  // execute | cancel
        List<XMLElement> commandElements = null;
        try {
            XMLElement commandElement = stanza.getSingleInnerElementsNamed("command");
            if (commandElement == null) {
                return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza,
                        StanzaErrorType.MODIFY, "command is missing", null, null));
            }
            commandNode = commandElement.getAttributeValue("node");
            requestedSessionId = commandElement.getAttributeValue("sessionid");
            action = commandElement.getAttributeValue("action");

            if (StringUtils.isEmpty(requestedSessionId)) {
                for (AdhocCommandSupport commandSupport : adhocCommandSupporters) {
                    commandHandler = commandSupport.getCommandHandler(commandNode, from);
                    if (commandHandler != null) {
                        runningCommands.put(commandHandler.getSessionId(), commandHandler);
                        break;
                    }
                }
            } else {
                commandHandler = runningCommands.get(requestedSessionId);
                if (commandHandler == null) {
                    return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza,
                            StanzaErrorType.CANCEL, "command session id not found: " + requestedSessionId, null, null));
                }
            }
            commandElements = commandElement.getInnerElements();
        } catch (XMLSemanticError xmlSemanticError) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza,
                    StanzaErrorType.MODIFY, "command is not well-formed", null, null));
        }

        if ("cancel".equals(action)) {
            runningCommands.remove(requestedSessionId);
            return buildResponse(stanza, from, commandNode, requestedSessionId, "canceled");
        }

        // handle unauthorized access (or command does not exist at all)
        if (commandHandler == null) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.FORBIDDEN, stanza,
                    StanzaErrorType.CANCEL, "command is not available", null, null));
        }

        List<Note> notes = new ArrayList<Note>();
        final XMLElement result = commandHandler.process(commandElements, notes);

        final String sessionId = commandHandler.getSessionId();
        final boolean isExecuting = commandHandler.isExecuting();

        return buildResponse(stanza, from, commandNode, sessionId, 
                                             isExecuting ? "executing" : "completed", result, notes,
                                              commandHandler.isPrevAllowed(), commandHandler.isNextAllowed());
    }