private static void Validate <T>(ModelBinderEventArgs <T> args, IDIResolver resolver) { foreach (var validator in ModelValidatorsTypes.Select(x => (IModelValidator)resolver.Resolve(x))) { validator.Validate(args.Model, resolver); } }
/// <summary> /// Processes the HTTP request for controllers. /// </summary> /// <param name="resolver">THE DI container resolver</param> /// <param name="context">The context.</param> /// <returns></returns> public RequestHandlingResult ProcessRequest(IDIResolver resolver, HttpContext context) { var result = _controllersProcessor.ProcessControllers(resolver, context); switch (result) { case ControllersProcessorResult.Ok: return(_pageProcessor.ProcessPage(resolver, context)); case ControllersProcessorResult.Http401: context.Response.StatusCode = 401; _redirector.SetLoginReturnUrlFromCurrentUri(); break; case ControllersProcessorResult.Http403: context.Response.StatusCode = 403; break; case ControllersProcessorResult.Http404: if (ApplicationBuilderExtensions.TerminalMiddleware) { context.Response.StatusCode = 404; } else { return(RequestHandlingResult.UnhandledResult()); } break; } return(RequestHandlingResult.HandledResult()); }
public override void Validate(object value, PropertyInfo propertyInfo, IDIResolver resolver) { var objectIsValid = false; if (value != null) { var objectType = value.GetType(); if (objectType == typeof(DateTime)) { objectIsValid = !value.Equals(default(DateTime)); } else { objectIsValid = true; } } if (objectIsValid) { return; } TryThrowCustomOrStringTableException(resolver); throw new ModelValidationException($"Required property '{propertyInfo.Name}' is null or empty"); }
/// <summary> /// Processes the HTTP request for controllers. /// </summary> /// <param name="resolver">THE DI container resolver</param> /// <param name="context">The context.</param> /// <returns></returns> public async Task <RequestHandlingStatus> ProcessRequest(IDIResolver resolver, HttpContext context) { var result = await _controllersProcessor.ProcessControllers(resolver, context); switch (result) { case ControllersProcessorResult.Ok: return(await _pageProcessor.ProcessPage(resolver, context)); case ControllersProcessorResult.Http401: context.Response.StatusCode = 401; _redirector.SetLoginReturnUrlFromCurrentUri(); break; case ControllersProcessorResult.Http403: context.Response.StatusCode = 403; break; case ControllersProcessorResult.Http404: if (TerminalMiddleware) { context.Response.StatusCode = 404; } else { return(RequestHandlingStatus.RequestWasUnhandled); } break; } return(RequestHandlingStatus.RequestWasHandled); }
public void SetVariables(IDIResolver resolver) { var environment = resolver.Resolve <IEnvironment>(); var languageManager = resolver.Resolve <ILanguageManagerProvider>().Get(); var context = resolver.Resolve <IWebContextProvider>().Get(); var stopWatchProvider = resolver.Resolve <IStopwatchProvider>(); _dataCollector.Add(VariableNameTemplatesPath, environment.TemplatesPath); _dataCollector.Add(VariableNameSiteStyle, environment.SiteStyle); if (!string.IsNullOrEmpty(languageManager.Language)) { _dataCollector.Add(VariableNameCurrentLanguage, languageManager.Language); _dataCollector.Add(VariableNameCurrentLanguageExtension, "." + languageManager.Language); _dataCollector.Add(VariableNameCurrentLanguageCultureName, Thread.CurrentThread.CurrentCulture.TextInfo.CultureName); _dataCollector.Add(VariableNameCurrentLanguageCultureNameExtension, "." + Thread.CurrentThread.CurrentCulture.TextInfo.CultureName); } else { _dataCollector.Add(VariableNameCurrentLanguage, (string)null); _dataCollector.Add(VariableNameCurrentLanguageExtension, (string)null); _dataCollector.Add(VariableNameCurrentLanguageCultureName, (string)null); _dataCollector.Add(VariableNameCurrentLanguageCultureNameExtension, (string)null); } _dataCollector.Add(VariableNameSiteUrl, context.SiteUrl); _dataCollector.Add(VariableNameSiteVirtualPath, context.VirtualPath); if (!_disableAutomaticSiteTitleSet) { SetSiteTitleFromStringTable(context.Request.Path.Value, resolver.Resolve <IStringTable>()); } _dataCollector.Add(VariableNameExecutionTime, stopWatchProvider.StopAndGetMeasurement().ToString("mm\\:ss\\:fff")); }
/// <summary> /// Processes (build web-page and send to client, process current page state) the current web-page /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> public async Task <RequestHandlingStatus> ProcessPage(IDIResolver resolver, HttpContext context) { context.Response.ContentType = "text/html"; await _responseWriter.WriteAsync(_pageBuilder.Build(resolver), context.Response); return(RequestHandlingStatus.RequestWasHandled); }
/// <summary> /// Processes (build web-page and send to client, process current page state) the current web-page /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> public RequestHandlingResult ProcessPage(IDIResolver resolver, HttpContext context) { context.Response.ContentType = "text/html"; _redirector.PreviousPageUrl = context.Request.GetEncodedUrl(); return(RequestHandlingResult.HandledResult(_responseWriter.WriteAsync(_pageBuilder.Build(resolver), context.Response))); }
/// <summary> /// Processes (build web-page and send to client, process current page state) the current web-page /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> public async Task <RequestHandlingStatus> ProcessPage(IDIResolver resolver, HttpContext context) { context.Response.ContentType = "text/html"; _redirector.PreviousPageUrl = context.Request.GetEncodedUrl(); await _responseWriter.WriteAsync(_pageBuilder.Build(resolver), context.Response); return(RequestHandlingStatus.RequestWasHandled); }
/// <summary> /// Validates the specified model. /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="model">The model.</param> /// <param name="resolver">The resolver.</param> /// <exception cref="ModelValidationException"></exception> public void Validate <T>(T model, IDIResolver resolver) { var type = typeof(T); foreach (var item in type.GetProperties()) { ValidateProperty(item.GetValue(model), item, resolver); } }
/// <summary> /// Creates the controller. /// </summary> /// <param name="controllerType">Type of the controller.</param> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public ControllerBase CreateController(Type controllerType, IDIResolver resolver, HttpContext context, dynamic?routeParameters = null) { var controller = (ControllerBase)resolver.Resolve(controllerType); BuildActionModulesAccessorProperties(controller, resolver); controller.RouteParameters = routeParameters; return(controller); }
/// <summary> /// Process controllers for current HTTP request /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> /// <returns></returns> public async Task <ControllersProcessorResult> ProcessControllers(IDIResolver resolver, HttpContext context) { var atleastOneNonAnyPageControllerMatched = false; foreach (var controller in _agent.GetStandardControllersMetaData()) { var matcherResult = _agent.MatchControllerRoute(controller, context.Request.Path.Value, context.Request.Method); if (matcherResult == null || !matcherResult.Success) { continue; } var securityResult = _agent.IsSecurityRulesViolated(controller, context.User); if (securityResult == SecurityRuleCheckResult.NotAuthenticated) { return(ControllersProcessorResult.Http401); } if (securityResult == SecurityRuleCheckResult.Forbidden) { return(await ProcessForbiddenSecurityRule(resolver, context)); } var result = await ProcessController(controller, resolver, context, matcherResult.RouteParameters); if (result != ControllersProcessorResult.Ok) { return(result); } if (!_agent.IsAnyPageController(controller)) { atleastOneNonAnyPageControllerMatched = true; } } if (!atleastOneNonAnyPageControllerMatched) { var result = await ProcessOnlyAnyPageControllersMatched(resolver, context); if (result != ControllersProcessorResult.Ok) { return(result); } } else { _redirector.SetPreviousPageUrlToCurrentPage(); } return(ControllersProcessorResult.Ok); }
/// <summary> /// Creates the view. /// </summary> /// <param name="viewType">Type of the view.</param> /// <param name="resolver"></param> /// <returns></returns> public View CreateView(Type viewType, IDIResolver resolver) { var view = (View)resolver.Resolve(viewType); BuildModulesAccessorProperties(view, resolver); view.Language = resolver.Resolve <ILanguageManagerProvider>().Get().Language; view.SiteUrl = resolver.Resolve <IWebContextProvider>().Get().SiteUrl; return(view); }
public override void Validate(object value, PropertyInfo propertyInfo, IDIResolver resolver) { if (!(value is string)) return; if (Regex.IsMatch((string)value, RegexString)) return; TryThrowCustomOrStringTableException(resolver); throw new ModelValidationException($"Property '{propertyInfo.Name}' regex not matched, actual value: '{value}', pattern: '{RegexString}'"); }
/// <summary> /// Throws the custom or string table exception. /// </summary> /// <param name="resolver">The resolver.</param> /// <exception cref="ModelValidationException"> /// </exception> protected void TryThrowCustomOrStringTableException(IDIResolver resolver) { if (string.IsNullOrEmpty(ErrorMessage)) { return; } if (IsMessageFromStringTable) { throw new ModelValidationException(resolver.Resolve <IStringTable>().GetItem(ErrorMessage)); } throw new ModelValidationException(ErrorMessage); }
/// <summary> /// Builds a web page /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <returns></returns> public string Build(IDIResolver resolver) { resolver.Resolve <IStringTableItemsSetter>().Set(); resolver.Resolve <IContextVariablesSetter>().SetVariables(resolver); var tpl = _templateFactory.Load(resolver.Resolve <IEnvironment>().MasterTemplateFileName); foreach (var item in _dataCollector.Items.Keys) { tpl.Set(item, _dataCollector.Items[item]); } return(tpl.Get()); }
/// <summary> /// Process controllers for current HTTP request /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> /// <returns></returns> public ControllersProcessorResult ProcessControllers(IDIResolver resolver, HttpContext context) { var atleastOneNonAnyPageControllerMatched = false; foreach (var metaData in _agent.GetStandardControllersMetaData()) { var matcherResult = _agent.MatchControllerRoute(metaData, context.Request.Path.Value, context.Request.Method); if (matcherResult == null || !matcherResult.Success) { continue; } var securityResult = _agent.IsSecurityRulesViolated(metaData, context.User); if (securityResult == SecurityRuleCheckResult.NotAuthenticated) { return(ControllersProcessorResult.Http401); } if (securityResult == SecurityRuleCheckResult.Forbidden) { return(ProcessForbiddenSecurityRule(resolver, context)); } var result = ProcessController(metaData.ControllerType, resolver, context, matcherResult.RouteParameters); if (result != ControllersProcessorResult.Ok) { return(result); } if (!_agent.IsAnyPageController(metaData)) { atleastOneNonAnyPageControllerMatched = true; } } if (!atleastOneNonAnyPageControllerMatched) { var result = ProcessOnlyAnyPageControllersMatched(resolver, context); if (result != ControllersProcessorResult.Ok) { return(result); } } return(ProcessAsyncControllersResponses(resolver)); }
public override void Validate(object value, PropertyInfo propertyInfo, IDIResolver resolver) { if (!(value is string)) { return; } if (StringHelper.ValidateEMail((string)value)) { return; } TryThrowCustomOrStringTableException(resolver); throw new ModelValidationException($"Property '{propertyInfo.Name}' should be an email, actual value: '{value}'"); }
private ControllersProcessorResult ProcessController(Type controllerType, IDIResolver resolver, HttpContext context, dynamic routeParameters) { var result = _controllerExecutor.Execute(controllerType, resolver, context, routeParameters); if (result == ControllerResponseResult.RawOutput) { return(ControllersProcessorResult.RawOutput); } if (result == ControllerResponseResult.Redirect) { return(ControllersProcessorResult.Redirect); } return(ControllersProcessorResult.Ok); }
private ControllersProcessorResult ProcessAsyncControllersResponses(IDIResolver resolver) { foreach (var controllerResponseResult in _controllerExecutor.ProcessAsyncControllersResponses(resolver)) { if (controllerResponseResult == ControllerResponseResult.RawOutput) { return(ControllersProcessorResult.RawOutput); } if (controllerResponseResult == ControllerResponseResult.Redirect) { return(ControllersProcessorResult.Redirect); } } return(ControllersProcessorResult.Ok); }
public override void Validate(object value, PropertyInfo propertyInfo, IDIResolver resolver) { if (!(value is string)) { return; } var checkValue = (string)value; if (checkValue.Length >= MinimumPropertyLength) { return; } TryThrowCustomOrStringTableException(resolver); throw new ModelValidationException($"Property '{propertyInfo.Name}' required minimum length is '{MinimumPropertyLength}', actual value: '{value}'"); }
/// <summary> /// Parses model and validates it asynchronously /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="resolver">The resolver.</param> /// <returns></returns> public async Task <T> ProcessAsync <T>(IDIResolver resolver) { var args = new ModelBinderEventArgs <T>(_context); foreach (var binder in ModelBindersTypes.Select(binderType => (IModelBinder)resolver.Resolve(binderType))) { await binder.BindAsync(args); if (!args.IsBound) { continue; } Validate(args, resolver); return(args.Model); } throw new ModelBindingException($"Unrecognized request content type for binding: {_context.Request.ContentType}"); }
/// <summary> /// Creates and executes the specified controller. /// </summary> /// <param name="controllerType">Type of the controller.</param> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public ControllerResponseResult Execute(Type controllerType, IDIResolver resolver, HttpContext context, dynamic routeParameters = null) { ControllerBase controller = _controllerFactory.CreateController(controllerType, resolver, context, routeParameters); var syncController = controller as SyncControllerBase; if (syncController != null) return ProcessControllerResponse(syncController.Invoke(), resolver); var asyncController = controller as AsyncControllerBase; if (asyncController != null) { var task = asyncController.Invoke(); _controllersResponses.Add(task); } return ControllerResponseResult.Default; }
private ControllersProcessorResult ProcessOnlyAnyPageControllersMatched(IDIResolver resolver, HttpContext context) { var http404Controller = _agent.GetHandlerController(HandlerControllerType.Http404Handler); if (http404Controller == null) { return(ControllersProcessorResult.Http404); } var handlerControllerResult = _controllerExecutor.Execute(http404Controller, resolver, context); if (handlerControllerResult == ControllerResponseResult.RawOutput) { return(ControllersProcessorResult.RawOutput); } if (handlerControllerResult == ControllerResponseResult.Redirect) { return(ControllersProcessorResult.Redirect); } return(ControllersProcessorResult.Ok); }
private ControllersProcessorResult ProcessForbiddenSecurityRule(IDIResolver resolver, HttpContext context) { var http403Controller = _agent.GetHandlerController(HandlerControllerType.Http403Handler); if (http403Controller == null) { return(ControllersProcessorResult.Http403); } var handlerControllerResult = _controllerExecutor.Execute(http403Controller, resolver, context); if (handlerControllerResult == ControllerResponseResult.RawOutput) { return(ControllersProcessorResult.RawOutput); } if (handlerControllerResult == ControllerResponseResult.Redirect) { return(ControllersProcessorResult.Redirect); } return(ControllersProcessorResult.Ok); }
public override void Validate(object value, PropertyInfo propertyInfo, IDIResolver resolver) { var objectIsValid = false; if (value != null) { objectIsValid = value switch { DateTime _ => !value.Equals(default(DateTime)), string s => !string.IsNullOrEmpty(s), _ => true }; } if (objectIsValid) { return; } TryThrowCustomOrStringTableException(resolver); throw new ModelValidationException($"Required property '{propertyInfo.Name}' is null or empty"); } }
/// <summary> /// Validates the specified property value. /// </summary> /// <param name="value">The object value.</param> /// <param name="propertyInfo">Information about the property containing this attribute.</param> /// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param> public abstract void Validate(object?value, PropertyInfo propertyInfo, IDIResolver resolver);
private ControllerResponseResult ProcessControllerResponse(ControllerResponse response, IDIResolver resolver) { if (response == null) { return(ControllerResponseResult.Default); } _controllerResponseBuilder.BuildControllerResponseProperties(response, resolver); return(response.Process()); }
/// <summary> /// Resolves the specified type. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resolver">The DI resolver.</param> /// <returns></returns> public static T Resolve <T>(this IDIResolver resolver) { return((T)resolver.Resolve(typeof(T))); }
/// <summary> /// Creates and executes the specified controller. /// </summary> /// <param name="controllerMetaData">Type of the controller.</param> /// <param name="resolver">The DI container resolver.</param> /// <param name="context">The context.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public async Task <ControllerResponseResult> Execute(IControllerMetaData controllerMetaData, IDIResolver resolver, HttpContext context, dynamic?routeParameters = null) { ControllerResponse?response = null; var controller = _controllerFactory.CreateController(controllerMetaData.ControllerType, resolver, context, routeParameters); switch (controller) { case SyncControllerBase syncController: { response = syncController.Invoke(); break; } case AsyncControllerBase asyncController: { response = await asyncController.Invoke(); break; } } if (response == null) { return(ControllerResponseResult.Default); } _controllerResponseBuilder.BuildControllerResponseProperties(response, resolver); return(await response.Process()); }
/// <summary> /// Processes the asynchronous controllers responses. /// </summary> /// <param name="resolver">The DI container resolver.</param> /// <returns></returns> public IEnumerable <ControllerResponseResult> ProcessAsyncControllersResponses(IDIResolver resolver) { foreach (var task in _controllersResponses) { task.Wait(); yield return(ProcessControllerResponse(task.Result, resolver)); } }