internal FilterChainResponse ExecuteChain() { Dictionary <string, object> filterInputs = new Dictionary <string, object>(); FilterChainResponse outData = new FilterChainResponse(); outData.FilterResponse = filterInputs; foreach (Type filterType in filterChain) { try { AbstractFilter filterObj = (AbstractFilter)Activator.CreateInstance(filterType); filterObj.DataBag = filterInputs; outData.LastResponse = filterObj.FilterResponse; filterObj.Process(); if (!filterObj.FilterResponse.Success) { break; } } catch (Exception ex) { outData.LastResponse.Error = ex; outData.LastResponse.StatusCode = 500; outData.LastResponse.Success = false; outData.LastResponse.Message = "Internal Server Error"; break; } } return(outData); }
public static Task Invoke(Kernel kernel, RadiumContext context) { DefaultResponseBody responseBody = new DefaultResponseBody(); var req = context.Request; var res = context.Response; string reqContentType = req.ContentType; if (String.IsNullOrEmpty(reqContentType)) { reqContentType = "application/json"; } else { reqContentType = reqContentType.ToLower(); } PathExecutionParams exeParams = ResourceRepository.Repo[req.Method, req.Path.Value]; try { if (exeParams != null) { Filters.FilterChainResponse chainResponse = kernel.FilterManager.ExecuteChain(); bool isSuccess = chainResponse.LastResponse != null ? chainResponse.LastResponse.Success : true; if (!isSuccess) { res.ContentType = reqContentType; responseBody.success = false; responseBody.result = chainResponse.LastResponse.Message; responseBody.error = chainResponse.LastResponse.Error; res.StatusCode = chainResponse.LastResponse.StatusCode; } else { RestResourceHandler newObj = (RestResourceHandler)Activator.CreateInstance(exeParams.ExecutionInfo.Type); newObj.OContext = context; newObj.DataBag = chainResponse.FilterResponse; var exeMethod = exeParams.ExecutionInfo.Method; List <object> activatorParams = new List <object>(); var methodParams = exeMethod.GetParameters(); foreach (var mParam in methodParams) { if (exeParams.Parameters.ContainsKey(mParam.Name)) { var strValue = exeParams.Parameters[mParam.Name]; object convertedValue = Convert.ChangeType(strValue, mParam.ParameterType); activatorParams.Add(convertedValue); } else { throw new ParameterMismatchException(); } } object output = exeMethod.Invoke(newObj, activatorParams.ToArray()); responseBody.success = true; responseBody.result = output; res.ContentType = reqContentType; } } else { res.ContentType = reqContentType; responseBody.success = false; responseBody.result = "404 Not Found"; res.StatusCode = 404; } } catch (Exception ex) { res.ContentType = reqContentType; responseBody.success = false; responseBody.result = ex; res.StatusCode = 500; } var formatter = kernel.ResponseFormatter.GetFormatter(reqContentType); return(res.WriteAsync(formatter.Format(responseBody))); }