public void processStanza()

in server/core/src/main/java/org/apache/vysper/xmpp/protocol/ProtocolWorker.java [88:198]


    public void processStanza(ServerRuntimeContext serverRuntimeContext, InternalSessionContext sessionContext, Stanza stanza,
							  SessionStateHolder sessionStateHolder) {
        if (stanza == null)
            throw new RuntimeException("cannot process NULL stanzas");

        StanzaHandler stanzaHandler = serverRuntimeContext.getHandler(stanza);
        if (stanzaHandler == null) {
            responseWriter.handleUnsupportedStanzaType(sessionContext, stanza);
            return;
        }
        if (sessionContext == null && stanzaHandler.isSessionRequired()) {
            throw new IllegalStateException("handler requires session context");
        }

        StateAwareProtocolWorker stateAwareProtocolWorker = stateWorker.get(sessionContext.getState());
        if (stateAwareProtocolWorker == null) {
            throw new IllegalStateException("no protocol worker for state " + sessionContext.getState().toString());
        }

        // check as of RFC3920/4.3:
        if (sessionStateHolder.getState() != SessionState.AUTHENTICATED) {
            // is not authenticated...
            if (XMPPCoreStanza.getWrapper(stanza) != null
                    && !(InBandRegistrationHandler.class.isAssignableFrom(stanzaHandler.unwrapType()))) {
                // ... and is a IQ/PRESENCE/MESSAGE stanza!
                responseWriter.handleNotAuthorized(sessionContext, stanza);
                return;
            }
        }

        Entity from = stanza.getFrom();
        if(sessionContext.isServerToServer()) {
            XMPPCoreStanza coreStanza = XMPPCoreStanza.getWrapper(stanza);
            
            if(coreStanza != null) {
                // stanza must come from the origin server
                if(from == null) {
                    Stanza errorStanza = ServerErrorResponses.getStanzaError(StanzaErrorCondition.UNKNOWN_SENDER,
                            coreStanza, StanzaErrorType.MODIFY, "Missing from attribute", null, null);
                    sessionContext.getResponseWriter().write(errorStanza);
                    return;
                } else if(!EntityUtils.isAddressingServer(sessionContext.getInitiatingEntity(), from)) {
                    // make sure the from attribute refers to the correct remote server
                    
                        Stanza errorStanza = ServerErrorResponses.getStanzaError(StanzaErrorCondition.UNKNOWN_SENDER,
                                coreStanza, StanzaErrorType.MODIFY, "Incorrect from attribute", null, null);
                    sessionContext.getResponseWriter().write(errorStanza);
                    return;
                }
                
                Entity to = stanza.getTo();
                if(to == null) {
                    // TODO what's the appropriate error? StreamErrorCondition.IMPROPER_ADDRESSING?
                    Stanza errorStanza = ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST,
                            coreStanza, StanzaErrorType.MODIFY, "Missing to attribute", null, null);
                    sessionContext.getResponseWriter().write(errorStanza);
                    return;                    
                } else if(!EntityUtils.isAddressingServer(serverRuntimeContext.getServerEntity(), to)) {
                    // TODO what's the appropriate error? StreamErrorCondition.IMPROPER_ADDRESSING?
                    Stanza errorStanza = ServerErrorResponses.getStanzaError(StanzaErrorCondition.BAD_REQUEST,
                            coreStanza, StanzaErrorType.MODIFY, "Invalid to attribute", null, null);
                    sessionContext.getResponseWriter().write(errorStanza);
                    return;                    
                    
                }

                // rewrite namespace
                stanza = StanzaBuilder.rewriteNamespace(stanza, NamespaceURIs.JABBER_SERVER, NamespaceURIs.JABBER_CLIENT);
            }                
        } else {
            // make sure that 'from' (if present) matches the bare authorized entity
            // else respond with a stanza error 'unknown-sender'
            // see rfc3920_draft-saintandre-rfc3920bis-04.txt#8.5.4
            if (from != null && sessionContext.getInitiatingEntity() != null) {
                Entity fromBare = from.getBareJID();
                Entity initiatingEntity = sessionContext.getInitiatingEntity();
                if (!initiatingEntity.equals(fromBare)) {
                    responseWriter.handleWrongFromJID(sessionContext, stanza);
                    return;
                }
            }
            // make sure that there is a bound resource entry for that from's resource id attribute!
            if (from != null && from.getResource() != null) {
                List<String> boundResources = sessionContext.getServerRuntimeContext().getResourceRegistry()
                        .getBoundResources(from, false);
                if (boundResources.size() == 0) {
                    responseWriter.handleWrongFromJID(sessionContext, stanza);
                    return;
                }
            }
            // make sure that there is a full from entity given in cases where more than one resource is bound
            // in the same session.
            // see rfc3920_draft-saintandre-rfc3920bis-04.txt#8.5.4
            if (from != null && from.getResource() == null) {
                List<String> boundResources = sessionContext.getServerRuntimeContext().getResourceRegistry()
                        .getResourcesForSession(sessionContext);
                if (boundResources.size() > 1) {
                    responseWriter.handleWrongFromJID(sessionContext, stanza);
                    return;
                }
            }
        }
        
        try {
            stateAwareProtocolWorker.processStanza(serverRuntimeContext, sessionContext, sessionStateHolder, stanza, stanzaHandler);
        } catch (Exception e) {
            logger.error("error executing handler {} with stanza {}", stanzaHandler.getClass().getName(),
                    DenseStanzaLogRenderer.render(stanza));
            logger.debug("error executing handler exception: ", e);
        }
    }