|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCallInterceptors.Interceptors;
namespace MvcCallInterceptors.Controllers
{
internal class BaseControllerInterceptorInvoker : ControllerActionInvoker
{
protected override ActionResult InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (actionDescriptor == null)
{
throw new ArgumentNullException("actionDescriptor");
}
if (actionDescriptor == null)
{
throw new ArgumentNullException("parameters");
}
_canResultConvertedToActionResult = true;
var basecontroller = controllerContext.Controller as BaseMvcController;
if (basecontroller != null)
{
bool terminateAll = false;
bool sendResult;
IEnumerable<ActionInterceptor> interceptors;
interceptors = InterceptorsRegistry.GetInterceptors(basecontroller, actionDescriptor);
//.OrderBy((t) => t.ActionInterceptorProperties.<If there is any specific order needs to be given>);
InterceptorExecutionContext context = new InterceptorExecutionContext(controllerContext, interceptors, parameters);
var before = interceptors.Where((t) => t.ActionInterceptorProperties.InterceptionOrder == InterceptionOrder.Before);
foreach (var interceptor in before)
{
ExecuteInterceptor(context, interceptor, out terminateAll, out sendResult);
if (terminateAll && !sendResult)
{
throw new Exception(context.ChainofExecutionTerminationMessage);
}
else if (terminateAll)
{
break;
}
}
//controller last result
//action parameters
if (!terminateAll)
{
_canResultConvertedToActionResult = false;
var result = base.InvokeActionMethod(controllerContext, actionDescriptor, parameters);
context.InterceptorResult = _interceptorResult;
_canResultConvertedToActionResult = true;
var after = interceptors.Where((t) => t.ActionInterceptorProperties.InterceptionOrder == InterceptionOrder.After);
foreach (var interceptor in after)
{
ExecuteInterceptor(context, interceptor, out terminateAll, out sendResult);
if (terminateAll && !sendResult)
{
throw new Exception(context.ChainofExecutionTerminationMessage);
}
else if (terminateAll)
{
break;
}
}
}
return CreateActionResult(controllerContext, actionDescriptor, context.InterceptorResult);
}
else
{
return base.InvokeActionMethod(controllerContext, actionDescriptor, parameters);
}
}
private void ExecuteInterceptor(InterceptorExecutionContext context, ActionInterceptor actionInterceptor, out bool terminateAll, out bool sendResult)
{
terminateAll = false;
sendResult = true;
if (context == null)
{
throw new ArgumentNullException("controllerContext");
}
var Dispatcher = new InterceptMethodDispatcher(actionInterceptor.InterceptorMethodInfo);
var controller = Activator.CreateInstance(actionInterceptor.InterceptorControllerType) as IInterceptorMvcController;
controller.SetInterceptContext(context);
context.CurrentInterceptor = actionInterceptor;
context.SetInterceptorStatus(InterceptorExecutionStatus.Executing);
try
{
object newReturnValue = Dispatcher.Execute(controller, context.ActionParameters, context.InterceptorResult);
if ((actionInterceptor.InterceptorMethodInfo.ReturnType != typeof(void)))
{
context.InterceptorResult = newReturnValue;
}
}
catch (Exception ex)
{
if (actionInterceptor.ActionInterceptorProperties.BreakExecutionOnException)
{
context.CancelAllExecutionsWithTerminationMessage(string.Empty);
terminateAll = true;
throw;
}
else
{
context.SetInterceptorStatus(InterceptorExecutionStatus.ExceptionThrown);
context.Exceptions.Add(ex);
//log mechanism to catch exception
}
}
if (context.CancelAllExecutions)
{
terminateAll = true;
}
if (!string.IsNullOrWhiteSpace(context.ChainofExecutionTerminationMessage))
{
sendResult = false;
}
if (context.GetInterceptorStatus() != InterceptorExecutionStatus.ExceptionThrown)
{
context.SetInterceptorStatus(InterceptorExecutionStatus.Executed);
}
}
bool _canResultConvertedToActionResult = true;
object _interceptorResult;
protected override ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue)
{
if (_canResultConvertedToActionResult)
{
return base.CreateActionResult(controllerContext, actionDescriptor, actionReturnValue);
}
_interceptorResult = actionReturnValue;
return new EmptyResult();
}
public MvcCallInterceptors.Interceptors.InterceptorExecutionContext InterceptorExecutionContext
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
internal MvcCallInterceptors.Interceptors.InterceptMethodDispatcher InterceptMethodDispatcher
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.