Example #1
0
        public static SitemapUrlSet GetSitemap(string sitemapName)
        {
            Initialize();

            SitemapUrlSet sitemapSet = new SitemapUrlSet();

            SitemapUrlStoreBase S = s_Stores[sitemapName.ToLower()];

            sitemapSet.Add(S.GetSitemapUrls(S.Name));

            return(sitemapSet);
        }
Example #2
0
        /// <summary>
        /// Create a collection of URL elements from XML files
        /// </summary>
        /// <returns>SitemapUrlSet</returns>
        public override SitemapUrlSet GetSitemapUrls(string sitemapName)
        {
            SitemapUrlSet sitemapUrls = new SitemapUrlSet();

            string path = SitemapConfig.GetProviderPathByName(sitemapName);

            if (path != null)
            {
                String file = HttpContext.Current.Server.MapPath(path);

                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SitemapUrlSet));
                    StreamReader  reader     = new StreamReader(file);
                    sitemapUrls = (SitemapUrlSet)serializer.Deserialize(reader);

                    foreach (SitemapUrl url in sitemapUrls)
                    {
                        url.changeFreq = sitemapChangeFreq.weekly;
                    }

                    reader.Close();
                }
                // If the file is malformed XML, create an error message.
                catch (Exception ex)
                {
                    log.Error("An XML file has failed parsing in FileSitemapUrlStore:GetSitemapUrls().\nFile: " + file + "\nEnvironment: " + System.Environment.MachineName + "\nRequest Host: " + HttpContext.Current.Request.Url.Host + "\n" + ex.ToString() + "\n");
                    return(new SitemapUrlSet());
                }

                return(new SitemapUrlSet(sitemapUrls));
            }
            else
            {
                log.ErrorFormat("Could not load dictionary provider file located at {0}.", path);
                return(new SitemapUrlSet());
            }
        }
Example #3
0
 public void Add(SitemapUrlSet fromSet)
 {
     this._sitemapCollection.AddRange(fromSet._sitemapCollection);
 }
Example #4
0
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;

            response.ContentType     = "application/xml";
            response.ContentEncoding = System.Text.Encoding.UTF8;
            byte[] utf8;

            string sitemapName = GetSitemapName(request.FilePath);

            if (sitemapName != null)
            {
                // Check if sitemap is already in the cache; if so, print the cached sitemap
                if (HttpContext.Current.Cache[sitemapName] != null)
                {
                    utf8 = (byte[])HttpContext.Current.Cache[sitemapName];
                    response.OutputStream.Write(utf8, 0, utf8.Length);
                }
                // Check if the exception is already in the cache; if so, go to error page
                else if (HttpContext.Current.Cache[sitemapName + "_ex"] != null)
                {
                    response.ContentType            = "text/html";
                    response.TrySkipIisCustomErrors = true;
                    response.StatusCode             = 500;
                    response.StatusDescription      = "Error Generating Sitemap";
                    response.End();
                }
                // If it isn't, get the current sitemap, save that in the cache, and output
                else
                {
                    // Create a stopwatch object for timing of sitemap retrieval
                    Stopwatch stopwatch = new Stopwatch();
                    TimeSpan  timeSpan;

                    try
                    {
                        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                        ns.Add(string.Empty, "http://www.sitemaps.org/schemas/sitemap/0.9");
                        XmlSerializer ser = new XmlSerializer(typeof(SitemapUrlSet), "http://www.sitemaps.org/schemas/sitemap/0.9");

                        using (MemoryStream memStream = new MemoryStream())
                            using (XmlWriter writer = XmlWriter.Create(memStream))
                            {
                                stopwatch.Start();
                                SitemapUrlSet set = Sitemaps.GetSitemap(sitemapName);
                                ser.Serialize(writer, set, ns);
                                utf8 = memStream.ToArray();

                                if (set.Count() > 0)
                                {
                                    HttpContext.Current.Cache.Add(sitemapName, utf8, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);

                                    response.OutputStream.Write(utf8, 0, utf8.Length);

                                    stopwatch.Stop();
                                }
                                else
                                {
                                    if (stopwatch.IsRunning)
                                    {
                                        stopwatch.Stop();
                                    }
                                    timeSpan = stopwatch.Elapsed;

                                    HttpContext.Current.Cache.Add(sitemapName + "_ex", new byte[0], null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);

                                    log.Fatal("Error generating Sitemap " + sitemapName + ". Check required files for " + sitemapName + " sitemap. \nEnvironment: " + System.Environment.MachineName + "\nRequest Host: " + HttpContext.Current.Request.Url.Host
                                              + "\nTime Elapsed for Sitemap " + sitemapName + " Retrieval: " + timeSpan.ToString() + " \nSitemapHandler.cs:ProcessRequest()");

                                    response.ContentType = "text/html";
                                    response.OutputStream.Write(new byte[0], 0, 0);
                                    response.TrySkipIisCustomErrors = true;
                                    response.StatusCode             = 500;
                                    response.StatusDescription      = "Error Generating Sitemap";
                                    response.End();
                                }
                            }

                        // Execution timeout is 110±15 seconds, so we'll log an error if the process takes more than 90 seconds.
                        // This will allow us to track instances where sitemap generation is slow, even if we don't hit a ThreadAbortException.
                        // See https://blogs.msdn.microsoft.com/pedram/2007/10/02/how-the-execution-timeout-is-managed-in-asp-net/)
                        timeSpan = stopwatch.Elapsed;
                        if (timeSpan.TotalSeconds > 90)
                        {
                            log.Error("Warning: XML Sitemap " + sitemapName + " is taking longer than expected to retrieve. Check page and file instruction XML files.\nTime Elapsed for Sitemap " + sitemapName + " Retrieval: " + timeSpan.ToString());
                        }
                        else
                        {
                            log.Debug("Time Elapsed for Sitemap " + sitemapName + " Retrieval: " + timeSpan.ToString());
                        }
                    }
                    // Save the exception in the cache and send an error email
                    catch (Exception ex)
                    {
                        if (stopwatch.IsRunning)
                        {
                            stopwatch.Stop();
                        }
                        timeSpan = stopwatch.Elapsed;

                        HttpContext.Current.Cache.Add(sitemapName + "_ex", new byte[0], null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);

                        log.Fatal("Error generating Sitemap " + sitemapName + ". Check required files for " + sitemapName + " sitemap. \nEnvironment: " + System.Environment.MachineName + "\nRequest Host: " + HttpContext.Current.Request.Url.Host
                                  + "\nTime Elapsed for Sitemap " + sitemapName + " Retrieval: " + timeSpan.ToString() + " \nSitemapHandler.cs:ProcessRequest()", ex);

                        response.ContentType = "text/html";
                        response.OutputStream.Write(new byte[0], 0, 0);
                        response.TrySkipIisCustomErrors = true;
                        response.StatusCode             = 500;
                        response.StatusDescription      = "Error Generating Sitemap";
                        response.End();
                    }
                }
            }
            else
            {
                log.Fatal("Error generating Sitemap for " + request.FilePath + " - sitemap not included in web config. \nEnvironment: " + System.Environment.MachineName + "\nRequest Host: " + HttpContext.Current.Request.Url.Host
                          + " \nSitemapHandler.cs:ProcessRequest()");

                response.ContentType            = "text/html";
                response.TrySkipIisCustomErrors = true;
                response.StatusCode             = 404;
                response.StatusDescription      = "Sitemap Not Found";
                response.End();
            }
        }