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);
        }
Ejemplo n.º 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);
     }
 }
        /// <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);
        }
Ejemplo n.º 4
0
 protected virtual Task OnCommandExceptionAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 => TaskUtils.GetCompletedTask();
Ejemplo n.º 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);
        /// <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));
        }
Ejemplo n.º 7
0
 private IEnumerable <IAuthorizeData> GetAuthorizeData(ActionInfo actionInfo)
 => actionInfo?.Binding?.ActionFilters != null?actionInfo.Binding.ActionFilters.OfType <IAuthorizeData>().Select(WrapInAuthorizeAttribute) : Enumerable.Empty <IAuthorizeData>();
Ejemplo n.º 8
0
        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));
            }
        }
        /// <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);
        }
Ejemplo n.º 10
0
 Task ICommandActionFilter.OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception) => OnCommandExecutedAsync(context, actionInfo, exception);
Ejemplo n.º 11
0
 Task ICommandActionFilter.OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) => OnCommandExecutingAsync(context, actionInfo);
Ejemplo n.º 12
0
 protected internal virtual Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception)
 => TaskUtils.GetCompletedTask();
        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());
        }
Ejemplo n.º 14
0
 /// <inheritdoc />
 protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 => exception != null?OnCommandExceptionAsync(context, actionInfo, exception) : Task.FromResult(0);
Ejemplo n.º 15
0
 /// <summary>
 /// Called after the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
 }
Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Called before the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
     Authorize(context);
     base.OnCommandExecuting(context, actionInfo);
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Called before the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
 }
Ejemplo n.º 19
0
        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));
                }
            }
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Called after the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Called when the exception occurs during the command invocation.
 /// </summary>
 protected virtual void OnCommandException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 {
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 23
0
        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));
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Called when the exception occurs during the command invocation.
 /// </summary>
 protected virtual void OnException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
 {
 }
Ejemplo n.º 25
0
 /// <inheritdoc />
 protected override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo)
 => Authorize(context, null);
Ejemplo n.º 26
0
 /// <inheritdoc />
 protected override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
     Authorize(context, null);
     return(TaskUtils.GetCompletedTask());
 }
Ejemplo n.º 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));
     }
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Called before the command is executed.
 /// </summary>
 protected internal virtual void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo)
 {
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Called before the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuting(DotvvmRequestContext context, ActionInfo actionInfo)
 {
     Authorize(context);
     base.OnCommandExecuting(context, actionInfo);
 }
Ejemplo n.º 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());
     }
 }