Program: 104998
Project: 104998
Buggy source:
/** 
 * Handles a <tt>ColibriConferenceIQ</tt> stanza which represents a request.
 * @param conferenceIQ the <tt>ColibriConferenceIQ</tt> stanza representsthe request to handle
 * @param options
 * @return an <tt>org.jivesoftware.smack.packet.IQ</tt> stanza whichrepresents the response to the specified request or <tt>null</tt> to reply with <tt>feature-not-implemented</tt>
 * @throws Exception to reply with <tt>internal-server-error</tt> to thespecified request
 */
public IQ handleColibriConferenceIQ(ColibriConferenceIQ conferenceIQ,int options) throws Exception {
  String focus=conferenceIQ.getFrom();
  Conference conference;
  if ((options & OPTION_ALLOW_ANY_FOCUS) > 0) {
    options|=OPTION_ALLOW_NO_FOCUS;
    focus=null;
  }
  if (focus == null && (options & OPTION_ALLOW_NO_FOCUS) == 0) {
    return IQ.createErrorResponse(conferenceIQ,new XMPPError(XMPPError.Condition.not_authorized));
  }
 else   if (authorizedSourcePattern != null && (focus == null || !authorizedSourcePattern.matcher(focus).matches())) {
    return IQ.createErrorResponse(conferenceIQ,new XMPPError(XMPPError.Condition.not_authorized));
  }
 else {
    String id=conferenceIQ.getID();
    if (id == null) {
      if (!isShutdownInProgress()) {
        conference=createConference(focus);
      }
 else {
        return ColibriConferenceIQ.createGracefulShutdownErrorResponse(conferenceIQ);
      }
    }
 else {
      conference=getConference(id,focus);
    }
    if (conference != null)     conference.setLastKnownFocus(conferenceIQ.getFrom());
  }
  ColibriConferenceIQ responseConferenceIQ;
  if (conference == null) {
    responseConferenceIQ=null;
  }
 else {
    String name=conferenceIQ.getName();
    if (name != null)     conference.setName(name);
    responseConferenceIQ=new ColibriConferenceIQ();
    conference.describeShallow(responseConferenceIQ);
    responseConferenceIQ.setGracefulShutdown(isShutdownInProgress());
    ColibriConferenceIQ.Recording recordingIQ=conferenceIQ.getRecording();
    if (recordingIQ != null) {
      String tokenIQ=recordingIQ.getToken();
      if (tokenIQ != null) {
        String tokenConfig=getConfigurationService().getString(Videobridge.MEDIA_RECORDING_TOKEN_PNAME);
        if (tokenIQ.equals(tokenConfig)) {
          ColibriConferenceIQ.Recording.State recState=recordingIQ.getState();
          boolean recording=conference.setRecording(ColibriConferenceIQ.Recording.State.ON.equals(recState) || ColibriConferenceIQ.Recording.State.PENDING.equals(recState));
          ColibriConferenceIQ.Recording responseRecordingIq=new ColibriConferenceIQ.Recording(recState);
          if (recording) {
            responseRecordingIq.setDirectory(conference.getRecordingDirectory());
          }
          responseConferenceIQ.setRecording(responseRecordingIq);
        }
      }
    }
    for (    ColibriConferenceIQ.Content contentIQ : conferenceIQ.getContents()) {
      Content content=conference.getOrCreateContent(contentIQ.getName());
      if (content == null) {
        responseConferenceIQ=null;
      }
 else {
        ColibriConferenceIQ.Content responseContentIQ=new ColibriConferenceIQ.Content(content.getName());
        responseConferenceIQ.addContent(responseContentIQ);
        for (        ColibriConferenceIQ.Channel channelIQ : contentIQ.getChannels()) {
          String channelID=channelIQ.getID();
          int channelExpire=channelIQ.getExpire();
          String channelBundleId=channelIQ.getChannelBundleId();
          RtpChannel channel=null;
          boolean channelCreated=false;
          String transportNamespace=channelIQ.getTransport() != null ? channelIQ.getTransport().getNamespace() : null;
          if (channelID == null) {
            if (channelExpire != 0) {
              channel=content.createRtpChannel(channelBundleId,transportNamespace,channelIQ.isInitiator(),channelIQ.getRTPLevelRelayType());
              if (channel instanceof VideoChannel) {
                VideoChannel videoChannel=(VideoChannel)channel;
                videoChannel.setReceiveSimulcastLayer(channelIQ.getReceivingSimulcastLayer());
              }
              channelCreated=true;
            }
          }
 else {
            channel=(RtpChannel)content.getChannel(channelID);
          }
          if (channel == null) {
            responseConferenceIQ=null;
          }
 else {
            if (channelExpire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
              channel.setExpire(channelExpire);
              if ((channelExpire == 0) && channel.isExpired())               continue;
            }
            String endpoint=channelIQ.getEndpoint();
            if (endpoint != null)             channel.setEndpoint(endpoint);
            Integer lastN=channelIQ.getLastN();
            if (lastN != null)             channel.setLastN(lastN);
            Boolean adaptiveLastN=channelIQ.getAdaptiveLastN();
            if (adaptiveLastN != null)             channel.setAdaptiveLastN(adaptiveLastN);
            Boolean adaptiveSimulcast=channelIQ.getAdaptiveSimulcast();
            if (adaptiveSimulcast != null)             channel.setAdaptiveSimulcast(adaptiveSimulcast);
            Boolean initiator=channelIQ.isInitiator();
            if (initiator != null)             channel.setInitiator(initiator);
            channel.setPayloadTypes(channelIQ.getPayloadTypes());
            channel.setRtpHeaderExtensions(channelIQ.getRtpHeaderExtensions());
            channel.setDirection(channelIQ.getDirection());
            channel.setSources(channelIQ.getSources());
            channel.setSourceGroups(channelIQ.getSourceGroups());
            if (channel instanceof VideoChannel) {
              SimulcastMode simulcastMode=channelIQ.getSimulcastMode();
              if (simulcastMode != null) {
                ((VideoChannel)channel).setSimulcastMode(simulcastMode);
              }
            }
            if (channelBundleId != null) {
              TransportManager transportManager=conference.getTransportManager(channelBundleId,true);
              transportManager.addChannel(channel);
            }
            channel.setTransport(channelIQ.getTransport());
            ColibriConferenceIQ.Channel responseChannelIQ=new ColibriConferenceIQ.Channel();
            channel.describe(responseChannelIQ);
            responseContentIQ.addChannel(responseChannelIQ);
            EventAdmin eventAdmin;
            if (channelCreated && (eventAdmin=getEventAdmin()) != null) {
              eventAdmin.sendEvent(EventFactory.channelCreated(channel));
            }
            content.fireChannelChanged(channel);
          }
          if (responseConferenceIQ == null)           break;
        }
        for (        ColibriConferenceIQ.SctpConnection sctpConnIq : contentIQ.getSctpConnections()) {
          String id=sctpConnIq.getID();
          String endpointID=sctpConnIq.getEndpoint();
          SctpConnection sctpConn;
          int expire=sctpConnIq.getExpire();
          String channelBundleId=sctpConnIq.getChannelBundleId();
          if (id == null) {
            Endpoint endpoint=(endpointID == null) ? null : conference.getOrCreateEndpoint(endpointID);
            sctpConn=content.getSctpConnection(endpoint);
            if (sctpConn == null) {
              if (expire == 0)               continue;
              int sctpPort=sctpConnIq.getPort();
              sctpConn=content.createSctpConnection(endpoint,sctpPort,channelBundleId,sctpConnIq.isInitiator());
            }
          }
 else {
            sctpConn=content.getSctpConnection(id);
            if (sctpConn == null && expire == 0)             continue;
            if (endpointID != null)             sctpConn.setEndpoint(endpointID);
          }
          if (expire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
            sctpConn.setExpire(expire);
          }
          if (sctpConn.isExpired())           continue;
          Boolean initiator=sctpConnIq.isInitiator();
          if (initiator != null)           sctpConn.setInitiator(initiator);
          sctpConn.setTransport(sctpConnIq.getTransport());
          if (channelBundleId != null) {
            TransportManager transportManager=conference.getTransportManager(channelBundleId,true);
            transportManager.addChannel(sctpConn);
          }
          ColibriConferenceIQ.SctpConnection responseSctpIq=new ColibriConferenceIQ.SctpConnection();
          sctpConn.describe(responseSctpIq);
          responseContentIQ.addSctpConnection(responseSctpIq);
        }
      }
      if (responseConferenceIQ == null)       break;
    }
    for (    ColibriConferenceIQ.ChannelBundle channelBundleIq : conferenceIQ.getChannelBundles()) {
      TransportManager transportManager=conference.getTransportManager(channelBundleIq.getId());
      IceUdpTransportPacketExtension transportIq=channelBundleIq.getTransport();
      if (transportManager != null && transportIq != null) {
        transportManager.startConnectivityEstablishment(transportIq);
      }
    }
  }
  if (conference != null) {
    for (    ColibriConferenceIQ.Endpoint colibriEndpoint : conferenceIQ.getEndpoints()) {
      conference.updateEndpoint(colibriEndpoint);
    }
    if (responseConferenceIQ != null)     conference.describeChannelBundles(responseConferenceIQ);
  }
  if (responseConferenceIQ != null) {
    responseConferenceIQ.setType(org.jivesoftware.smack.packet.IQ.Type.RESULT);
  }
  return responseConferenceIQ;
}

Fixed version (ground truth):
/** 
 * Handles a <tt>ColibriConferenceIQ</tt> stanza which represents a request.
 * @param conferenceIQ the <tt>ColibriConferenceIQ</tt> stanza representsthe request to handle
 * @param options
 * @return an <tt>org.jivesoftware.smack.packet.IQ</tt> stanza whichrepresents the response to the specified request or <tt>null</tt> to reply with <tt>feature-not-implemented</tt>
 * @throws Exception to reply with <tt>internal-server-error</tt> to thespecified request
 */
public IQ handleColibriConferenceIQ(ColibriConferenceIQ conferenceIQ,int options) throws Exception {
  String focus=conferenceIQ.getFrom();
  Conference conference;
  if ((options & OPTION_ALLOW_ANY_FOCUS) > 0) {
    options|=OPTION_ALLOW_NO_FOCUS;
    focus=null;
  }
  if (focus == null && (options & OPTION_ALLOW_NO_FOCUS) == 0) {
    return IQ.createErrorResponse(conferenceIQ,new XMPPError(XMPPError.Condition.not_authorized));
  }
 else   if (authorizedSourcePattern != null && (focus == null || !authorizedSourcePattern.matcher(focus).matches())) {
    return IQ.createErrorResponse(conferenceIQ,new XMPPError(XMPPError.Condition.not_authorized));
  }
 else {
    String id=conferenceIQ.getID();
    if (id == null) {
      if (!isShutdownInProgress()) {
        conference=createConference(focus);
      }
 else {
        return ColibriConferenceIQ.createGracefulShutdownErrorResponse(conferenceIQ);
      }
    }
 else {
      conference=getConference(id,focus);
    }
    if (conference != null)     conference.setLastKnownFocus(conferenceIQ.getFrom());
  }
  ColibriConferenceIQ responseConferenceIQ;
  if (conference == null) {
    responseConferenceIQ=null;
  }
 else {
    String name=conferenceIQ.getName();
    if (name != null)     conference.setName(name);
    responseConferenceIQ=new ColibriConferenceIQ();
    conference.describeShallow(responseConferenceIQ);
    responseConferenceIQ.setGracefulShutdown(isShutdownInProgress());
    ColibriConferenceIQ.Recording recordingIQ=conferenceIQ.getRecording();
    if (recordingIQ != null) {
      String tokenIQ=recordingIQ.getToken();
      if (tokenIQ != null) {
        String tokenConfig=getConfigurationService().getString(Videobridge.MEDIA_RECORDING_TOKEN_PNAME);
        if (tokenIQ.equals(tokenConfig)) {
          ColibriConferenceIQ.Recording.State recState=recordingIQ.getState();
          boolean recording=conference.setRecording(ColibriConferenceIQ.Recording.State.ON.equals(recState) || ColibriConferenceIQ.Recording.State.PENDING.equals(recState));
          ColibriConferenceIQ.Recording responseRecordingIq=new ColibriConferenceIQ.Recording(recState);
          if (recording) {
            responseRecordingIq.setDirectory(conference.getRecordingDirectory());
          }
          responseConferenceIQ.setRecording(responseRecordingIq);
        }
      }
    }
    for (    ColibriConferenceIQ.Content contentIQ : conferenceIQ.getContents()) {
      Content content=conference.getOrCreateContent(contentIQ.getName());
      if (content == null) {
        responseConferenceIQ=null;
      }
 else {
        ColibriConferenceIQ.Content responseContentIQ=new ColibriConferenceIQ.Content(content.getName());
        responseConferenceIQ.addContent(responseContentIQ);
        for (        ColibriConferenceIQ.Channel channelIQ : contentIQ.getChannels()) {
          String channelID=channelIQ.getID();
          int channelExpire=channelIQ.getExpire();
          String channelBundleId=channelIQ.getChannelBundleId();
          RtpChannel channel=null;
          boolean channelCreated=false;
          String transportNamespace=channelIQ.getTransport() != null ? channelIQ.getTransport().getNamespace() : null;
          if (channelID == null) {
            if (channelExpire != 0) {
              channel=content.createRtpChannel(channelBundleId,transportNamespace,channelIQ.isInitiator(),channelIQ.getRTPLevelRelayType());
              if (channel instanceof VideoChannel) {
                VideoChannel videoChannel=(VideoChannel)channel;
                Integer receiveSimulcastLayer=channelIQ.getReceivingSimulcastLayer();
                videoChannel.setReceiveSimulcastLayer(receiveSimulcastLayer);
              }
              channelCreated=true;
            }
          }
 else {
            channel=(RtpChannel)content.getChannel(channelID);
          }
          if (channel == null) {
            responseConferenceIQ=null;
          }
 else {
            if (channelExpire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
              channel.setExpire(channelExpire);
              if ((channelExpire == 0) && channel.isExpired())               continue;
            }
            String endpoint=channelIQ.getEndpoint();
            if (endpoint != null)             channel.setEndpoint(endpoint);
            Integer lastN=channelIQ.getLastN();
            if (lastN != null)             channel.setLastN(lastN);
            Boolean adaptiveLastN=channelIQ.getAdaptiveLastN();
            if (adaptiveLastN != null)             channel.setAdaptiveLastN(adaptiveLastN);
            Boolean adaptiveSimulcast=channelIQ.getAdaptiveSimulcast();
            if (adaptiveSimulcast != null)             channel.setAdaptiveSimulcast(adaptiveSimulcast);
            Boolean initiator=channelIQ.isInitiator();
            if (initiator != null)             channel.setInitiator(initiator);
            channel.setPayloadTypes(channelIQ.getPayloadTypes());
            channel.setRtpHeaderExtensions(channelIQ.getRtpHeaderExtensions());
            channel.setDirection(channelIQ.getDirection());
            channel.setSources(channelIQ.getSources());
            channel.setSourceGroups(channelIQ.getSourceGroups());
            if (channel instanceof VideoChannel) {
              SimulcastMode simulcastMode=channelIQ.getSimulcastMode();
              if (simulcastMode != null) {
                ((VideoChannel)channel).setSimulcastMode(simulcastMode);
              }
            }
            if (channelBundleId != null) {
              TransportManager transportManager=conference.getTransportManager(channelBundleId,true);
              transportManager.addChannel(channel);
            }
            channel.setTransport(channelIQ.getTransport());
            ColibriConferenceIQ.Channel responseChannelIQ=new ColibriConferenceIQ.Channel();
            channel.describe(responseChannelIQ);
            responseContentIQ.addChannel(responseChannelIQ);
            EventAdmin eventAdmin;
            if (channelCreated && (eventAdmin=getEventAdmin()) != null) {
              eventAdmin.sendEvent(EventFactory.channelCreated(channel));
            }
            content.fireChannelChanged(channel);
          }
          if (responseConferenceIQ == null)           break;
        }
        for (        ColibriConferenceIQ.SctpConnection sctpConnIq : contentIQ.getSctpConnections()) {
          String id=sctpConnIq.getID();
          String endpointID=sctpConnIq.getEndpoint();
          SctpConnection sctpConn;
          int expire=sctpConnIq.getExpire();
          String channelBundleId=sctpConnIq.getChannelBundleId();
          if (id == null) {
            Endpoint endpoint=(endpointID == null) ? null : conference.getOrCreateEndpoint(endpointID);
            sctpConn=content.getSctpConnection(endpoint);
            if (sctpConn == null) {
              if (expire == 0)               continue;
              int sctpPort=sctpConnIq.getPort();
              sctpConn=content.createSctpConnection(endpoint,sctpPort,channelBundleId,sctpConnIq.isInitiator());
            }
          }
 else {
            sctpConn=content.getSctpConnection(id);
            if (sctpConn == null && expire == 0)             continue;
            if (endpointID != null)             sctpConn.setEndpoint(endpointID);
          }
          if (expire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
            sctpConn.setExpire(expire);
          }
          if (sctpConn.isExpired())           continue;
          Boolean initiator=sctpConnIq.isInitiator();
          if (initiator != null)           sctpConn.setInitiator(initiator);
          sctpConn.setTransport(sctpConnIq.getTransport());
          if (channelBundleId != null) {
            TransportManager transportManager=conference.getTransportManager(channelBundleId,true);
            transportManager.addChannel(sctpConn);
          }
          ColibriConferenceIQ.SctpConnection responseSctpIq=new ColibriConferenceIQ.SctpConnection();
          sctpConn.describe(responseSctpIq);
          responseContentIQ.addSctpConnection(responseSctpIq);
        }
      }
      if (responseConferenceIQ == null)       break;
    }
    for (    ColibriConferenceIQ.ChannelBundle channelBundleIq : conferenceIQ.getChannelBundles()) {
      TransportManager transportManager=conference.getTransportManager(channelBundleIq.getId());
      IceUdpTransportPacketExtension transportIq=channelBundleIq.getTransport();
      if (transportManager != null && transportIq != null) {
        transportManager.startConnectivityEstablishment(transportIq);
      }
    }
  }
  if (conference != null) {
    for (    ColibriConferenceIQ.Endpoint colibriEndpoint : conferenceIQ.getEndpoints()) {
      conference.updateEndpoint(colibriEndpoint);
    }
    if (responseConferenceIQ != null)     conference.describeChannelBundles(responseConferenceIQ);
  }
  if (responseConferenceIQ != null) {
    responseConferenceIQ.setType(org.jivesoftware.smack.packet.IQ.Type.RESULT);
  }
  return responseConferenceIQ;
}