Program: 106595
Project: 106595
Buggy source:
private Long extract(Account caller,Long templateId,String url,Long zoneId,String mode,Long eventId,boolean isISO,AsyncJobVO job,AsyncJobManager mgr){
  String desc=Upload.Type.TEMPLATE.toString();
  if (isISO) {
    desc=Upload.Type.ISO.toString();
  }
  eventId=eventId == null ? 0 : eventId;
  if (!_accountMgr.isRootAdmin(caller.getType()) && _disableExtraction) {
    throw new PermissionDeniedException("String_Node_Str");
  }
  VMTemplateVO template=_tmpltDao.findById(templateId);
  if (template == null || template.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str"+ templateId);
  }
  if (template.getTemplateType() == Storage.TemplateType.SYSTEM) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str"+ template.getName()+ "String_Node_Str");
  }
 else   if (template.getTemplateType() == Storage.TemplateType.PERHOST) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str"+ template.getName()+ "String_Node_Str");
  }
  if (isISO) {
    if (template.getFormat() != ImageFormat.ISO) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
 else {
    if (template.getFormat() == ImageFormat.ISO) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (_dcDao.findById(zoneId) == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (!_accountMgr.isRootAdmin(caller.getType()) && !template.isExtractable()) {
    throw new InvalidParameterValueException("String_Node_Str" + templateId + "String_Node_Str");
  }
  _accountMgr.checkAccess(caller,AccessType.ModifyEntry,template);
  List<HostVO> sservers=_storageMgr.getSecondaryStorageHosts(zoneId);
  VMTemplateHostVO tmpltHostRef=null;
  if (sservers != null) {
    for (    HostVO secondaryStorageHost : sservers) {
      tmpltHostRef=_tmpltHostDao.findByHostTemplate(secondaryStorageHost.getId(),templateId);
      if (tmpltHostRef != null) {
        if (tmpltHostRef.getDownloadState() != com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
          tmpltHostRef=null;
        }
 else {
          break;
        }
      }
    }
  }
  if (tmpltHostRef == null && _swiftMgr.isSwiftEnabled()) {
    SwiftTO swift=_swiftMgr.getSwiftTO(templateId);
    if (swift != null && sservers != null) {
      for (      HostVO secondaryStorageHost : sservers) {
        downloadTemplateFromSwiftToSecondaryStorageCommand cmd=new downloadTemplateFromSwiftToSecondaryStorageCommand(swift,secondaryStorageHost.getName(),zoneId,template.getAccountId(),templateId,_primaryStorageDownloadWait);
        try {
          Answer answer=_agentMgr.sendToSSVM(zoneId,cmd);
          if (answer == null || !answer.getResult()) {
            String errMsg="String_Node_Str" + (answer == null ? "String_Node_Str" : answer.getDetails());
            s_logger.warn(errMsg);
            throw new CloudRuntimeException(errMsg);
          }
          tmpltHostRef=_tmpltHostDao.findByHostTemplate(secondaryStorageHost.getId(),templateId);
          if (tmpltHostRef != null) {
            if (tmpltHostRef.getDownloadState() != com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
              tmpltHostRef=null;
            }
 else {
              break;
            }
          }
        }
 catch (        Exception e) {
          String errMsg="String_Node_Str" + e.toString();
          s_logger.warn(errMsg);
          throw new CloudRuntimeException(errMsg);
        }
      }
    }
  }
  if (tmpltHostRef == null) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str");
  }
  Upload.Mode extractMode;
  if (mode == null || (!mode.equalsIgnoreCase(Upload.Mode.FTP_UPLOAD.toString()) && !mode.equalsIgnoreCase(Upload.Mode.HTTP_DOWNLOAD.toString()))) {
    throw new InvalidParameterValueException("String_Node_Str" + Upload.Mode.FTP_UPLOAD + "String_Node_Str"+ Upload.Mode.HTTP_DOWNLOAD);
  }
 else {
    extractMode=mode.equalsIgnoreCase(Upload.Mode.FTP_UPLOAD.toString()) ? Upload.Mode.FTP_UPLOAD : Upload.Mode.HTTP_DOWNLOAD;
  }
  if (extractMode == Upload.Mode.FTP_UPLOAD) {
    URI uri=null;
    try {
      uri=new URI(url);
      if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("String_Node_Str"))) {
        throw new InvalidParameterValueException("String_Node_Str" + url);
      }
    }
 catch (    Exception ex) {
      throw new InvalidParameterValueException("String_Node_Str" + url);
    }
    String host=uri.getHost();
    try {
      InetAddress hostAddr=InetAddress.getByName(host);
      if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress()|| hostAddr.isMulticastAddress()) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      if (hostAddr instanceof Inet6Address) {
        throw new InvalidParameterValueException("String_Node_Str" + hostAddr.getHostAddress() + "String_Node_Str");
      }
    }
 catch (    UnknownHostException uhe) {
      throw new InvalidParameterValueException("String_Node_Str" + host);
    }
    if (_uploadMonitor.isTypeUploadInProgress(templateId,isISO ? Type.ISO : Type.TEMPLATE)) {
      throw new IllegalArgumentException(template.getName() + "String_Node_Str");
    }
    return _uploadMonitor.extractTemplate(template,url,tmpltHostRef,zoneId,eventId,job.getId(),mgr);
  }
  UploadVO vo=_uploadMonitor.createEntityDownloadURL(template,tmpltHostRef,zoneId,eventId);
  if (vo != null) {
    return vo.getId();
  }
 else {
    return null;
  }
}

Fixed version (ground truth):
private Long extract(Account caller,Long templateId,String url,Long zoneId,String mode,Long eventId,boolean isISO,AsyncJobVO job,AsyncJobManager mgr){
  String desc=Upload.Type.TEMPLATE.toString();
  if (isISO) {
    desc=Upload.Type.ISO.toString();
  }
  eventId=eventId == null ? 0 : eventId;
  if (!_accountMgr.isRootAdmin(caller.getType()) && _disableExtraction) {
    throw new PermissionDeniedException("String_Node_Str");
  }
  VMTemplateVO template=_tmpltDao.findById(templateId);
  if (template == null || template.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str"+ templateId);
  }
  if (template.getTemplateType() == Storage.TemplateType.SYSTEM) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str"+ template.getName()+ "String_Node_Str");
  }
 else   if (template.getTemplateType() == Storage.TemplateType.PERHOST) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str"+ template.getName()+ "String_Node_Str");
  }
  if (isISO) {
    if (template.getFormat() != ImageFormat.ISO) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
 else {
    if (template.getFormat() == ImageFormat.ISO) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (zoneId == null) {
    zoneId=_swiftMgr.chooseZoneForTmpltExtract(templateId);
  }
  if (_dcDao.findById(zoneId) == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (!_accountMgr.isRootAdmin(caller.getType()) && !template.isExtractable()) {
    throw new InvalidParameterValueException("String_Node_Str" + templateId + "String_Node_Str");
  }
  _accountMgr.checkAccess(caller,AccessType.ModifyEntry,template);
  List<HostVO> sservers=_storageMgr.getSecondaryStorageHosts(zoneId);
  VMTemplateHostVO tmpltHostRef=null;
  if (sservers != null) {
    for (    HostVO secondaryStorageHost : sservers) {
      tmpltHostRef=_tmpltHostDao.findByHostTemplate(secondaryStorageHost.getId(),templateId);
      if (tmpltHostRef != null) {
        if (tmpltHostRef.getDownloadState() != com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
          tmpltHostRef=null;
        }
 else {
          break;
        }
      }
    }
  }
  if (tmpltHostRef == null && _swiftMgr.isSwiftEnabled()) {
    SwiftTO swift=_swiftMgr.getSwiftTO(templateId);
    if (swift != null && sservers != null) {
      for (      HostVO secondaryStorageHost : sservers) {
        downloadTemplateFromSwiftToSecondaryStorageCommand cmd=new downloadTemplateFromSwiftToSecondaryStorageCommand(swift,secondaryStorageHost.getName(),zoneId,template.getAccountId(),templateId,_primaryStorageDownloadWait);
        try {
          Answer answer=_agentMgr.sendToSSVM(zoneId,cmd);
          if (answer == null || !answer.getResult()) {
            String errMsg="String_Node_Str" + (answer == null ? "String_Node_Str" : answer.getDetails());
            s_logger.warn(errMsg);
            throw new CloudRuntimeException(errMsg);
          }
          tmpltHostRef=_tmpltHostDao.findByHostTemplate(secondaryStorageHost.getId(),templateId);
          if (tmpltHostRef != null) {
            if (tmpltHostRef.getDownloadState() != com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
              tmpltHostRef=null;
            }
 else {
              break;
            }
          }
        }
 catch (        Exception e) {
          String errMsg="String_Node_Str" + e.toString();
          s_logger.warn(errMsg);
          throw new CloudRuntimeException(errMsg);
        }
      }
    }
  }
  if (tmpltHostRef == null) {
    throw new InvalidParameterValueException("String_Node_Str" + desc + "String_Node_Str");
  }
  Upload.Mode extractMode;
  if (mode == null || (!mode.equalsIgnoreCase(Upload.Mode.FTP_UPLOAD.toString()) && !mode.equalsIgnoreCase(Upload.Mode.HTTP_DOWNLOAD.toString()))) {
    throw new InvalidParameterValueException("String_Node_Str" + Upload.Mode.FTP_UPLOAD + "String_Node_Str"+ Upload.Mode.HTTP_DOWNLOAD);
  }
 else {
    extractMode=mode.equalsIgnoreCase(Upload.Mode.FTP_UPLOAD.toString()) ? Upload.Mode.FTP_UPLOAD : Upload.Mode.HTTP_DOWNLOAD;
  }
  if (extractMode == Upload.Mode.FTP_UPLOAD) {
    URI uri=null;
    try {
      uri=new URI(url);
      if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("String_Node_Str"))) {
        throw new InvalidParameterValueException("String_Node_Str" + url);
      }
    }
 catch (    Exception ex) {
      throw new InvalidParameterValueException("String_Node_Str" + url);
    }
    String host=uri.getHost();
    try {
      InetAddress hostAddr=InetAddress.getByName(host);
      if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress()|| hostAddr.isMulticastAddress()) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      if (hostAddr instanceof Inet6Address) {
        throw new InvalidParameterValueException("String_Node_Str" + hostAddr.getHostAddress() + "String_Node_Str");
      }
    }
 catch (    UnknownHostException uhe) {
      throw new InvalidParameterValueException("String_Node_Str" + host);
    }
    if (_uploadMonitor.isTypeUploadInProgress(templateId,isISO ? Type.ISO : Type.TEMPLATE)) {
      throw new IllegalArgumentException(template.getName() + "String_Node_Str");
    }
    return _uploadMonitor.extractTemplate(template,url,tmpltHostRef,zoneId,eventId,job.getId(),mgr);
  }
  UploadVO vo=_uploadMonitor.createEntityDownloadURL(template,tmpltHostRef,zoneId,eventId);
  if (vo != null) {
    return vo.getId();
  }
 else {
    return null;
  }
}