xmppSession.addIQListener(new IQListener() {
@Override
public void handle(IQEvent e) {
IQ iq = e.getIQ();
if (e.isIncoming() && isEnabled() && !e.isConsumed() && iq.getType() == IQ.Type.SET) {
StreamInitiation streamInitiation = iq.getExtension(StreamInitiation.class);
if (streamInitiation != null) {
FeatureNegotiation featureNegotiation = streamInitiation.getFeatureNegotiation();
// Assume no valid streams by default, unless valid streams are found.
boolean noValidStreams = true;
if (featureNegotiation != null) {
DataForm dataForm = featureNegotiation.getDataForm();
if (dataForm != null) {
DataForm.Field field = dataForm.findField(STREAM_METHOD);
if (field != null) {
List<String> streamMethods = new ArrayList<>();
for (DataForm.Option option : field.getOptions()) {
streamMethods.add(option.getValue());
}
if (!Collections.disjoint(streamMethods, supportedStreamMethod)) {
// Request contains valid streams
noValidStreams = false;
}
}
}
}
if (noValidStreams) {
StanzaError error = new StanzaError(new BadRequest());
error.setExtension(new NoValidStreams());
xmppSession.send(iq.createError(error));
} else {
ProfileManager profileManager = profileManagers.get(streamInitiation.getProfile());
if (profileManager == null) {
StanzaError error = new StanzaError(new BadRequest());
error.setExtension(new BadProfile());
xmppSession.send(iq.createError(error));