private string RenderAsJson(IRebelRenderModel model)
        {
            var typedEntity = (CustomTypedEntity <Content>)model.CurrentNode;
            var mapper      = new SimpleFlattenedTypedEntityMapper(
                _context.Hive, _urlHelper);

            var flattenedObject = mapper.Flatten(model.CurrentNode.NiceUrl(), typedEntity);

            return(flattenedObject.ToJsonString());
        }
        private string GetViewHtml(IRebelRenderModel model)
        {
            using (IReadonlyGroupUnit <IFileStore> uow =
                       _context.Hive.OpenReader <IFileStore>(new Uri("storage://templates")))
            {
                File templateFile = model.CurrentNode.CurrentTemplate != null
                                        ? uow.Repositories.Get <File>(model.CurrentNode.CurrentTemplate.Id)
                                        : null;

                if (templateFile != null)
                {
                    return(RenderRazorViewToString(templateFile, model.CurrentNode));
                }
            }

            return(String.Empty);
        }
Esempio n. 3
0
        //[EnableCompression]
        public virtual ActionResult Index(IRebelRenderModel model)
        {
            if (model.CurrentNode == null)
            {
                return(new HttpNotFoundResult());
            }

            var contentCacheManager = new ContentCacheManager(RoutableRequestContext.Application, this);
            var content             = contentCacheManager.RenderContent(model);

            if (string.IsNullOrEmpty(content.Key))
            {
                return(View(
                           EmbeddedViewPath.Create("Rebel.Cms.Web.EmbeddedViews.Views.TemplateNotFound.cshtml,Rebel.Cms.Web"),
                           model.CurrentNode));
            }

            return(Content(content.Value, content.Key));
        }
Esempio n. 4
0
        public KeyValuePair<string, string> RenderContent(IRebelRenderModel model)
        {
            bool isPreview;
            Boolean.TryParse(_requestContext.QueryString[ContentEditorModel.PreviewQuerystringKey], out isPreview);

            if (IsJsonRequest())
            {
                var jsonResult = isPreview
                                     ? RenderAsJson(model)
                                     : Cache(model.CurrentNode.NiceUrl() + "/json", () => RenderAsJson(model));

                return new KeyValuePair<string, string>("application/json",jsonResult);
            }

            var htmlResult = GetViewHtml(model);

            if (string.IsNullOrEmpty(htmlResult))
                return new KeyValuePair<string, string>(string.Empty, string.Empty);

            return new KeyValuePair<string, string>("text/html", htmlResult);
        }
        public KeyValuePair <string, string> RenderContent(IRebelRenderModel model)
        {
            bool isPreview;

            Boolean.TryParse(_requestContext.QueryString[ContentEditorModel.PreviewQuerystringKey], out isPreview);

            if (IsJsonRequest())
            {
                var jsonResult = isPreview
                                     ? RenderAsJson(model)
                                     : Cache(model.CurrentNode.NiceUrl() + "/json", () => RenderAsJson(model));

                return(new KeyValuePair <string, string>("application/json", jsonResult));
            }

            var htmlResult = GetViewHtml(model);

            if (string.IsNullOrEmpty(htmlResult))
            {
                return(new KeyValuePair <string, string>(string.Empty, string.Empty));
            }

            return(new KeyValuePair <string, string>("text/html", htmlResult));
        }
Esempio n. 6
0
 public ActionResult HomePage(IRebelRenderModel model)
 {
     return(View());
 }
Esempio n. 7
0
        /// <summary>
        /// this will determine the controller and set the values in the route data
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        protected internal IHttpHandler GetHandlerForRoute(RequestContext requestContext, IRebelRenderModel renderModel)
        {
            var routeDef = GetRebelRouteDefinition(requestContext, renderModel);

            //Need to check for a special case if there is form data being posted back to an Rebel URL
            var postedInfo = GetPostedFormInfo(requestContext);
            if (postedInfo != null)
            {
                return HandlePostedValues(requestContext, postedInfo, renderModel, routeDef);    
            }

            //no post values, just route to the controller/action requried (local)

            requestContext.RouteData.Values["controller"] = routeDef.ControllerName;
            if (!routeDef.ActionName.IsNullOrWhiteSpace())
            {
                requestContext.RouteData.Values["action"] = routeDef.ActionName;
            }
            return new MvcHandler(requestContext);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        /// <returns></returns>
        protected virtual RouteDefinition GetRebelRouteDefinition(RequestContext requestContext, IRebelRenderModel renderModel)
        {
            //creates the default route definition which maps to the 'RebelController' controller
            var def = new RouteDefinition
                {
                    ControllerName = RebelController.GetControllerName<RebelController>(), 
                    Controller = new RebelController(),
                    RenderModel = renderModel,
                    ActionName = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString()
                };

            //check that a template is defined)
            if (renderModel.CurrentNode != null && renderModel.CurrentNode.CurrentTemplate != null)
            {
                using (var uow = _applicationContext.Hive.OpenReader<IFileStore>(new Uri("storage://templates")))
                {
                    var template = uow.Repositories.Get<File>(renderModel.CurrentNode.CurrentTemplate.Id);
                    
                    //check if there's a custom controller assigned, base on the document type alias.
                    var controller = _controllerFactory.CreateController(requestContext, renderModel.CurrentNode.ContentType.Alias);

                    //check if that controller exists
                    if (controller != null)
                    {                        

                        //ensure the controller is of type 'RebelController'
                        if (controller is RebelController)
                        {
                            //set the controller and name to the custom one
                            def.Controller = (ControllerBase)controller;
                            def.ControllerName = RebelController.GetControllerName(controller.GetType());    
                        }
                        else
                        {
                            LogHelper.Warn<RenderRouteHandler>("The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Rebel routing must inherit from '{2}'.", renderModel.CurrentNode.ContentType.Alias, controller.GetType().FullName, typeof(RebelController).FullName);
                            //exit as we cannnot route to the custom controller, just route to the standard one.
                            return def;
                        }
                        
                        // Template might be null if none is assigned to the node
                        if (template != null)
                        {
                            //check if the custom controller has an action with the same name as the template name (we convert ToRebelAlias since the template name might have invalid chars).
                            //NOTE: This also means that all custom actions MUST be PascalCase.. but that should be standard.
                            var templateName = template.Name.Split('.')[0].ToRebelAlias(StringAliasCaseType.PascalCase);
                            var action = controller.GetType().GetMethod(templateName);
                            if (action != null)
                            {
                                def.ActionName = templateName;
                            }
                            //otherwise it will continue to route to Index since thats teh default route
                        }
                        
                    }
                }
            }

            return def;
        }
Esempio n. 9
0
        /// <summary>
        /// Handles a posted form to an Rebel Url and ensures the correct controller is routed to and that
        /// the right DataTokens are set.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="postedInfo"></param>
        /// <param name="renderModel"></param>
        /// <param name="routeDefinition">The original route definition that would normally be used to route if it were not a POST</param>
        protected virtual IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo, IRebelRenderModel renderModel, RouteDefinition routeDefinition)
        {
         
            //set the standard route values/tokens
            requestContext.RouteData.Values["controller"] = postedInfo.ControllerName;
            requestContext.RouteData.Values["action"] = postedInfo.ActionName;            
            requestContext.RouteData.DataTokens["area"] = postedInfo.Area;

            IHttpHandler handler = new MvcHandler(requestContext);

            //ensure the surface id is set if found, meaning it is a plugin, not locally declared
            if (postedInfo.SurfaceId != default(Guid))
            {
                requestContext.RouteData.Values["surfaceId"] = postedInfo.SurfaceId.ToString("N");
                //find the other data tokens for this route and merge... things like Namespace will be included here
                using (RouteTable.Routes.GetReadLock())
                {
                    var surfaceRoute = RouteTable.Routes.OfType<Route>()
                        .Where(x => x.Defaults != null && x.Defaults.ContainsKey("surfaceId") &&
                            x.Defaults["surfaceId"].ToString() == postedInfo.SurfaceId.ToString("N"))
                        .SingleOrDefault();
                    if (surfaceRoute == null)
                        throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for id " + postedInfo.SurfaceId);
                    //set the 'Namespaces' token so the controller factory knows where to look to construct it
                    if (surfaceRoute.DataTokens.ContainsKey("Namespaces"))
                    {
                        requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"];    
                    }
                    handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext);
                }
                
            }

            //store the original URL this came in on
            requestContext.RouteData.DataTokens["rebel-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath;
            //store the original route definition
            requestContext.RouteData.DataTokens["rebel-route-def"] = routeDefinition;

            return handler;
        }
Esempio n. 10
0
 public ActionResult HomePage(IRebelRenderModel model)
 {
     return View();
 }
Esempio n. 11
0
        private string RenderAsJson(IRebelRenderModel model)
        {
            var typedEntity = (CustomTypedEntity<Content>)model.CurrentNode;
            var mapper = new SimpleFlattenedTypedEntityMapper(
                    _context.Hive, _urlHelper);

            var flattenedObject = mapper.Flatten(model.CurrentNode.NiceUrl(), typedEntity);
            return flattenedObject.ToJsonString();
        }
Esempio n. 12
0
        private string GetViewHtml(IRebelRenderModel model)
        {
            using (IReadonlyGroupUnit<IFileStore> uow =
                _context.Hive.OpenReader<IFileStore>(new Uri("storage://templates")))
            {
                File templateFile = model.CurrentNode.CurrentTemplate != null
                                        ? uow.Repositories.Get<File>(model.CurrentNode.CurrentTemplate.Id)
                                        : null;

                if (templateFile != null)
                {
                    return RenderRazorViewToString(templateFile, model.CurrentNode);
                }
            }

            return String.Empty;
        }
Esempio n. 13
0
        /// <summary>
        /// this will determine the controller and set the values in the route data
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        protected internal IHttpHandler GetHandlerForRoute(RequestContext requestContext, IRebelRenderModel renderModel)
        {
            var routeDef = GetRebelRouteDefinition(requestContext, renderModel);

            //Need to check for a special case if there is form data being posted back to an Rebel URL
            var postedInfo = GetPostedFormInfo(requestContext);

            if (postedInfo != null)
            {
                return(HandlePostedValues(requestContext, postedInfo, renderModel, routeDef));
            }

            //no post values, just route to the controller/action requried (local)

            requestContext.RouteData.Values["controller"] = routeDef.ControllerName;
            if (!routeDef.ActionName.IsNullOrWhiteSpace())
            {
                requestContext.RouteData.Values["action"] = routeDef.ActionName;
            }
            return(new MvcHandler(requestContext));
        }
Esempio n. 14
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        /// <returns></returns>
        protected virtual RouteDefinition GetRebelRouteDefinition(RequestContext requestContext, IRebelRenderModel renderModel)
        {
            //creates the default route definition which maps to the 'RebelController' controller
            var def = new RouteDefinition
            {
                ControllerName = RebelController.GetControllerName <RebelController>(),
                Controller     = new RebelController(),
                RenderModel    = renderModel,
                ActionName     = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString()
            };

            //check that a template is defined)
            if (renderModel.CurrentNode != null && renderModel.CurrentNode.CurrentTemplate != null)
            {
                using (var uow = _applicationContext.Hive.OpenReader <IFileStore>(new Uri("storage://templates")))
                {
                    var template = uow.Repositories.Get <File>(renderModel.CurrentNode.CurrentTemplate.Id);

                    //check if there's a custom controller assigned, base on the document type alias.
                    var controller = _controllerFactory.CreateController(requestContext, renderModel.CurrentNode.ContentType.Alias);

                    //check if that controller exists
                    if (controller != null)
                    {
                        //ensure the controller is of type 'RebelController'
                        if (controller is RebelController)
                        {
                            //set the controller and name to the custom one
                            def.Controller     = (ControllerBase)controller;
                            def.ControllerName = RebelController.GetControllerName(controller.GetType());
                        }
                        else
                        {
                            LogHelper.Warn <RenderRouteHandler>("The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Rebel routing must inherit from '{2}'.", renderModel.CurrentNode.ContentType.Alias, controller.GetType().FullName, typeof(RebelController).FullName);
                            //exit as we cannnot route to the custom controller, just route to the standard one.
                            return(def);
                        }

                        // Template might be null if none is assigned to the node
                        if (template != null)
                        {
                            //check if the custom controller has an action with the same name as the template name (we convert ToRebelAlias since the template name might have invalid chars).
                            //NOTE: This also means that all custom actions MUST be PascalCase.. but that should be standard.
                            var templateName = template.Name.Split('.')[0].ToRebelAlias(StringAliasCaseType.PascalCase);
                            var action       = controller.GetType().GetMethod(templateName);
                            if (action != null)
                            {
                                def.ActionName = templateName;
                            }
                            //otherwise it will continue to route to Index since thats teh default route
                        }
                    }
                }
            }

            return(def);
        }
Esempio n. 15
0
        /// <summary>
        /// Handles a posted form to an Rebel Url and ensures the correct controller is routed to and that
        /// the right DataTokens are set.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="postedInfo"></param>
        /// <param name="renderModel"></param>
        /// <param name="routeDefinition">The original route definition that would normally be used to route if it were not a POST</param>
        protected virtual IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo, IRebelRenderModel renderModel, RouteDefinition routeDefinition)
        {
            //set the standard route values/tokens
            requestContext.RouteData.Values["controller"] = postedInfo.ControllerName;
            requestContext.RouteData.Values["action"]     = postedInfo.ActionName;
            requestContext.RouteData.DataTokens["area"]   = postedInfo.Area;

            IHttpHandler handler = new MvcHandler(requestContext);

            //ensure the surface id is set if found, meaning it is a plugin, not locally declared
            if (postedInfo.SurfaceId != default(Guid))
            {
                requestContext.RouteData.Values["surfaceId"] = postedInfo.SurfaceId.ToString("N");
                //find the other data tokens for this route and merge... things like Namespace will be included here
                using (RouteTable.Routes.GetReadLock())
                {
                    var surfaceRoute = RouteTable.Routes.OfType <Route>()
                                       .Where(x => x.Defaults != null && x.Defaults.ContainsKey("surfaceId") &&
                                              x.Defaults["surfaceId"].ToString() == postedInfo.SurfaceId.ToString("N"))
                                       .SingleOrDefault();
                    if (surfaceRoute == null)
                    {
                        throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for id " + postedInfo.SurfaceId);
                    }
                    //set the 'Namespaces' token so the controller factory knows where to look to construct it
                    if (surfaceRoute.DataTokens.ContainsKey("Namespaces"))
                    {
                        requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"];
                    }
                    handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext);
                }
            }

            //store the original URL this came in on
            requestContext.RouteData.DataTokens["rebel-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath;
            //store the original route definition
            requestContext.RouteData.DataTokens["rebel-route-def"] = routeDefinition;

            return(handler);
        }