public IActionResult AddHeader(SimpleRestfulDescriptorViewModel viewModel, string serviceJson)
        {
            var originalViewModel = RestoreAndUpdateService(viewModel, serviceJson);

            try
            {
                if (string.IsNullOrWhiteSpace(viewModel.NewHeaderKey) ||
                    string.IsNullOrWhiteSpace(viewModel.NewHeaderValue))
                {
                    throw new Exception("请求头的Key和Value都不能是空白");
                }

                var duplics = from key in originalViewModel.Headers.Keys
                              where key.Equals(viewModel.NewHeaderKey.Trim(),
                                               StringComparison.OrdinalIgnoreCase)
                              select key;
                if (duplics.Count() > 0)
                {
                    throw new Exception($"请求头已经包含{originalViewModel.NewHeaderKey},Key是大小写不敏感的");
                }

                originalViewModel.Headers.Add(viewModel.NewHeaderKey.Trim(), viewModel.NewHeaderValue.Trim());
            }
            catch (Exception ex)
            {
                originalViewModel.NewHeaderKey   = viewModel.NewHeaderKey;
                originalViewModel.NewHeaderValue = viewModel.NewHeaderValue;
                TempData[STATE_ADD_HEADER_ERROR] = ex.Message;
            }
            TempData.Put(EDIT_SERVICE_CROSS_ACTION_DATA_KEY, originalViewModel);
            return(RedirectToAction(nameof(EditSimpleRestfulService)));
        }
        public IActionResult RemoveHeader(SimpleRestfulDescriptorViewModel viewModel,
                                          string serviceJson, string headerKey)
        {
            var originalViewModel = RestoreAndUpdateService(viewModel, serviceJson);

            originalViewModel.Headers.Remove(headerKey);
            TempData.Put(EDIT_SERVICE_CROSS_ACTION_DATA_KEY, originalViewModel);
            return(RedirectToAction(nameof(EditSimpleRestfulService)));
        }
        public IActionResult CreateJson(SimpleRestfulDescriptorViewModel viewModel, string serviceJson)
        {
            var originalViewModel = RestoreAndUpdateService(viewModel, serviceJson);

            if (originalViewModel.JsonBodyTemplate == null)
            {
                originalViewModel.JsonBodyTemplate           = new JsonTemplate();
                originalViewModel.JsonBodyTemplate.ValueType = Models.ValueType.Integer;
            }
            originalViewModel.SelectedTab = "nav-json";
            TempData.Put(EDIT_SERVICE_CROSS_ACTION_DATA_KEY, originalViewModel);
            return(RedirectToAction(nameof(EditSimpleRestfulService)));
        }
        //GET: /Service/ConfigureService/5

        public async Task <IActionResult> ConfigureService(int id)
        {
            var service = await _esbModelContext.Services.FindAsync(id);

            if (service == null)
            {
                return(NotFound());
            }

            if (service.Type == ServiceType.SimpleRESTful)
            {
                var viewModel = SimpleRestfulDescriptorViewModel.CreateFrom(service);
                if (viewModel.JsonBodyTemplate != null)
                {
                    viewModel.SelectedTab = "nav-json";
                }
                else if (!string.IsNullOrWhiteSpace(viewModel.RouteDataTemplate))
                {
                    viewModel.SelectedTab = "nav-route";
                }
                else if (!string.IsNullOrWhiteSpace(viewModel.QueryStringTemplate))
                {
                    viewModel.SelectedTab = "nav-query";
                }
                else if (!string.IsNullOrWhiteSpace(viewModel.FormBodyTemplate))
                {
                    viewModel.SelectedTab = "nav-form";
                }
                TempData.Put(EDIT_SERVICE_CROSS_ACTION_DATA_KEY, viewModel);
                return(RedirectToAction(nameof(EditSimpleRestfulService)));
            }
            else if (service.Type == ServiceType.NCWebService)
            {
                var viewModel = NCDescriptorViewModel.CreateFrom(service);
                TempData.Put(EDIT_SERVICE_CROSS_ACTION_DATA_KEY, viewModel);
                return(RedirectToAction(nameof(EditNCWebService)));
            }
            else if (service.Type == ServiceType.PrimetonService)
            {
                var viewModel = PrimetonDescriptorViewModel.CreateFrom(service);
                TempData.Put(EDIT_SERVICE_CROSS_ACTION_DATA_KEY, viewModel);
                return(RedirectToAction(nameof(EditPrimetonService)));
            }
            else
            {
                throw new Exception($"Not yet impletement API of type:{service.Type.ToString()}");
            }
        }
 public async Task <IActionResult> SaveSimpleRestfulService(
     SimpleRestfulDescriptorViewModel viewModel, string serviceJson)
 {
     return(await SaveServiceAction(viewModel, serviceJson));
 }
 public IActionResult DeleteReturnJson(SimpleRestfulDescriptorViewModel viewModel, string serviceJson)
 {
     return(DeleteJsonAction(JsonTemplateNames.RESTReturnValue, viewModel, serviceJson));
 }
 public IActionResult EditJson(SimpleRestfulDescriptorViewModel viewModel, string serviceJson)
 {
     return(EditJsonAction(JsonTemplateNames.RESTParameter, viewModel, serviceJson));
 }
Exemple #8
0
        public async Task <IActionResult> GetHelp(int id)
        {
            var instance = await _esbModelContext.Instances.Where(i => i.ID == id)
                           .Include(i => i.Client)
                           .Include(i => i.Service).SingleOrDefaultAsync();

            //.Include(i => i.InstanceMapping).SingleOrDefaultAsync();
            if (instance == null || instance.ActiveStatus == 0)
            {
                ViewBag.ErrorMessage = "指定的API调用实例不存在,或者已经被停用。";
                return(View());
            }
            if (instance.Client == null || instance.Client.ActiveStatus == 0)
            {
                ViewBag.ErrorMessage = "客户程序没有注册,或者已经被禁用。";
                return(View());
            }
            if (instance.Service == null || instance.Service.ActiveStatus == 0)
            {
                ViewBag.ErrorMessage = "请求的服务不存在,或者尚未启用";
                return(View());
            }
            ViewBag.Url = "/api/Invoke/" + id.ToString();
            IServiceDescriptorViewModel api;

            Models.SimpleRESTfulReturn returnType;
            if (instance.Service.Type == ServiceType.SimpleRESTful)
            {
                api        = SimpleRestfulDescriptorViewModel.CreateFrom(instance.Service);
                returnType = ((SimpleRestfulDescriptorViewModel)api).ReturnType;
            }
            else if (instance.Service.Type == ServiceType.NCWebService)
            {
                api        = NCDescriptorViewModel.CreateFrom(instance.Service);
                returnType = Models.SimpleRESTfulReturn.Json;
            }
            else if (instance.Service.Type == ServiceType.PrimetonService)
            {
                api        = PrimetonDescriptorViewModel.CreateFrom(instance.Service);
                returnType = Models.SimpleRESTfulReturn.Json;
            }
            else
            {
                ViewBag.ErrorMessage = $"尚未实现的服务协定类型:{instance.Service.Type}";
                return(View());
            }
            ViewBag.BodyExample = api.GetPostSample();
            ViewBag.ReturnType  = StringHelper.GetEnumDisplayName(typeof(Models.SimpleRESTfulReturn), returnType);
            switch (returnType)
            {
            case Models.SimpleRESTfulReturn.Empty:
                ViewBag.ReturnExample = "服务不返回任何值,或者返回值没有意义。";
                break;

            case Models.SimpleRESTfulReturn.PlainText:
                ViewBag.ReturnExample = "服务返回无固定格式文本,您需要具体联系服务提供方获得说明。";
                break;

            case Models.SimpleRESTfulReturn.Json:
                ViewBag.ReturnExample = api.GetReturnSample();
                break;

            default:
                break;
            }
            return(View());
        }
Exemple #9
0
        public async Task <ContentResult> CallInstance(int id)
        {
            string task = Guid.NewGuid().ToString();

            await LogStart(task, id);

            var instance = await _esbModelContext.Instances.Where(i => i.ID == id)
                           .Include(i => i.Client)
                           .Include(i => i.Service).SingleOrDefaultAsync();

            //        .Include(i => i.InstanceMapping).SingleAsync();
            #region 检查API实例定义的有效性
            if (instance == null || instance.ActiveStatus == 0)
            {
                return(await Error(-100, "指定的API调用实例不存在,或者已经被停用。", task, id, EsbOperation.Checking));
            }
            if (instance.Client == null || instance.Client.ActiveStatus == 0)
            {
                return(await Error(-5, "客户程序没有注册,或者已经被禁用。", task, id, EsbOperation.Checking));
            }
            string   requestIp    = HttpContext.Connection.RemoteIpAddress.ToString();
            string[] availableIps = instance.Client.HostIP.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (availableIps == null || availableIps.Length <= 0)
            {
                return(await Error(-1, "没有定义客户端有效IP地址列表", task, id, EsbOperation.Checking));
            }
            bool hasFound = false;
            foreach (string ip in availableIps)
            {
                if (requestIp.EndsWith(ip))
                {
                    hasFound = true;
                    break;
                }
            }
            if (!hasFound) //(instance.Client.HostIP != requestIp)
            {
                return(await Error(-1, "请求的客户端IP地址不在API客户程序注册IP地址清单中", task, id, EsbOperation.Checking));
            }
            #endregion

            using (StreamReader reader = new StreamReader(Request.Body))
            {
                try
                {
                    //RequestBody request = JsonConvert.DeserializeObject<RequestBody>(await reader.ReadToEndAsync());
                    JObject originalRequest = JObject.Parse(await reader.ReadToEndAsync());
                    #region 检查请求和服务是否正确
                    int    clientId    = originalRequest.SelectToken("$.id").Value <int>();
                    string clientToken = originalRequest.SelectToken("$.token").Value <string>();
                    if (clientId != instance.Client.ID || clientToken != instance.Client.Token)
                    {
                        return(await Error(-4, "请求客户端提供的客户程序鉴权错误", task, id, EsbOperation.Checking));
                    }
                    if (instance.Service == null || instance.Service.ActiveStatus == 0)
                    {
                        return(await Error(1, "请求的服务不存在,或者尚未启用", task, id, EsbOperation.Checking));
                    }
                    #endregion

                    await LogPreparation(task, id, "已准备好输入参数");

                    IServiceDescriptorViewModel api;
                    if (instance.Service.Type == ServiceType.SimpleRESTful)
                    {
                        api = SimpleRestfulDescriptorViewModel.CreateFrom(instance.Service);
                    }
                    else if (instance.Service.Type == ServiceType.NCWebService)
                    {
                        api = NCDescriptorViewModel.CreateFrom(instance.Service);
                    }
                    else if (instance.Service.Type == ServiceType.PrimetonService)
                    {
                        api = PrimetonDescriptorViewModel.CreateFrom(instance.Service);
                    }
                    else
                    {
                        throw new Exception($"尚未实现的服务协定:{instance.Service.Type}");
                    }

                    await LogInvoking(task, id, "", "API请求已就绪,正在发出...");

                    RawResponse rawResponse = await api.GetResponse(originalRequest);

                    await LogInvoking(task, id, rawResponse.RequestUrl, "API请求已经返回,正在检查服务器状态");

                    try
                    {
                        string apiReturnRaw = await rawResponse.ReadContentAsync();

                        if (api.CheckResponse(apiReturnRaw, out string response, out var type))
                        {
                            await LogEnd(task, id, rawResponse.RequestUrl, response.Length + 4);

                            return(Content((new ResponseBody(0, "", response, type)).Value));
                        }
                        else
                        {
                            await LogApiError(task, id, rawResponse.RequestUrl,
                                              $"in-params={originalRequest.ToString()} response={apiReturnRaw}");

                            return(await Error(1, "服务器返回错误。具体消息需要查看日志。", task, id,
                                               EsbOperation.Respond, rawResponse.RequestUrl));
                        }
                    }
                    catch (Exception ex)
                    {
                        return(await Error(1, ex.ToString(), task, id, EsbOperation.Checking));
                    }
                }