SessionContext sessionContext) {
// 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 ServerErrorResponses.getInstance().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.getServerEnitity());
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 ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.SERVICE_UNAVAILABLE,
stanza, StanzaErrorType.CANCEL, null, null, null);
}
Stanza forwardedStanza = StanzaBuilder.createForward(stanza, from, null).build();
serverRuntimeContext.getStanzaRelay().relay(to, forwardedStanza,
new ReturnErrorToSenderFailureStrategy(serverRuntimeContext.getStanzaRelay()));
} 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.getServerEnitity());
// 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