コード例 #1
0
        protected override void OnCommandException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
        {
            ((ActionFilterErrorHandlingViewModel) context.ViewModel).Result = "error was handled";
            context.IsCommandExceptionHandled = true;

            base.OnCommandException(context, actionInfo, ex);
        }
コード例 #2
0
 /// <summary>
 /// Called after the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
     if (exception != null)
     {
         OnCommandException(context, actionInfo, exception);
     }
 }
コード例 #3
0
        /// <summary>
        /// Called before the command is executed.
        /// </summary>
        protected internal override void OnCommandExecuting(DotvvmRequestContext context, ActionInfo actionInfo)
        {
            if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath))
            {
                // perform the validation
                context.ModelState.Errors.AddRange(viewModelValidator.ValidateViewModel(context.ModelState.ValidationTarget));

                // return the model state when error occurs
                context.FailOnInvalidModelState();
            }

            base.OnCommandExecuting(context, actionInfo);
        }
コード例 #4
0
 protected virtual Task OnCommandExceptionAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 => TaskUtils.GetCompletedTask();
コード例 #5
0
 /// <summary>
 /// Called when the exception occurs during the command invocation.
 /// </summary>
 protected virtual Task OnCommandExceptionAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 => Task.FromResult(0);
コード例 #6
0
        /// <inheritdoc />
        protected internal override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo)
        {
            if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath))
            {
                var validator = context.Configuration.ServiceLocator.GetService <IViewModelValidator>();
                context.ModelState.Errors.AddRange(validator.ValidateViewModel(context.ModelState.ValidationTarget));
                context.FailOnInvalidModelState();
            }

            return(Task.FromResult(0));
        }
コード例 #7
0
 private IEnumerable <IAuthorizeData> GetAuthorizeData(ActionInfo actionInfo)
 => actionInfo?.Binding?.ActionFilters != null?actionInfo.Binding.ActionFilters.OfType <IAuthorizeData>().Select(WrapInAuthorizeAttribute) : Enumerable.Empty <IAuthorizeData>();
コード例 #8
0
ファイル: DotvvmPresenter.cs プロジェクト: darilek/dotvvm
        public async Task ProcessStaticCommandRequest(DotvvmRequestContext context)
        {
            JObject postData;
            using (var jsonReader = new JsonTextReader(new StreamReader(context.OwinContext.Request.Body)))
            {
                postData = JObject.Load(jsonReader);
            }
            // validate csrf token
            context.CsrfToken = postData["$csrfToken"].Value<string>();
            CsrfProtector.VerifyToken(context, context.CsrfToken);

            var command = postData["command"].Value<string>();
            var arguments = postData["args"] as JArray;
            var lastDot = command.LastIndexOf('.');
            var typeName = command.Remove(lastDot);
            var methodName = command.Substring(lastDot + 1);
            var methodInfo = Type.GetType(typeName).GetMethod(methodName);

            if (!Attribute.IsDefined(methodInfo, typeof(AllowStaticCommandAttribute)))
            {
                throw new DotvvmHttpException($"This method cannot be called from the static command. If you need to call this method, add the '{nameof(AllowStaticCommandAttribute)}' to the method.");
            }
            var target = methodInfo.IsStatic ? null : arguments[0].ToObject(methodInfo.DeclaringType);
            var methodArguments =
                arguments.Skip(methodInfo.IsStatic ? 0 : 1)
                .Zip(methodInfo.GetParameters(), (arg, parameter) => arg.ToObject(parameter.ParameterType))
                .ToArray();
            var actionInfo = new ActionInfo()
            {
                IsControlCommand = false,
                Action = () => methodInfo.Invoke(target, methodArguments)
            };
            var filters = context.Configuration.Runtime.GlobalFilters
                .Concat(methodInfo.DeclaringType.GetCustomAttributes<ActionFilterAttribute>())
                .Concat(methodInfo.GetCustomAttributes<ActionFilterAttribute>())
                .ToArray();

            var task = ExecuteCommand(actionInfo, context, filters);
            await task;
            object result = TaskUtils.GetResult(task);

            using (var writer = new StreamWriter(context.OwinContext.Response.Body))
            {
                writer.WriteLine(JsonConvert.SerializeObject(result));
            }
        }
コード例 #9
0
        /// <summary>
        /// Called before the command is executed.
        /// </summary>
        protected internal override void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
        {
            if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath))
            {
                // perform the validation
                context.ModelState.Errors.AddRange(viewModelValidator.ValidateViewModel(context.ModelState.ValidationTarget));

                // return the model state when error occurs
                context.FailOnInvalidModelState();
            }

            base.OnCommandExecuting(context, actionInfo);
        }
コード例 #10
0
 Task ICommandActionFilter.OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception) => OnCommandExecutedAsync(context, actionInfo, exception);
コード例 #11
0
 Task ICommandActionFilter.OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) => OnCommandExecutingAsync(context, actionInfo);
コード例 #12
0
 protected internal virtual Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception)
 => TaskUtils.GetCompletedTask();
コード例 #13
0
        protected internal override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo)
        {
            if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath))
            {
                var validator = context.Services.GetRequiredService <IViewModelValidator>();
                context.ModelState.Errors.AddRange(validator.ValidateViewModel(context.ModelState.ValidationTarget));
                context.FailOnInvalidModelState();
            }

            return(TaskUtils.GetCompletedTask());
        }
コード例 #14
0
 /// <inheritdoc />
 protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 => exception != null?OnCommandExceptionAsync(context, actionInfo, exception) : Task.FromResult(0);
コード例 #15
0
 /// <summary>
 /// Called after the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
 }
コード例 #16
0
        /// <summary>
        /// Resolves the command for the specified post data.
        /// </summary>
        public void ResolveCommand(DotvvmRequestContext context, DotvvmView view, string serializedPostData, out ActionInfo actionInfo)
        {
            // get properties
            var data = JObject.Parse(serializedPostData);
            var path = data["currentPath"].Values<string>().ToArray();
            var command = data["command"].Value<string>();
            var controlUniqueId = data["controlUniqueId"].Value<string>();

            if (string.IsNullOrEmpty(command))
            {
                // empty command
                actionInfo = null;
            }
            else
            {
                // find the command target
                if (!string.IsNullOrEmpty(controlUniqueId))
                {
                    var target = view.FindControl(controlUniqueId);
                    if (target == null)
                    {
                        throw new Exception(string.Format("The control with ID '{0}' was not found!", controlUniqueId));
                    }
                    actionInfo = commandResolver.GetFunction(target, view, context, path, command);
                }
                else
                {
                    actionInfo = commandResolver.GetFunction(view, context, path, command);
                }
            }
        }
コード例 #17
0
ファイル: AuthorizeAttribute.cs プロジェクト: samdubey/dotvvm
 /// <summary>
 /// Called before the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
     Authorize(context);
     base.OnCommandExecuting(context, actionInfo);
 }
コード例 #18
0
 /// <summary>
 /// Called before the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
 }
コード例 #19
0
ファイル: DotvvmPresenter.cs プロジェクト: holajan/dotvvm
        public void ProcessStaticCommandRequest(DotvvmRequestContext context)
        {
            JObject postData;
            using (var jsonReader = new JsonTextReader(new StreamReader(context.OwinContext.Request.Body)))
            {
                postData = JObject.Load(jsonReader);
            }
            // validate csrf token
            context.CsrfToken = postData["$csrfToken"].Value<string>();
            CsrfProtector.VerifyToken(context, context.CsrfToken);

            var command = postData["command"].Value<string>();
            var arguments = postData["args"].ToObject<object[]>();
            var lastDot = command.LastIndexOf('.');
            var typeName = command.Remove(lastDot);
            var methodName = command.Substring(lastDot + 1);
            var methodInfo = Type.GetType(typeName).GetMethod(methodName);

            if (!Attribute.IsDefined(methodInfo, typeof(StaticCommandCallableAttribute)))
            {
                throw new DotvvmHttpException("method validation failed");
            }
            var actionInfo = new ActionInfo()
            {
                IsControlCommand = false,
                Action = () => methodInfo.Invoke(null, arguments)
            };
            var filters = methodInfo.GetCustomAttributes<ActionFilterAttribute>();
            foreach (var filter in filters)
            {
                filter.OnCommandExecuting(context, actionInfo);
            }
            Exception exception = null;
            object result = null;
            try
            {
                result = methodInfo.Invoke(null, arguments);
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ex.InnerException;
                }
                if (ex is DotvvmInterruptRequestExecutionException)
                {
                    throw new DotvvmInterruptRequestExecutionException("The request execution was interrupted in the command!", ex);
                }
                exception = ex;
            }
            foreach (var filter in filters)
            {
                filter.OnCommandExecuted(context, actionInfo, exception);
            }
            if (exception != null)
            {
                throw new Exception("unhandled exception in command", exception);
            }

            if (result != null)
            {
                using (var writer = new StreamWriter(context.OwinContext.Response.Body))
                {
                    writer.WriteLine(JsonConvert.SerializeObject(result));
                }
            }
        }
コード例 #20
0
 /// <summary>
 /// Called after the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
 }
コード例 #21
0
 /// <summary>
 /// Called when the exception occurs during the command invocation.
 /// </summary>
 protected virtual void OnCommandException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 {
 }
コード例 #22
0
 /// <summary>
 /// Called after the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
     if (exception != null)
     {
         OnException(context, actionInfo, exception);
     }
 }
コード例 #23
0
ファイル: DotvvmPresenter.cs プロジェクト: darilek/dotvvm
        protected Task ExecuteCommand(ActionInfo action, DotvvmRequestContext context, IEnumerable<ActionFilterAttribute> methodFilters)
        {
            // run OnCommandExecuting on action filters
            foreach (var filter in methodFilters)
            {
                filter.OnCommandExecuting(context, action);
            }
            object result = null;
            try
            {
                result = action.Action();
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ex.InnerException;
                }
                if (ex is DotvvmInterruptRequestExecutionException)
                {
                    throw new DotvvmInterruptRequestExecutionException("The request execution was interrupted in the command!", ex);
                }
                context.CommandException = ex;
            }

            // run OnCommandExecuted on action filters
            foreach (var filter in methodFilters.Reverse())
            {
                filter.OnCommandExecuted(context, action, context.CommandException);
            }

            if (context.CommandException != null && !context.IsCommandExceptionHandled)
            {
                throw new Exception("Unhandled exception occured in the command!", context.CommandException);
            }

            return result as Task ?? (result == null ? TaskUtils.GetCompletedTask() : Task.FromResult(result));
        }
コード例 #24
0
 /// <summary>
 /// Called when the exception occurs during the command invocation.
 /// </summary>
 protected virtual void OnException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 {
 }
コード例 #25
0
 /// <inheritdoc />
 protected override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo)
 => Authorize(context, null);
コード例 #26
0
 /// <inheritdoc />
 protected override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
     Authorize(context, null);
     return(TaskUtils.GetCompletedTask());
 }
コード例 #27
0
 protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
     if (exception != null)
     {
         return(OnCommandExceptionAsync(context, actionInfo, exception));
     }
     else
     {
         return(Task.FromResult(0));
     }
 }
コード例 #28
0
 /// <summary>
 /// Called before the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
 }
コード例 #29
0
ファイル: AuthorizeAttribute.cs プロジェクト: holajan/dotvvm
 /// <summary>
 /// Called before the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuting(DotvvmRequestContext context, ActionInfo actionInfo)
 {
     Authorize(context);
     base.OnCommandExecuting(context, actionInfo);
 }
コード例 #30
0
 protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception)
 {
     if (exception != null)
     {
         return(OnCommandExceptionAsync(context, actionInfo, exception));
     }
     else
     {
         return(TaskUtils.GetCompletedTask());
     }
 }