예제 #1
0
        /// <summary>
        /// Gets a single PromoUrlMapping without any parents.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static PromoUrlMapping GetPromoUrlMapping(string path, ref FileInfo fileInfo)
        {
            PromoUrlMapping promoUrlMapping = null;
            string          xmlFileName     = null;

            try
            {
                xmlFileName = String.Format(ContentDeliveryEngineConfig.PathInformation.PromoUrlMappingPath.Path, (path == "/" ? String.Empty : path));
                xmlFileName = Server.MapPath(xmlFileName);
                FileInfo promoUrlFileInfo = new FileInfo(xmlFileName);
                if (!promoUrlFileInfo.Exists)
                {
                    log.Warn("GetPromoUrlMapping(): PromoUrl Mapping file does not exist");
                    return(null);
                }
                else
                {
                    fileInfo = promoUrlFileInfo;
                }

                using (FileStream xmlFile = File.Open(xmlFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
                {
                    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                    xmlReaderSettings.IgnoreWhitespace = true;
                    using (XmlReader xmlReader = XmlReader.Create(xmlFile, xmlReaderSettings))
                    {
                        promoUrlMapping = (PromoUrlMapping)_serializer.Deserialize(xmlReader);
                    }
                }
            }
            catch (Exception ex)
            {
                string message = String.Format("GetPromoUrlMapping(): Unable to load section Promo Url mapping from file \"{0}.\"  The file may not exist or the XML in the file may not be serializable into a valid promoUrlMapping object.", xmlFileName);
                log.Error(message, ex);
                return(null);
            }
            return(promoUrlMapping);
        }
예제 #2
0
        void OnBeginRequest(object sender, EventArgs e)
        {
            context = ((HttpApplication)sender).Context;

            // Get absolute path of the request URL as pretty URL
            String url = context.Server.UrlDecode(context.Request.Url.AbsolutePath);

            if (Utility.IgnoreWebResource(url))
            {
                return;
            }

            //Check if the PageAssemblyInstruction is not null then it was processed as pretty url.
            if (PageAssemblyContext.Current.PageAssemblyInstruction != null)
            {
                return;
            }

            try
            {
                //Check if Promo Url information is in the Application State
                PromoUrlMapping promoUrlMapping            = null;
                bool            reLoad                     = (bool)context.Application["reloadPromoUrlMappingInfo"];
                bool            promoUrlMappingFileChanged = IsPromoUrlMappingFileChanged("/");

                if (context.Application["PromoUrlMapping"] != null && !reLoad && !promoUrlMappingFileChanged)
                {
                    promoUrlMapping = (PromoUrlMapping)context.Application["PromoUrlMapping"];
                }
                else
                {
                    context.Application.Lock();
                    FileInfo        fileInfo             = null;
                    PromoUrlMapping freshPromoUrlMapping = PromoUrlMappingInfoFactory.GetPromoUrlMapping("/", ref fileInfo);
                    if (freshPromoUrlMapping != null)
                    {
                        context.Application["reloadPromoUrlMappingInfo"] = false;
                        context.Application["PromoUrlMapping"]           = freshPromoUrlMapping;
                        CachedPromoMappingFileInfo = fileInfo;
                    }
                    context.Application.UnLock();

                    // Log an event saying the promo url mapping file was loaded
                    log.DebugFormat("Promo Url Mapping file successfully loaded reLoad: {0}, promoUrlMappingFileChanged: {1}",
                                    reLoad, promoUrlMappingFileChanged);
                }

                promoUrlMapping = (PromoUrlMapping)context.Application["PromoUrlMapping"];

                if (promoUrlMapping != null)
                {
                    PromoUrl promoUrl = null;
                    if (promoUrlMapping.PromoUrls.ContainsKey(url.ToLower()))
                    {
                        promoUrl = promoUrlMapping.PromoUrls[url.ToLower()];
                        string mappedToUrl = promoUrl.MappedTo + (string.IsNullOrEmpty(context.Request.Url.Query) ? "?redirect=true" : context.Request.Url.Query);

                        // Add redirect parameter for analytics (if not previously redirected)
                        if (!string.IsNullOrEmpty(context.Request.Url.Query))
                        {
                            mappedToUrl += (!context.Request.Url.Query.Contains("redirect=true") ? "&redirect=true" : String.Empty);
                        }


                        // If the original request is post then save target promo url
                        // for use in the page instructions assembly loader.
                        if (context.Request.RequestType == "POST")
                        {
                            context.RewritePath(mappedToUrl);
                        }
                        else
                        {
                            NCI.Web.CDE.Application.PermanentRedirector.DoPermanentRedirect(context.Response, mappedToUrl, "Promo Url");
                        }
                    }
                    else if (!url.EndsWith("/") && promoUrlMapping.PromoUrls.ContainsKey(url.ToLower() + "/"))
                    {
                        promoUrl = promoUrlMapping.PromoUrls[url.ToLower() + "/"];
                        string mappedToUrl = promoUrl.MappedTo + (string.IsNullOrEmpty(context.Request.Url.Query) ? "?redirect=true" : context.Request.Url.Query);

                        // Add redirect parameter for analytics (if not previously redirected)
                        if (!string.IsNullOrEmpty(context.Request.Url.Query))
                        {
                            mappedToUrl += (!context.Request.Url.Query.Contains("redirect=true") ? "&redirect=true" : String.Empty);
                        }

                        // If the original request is post then save target promo url
                        // for use in the page instructions assembly loader.
                        if (context.Request.RequestType == "POST")
                        {
                            context.RewritePath(mappedToUrl);
                        }
                        else
                        {
                            NCI.Web.CDE.Application.PermanentRedirector.DoPermanentRedirect(context.Response, mappedToUrl, "Promo Url");
                        }
                    }
                    else
                    {
                        //1. Remove last part of path, e.g. /cancertopics/wyntk/bladder/page10 becomes /cancertopics/wyntk/bladder
                        string truncUrl  = url.Substring(0, url.LastIndexOf('/'));
                        string appendUrl = url.Substring(url.LastIndexOf('/'));

                        if (truncUrl != string.Empty)
                        {
                            if (promoUrlMapping.PromoUrls.ContainsKey(truncUrl.ToLower()))
                            {
                                promoUrl = promoUrlMapping.PromoUrls[truncUrl.ToLower()];
                                string mappedToUrl = promoUrl.MappedTo + appendUrl + (string.IsNullOrEmpty(context.Request.Url.Query) ? "?redirect=true" : context.Request.Url.Query);

                                // Add redirect parameter for analytics (if not previously redirected)
                                if (!string.IsNullOrEmpty(context.Request.Url.Query))
                                {
                                    mappedToUrl += (!context.Request.Url.Query.Contains("redirect=true") ? "&redirect=true" : String.Empty);
                                }

                                if (context.Request.RequestType == "POST")
                                {
                                    context.RewritePath(mappedToUrl);
                                }
                                else
                                {
                                    NCI.Web.CDE.Application.PermanentRedirector.DoPermanentRedirect(context.Response, mappedToUrl, "Promo Url multi-page");
                                }
                            }
                        }
                        else
                        {
                            log.DebugFormat("OnBeginRequest(): Promo Url Mapping information not found for {0}", url);
                        }
                    }
                }
                else
                {
                    log.Warn("OnBeginRequest(): No Promo Url Mapping information");
                }
            }
            catch (System.Threading.ThreadAbortException)
            { }
            catch (Exception ex)
            {
                log.Error("OnBeginRequest(): Failed to Process Promo URL - " + url, ex);
            }
        }