protected List executeIQLogic()

in server/core/src/main/java/org/apache/vysper/xmpp/modules/core/base/handler/RelayingIQHandler.java [49:121]


    protected List<Stanza> executeIQLogic(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, boolean outboundStanza,
                                          SessionContext sessionContext, StanzaBroker stanzaBroker) {
        // only handle IQs which are not directed to the server (vysper.org).
        // in the case where an IQ is send to the server, StanzaHandlerLookup.getIQHandler is responsible for
        // looking it up and we shouldn't have been come here in the first place.
        // but we might will relay to a component (chat.vysper.org)
        Entity to = stanza.getTo();
        if (to == null || to.equals(sessionContext.getServerJID())) {
            return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.FEATURE_NOT_IMPLEMENTED,
                    stanza, StanzaErrorType.CANCEL, null, null, null));
        }

        RosterManager rosterManager = RosterManagerUtils.getRosterInstance(serverRuntimeContext, sessionContext);

        if (outboundStanza) {
            try {

                boolean toComponent = EntityUtils.isAddressingServerComponent(to, serverRuntimeContext.getServerEntity());

                Entity from = stanza.getFrom();
                if (from == null || !from.isResourceSet()) {
                    from = new EntityImpl(sessionContext.getInitiatingEntity(), serverRuntimeContext
                            .getResourceRegistry().getUniqueResourceForSession(sessionContext));
                }

                // determine if the is a matching subscription...
                boolean isFromContact = false;
                if (!toComponent) {
                    try {
                        isFromContact = rosterManager.retrieve(from.getBareJID()).getEntry(to.getBareJID()).hasFrom();
                    } catch (Exception e) {
                        isFromContact = false;
                    }
                }
                // deny relaying if neither isFromContact nor toComponent
                if (!isFromContact && !toComponent) {
                    return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.SERVICE_UNAVAILABLE,
                            stanza, StanzaErrorType.CANCEL, null, null, null));
                }

                Stanza forwardedStanza = StanzaBuilder.createForward(stanza, from, null).build();
                stanzaBroker.write(to, forwardedStanza,
                        new ReturnErrorToSenderFailureStrategy(stanzaBroker));
            } catch (DeliveryException e) {
                // TODO how to handle this exception?
            }
        } else {
            // write inbound stanza to the user

            Entity from = stanza.getFrom();

            boolean fromComponent = (from != null) && EntityUtils.isAddressingServerComponent(from, serverRuntimeContext.getServerEntity());

            // determine if 'from' is a component or a matching subscription...
            boolean isToContact = false;
            if (!fromComponent) {
                try {
                    isToContact = rosterManager.retrieve(to.getBareJID()).getEntry(from.getBareJID()).hasTo();
                } catch (Exception e) {
                    isToContact = false;
                }
            }
            // ...otherwise relaying is denied
            if (!isToContact && !fromComponent) {
                return Collections.singletonList(ServerErrorResponses.getStanzaError(StanzaErrorCondition.SERVICE_UNAVAILABLE,
                        stanza, StanzaErrorType.CANCEL, null, null, null));
            }

            stanzaBroker.writeToSession(stanza);
        }

        return Collections.emptyList();
    }