Ejemplo n.º 1
0
        /// <summary>
        /// Gets the PageTemplateInfo for a given template and Display Version.
        /// </summary>
        /// <param name="templateName">The template we are looking for </param>
        /// <param name="version">The Display Version to get the PageTemplateInfo</param>
        /// <returns>The PageTemplateInfo for the given Display Version if defined, otherwise null
        /// </returns>
        public PageTemplateInfo GetPageTemplateInfo(string templateName, DisplayVersions version)
        {
            PageTemplateCollection col = PageTemplateCollections.FirstOrDefault(ptc => ptc.TemplateName == templateName);

            //If we could not find the page template col then return null, otherwise, ask the PTC for the template info
            return(col == null ? null : col.GetPageTemplateInfo(version));
        }
Ejemplo n.º 2
0
        //[DeploymentItem("XmlFiles\\cancertopics.xml")]
        //[DeploymentItem("XmlFiles\\PageTemplateConfiguration.xml")]
        public void InitializePageAssemblyInfo_Test()
        {
            PageAssemblyContext_Accessor target = new PageAssemblyContext_Accessor(); // TODO: Initialize to an appropriate value

            string xmlFilePath = TestContext.TestDeploymentDir + "\\PublishedContent\\PageInstructions\\cancertopics.xml";
            IPageAssemblyInstruction pageAssemblyInfo = null;

            using (HttpSimulator httpSimulator = GetStandardSimulatedRequest())
            {
                using (XmlReader xmlReader = XmlReader.Create(xmlFilePath))
                {
                    xmlReader.MoveToContent();
                    string pageAssemblyInfoTypeName = xmlReader.LocalName;

                    //XmlSerializer serializer = _serializers[pageAssemblyInfoTypeName];
                    XmlSerializer serializer = new XmlSerializer(typeof(SinglePageAssemblyInstruction));

                    // Deserialize the XML into an object.
                    pageAssemblyInfo = (IPageAssemblyInstruction)serializer.Deserialize(xmlReader);
                }

                IPageAssemblyInstruction info          = pageAssemblyInfo;
                DisplayVersions          dispayVersion = DisplayVersions.Web;

                //Load the page template info for the current request
                PageTemplateInfo pageTemplateInfo = new PageTemplateInfo();
                pageTemplateInfo.PageTemplatePath = "FOO.ASPX";

                target.InitializePageAssemblyInfo(info, dispayVersion, pageTemplateInfo, "url");

                Assert.Inconclusive("A method that does not return a value cannot be verified.");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the PageTemplateInfo for a given Display Version.
        /// </summary>
        /// <param name="version">The Display Version to get the PageTemplateInfo</param>
        /// <returns>The PageTemplateInfo for the given Display Version if defined, otherwise it defaults to the
        /// PageTemplateInfo for the Web Display Version.
        /// </returns>
        public PageTemplateInfo GetPageTemplateInfo(DisplayVersions version)
        {
            PageTemplateInfo rtnInfo = null;

            //Find the PageTemplateInfo for the requested version type.
            rtnInfo = PageTemplateInfos.FirstOrDefault(pti => pti.DisplayVersion == version);

            //If there was no PageTemplateInfo for the requested version type.
            if (rtnInfo == null)
            {
                if (version == DisplayVersions.PrintAll)
                {
                    //For PrintAll default to Print if available
                    rtnInfo = PageTemplateInfos.FirstOrDefault(pti => pti.DisplayVersion == DisplayVersions.Print);

                    //If Print is not available default to ViewAll
                    if (rtnInfo == null)
                    {
                        rtnInfo = PageTemplateInfos.FirstOrDefault(pti => pti.DisplayVersion == DisplayVersions.ViewAll);
                    }
                }

                //If nothing is available default to Web
                if (rtnInfo == null)
                {
                    rtnInfo = PageTemplateInfos.FirstOrDefault(pti => pti.DisplayVersion == DisplayVersions.Web);
                }
            }

            return(rtnInfo);
        }
Ejemplo n.º 4
0
        public void GetTemplateInfoTest_DefaultCancerGovTemplate_Print()
        {
            using (HttpSimulator httpSimulator = GetStandardSimulatedRequest())
            {
                string          templateName = "DefaultCancerGovTemplate";
                DisplayVersions version      = DisplayVersions.Print;

                PageTemplateInfo expected = GetPrintPageTemplateInfo();

                PageTemplateInfo actual;
                actual = PageTemplateResolver.GetPageTemplateInfo(string.Empty, templateName, version);
                Assert.AreEqual(expected, actual);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Stores the current page assembly instruction object in the context so it can be made
 /// available in one central location.
 /// </summary>
 /// <param name="info"></param>
 public void InitializePageAssemblyInfo(IPageAssemblyInstruction info, DisplayVersions displayVersion, PageTemplateInfo pageTemplateInfo, string requestedPath)
 {
     if (PageAssemblyInstruction != null)
     {
         throw new Exception("You cannot initialize the page assembly context with more than one IPageAssemblyInfo instance.");
     }
     else
     {
         PageAssemblyInstruction = info;
         PageAssemblyContext.Current.DisplayVersion   = displayVersion;
         PageAssemblyContext.Current.PageTemplateInfo = pageTemplateInfo;
         PageAssemblyContext.Current.requestedUrl     = requestedPath;
     }
 }
Ejemplo n.º 6
0
        private void SetReturnToTop(DisplayVersions version, DisplayLanguage language)
        {
            this.Class = "backtotop-link";

            switch (language)
            {
            case DisplayLanguage.Spanish:
                this.InnerHtml = "Volver arriba";
                break;
            }

            if (version != DisplayVersions.Text)
            {
                HtmlImage backToTopArrow = new HtmlImage("/images/backtotop_red.gif", this.InnerHtml);
                backToTopArrow.Border = "0";
                this.InnerHtml        = backToTopArrow.Render() + this.InnerHtml;
            }
        }
        /// <summary>
        /// Identifies the display type print or web.loads the appropriate xml file and loads the IPageAssemblyInstruction
        /// Loads the page template info and provides the complete intructions/assemblyinfo to assemble a page for the requested URL.
        /// </summary>
        /// <param name="context">httpcontext</param>
        /// <param name="url">Requested URL</param>
        void RewriteUrl(HttpContext context, string url)
        {
            DisplayVersions displayVersion = DisplayVersions.Web;

            if (url.EndsWith("/")) //The key will never be just / because of the statement above
            {
                //strip the trailing /
                url = url.Substring(0, url.LastIndexOf('/'));
                if (string.IsNullOrEmpty(url))
                {
                    url = ContentDeliveryEngineConfig.DefaultHomePage.Homepage;
                }
            }

            //Store the url so it can be rewritten for logging.
            context.Items[REQUEST_URL_KEY] = url;

            bool isPrint = false;

            if (url.EndsWith(PRINT_URL_ENDING))
            {
                //Do not take a substring if someone is trying to print the homepage
                //of the site
                if (url != PRINT_URL_ENDING)
                {
                    //Since the pretty url map knows nothing about urls
                    //that end with print we need to remove /print from
                    //the key
                    url = url.Substring(0, url.Length - PRINT_URL_ENDING.Length);
                }
                else
                {
                    //We know the key for the homepage
                    //This code does not seem to actually work
                    //At the very least it should rewrite the url to /?print=1 or whatever
                    url = "/";
                }

                //Set the URL to Default home page if the requested url is "/".
                if (url == "/")
                {
                    url = ContentDeliveryEngineConfig.DefaultHomePage.Homepage;
                }

                isPrint        = true;
                displayVersion = DisplayVersions.Print;
            }

            //Now check to see if it is the view all. (For MultiPageAssemblyInstructions)
            if (url.EndsWith(VIEWALL_URL_ENDING))
            {
                //Do not take a substring if someone is trying to print the homepage
                //of the site
                if (url != VIEWALL_URL_ENDING)
                {
                    //Since the pretty url map knows nothing about urls
                    //that end with print we need to remove /print from
                    //the key
                    url = url.Substring(0, url.Length - VIEWALL_URL_ENDING.Length);
                }
                else
                {
                    //We know the key for the homepage
                    //This code does not seem to actually work
                    //At the very least it should rewrite the url to /?print=1 or whatever
                    url = "/";
                }

                if (isPrint)
                {
                    displayVersion = DisplayVersions.PrintAll;
                }
                else
                {
                    displayVersion = DisplayVersions.ViewAll;
                }
            }

            //Handle for Dictionary Print pages AND foia summaries pages.
            if (context.Request.Url.Query.Contains("print") && context.Request.Url.Query.Contains("allpages"))
            {
                displayVersion = DisplayVersions.PrintAll;
            }
            else if (context.Request.Url.Query.Contains("print"))
            {
                displayVersion = DisplayVersions.Print;
            }


            // Set Display version before loading the assembly instructions so it can be accessed in the constructor
            PageAssemblyContext.CurrentDisplayVersion = displayVersion;

            //Now lookup the url..

            IPageAssemblyInstruction assemblyInfo = null;

            try
            {
                //Try to get the assemblyinfo following these rules.
                // When depth == 1, and we get an assemblyInfo, then we are looking for
                //     the full URL that was requested
                // When depth == 2, and we get an assemblyInfo, then we are looking for
                //     either a MPAI, or a SPAI that implements PushState
                // When depth >= 3, and we get an assemblyInfo, then we are looking for
                //     only a SPAI that implements PushState

                string currUrlPath = url;
                int    depth       = 1;

                while (assemblyInfo == null && currUrlPath != string.Empty)
                {
                    //Load the assembly info from a path/file.
                    assemblyInfo = PageAssemblyInstructionFactory.GetPageAssemblyInfo(currUrlPath);

                    //Did not find anything, let's continue.
                    if (assemblyInfo == null)
                    {
                        //Chop off the current path and try again.
                        currUrlPath = currUrlPath.Substring(0, currUrlPath.LastIndexOf('/'));

                        // Increment the depth one more time as we continue diving.
                        depth++;

                        continue;
                    }

                    // We have an MPAI & depth is greater than 2 (e.g. MPAI is /foo and URL is /foo/bar/bazz)
                    if (assemblyInfo is IMultiPageAssemblyInstruction && depth != 2)
                    {
                        assemblyInfo = null;
                        break;
                    }

                    // We have a Single page that is 2 or more levels deep, and does not implement pushstate
                    // e.g. SPAI is /foo and URL is /foo/bar.
                    if (
                        assemblyInfo is SinglePageAssemblyInstruction &&
                        depth > 1 &&
                        !assemblyInfo.ImplementsPushState
                        )
                    {
                        assemblyInfo = null;
                        break;
                    }

                    // Good so far, so let's check if it is a MPAI and it has the requested page.
                    if (assemblyInfo is MultiPageAssemblyInstruction)
                    {
                        int index = ((IMultiPageAssemblyInstruction)assemblyInfo).GetPageIndexOfUrl(url);
                        if (index >= 0)
                        {
                            //This url is a page, so set the current index so we can get the page template later.
                            ((IMultiPageAssemblyInstruction)assemblyInfo).SetCurrentPageIndex(index);
                            assemblyInfo.Initialize();
                        }
                        else
                        {
                            assemblyInfo = null;
                            break;
                        }
                    }

                    //By now we have sussed out if we have a PAI or not.
                    //If we do, then we need to initialize it.
                    if (assemblyInfo != null)
                    {
                        assemblyInfo.Initialize();
                    }

                    //Break out of loop.
                    break;
                }
            }
            catch (Exception ex)
            {
                string errMessage = "RewriteUrl(): Requested URL: " + context.Items[REQUEST_URL_KEY] + "\nFailed to Create IPageAssemblyInstruction with the XML file Provided.";
                log.Error(errMessage, ex);
                RaiseErrorPage(errMessage, ex);
                return;
            }

            //Exiting because there is no page assembly instruction for the requested URL.
            if (assemblyInfo == null)
            {
                return;
            }

            //Load the page template info for the current request
            PageTemplateInfo pageTemplateInfo = null;

            try
            {
                pageTemplateInfo = PageTemplateResolver.GetPageTemplateInfo(assemblyInfo.TemplateTheme, assemblyInfo.PageTemplateName, displayVersion);
            }
            catch (Exception ex)
            {
                string errMessage = "RewriteUrl(): Requested URL: " + context.Items[REQUEST_URL_KEY] + "\nCannot Load the pageTemplateInfo problem with the PageTemplateConfiguration XML file ";
                log.Error(errMessage, ex);
                RaiseErrorPage(errMessage, ex);
                return;
            }

            if (pageTemplateInfo == null)
            {
                string errMessage = "RewriteUrl(): Requested URL: " + context.Items[REQUEST_URL_KEY] + "\nPage Template Not Found";
                log.Error(errMessage);
                RaiseErrorPage(errMessage, null);
                return;
            }

            //set the page assembly context with the assemblyInfo, dispayVersion and pageTemplateInfo
            PageAssemblyContext.Current.InitializePageAssemblyInfo(assemblyInfo, displayVersion, pageTemplateInfo, url);

            // Set culture for selected content.
            // The language and culture are formatted as xx-yy (eg. "en-us") when a locale is chosen in Percussion.
            // The hyphenated four-letter code is then trimmed to a 2-character neutral culture (eg. "en") by the Velocity
            // user macros file and is added to the XML file to be published in /PublishedContent/PageInstructions. The
            // assemblyInfo object uses the neutral culture (found in the <Language> tag in the XML file) for page assembly.
            // The only exception to this is the Chinese language, in which the culture MUST be specified as either
            // "zh-hans" (Simplified Chinese) or "zh-hant" (Traditional Chinese). There is no support for a 2-character "zh"
            // culture - see http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.90).aspx for
            // details. The logic below is a workaround to catch the "zh" and convert it to the full "zh-hans" culture.
            if (!string.IsNullOrEmpty(assemblyInfo.Language))
            {
                if (assemblyInfo.Language == "zh")
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-hans");
                }
                else
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(assemblyInfo.Language);
                }
            }
            string rewriteUrl = PageAssemblyContext.Current.PageTemplateInfo.PageTemplatePath;

            // Append original parameters in the request URL
            if (!String.IsNullOrEmpty(context.Request.Url.Query))
            {
                //The query should contain a ?
                rewriteUrl += context.Request.Url.Query;
            }

            //If we are showing the print version then append the print query
            //variable
            if (isPrint)
            {
                if (rewriteUrl.Contains("?"))
                {
                    rewriteUrl += "&print=1";
                }
                else
                {
                    rewriteUrl += "?print=1";
                }
            }

            // Do not reset the client path because it'll break form action url's.
            try
            {
                context.RewritePath(rewriteUrl, false);
            }

            catch (HttpException ex)
            {
                string errMessage = "RewriteUrl(): Requested URL: " + context.Items[REQUEST_URL_KEY] + "\nFailed to rewrite URL.";
                log.Error(errMessage, ex);
                RaiseErrorPage(errMessage, ex);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Retrieves the LayoutTemplateInfo representing the named template.
        /// <remarks>
        /// So we will handle backwards compatibility by using the Theme approach if TemplateThemeCollection
        /// has something in it, OR fallback to the old PageTemplateCollection way if there are no themes.
        ///
        /// This way the DOCs can use the old mechanism, and CGov can use the new.  Also the go live for
        /// CGov will not require the new PTC to be published before the CDE code is deployed.
        /// </remarks>
        /// </summary>
        /// <param name="themeName">The name of the theme that the template belongs to.</param>
        /// <param name="templateName">The logical name of the page template we are looking for</param>
        /// <param name="version">The current display version that is being used.  This is used to choose the correct page template. </param>
        /// <returns></returns>
        public static PageTemplateInfo GetPageTemplateInfo(string themeName, string templateName, DisplayVersions version)
        {
            //Error if the templateName is null
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException("templateName", "The templateName parameter cannot be null or empty.");
            }

            //Error if we cannot get to the PTC
            if (PageTemplateConfiguration == null)
            {
                throw new Exception("PageTemplateConfiguration is null, Cannot determine page template");
            }

            //Check and see if we either have Template Themes or old school PageTemplateCollections and
            //if we do not have either, throw and exception
            if (!(
                    (PageTemplateConfiguration.TemplateThemeCollection != null && PageTemplateConfiguration.TemplateThemeCollection.Length > 0) ||
                    (PageTemplateConfiguration.PageTemplateCollections != null && PageTemplateConfiguration.PageTemplateCollections.Length > 0)
                    ))
            {
                throw new Exception("Neither Template Themes nor Page template collections exist in the PageTemplateConfiguration, cannot load page template.");
            }


            // Now we determine the way to get to the templateInfo.  Themes will take priority over old
            // way.
            PageTemplateInfo rtnTempInfo = null;

            if (PageTemplateConfiguration.TemplateThemeCollection.Length > 0)
            {
                //Check Theme Name
                if (String.IsNullOrWhiteSpace(themeName))
                {
                    throw new ArgumentNullException("themeName", "The themeName parameter cannot be null or empty.");
                }

                //Handle New
                TemplateThemeInfo themeInfo = PageTemplateConfiguration.TemplateThemeCollection.FirstOrDefault(tti => tti.ThemeName == themeName);

                if (themeInfo == null)
                {
                    throw new Exception("Cannot Find Theme named: " + themeName);
                }

                rtnTempInfo = themeInfo.GetPageTemplateInfo(templateName, version);
            }
            else if (PageTemplateConfiguration.PageTemplateCollections.Length > 0)
            {
                PageTemplateCollection tempColl = PageTemplateConfiguration.PageTemplateCollections.FirstOrDefault(ptc => ptc.TemplateName == templateName);

                if (tempColl != null)
                {
                    rtnTempInfo = tempColl.GetPageTemplateInfo(version);
                }
            }

            return(rtnTempInfo);
        }