Exemplo n.º 1
0
        /// <inheritdoc />
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="browser" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="response" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="result" /> parameter is <c>null</c>.
        /// </exception>
        public void Initialize(IBrowser browser, HttpResponseMessage response, HttpResult result)
        {
            if (browser == null)
            {
                throw new ArgumentNullException("browser");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            // Look at the content type header to determine the correct type of page to return
            var mediaType = DetermineMediaType(response);

            var contentType = browser.ContentTypeResolver.DeterminePageType(mediaType);

            if (contentType == PageContentType.Html)
            {
                _resolvedPage = new DynamicHtmlPage();
            }
            else if (contentType == PageContentType.Binary)
            {
                _resolvedPage = new BinaryPageWrapper();
            }
            else
            {
                _resolvedPage = new TextPageWrapper();
            }

            ResolvedPage.Initialize(browser, response, result);
        }
Exemplo n.º 2
0
 /// <inheritdoc />
 public bool IsOn(Uri location)
 {
     return(ResolvedPage.IsOn(location));
 }
        private static ResolvedPage ResolvePage(
            ref Dictionary <string, AgilityRouteCacheItem> routes,
            AgilityRouteCacheItem routeItem,
            string dynamicRoute,
            string languageCode,
            string websiteName,
            ref List <ResolvedPage> resolvedPages,
            string url)
        {
            AgilityPage page = BaseCache.GetPageFromID(routeItem.PageID, languageCode, websiteName, url);



            if (string.IsNullOrEmpty(dynamicRoute))
            {
                //make sure we have the page, if not, then kick out
                if (page == null)
                {
                    return(null);
                }

                if (!string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName) ||
                    !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //we have NO dynamic route, but the page is dynamic, kick out
                    return(null);
                }

                //not a dynamic page (a static page nested under a dynamic one...)
                return(new ResolvedPage(page, null));
            }
            else
            {
                //resolve the Dynamic Route

                if (string.IsNullOrEmpty(routeItem.ChildDynamicPagePath))
                {
                    //no dynamic page attached to this static page.. kick out
                    return(null);
                }

                //get the route item for the child dynamic page
                if (!routes.TryGetValue(routeItem.ChildDynamicPagePath, out routeItem))
                {
                    return(null);
                }



                //get the dynamic page
                page = BaseCache.GetPageFromID(routeItem.PageID, languageCode, websiteName, url);

                if (page == null)
                {
                    Agility.Web.Tracing.WebTrace.WriteWarningLine(string.Format("Routing: could not load page with id: {0} in language {1} websitename {2} url {3}", routeItem.PageID, languageCode, websiteName, url));
                    return(null);
                }


                if (string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName) &&
                    string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //we have a dynamic route, but the page is not dynamic, kick out
                    return(null);
                }

                string contentReferenceName = page.DynamicPageContentViewReferenceName;
                int    parentContentID      = -1;
                if (string.IsNullOrEmpty(contentReferenceName))
                {
                    if (!string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                    {
                        for (int i = resolvedPages.Count - 1; i >= 0; i--)
                        {
                            DynamicPageFormulaItem dpParent = resolvedPages[i].DynamicPageItem;
                            if (dpParent == null)
                            {
                                continue;
                            }

                            //get the content reference name from the parent page...
                            object fieldValueObj = dpParent.GetContentItemValue(page.DynamicPageParentFieldName);
                            if (fieldValueObj != null)
                            {
                                contentReferenceName = fieldValueObj as string;
                                break;
                            }
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //filter the dynamic page list by the parent field...
                    for (int i = resolvedPages.Count - 1; i >= 0; i--)
                    {
                        DynamicPageFormulaItem dpParent = resolvedPages[i].DynamicPageItem;
                        if (dpParent == null)
                        {
                            continue;
                        }
                        if (dpParent != null)
                        {
                            parentContentID = dpParent.ContentID;
                            break;
                        }
                    }
                    if (parentContentID < 1)
                    {
                        return(null);
                    }
                }

                //if we didn't resolve a content view, kick out...
                if (string.IsNullOrEmpty(contentReferenceName))
                {
                    return(null);
                }

                //create/update the dynamic page index with this page id and dynamic page list reference name
                BaseCache.UpdateDynamicPageIndex(page.ID, contentReferenceName);

                //get the content first if we are in development or staging mode...
                if (AgilityContext.CurrentMode == Enum.Mode.Staging || Agility.Web.Configuration.Current.Settings.DevelopmentMode)
                {
                    AgilityContentServer.AgilityContent content = BaseCache.GetContent(contentReferenceName, page.LanguageCode, AgilityContext.WebsiteName);
                }

                //get the dynamic page list dictionary for this page (this method also updates if neccessary)...
                DynamicPageFormulaItemIndex dpIndex = BaseCache.GetDynamicPageFormulaIndex(page.ID, contentReferenceName, page.LanguageCode, page, true);

                DynamicPageFormulaItem dpItem = null;
                if (dpIndex == null || !dpIndex.TryGetValue(dynamicRoute, out dpItem))
                {
                    //check if friendly-making the route would result in a match
                    string friendly = DynamicPageFormulaItem.MakePathFriendly(dynamicRoute);
                    if (friendly == dynamicRoute || !dpIndex.TryGetValue(friendly, out dpItem))
                    {
                        //we didn't find the item, and making the given path friendly didn't work either. Kick out.
                        return(null);
                    }

                    //there's an item, but only after fixing the path. redirect.
                    string baseUrl     = Data.GetSiteMapNode(page.ID).PagePath;
                    string redirectUrl = baseUrl.LastIndexOf('/') > 0
                                                                                   ? baseUrl.Substring(0, baseUrl.LastIndexOf('/')) + friendly
                                                                                   : friendly;
                    if (url.ToLower().EndsWith(".aspx"))
                    {
                        redirectUrl += ".aspx";
                    }

                    //add the current query string..
                    string query = AgilityContext.HttpContext.Request.QueryString.Value;
                    if (!string.IsNullOrEmpty(query))
                    {
                        redirectUrl += query;
                    }

                    //set the url to get this page, and the ActionResult or HttpModule will 301 redirect to it.
                    ResolvedPage resolvedPage = new ResolvedPage(page, dpItem);
                    resolvedPage.RedirectURL = redirectUrl;
                    return(resolvedPage);
                }

                //check parent content if neccessary
                if (parentContentID > 0 &&
                    !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    object testParentContentIDObj = dpItem.GetContentItemValue(page.DynamicPageParentFieldName);

                    if (testParentContentIDObj != null)
                    {
                        int    testParentContentID    = -1;
                        string testParentContentIDStr = string.Format("{0}", testParentContentIDObj);

                        if (int.TryParse(testParentContentIDStr, out testParentContentID))
                        {
                            //if the value is an int, test for equality...
                            if (parentContentID != testParentContentID)
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            //value is NOT an int, test for "in" '[id],' or ',[id],' or ',[id]'
                            if (!testParentContentIDStr.StartsWith(string.Format("{0},", parentContentID)) &&
                                !testParentContentIDStr.EndsWith(string.Format(",{0}", parentContentID)) &&
                                !testParentContentIDStr.Contains(string.Format(",{0},", parentContentID)))
                            {
                                return(null);
                            }
                        }
                    }
                }

                return(new ResolvedPage(page, dpItem));
            }
        }
        internal static List <ResolvedPage> ResolveRoutePath(string path, string languageCode)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }



            path = path.ToLowerInvariant();

            List <ResolvedPage> lstResolvedPages = new List <ResolvedPage>();

            AgilityRouteCacheItem routeItem = null;

            string websiteName = AgilityContext.WebsiteName;
            Dictionary <string, AgilityRouteCacheItem> routes = AgilityRouteTable.GetRouteTable(languageCode, AgilityContext.CurrentChannel.ID);

            #region *** STATIC PAGE ***

            //STATIC PAGE

            if (routes.TryGetEncoded(path, out routeItem))
            {
                var appRelativeCurrentExecutionFilePath = "/";

                //try to resolve the entire path first... if that works, we can kick out right away...
                AgilityPage page = BaseCache.GetPageFromID(routeItem.PageID, languageCode, websiteName, appRelativeCurrentExecutionFilePath);
                if (page == null)
                {
                    return(null);
                }
                if (!string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName) ||
                    !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //we are on a static page, but we don't have the dynamic part of the URL in place... s

                    if (Agility.Web.HttpModules.AgilityHttpModule.CheckPreviewMode(AgilityContext.HttpContext))
                    {
                        AgilityContext.IsResponseEnded = true;
                    }
                    else                     //if (AgilityContext.IsPreview || Configuration.Current.Settings.DevelopmentMode)
                    {
                        //add in the dynamic part of the URL if we are in preview mode...
                        RedirectToDynamicPreviewUrl(routes, path, languageCode);
                    }

                    return(null);
                }

                lstResolvedPages.Add(new ResolvedPage(page));
                return(lstResolvedPages);
            }
            #endregion

            //DYNAMIC PAGE
            //if we can't find the path, start peeling off parts of the path left to right and get the
            //static paths combined with the dynamic paths

            string[] pathParts = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            //if there are less than 2 parts to the path, it CAN'T be a dynamic path...
            if (pathParts.Length < 2)
            {
                return(null);
            }

            string staticRoute  = "";
            string dynamicRoute = string.Empty;
            AgilityRouteCacheItem testRouteItem = null;
            routeItem = null;

            ResolvedPage parentPage = null;


            //Assume we are a WEB SITE (not a virtual directory)
            string url = "/";

            //loop the rest of the path...
            bool foundRoute        = false;  // signal that we found a route, we can return safely
            bool foundDynamicRoute = false;  //signal that we found a dynamic route

            var pathIndex = -1;

            foreach (string pathPart in pathParts)
            {
                pathIndex++;
                foundRoute = false;             // init to not found for every iteration

                string testStaticRoute = string.Format("{0}/{1}", staticRoute, pathPart);

                //check for a secondary static route (eg: /Static1/Dyn1/Dyn2/Static2
                if (routes.TryGetValue(testStaticRoute, out testRouteItem))
                {
                    foundRoute = true;                 // static route means we found a route

                    //found a static route, add it to the list of resolved pages...
                    AgilityPage page = BaseCache.GetPageFromID(testRouteItem.PageID, languageCode, websiteName, null);
                    if (page == null)
                    {
                        //assume a null page means a folder...
                        lstResolvedPages.Add(new ResolvedPage(null));
                        staticRoute = testStaticRoute;
                        routeItem   = testRouteItem;
                        continue;
                    }


                    //todo...
                    ////check if this page is routing to a dynamic page redirect... (/path/dynamic-page/item-[contentid]
                    //if (!string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName)
                    //	|| !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                    //{
                    //	if (pathParts.Length == pathIndex + 1)
                    //	{

                    //		xxx

                    //		//check for item- in the last part of the path to see if this is a redirect...
                    //	}
                    //}


                    parentPage = new ResolvedPage(page);
                    lstResolvedPages.Add(parentPage);

                    routeItem   = testRouteItem;
                    staticRoute = testStaticRoute;
                }
                else
                {
                    //did not find a static route, add this to the dynamic route...
                    dynamicRoute = string.Format("{0}/{1}", dynamicRoute, pathPart);

                    //if we have a dynamic Route left over, and the static page was set, then we have our first page candidate..
                    if (routeItem != null)
                    {
                        //derive a resolved page from routeItem and dynamic route...
                        ResolvedPage resolvedPage = ResolvePage(ref routes, routeItem, dynamicRoute, languageCode, websiteName, ref lstResolvedPages, url);
                        if (resolvedPage != null)
                        {
                            foundRoute        = true;
                            foundDynamicRoute = true;

                            parentPage = resolvedPage;
                            lstResolvedPages.Add(resolvedPage);

                            parentPage   = resolvedPage;
                            dynamicRoute = string.Empty;

                            //now the NEXT route item becomes the child of this dynamic page..
                            if (!string.IsNullOrEmpty(routeItem.ChildDynamicPagePath) &&
                                routes.TryGetValue(routeItem.ChildDynamicPagePath, out testRouteItem))
                            {
                                staticRoute = routeItem.ChildDynamicPagePath;
                                routeItem   = testRouteItem;
                            }
                        }
                    }
                }
            }

            //if we didn't manage to find a dynamic page in there, return null;
            if (foundRoute && foundDynamicRoute)
            {
                return(lstResolvedPages);
            }

            return(null);
        }