Program: 104985
Project: 104985
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 IQUtils.createError(conferenceIQ,XMPPError.Condition.not_authorized);
  }
 else   if (authorizedSourcePattern != null && (focus == null || !authorizedSourcePattern.matcher(focus).matches())) {
    return IQUtils.createError(conferenceIQ,XMPPError.Condition.not_authorized);
  }
 else {
    String id=conferenceIQ.getID();
    if (id == null) {
      if (isShutdownInProgress()) {
        return ColibriConferenceIQ.createGracefulShutdownErrorResponse(conferenceIQ);
      }
 else {
        conference=createConference(focus,conferenceIQ.getName());
        if (conference == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str");
        }
      }
    }
 else {
      conference=getConference(id,focus);
      if (conference == null) {
        return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + id);
      }
    }
    conference.setLastKnownFocus(conferenceIQ.getFrom());
  }
  ColibriConferenceIQ 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()) {
    String contentName=contentIQ.getName();
    Content content=conference.getOrCreateContent(contentName);
    if (content == null) {
      return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str" + contentName);
    }
    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;
      boolean channelCreated=false;
      String transportNamespace=channelIQ.getTransport() != null ? channelIQ.getTransport().getNamespace() : null;
      if (channelID == null) {
        if (channelExpire == 0) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str");
        }
        channel=content.createRtpChannel(channelBundleId,transportNamespace,channelIQ.isInitiator(),channelIQ.getRTPLevelRelayType());
        if (channel == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str");
        }
        channelCreated=true;
      }
 else {
        channel=(RtpChannel)content.getChannel(channelID);
        if (channel == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + channelID);
        }
      }
      if (channelExpire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
        if (channelExpire < 0) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + channelExpire);
        }
        channel.setExpire(channelExpire);
        if ((channelExpire == 0) && channel.isExpired())         continue;
      }
      if (channelCreated && channel instanceof VideoChannel) {
        VideoChannel videoChannel=(VideoChannel)channel;
        Integer receiveSimulcastLayer=channelIQ.getReceivingSimulcastLayer();
        videoChannel.setReceiveSimulcastLayer(receiveSimulcastLayer);
      }
      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);
      Integer packetDelay=channelIQ.getPacketDelay();
      if (packetDelay != null) {
        channel.setPacketDelay(packetDelay);
      }
      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);
    }
    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) {
        if (expire == 0) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str");
        }
        if (endpointID == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + "String_Node_Str");
        }
        Endpoint endpoint=conference.getOrCreateEndpoint(endpointID);
        if (endpoint == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str" + endpointID);
        }
 else {
          int sctpPort=sctpConnIq.getPort();
          sctpConn=content.createSctpConnection(endpoint,sctpPort,channelBundleId,sctpConnIq.isInitiator());
          if (sctpConn == null) {
            return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str");
          }
        }
      }
 else {
        sctpConn=content.getSctpConnection(id);
        if (sctpConn == null && expire == 0) {
          continue;
        }
 else         if (sctpConn == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + id);
        }
        if (expire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
          if (expire < 0) {
            return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + expire);
          }
          if (expire == 0 && sctpConn.isExpired())           continue;
          sctpConn.setExpire(expire);
        }
        if (endpointID != null)         sctpConn.setEndpoint(endpointID);
      }
      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);
    }
  }
  for (  ColibriConferenceIQ.ChannelBundle channelBundleIq : conferenceIQ.getChannelBundles()) {
    TransportManager transportManager=conference.getTransportManager(channelBundleIq.getId());
    IceUdpTransportPacketExtension transportIq=channelBundleIq.getTransport();
    if (transportManager != null && transportIq != null) {
      transportManager.startConnectivityEstablishment(transportIq);
    }
  }
  for (  ColibriConferenceIQ.Endpoint colibriEndpoint : conferenceIQ.getEndpoints()) {
    conference.updateEndpoint(colibriEndpoint);
  }
  conference.describeChannelBundles(responseConferenceIQ);
  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 IQUtils.createError(conferenceIQ,XMPPError.Condition.not_authorized);
  }
 else   if (authorizedSourcePattern != null && (focus == null || !authorizedSourcePattern.matcher(focus).matches())) {
    return IQUtils.createError(conferenceIQ,XMPPError.Condition.not_authorized);
  }
 else {
    String id=conferenceIQ.getID();
    if (id == null) {
      if (isShutdownInProgress()) {
        return ColibriConferenceIQ.createGracefulShutdownErrorResponse(conferenceIQ);
      }
 else {
        conference=createConference(focus,conferenceIQ.getName());
        if (conference == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str");
        }
      }
    }
 else {
      conference=getConference(id,focus);
      if (conference == null) {
        return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + id);
      }
    }
    conference.setLastKnownFocus(conferenceIQ.getFrom());
  }
  ColibriConferenceIQ 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()) {
    String contentName=contentIQ.getName();
    Content content=conference.getOrCreateContent(contentName);
    if (content == null) {
      return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str" + contentName);
    }
    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;
      boolean channelCreated=false;
      String transportNamespace=channelIQ.getTransport() != null ? channelIQ.getTransport().getNamespace() : null;
      if (channelID == null) {
        if (channelExpire == 0) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str");
        }
        channel=content.createRtpChannel(channelBundleId,transportNamespace,channelIQ.isInitiator(),channelIQ.getRTPLevelRelayType());
        if (channel == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str");
        }
        channelCreated=true;
      }
 else {
        channel=(RtpChannel)content.getChannel(channelID);
        if (channel == null) {
          if (channelExpire == 0) {
            continue;
          }
 else {
            return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + channelID);
          }
        }
      }
      if (channelExpire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
        if (channelExpire < 0) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + channelExpire);
        }
        channel.setExpire(channelExpire);
        if ((channelExpire == 0) && channel.isExpired())         continue;
      }
      if (channelCreated && channel instanceof VideoChannel) {
        VideoChannel videoChannel=(VideoChannel)channel;
        Integer receiveSimulcastLayer=channelIQ.getReceivingSimulcastLayer();
        videoChannel.setReceiveSimulcastLayer(receiveSimulcastLayer);
      }
      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);
      Integer packetDelay=channelIQ.getPacketDelay();
      if (packetDelay != null) {
        channel.setPacketDelay(packetDelay);
      }
      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);
    }
    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) {
        if (expire == 0) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str");
        }
        if (endpointID == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + "String_Node_Str");
        }
        Endpoint endpoint=conference.getOrCreateEndpoint(endpointID);
        if (endpoint == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str" + endpointID);
        }
 else {
          int sctpPort=sctpConnIq.getPort();
          sctpConn=content.createSctpConnection(endpoint,sctpPort,channelBundleId,sctpConnIq.isInitiator());
          if (sctpConn == null) {
            return IQUtils.createError(conferenceIQ,XMPPError.Condition.interna_server_error,"String_Node_Str");
          }
        }
      }
 else {
        sctpConn=content.getSctpConnection(id);
        if (sctpConn == null && expire == 0) {
          continue;
        }
 else         if (sctpConn == null) {
          return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + id);
        }
        if (expire != ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED) {
          if (expire < 0) {
            return IQUtils.createError(conferenceIQ,XMPPError.Condition.bad_request,"String_Node_Str" + expire);
          }
          if (expire == 0 && sctpConn.isExpired())           continue;
          sctpConn.setExpire(expire);
        }
        if (endpointID != null)         sctpConn.setEndpoint(endpointID);
      }
      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);
    }
  }
  for (  ColibriConferenceIQ.ChannelBundle channelBundleIq : conferenceIQ.getChannelBundles()) {
    TransportManager transportManager=conference.getTransportManager(channelBundleIq.getId());
    IceUdpTransportPacketExtension transportIq=channelBundleIq.getTransport();
    if (transportManager != null && transportIq != null) {
      transportManager.startConnectivityEstablishment(transportIq);
    }
  }
  for (  ColibriConferenceIQ.Endpoint colibriEndpoint : conferenceIQ.getEndpoints()) {
    conference.updateEndpoint(colibriEndpoint);
  }
  conference.describeChannelBundles(responseConferenceIQ);
  responseConferenceIQ.setType(org.jivesoftware.smack.packet.IQ.Type.RESULT);
  return responseConferenceIQ;
}