Exemplo n.º 1
0
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            PageTemplateInfo target = obj as PageTemplateInfo;

            if (target == null)
            {
                return(false);
            }

            if (DisplayVersion != target.DisplayVersion)
            {
                return(false);
            }

            if (PageTemplatePath != target.PageTemplatePath)
            {
                return(false);
            }

            if (
                (StyleSheets == null && target.StyleSheets != null) ||
                (StyleSheets != null && target.StyleSheets == null)
                )
            {
                return(false);
            }

            if (
                (Javascripts == null && target.Javascripts != null) ||
                (Javascripts != null && target.Javascripts == null)
                )
            {
                return(false);
            }


            if (StyleSheets != null && target.StyleSheets != null)
            {
                if (StyleSheets.Length != target.StyleSheets.Length)
                {
                    return(false);
                }

                for (int i = 0; i < StyleSheets.Length; i++)
                {
                    if (StyleSheets[i] == null)
                    {
                        if (target.StyleSheets[i] != null)
                        {
                            return(false);
                        }

                        //If we did not return then we know that target.Stylesheets[i] is also null
                    }
                    else
                    {
                        if (!StyleSheets[i].Equals(target.StyleSheets[i]))
                        {
                            return(false);
                        }
                    }
                }
            }
            if (Javascripts != null && target.Javascripts != null)
            {
                if (Javascripts.Length != target.Javascripts.Length)
                {
                    return(false);
                }

                for (int i = 0; i < Javascripts.Length; i++)
                {
                    if (Javascripts[i] == null)
                    {
                        if (target.Javascripts[i] != null)
                        {
                            return(false);
                        }

                        //If we did not return then we know that target.Stylesheets[i] is also null
                    }
                    else
                    {
                        if (!Javascripts[i].Equals(target.Javascripts[i]))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
        /// <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);
            }
        }
Exemplo n.º 3
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;
     }
 }