/// <summary> /// Pendent /// </summary> /// <param name="area">The area.</param> /// <param name="controller">The controller.</param> /// <param name="action">The action.</param> /// <param name="metaDescriptor">The meta descriptor.</param> /// <param name="match">The routing match.</param> /// <returns></returns> public IControllerContext Create(string area, string controller, string action, ControllerMetaDescriptor metaDescriptor, RouteMatch match) { var context = new ControllerContext(controller, area, action, metaDescriptor) { RouteMatch = match }; context.ViewFolder = ResolveViewFolder(context, area, controller, action); context.SelectedViewName = ResolveDefaultViewSelection(context, area, controller, action); foreach (var pair in match.Parameters) { if (pair.Value == null || pair.Key == "controller" || pair.Key == "action" || pair.Key == "area") { // We skip those only to avoid compatibility issues as // customactionparameters have higher precedence on parameters matching continue; } context.CustomActionParameters[pair.Key] = pair.Value; } return(context); }
public void Request_CreatesSessionfulHandler() { StringWriter writer = new StringWriter(); HttpResponse res = new HttpResponse(writer); HttpRequest req = new HttpRequest(Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Handlers/Files/simplerequest.txt"), "http://localhost:1333/home/something", ""); RouteMatch routeMatch = new RouteMatch(); HttpContext httpCtx = new HttpContext(req, res); httpCtx.Items[RouteMatch.RouteMatchKey] = routeMatch; using (_MockRepository.Record()) { ControllerMetaDescriptor controllerDesc = new ControllerMetaDescriptor(); controllerDesc.ControllerDescriptor = new ControllerDescriptor(typeof(Controller), "home", "", false); Expect.Call(_ControllerFactoryMock.CreateController("", "home")).IgnoreArguments().Return(_ControllerMock); Expect.Call(_ControllerDescriptorProviderMock.BuildDescriptor(_ControllerMock)).Return(controllerDesc); Expect.Call(_ControllerContextFactoryMock.Create("", "home", "something", controllerDesc, routeMatch)). Return(new ControllerContext()); } using (_MockRepository.Playback()) { IHttpHandler handler = _HandlerFactory.GetHandler(httpCtx, "GET", "", ""); Assert.IsNotNull(handler); Assert.IsInstanceOfType(typeof(MonoRailHttpHandler), handler); } }
public void Request_CreatesSessionfulHandler() { StringWriter writer = new StringWriter(); HttpResponse res = new HttpResponse(writer); HttpRequest req = new HttpRequest(Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Handlers/Files/simplerequest.txt"), "http://localhost:1333/home/something", ""); RouteMatch routeMatch = new RouteMatch(); HttpContext httpCtx = new HttpContext(req, res); httpCtx.Items[RouteMatch.RouteMatchKey] = routeMatch; using(mockRepository.Record()) { ControllerMetaDescriptor controllerDesc = new ControllerMetaDescriptor(); controllerDesc.ControllerDescriptor = new ControllerDescriptor(typeof(Controller), "home", "", false); Expect.Call(controllerFactoryMock.CreateController("", "home")).IgnoreArguments().Return(controllerMock); Expect.Call(controllerDescriptorProviderMock.BuildDescriptor(controllerMock)).Return(controllerDesc); ControllerContext controllerContext = new ControllerContext(); controllerContext.ControllerDescriptor = new ControllerMetaDescriptor(); Expect.Call(controllerContextFactoryMock.Create("", "home", "something", controllerDesc, routeMatch)). Return(controllerContext); } using(mockRepository.Playback()) { IHttpHandler handler = handlerFactory.GetHandler(httpCtx, "GET", "", ""); Assert.IsNotNull(handler); Assert.IsInstanceOf(typeof(MonoRailHttpHandler), handler); } }
/// <summary> /// Initializes a new instance of the <see cref="ControllerContext"/> class. /// </summary> /// <param name="name">The controller name.</param> /// <param name="areaName">The area name.</param> /// <param name="action">The action name.</param> /// <param name="metaDescriptor">The meta descriptor.</param> public ControllerContext(string name, string areaName, string action, ControllerMetaDescriptor metaDescriptor) { this.Name = name; this.AreaName = areaName; this.Action = action; this.ControllerDescriptor = metaDescriptor; }
public void ExecuteMethodUntilSignal() { startEvent.WaitOne(int.MaxValue, false); while (!stopEvent.WaitOne(1, false)) { ControllerMetaDescriptor desc1 = builder.BuildDescriptor(new Controller1()); ControllerMetaDescriptor desc2 = builder.BuildDescriptor(new Controller2()); Assert.AreEqual(0, desc1.ActionProviders.Count); Assert.AreEqual(1, desc1.Filters.Count); Assert.IsNotNull(desc1.Layout); Assert.IsNotNull(desc1.Rescues); Assert.AreEqual(0, desc2.ActionProviders.Count); Assert.AreEqual(1, desc2.Filters.Count); Assert.IsNotNull(desc2.Layout); Assert.IsNotNull(desc2.Rescues); ActionMetaDescriptor ac1 = desc1.GetAction(typeof(Controller1).GetMethod("Index")); Assert.IsNotNull(ac1.SkipRescue); Assert.AreEqual(1, ac1.SkipFilters.Count); ActionMetaDescriptor ac2 = desc2.GetAction(typeof(Controller2).GetMethod("Index", new Type[] { typeof(int) })); Assert.IsNotNull(ac2.SkipRescue); Assert.AreEqual(1, ac2.SkipFilters.Count); ActionMetaDescriptor ac3 = desc2.GetAction(typeof(Controller2).GetMethod("Index", new Type[] { typeof(String) })); Assert.IsNotNull(ac3.SkipRescue); Assert.AreEqual(0, ac3.SkipFilters.Count); } }
/// <summary> /// Collects the action level attributes. /// </summary> /// <param name="descriptor">The descriptor.</param> private void CollectActionLevelAttributes(ControllerMetaDescriptor descriptor) { foreach (var action in descriptor.Actions.Values) { if (action is IList) { foreach (MethodInfo overloadedAction in (action as IList)) { CollectActionAttributes(overloadedAction, descriptor); } continue; } var methodInfo = action as MethodInfo; if (methodInfo != null) { CollectActionAttributes(methodInfo, descriptor); } else { var asyncActionPair = (AsyncActionPair)action; CollectActionAttributes(asyncActionPair.BeginActionInfo, descriptor); CollectActionAttributes(asyncActionPair.EndActionInfo, descriptor); } } }
/// <summary> /// Initializes a new instance of the <see cref="ControllerContext"/> class. /// </summary> /// <param name="name">The controller name.</param> /// <param name="areaName">The area name.</param> /// <param name="action">The action name.</param> /// <param name="metaDescriptor">The meta descriptor.</param> public ControllerContext(string name, string areaName, string action, ControllerMetaDescriptor metaDescriptor) { this.name = name; this.areaName = areaName; this.action = action; this.metaDescriptor = metaDescriptor; }
/// <summary> /// Creates the handler. /// </summary> /// <param name="controllerDesc">The controller descriptor.</param> /// <param name="engineContext">The engine context.</param> /// <param name="controller">The controller.</param> /// <param name="controllerContext">The controller context.</param> /// <returns> /// A new <see cref="T:System.Web.IHttpHandler"></see> object that processes the request. /// </returns> protected virtual IHttpHandler CreateHandler(ControllerMetaDescriptor controllerDesc, IEngineContext engineContext, IController controller, IControllerContext controllerContext) { if (IgnoresSession(controllerDesc.ControllerDescriptor)) { return(new SessionlessMonoRailHttpHandler(engineContext, controller, controllerContext)); } return(new MonoRailHttpHandler(engineContext, controller, controllerContext)); }
/// <summary> /// Collects the default action. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectDefaultAction(ControllerMetaDescriptor descriptor, Type controllerType) { var attributes = controllerType.GetCustomAttributes(typeof(DefaultActionAttribute), true); if (attributes.Length != 0) { descriptor.DefaultAction = (DefaultActionAttribute)attributes[0]; } }
/// <summary> /// Collects the class level attributes. /// </summary> /// <param name="controllerType">Type of the controller.</param> /// <param name="descriptor">The descriptor.</param> private void CollectClassLevelAttributes(Type controllerType, ControllerMetaDescriptor descriptor) { CollectHelpers(descriptor, controllerType); CollectResources(descriptor, controllerType); CollectFilters(descriptor, controllerType); CollectLayout(descriptor, controllerType); CollectRescues(descriptor, controllerType); CollectDefaultAction(descriptor, controllerType); CollectScaffolding(descriptor, controllerType); CollectDynamicActionProviders(descriptor, controllerType); CollectCacheConfigure(descriptor, controllerType); }
/// <summary> /// Collects the scaffolding. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectScaffolding(ControllerMetaDescriptor descriptor, Type controllerType) { var attributes = controllerType.GetCustomAttributes(typeof(ScaffoldingAttribute), false); if (attributes.Length != 0) { foreach (ScaffoldingAttribute scaffolding in attributes) { descriptor.Scaffoldings.Add(scaffolding); } } }
public void CanSelectMethodAndCreatesAnActionMethodExecutorForIt() { var controllerMeta = new ControllerMetaDescriptor(); var controller = new BaseClassController(); var context = new ControllerContext("baseclass", "", "action1", controllerMeta); controllerMeta.Actions["action1"] = typeof(BaseClassController).GetMethod("Action1"); var action = selector.Select(engine, controller, context, ActionType.Sync); Assert.IsNotNull(action); Assert.IsInstanceOf(typeof(ActionMethodExecutor), action); }
public void CanSelectDynActionAndCreatesADynamicActionExecutor() { var controllerMeta = new ControllerMetaDescriptor(); var controller = new BaseClassController(); var context = new ControllerContext("baseclass", "", "action2", controllerMeta); context.DynamicActions.Add("action2", new DummyDynamicAction()); var action = selector.Select(engine, controller, context, ActionType.Sync); Assert.IsNotNull(action); Assert.IsInstanceOf(typeof(DynamicActionExecutor), action); }
/// <summary> /// Collects the actions. /// </summary> /// <param name="controllerType">Type of the controller.</param> /// <param name="desc">The desc.</param> private void CollectActions(Type controllerType, ControllerMetaDescriptor desc) { // HACK: GetRealControllerType is a workaround for DYNPROXY-14 bug // see: http://support.castleproject.org/browse/DYNPROXY-14 controllerType = GetRealControllerType(controllerType); var methods = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (var method in methods) { var declaringType = method.DeclaringType; if (method.IsSpecialName) { continue; } if (declaringType == typeof(Object) || declaringType == typeof(IController) || declaringType == typeof(Controller)) // || declaringType == typeof(SmartDispatcherController)) { continue; } if (desc.Actions.Contains(method.Name)) { var list = desc.Actions[method.Name] as ArrayList; if (list == null) { list = new ArrayList { desc.Actions[method.Name] }; desc.Actions[method.Name] = list; } list.Add(method); } else { desc.Actions[method.Name] = method; } } MergeAsyncMethodPairsToSingleAction(desc); }
/// <summary> /// Collects the action attributes. /// </summary> /// <param name="method">The method.</param> /// <param name="descriptor">The descriptor.</param> private void CollectActionAttributes(MethodInfo method, ControllerMetaDescriptor descriptor) { if (logger.IsDebugEnabled) { logger.DebugFormat("Collection attributes for action {0}", method.Name); } var actionDescriptor = descriptor.GetAction(method); if (actionDescriptor == null) { actionDescriptor = CreateActionDescriptor(); descriptor.AddAction(method, actionDescriptor); } CollectResources(actionDescriptor, method); CollectSkipFilter(actionDescriptor, method); CollectRescues(actionDescriptor, method); CollectAccessibleThrough(actionDescriptor, method); CollectSkipRescue(actionDescriptor, method); CollectLayout(actionDescriptor, method); CollectCacheConfigure(actionDescriptor, method); CollectTransformFilter(actionDescriptor, method); CollectReturnTypeBinder(actionDescriptor, method); CollectActionLevelFiltersIntoActionDescriptor(method, actionDescriptor); if (method.IsDefined(typeof(AjaxActionAttribute), true)) { descriptor.AjaxActions.Add(method); } if (method.IsDefined(typeof(DefaultActionAttribute), true)) { if (descriptor.DefaultAction != null) { throw new MonoRailException( "Cannot resolve a default action for {0}, DefaultActionAttribute was declared more than once.", method.DeclaringType.FullName); } descriptor.DefaultAction = new DefaultActionAttribute(method.Name); } if (AfterActionProcess != null) { AfterActionProcess(actionDescriptor); } }
/// <summary> /// The following lines were added to handle _default processing /// if present look for and load _default action method /// <seealso cref="DefaultActionAttribute"/> /// </summary> private IExecutableAction ResolveDefaultMethod(ControllerMetaDescriptor controllerDesc, IController controller, IControllerContext context, ActionType actionType) { if (controllerDesc.DefaultAction != null) { var method = SelectActionMethod( controller, context, controllerDesc.DefaultAction.DefaultAction, actionType); if (method != null) { var actionDesc = controllerDesc.GetAction(method); return(new ActionMethodExecutor(method, actionDesc ?? new ActionMetaDescriptor())); } } return(null); }
public void Request_CreatesSessionlessHandler_Async() { var writer = new StringWriter(); var res = new HttpResponse(writer); var req = new HttpRequest(Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Handlers/Files/simplerequest.txt"), "http://localhost:1333/home/something", ""); var routeMatch = new RouteMatch(); var httpCtx = new HttpContext(req, res); httpCtx.Items[RouteMatch.RouteMatchKey] = routeMatch; using (mockRepository.Record()) { var controllerDesc = new ControllerMetaDescriptor { ControllerDescriptor = new ControllerDescriptor(typeof(Controller), "home", "", true) }; Expect.Call(controllerFactoryMock.CreateController("", "home")).IgnoreArguments().Return(controllerMock); Expect.Call(controllerDescriptorProviderMock.BuildDescriptor(controllerMock)).Return(controllerDesc); var controllerContext = new ControllerContext { Action = "something", ControllerDescriptor = new ControllerMetaDescriptor() }; controllerContext.ControllerDescriptor.Actions["something"] = new AsyncActionPair(null, null, null); Expect.Call(controllerContextFactoryMock.Create("", "home", "something", controllerDesc, routeMatch)). Return(controllerContext); } using (mockRepository.Playback()) { var handler = handlerFactory.GetHandler(httpCtx, "GET", "", ""); Assert.IsNotNull(handler); Assert.IsInstanceOf(typeof(AsyncSessionlessMonoRailHttpHandler), handler); } }
/// <summary> /// Pendent /// </summary> /// <param name="area">The area.</param> /// <param name="controller">The controller.</param> /// <param name="action">The action.</param> /// <param name="metaDescriptor">The meta descriptor.</param> /// <param name="match">The routing match.</param> /// <returns></returns> public IControllerContext Create(string area, string controller, string action, ControllerMetaDescriptor metaDescriptor, RouteMatch match) { ControllerContext context = new ControllerContext(controller, area, action, metaDescriptor); context.RouteMatch = match; context.ViewFolder = ResolveViewFolder(context, area, controller, action); context.SelectedViewName = ResolveDefaultViewSelection(context, area, controller, action); foreach(KeyValuePair<string, string> pair in match.Parameters) { if (pair.Value == null || pair.Key == "controller" || pair.Key == "action" || pair.Key == "area") { // We skip those only to avoid compatibility issues as // customactionparameters have higher precedence on parameters matching continue; } context.CustomActionParameters[pair.Key] = pair.Value; } return context; }
/// <summary> /// Builds the <see cref="ControllerMetaDescriptor"/>. /// </summary> /// <param name="controller">Controller.</param> /// <returns></returns> public ControllerMetaDescriptor BuildDescriptor(Controller controller) { Type controllerType = controller.GetType(); ControllerMetaDescriptor desc = null; locker.AcquireReaderLock(-1); try { desc = (ControllerMetaDescriptor) descriptorRepository[controllerType]; if (desc != null) { return(desc); } LockCookie lc = locker.UpgradeToWriterLock(-1); try { desc = BuildDescriptor(controllerType); descriptorRepository[controllerType] = desc; } finally { locker.DowngradeFromWriterLock(ref lc); } } finally { locker.ReleaseReaderLock(); } return(desc); }
private ControllerMetaDescriptor BuildDescriptor(Type controllerType) { ControllerMetaDescriptor descriptor = new ControllerMetaDescriptor(controllerType); CollectClassLevelAttributes(controllerType, descriptor); foreach (object action in descriptor.Actions.Values) { if (action is IList) { foreach (MethodInfo overloadedAction in (action as IList)) { CollectActionAttributes(overloadedAction, descriptor); } continue; } CollectActionAttributes(action as MethodInfo, descriptor); } return(descriptor); }
private void CollectActionAttributes(MethodInfo method, ControllerMetaDescriptor descriptor) { object[] attributes = method.GetCustomAttributes(typeof(ResourceAttribute), true); foreach (ResourceAttribute resource in attributes) { descriptor.GetAction(method).Resources.Add(resource); } attributes = method.GetCustomAttributes(typeof(SkipFilterAttribute), true); foreach (SkipFilterAttribute resource in attributes) { descriptor.GetAction(method).SkipFilters.Add(resource); } attributes = method.GetCustomAttributes(typeof(RescueAttribute), true); if (attributes.Length != 0) { descriptor.GetAction(method).Rescues = attributes; } attributes = method.GetCustomAttributes(typeof(SkipRescueAttribute), true); if (attributes.Length != 0) { descriptor.GetAction(method).SkipRescue = (SkipRescueAttribute)attributes[0]; } attributes = method.GetCustomAttributes(typeof(LayoutAttribute), true); if (attributes.Length != 0) { descriptor.GetAction(method).Layout = (LayoutAttribute)attributes[0]; } }
/// <summary> /// Collects the helpers. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectHelpers(ControllerMetaDescriptor descriptor, Type controllerType) { descriptor.Helpers = helperDescriptorProvider.CollectHelpers(controllerType); }
/// <summary> /// Collects the filters. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectFilters(ControllerMetaDescriptor descriptor, Type controllerType) { descriptor.Filters = filterDescriptorProvider.CollectFilters(controllerType); Array.Sort(descriptor.Filters, FilterDescriptorComparer.Instance); }
/// <summary> /// Collects the helpers. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectHelpers(ControllerMetaDescriptor descriptor, Type controllerType) { descriptor.Helpers = helperDescriptorProvider.CollectHelpers(controllerType); }
/// <summary> /// Collects the dynamic action providers. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectDynamicActionProviders(ControllerMetaDescriptor descriptor, Type controllerType) { descriptor.DynamicActionProviders = dynamicActionProviderDescriptorProvider.CollectProviders(controllerType); }
/// <summary> /// Collects the scaffolding. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectScaffolding(ControllerMetaDescriptor descriptor, Type controllerType) { var attributes = controllerType.GetCustomAttributes(typeof(ScaffoldingAttribute), false); if (attributes.Length != 0) { foreach(ScaffoldingAttribute scaffolding in attributes) { descriptor.Scaffoldings.Add(scaffolding); } } }
/// <summary> /// Collects the default action. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectDefaultAction(ControllerMetaDescriptor descriptor, Type controllerType) { var attributes = controllerType.GetCustomAttributes(typeof(DefaultActionAttribute), true); if (attributes.Length != 0) { descriptor.DefaultAction = (DefaultActionAttribute) attributes[0]; } }
/// <summary> /// Collects the class level attributes. /// </summary> /// <param name="controllerType">Type of the controller.</param> /// <param name="descriptor">The descriptor.</param> private void CollectClassLevelAttributes(Type controllerType, ControllerMetaDescriptor descriptor) { CollectHelpers(descriptor, controllerType); CollectResources(descriptor, controllerType); CollectFilters(descriptor, controllerType); CollectLayout(descriptor, controllerType); CollectRescues(descriptor, controllerType); CollectDefaultAction(descriptor, controllerType); CollectScaffolding(descriptor, controllerType); CollectDynamicActionProviders(descriptor, controllerType); CollectCacheConfigure(descriptor, controllerType); }
private void MergeAsyncMethodPairsToSingleAction(ControllerMetaDescriptor desc) { foreach (string name in new ArrayList(desc.Actions.Keys)) { // skip methods that are not named "BeginXyz" if (!name.StartsWith("Begin", StringComparison.InvariantCultureIgnoreCase)) { continue; } var actionName = name.Substring("Begin".Length); var list = desc.Actions[name] as ArrayList; if (list != null) { foreach (MethodInfo info in list) { if (info.ReturnType == typeof(IAsyncResult)) { throw new MonoRailException("Action '" + actionName + "' on controller '" + desc.ControllerDescriptor.Name + "' is an async action, but there are method overloads '" + name + "(...)', which is not allowed on async actions."); } } continue; } if (desc.Actions.Contains(actionName)) { throw new MonoRailException("Found both async method '" + name + "' and sync method '" + actionName + "' on controller '" + desc.ControllerDescriptor.Name + "'. MonoRail doesn't support mixing sync and async methods for the same action"); } var beginActionInfo = (MethodInfo)desc.Actions[name]; // we allow BeginXyz method as sync methods, as long as they do not return // IAsyncResult if (beginActionInfo.ReturnType != typeof(IAsyncResult)) { continue; } var endActionName = "End" + actionName; if (desc.Actions.Contains(endActionName) == false) { throw new MonoRailException("Found beginning of async pair '" + name + "' but not the end '" + endActionName + "' on controller '" + desc.ControllerDescriptor.Name + "', did you forget to define " + endActionName + "(IAsyncResult ar) ?"); } if (desc.Actions[endActionName] is IList) { throw new MonoRailException("Found more than a single " + endActionName + " method, for async action '" + actionName + "' on controller '" + desc.ControllerDescriptor.Name + "', only a single " + endActionName + " may be defined as part of an async action"); } var endActionInfo = (MethodInfo)desc.Actions[endActionName]; desc.Actions.Remove(name); desc.Actions.Remove(endActionName); desc.Actions[actionName] = new AsyncActionPair(actionName, beginActionInfo, endActionInfo); } }
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { IController controller; PerformOneTimeInitializationIfNecessary(context); EnsureServices(); HttpRequest request = context.Request; RouteMatch routeMatch = (( RouteMatch )context.Items[RouteMatch.RouteMatchKey]) ?? new RouteMatch(); UrlInfo urlInfo = UrlTokenizer.TokenizeUrl( request.FilePath, request.PathInfo, request.Url, request.IsLocal, request.ApplicationPath); if (urlInfo.Area.Equals("MonoRail", StringComparison.CurrentCultureIgnoreCase) && urlInfo.Controller.Equals("Files", StringComparison.CurrentCultureIgnoreCase)) { return(new ResourceFileHandler(urlInfo, new DefaultStaticResourceRegistry())); } if (urlInfo.Area.Equals("MonoRail", StringComparison.CurrentCultureIgnoreCase) && urlInfo.Controller.Equals("Resources", StringComparison.CurrentCultureIgnoreCase)) { return(new ResourceFileHandlerEx(urlInfo, new DefaultStaticResourceRegistryEx())); } IEngineContext serviceInstance = _EngineContextFactory.Create( _MonoRailContainer, urlInfo, context, routeMatch); serviceInstance.AddService(typeof(IEngineContext), serviceInstance); try { controller = _ControllerFactory.CreateController(urlInfo.Area, urlInfo.Controller); } catch (ControllerNotFoundException) { return(new MonoRailHttpHandlerFactory.NotFoundHandler( urlInfo.Area, urlInfo.Controller, serviceInstance)); } ControllerMetaDescriptor metaDescriptor = _MonoRailContainer.ControllerDescriptorProvider.BuildDescriptor(controller); IControllerContext context3 = _ControllerContextFactory.Create( urlInfo.Area, urlInfo.Controller, urlInfo.Action, metaDescriptor, routeMatch); serviceInstance.CurrentController = controller; serviceInstance.CurrentControllerContext = context3; context.Items[CurrentEngineContextKey] = serviceInstance; context.Items[CurrentControllerKey] = controller; context.Items[CurrentControllerContextKey] = context3; if (IgnoresSession(metaDescriptor.ControllerDescriptor)) { return(new SessionlessMonoRailHttpHandler(serviceInstance, controller, context3)); } return(new MonoRailHttpHandler(serviceInstance, controller, context3)); }
/// <summary> /// Initializes a new instance of the <see cref="ControllerContext"/> class. /// </summary> /// <param name="name">The controller name.</param> /// <param name="action">The action name.</param> /// <param name="metaDescriptor">The meta descriptor.</param> public ControllerContext(string name, string action, ControllerMetaDescriptor metaDescriptor) : this(name, string.Empty, action, metaDescriptor) { }
private void CollectClassLevelAttributes(Type controllerType, ControllerMetaDescriptor descriptor) { object[] attributes = controllerType.GetCustomAttributes(typeof(DefaultActionAttribute), true); if (attributes.Length != 0) { descriptor.DefaultAction = (DefaultActionAttribute)attributes[0]; } attributes = controllerType.GetCustomAttributes(typeof(HelperAttribute), true); if (attributes.Length != 0) { foreach (HelperAttribute helper in attributes) { descriptor.Helpers.Add(helper); } } attributes = controllerType.GetCustomAttributes(typeof(ResourceAttribute), true); if (attributes.Length != 0) { foreach (ResourceAttribute resource in attributes) { descriptor.Resources.Add(resource); } } attributes = controllerType.GetCustomAttributes(typeof(FilterAttribute), true); if (attributes.Length != 0) { foreach (FilterAttribute filter in attributes) { descriptor.Filters.Add(filter); } } attributes = controllerType.GetCustomAttributes(typeof(LayoutAttribute), true); if (attributes.Length != 0) { descriptor.Layout = (LayoutAttribute)attributes[0]; } attributes = controllerType.GetCustomAttributes(typeof(RescueAttribute), true); if (attributes.Length != 0) { descriptor.Rescues = attributes; } attributes = controllerType.GetCustomAttributes(typeof(ScaffoldingAttribute), false); if (attributes.Length != 0) { foreach (ScaffoldingAttribute scaffolding in attributes) { descriptor.Scaffoldings.Add(scaffolding); } } attributes = controllerType.GetCustomAttributes(typeof(DynamicActionProviderAttribute), true); if (attributes.Length != 0) { foreach (DynamicActionProviderAttribute actionProvider in attributes) { descriptor.ActionProviders.Add(actionProvider); } } }
/// <summary> /// Collects the dynamic action providers. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectDynamicActionProviders(ControllerMetaDescriptor descriptor, Type controllerType) { descriptor.DynamicActionProviders = dynamicActionProviderDescriptorProvider.CollectProviders(controllerType); }
/// <summary> /// Collects the filters. /// </summary> /// <param name="descriptor">The descriptor.</param> /// <param name="controllerType">Type of the controller.</param> private void CollectFilters(ControllerMetaDescriptor descriptor, Type controllerType) { descriptor.Filters = filterDescriptorProvider.CollectFilters(controllerType); Array.Sort(descriptor.Filters, FilterDescriptorComparer.Instance); }
/// <summary> /// Pendent /// </summary> /// <param name="area"></param> /// <param name="controller"></param> /// <param name="action"></param> /// <param name="metaDescriptor"></param> /// <returns></returns> public IControllerContext Create(string area, string controller, string action, ControllerMetaDescriptor metaDescriptor) { return Create(area, controller, action, metaDescriptor, new RouteMatch()); }
private void MergeAsyncMethodPairsToSingleAction(ControllerMetaDescriptor desc) { foreach(string name in new ArrayList(desc.Actions.Keys)) { // skip methods that are not named "BeginXyz" if (!name.StartsWith("Begin", StringComparison.InvariantCultureIgnoreCase)) { continue; } var actionName = name.Substring("Begin".Length); var list = desc.Actions[name] as ArrayList; if (list != null) { foreach(MethodInfo info in list) { if (info.ReturnType == typeof(IAsyncResult)) { throw new MonoRailException("Action '" + actionName + "' on controller '" + desc.ControllerDescriptor.Name + "' is an async action, but there are method overloads '" + name + "(...)', which is not allowed on async actions."); } } continue; } if (desc.Actions.Contains(actionName)) { throw new MonoRailException("Found both async method '" + name + "' and sync method '" + actionName + "' on controller '" + desc.ControllerDescriptor.Name + "'. MonoRail doesn't support mixing sync and async methods for the same action"); } var beginActionInfo = (MethodInfo) desc.Actions[name]; // we allow BeginXyz method as sync methods, as long as they do not return // IAsyncResult if(beginActionInfo.ReturnType != typeof(IAsyncResult)) continue; var endActionName = "End" + actionName; if (desc.Actions.Contains(endActionName) == false) { throw new MonoRailException("Found beginning of async pair '" + name + "' but not the end '" + endActionName + "' on controller '" + desc.ControllerDescriptor.Name + "', did you forget to define " + endActionName + "(IAsyncResult ar) ?"); } if (desc.Actions[endActionName] is IList) { throw new MonoRailException("Found more than a single " + endActionName + " method, for async action '" + actionName + "' on controller '" + desc.ControllerDescriptor.Name + "', only a single " + endActionName + " may be defined as part of an async action"); } var endActionInfo = (MethodInfo) desc.Actions[endActionName]; desc.Actions.Remove(name); desc.Actions.Remove(endActionName); desc.Actions[actionName] = new AsyncActionPair(actionName, beginActionInfo, endActionInfo); } }
/// <summary> /// Initializes a new instance of the <see cref="ControllerContext"/> class. /// </summary> /// <param name="name">The controller name.</param> /// <param name="action">The action name.</param> /// <param name="metaDescriptor">The meta descriptor.</param> public ControllerContext(string name, string action, ControllerMetaDescriptor metaDescriptor) : this(name, string.Empty, action, metaDescriptor) { }
/// <summary> /// Collects the action level attributes. /// </summary> /// <param name="descriptor">The descriptor.</param> private void CollectActionLevelAttributes(ControllerMetaDescriptor descriptor) { foreach(var action in descriptor.Actions.Values) { if (action is IList) { foreach(MethodInfo overloadedAction in (action as IList)) { CollectActionAttributes(overloadedAction, descriptor); } continue; } var methodInfo = action as MethodInfo; if (methodInfo != null) { CollectActionAttributes(methodInfo, descriptor); } else { var asyncActionPair = (AsyncActionPair) action; CollectActionAttributes(asyncActionPair.BeginActionInfo, descriptor); CollectActionAttributes(asyncActionPair.EndActionInfo, descriptor); } } }
/// <summary> /// The following lines were added to handle _default processing /// if present look for and load _default action method /// <seealso cref="DefaultActionAttribute"/> /// </summary> private IExecutableAction ResolveDefaultMethod(ControllerMetaDescriptor controllerDesc, IController controller, IControllerContext context, ActionType actionType) { if (controllerDesc.DefaultAction != null) { MethodInfo method = SelectActionMethod( controller, context, controllerDesc.DefaultAction.DefaultAction, actionType); if (method != null) { ActionMetaDescriptor actionDesc = controllerDesc.GetAction(method); return new ActionMethodExecutor(method, actionDesc ?? new ActionMetaDescriptor()); } } return null; }
/// <summary> /// Collects the action attributes. /// </summary> /// <param name="method">The method.</param> /// <param name="descriptor">The descriptor.</param> private void CollectActionAttributes(MethodInfo method, ControllerMetaDescriptor descriptor) { if (logger.IsDebugEnabled) { logger.DebugFormat("Collection attributes for action {0}", method.Name); } var actionDescriptor = descriptor.GetAction(method); if (actionDescriptor == null) { actionDescriptor = CreateActionDescriptor(); descriptor.AddAction(method, actionDescriptor); } CollectResources(actionDescriptor, method); CollectSkipFilter(actionDescriptor, method); CollectRescues(actionDescriptor, method); CollectAccessibleThrough(actionDescriptor, method); CollectSkipRescue(actionDescriptor, method); CollectLayout(actionDescriptor, method); CollectCacheConfigure(actionDescriptor, method); CollectTransformFilter(actionDescriptor, method); CollectReturnTypeBinder(actionDescriptor, method); CollectActionLevelFiltersIntoActionDescriptor(method, actionDescriptor); if (method.IsDefined(typeof(AjaxActionAttribute), true)) { descriptor.AjaxActions.Add(method); } if (method.IsDefined(typeof(DefaultActionAttribute), true)) { if (descriptor.DefaultAction != null) { throw new MonoRailException( "Cannot resolve a default action for {0}, DefaultActionAttribute was declared more than once.", method.DeclaringType.FullName); } descriptor.DefaultAction = new DefaultActionAttribute(method.Name); } if (AfterActionProcess != null) { AfterActionProcess(actionDescriptor); } }
/// <summary> /// Collects the actions. /// </summary> /// <param name="controllerType">Type of the controller.</param> /// <param name="desc">The desc.</param> private void CollectActions(Type controllerType, ControllerMetaDescriptor desc) { // HACK: GetRealControllerType is a workaround for DYNPROXY-14 bug // see: http://support.castleproject.org/browse/DYNPROXY-14 controllerType = GetRealControllerType(controllerType); var methods = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach(var method in methods) { var declaringType = method.DeclaringType; if (method.IsSpecialName) { continue; } if (declaringType == typeof(Object) || declaringType == typeof(IController) || declaringType == typeof(Controller)) // || declaringType == typeof(SmartDispatcherController)) { continue; } if (desc.Actions.Contains(method.Name)) { var list = desc.Actions[method.Name] as ArrayList; if (list == null) { list = new ArrayList { desc.Actions[method.Name] }; desc.Actions[method.Name] = list; } list.Add(method); } else { desc.Actions[method.Name] = method; } } MergeAsyncMethodPairsToSingleAction(desc); }
/// <summary> /// Pendent /// </summary> /// <param name="area"></param> /// <param name="controller"></param> /// <param name="action"></param> /// <param name="metaDescriptor"></param> /// <returns></returns> public IControllerContext Create(string area, string controller, string action, ControllerMetaDescriptor metaDescriptor) { return(Create(area, controller, action, metaDescriptor, new RouteMatch())); }