Exemple #1
0
        public override void ProcessRequest(HttpContextBase context)
        {
            bool             forNews   = context.Request.Path.EndsWith("newssitemap.axd", StringComparison.OrdinalIgnoreCase);
            HandlerCacheItem cacheItem = forNews ? GetNewsSiteMap(context) : GetRegularSiteMap(context);

            if (GenerateETag)
            {
                if (HandleIfNotModified(context, cacheItem.ETag))
                {
                    return;
                }
            }

            if (Compress)
            {
                context.CompressResponse();
            }

            HttpResponseBase response = context.Response;

            response.ContentType = "text/xml";
            response.Write(cacheItem.Content);

            if (CacheDurationInMinutes > 0)
            {
                if (GenerateETag)
                {
                    response.Cache.SetETag(cacheItem.ETag);
                }

                context.CacheResponseFor(TimeSpan.FromMinutes(CacheDurationInMinutes));
            }
        }
        public override void ProcessRequest(HttpContextBase context)
        {
            string assetName = context.Request.QueryString["name"];

            if (!string.IsNullOrEmpty(assetName))
            {
                AssetElement setting = GetSetting(Configuration, assetName);

                if (setting != null)
                {
                    HandlerCacheItem asset = GetAsset(context, setting);

                    if (asset != null)
                    {
                        if (setting.GenerateETag)
                        {
                            if (HandleIfNotModified(context, asset.ETag))
                            {
                                return;
                            }
                        }

                        HttpResponseBase response = context.Response;

                        // Set the content type
                        response.ContentType = setting.ContentType;

                        // Compress
                        if (setting.Compress)
                        {
                            context.CompressResponse();
                        }

                        // Write
                        using (StreamWriter sw = new StreamWriter(response.OutputStream))
                        {
                            sw.Write(asset.Content);
                        }

                        // Cache
                        if (setting.CacheDurationInDays > 0)
                        {
                            // Helpful when hosting in Single Web server
                            if (setting.GenerateETag)
                            {
                                response.Cache.SetETag(asset.ETag);
                            }

                            context.CacheResponseFor(TimeSpan.FromDays(setting.CacheDurationInDays));
                        }
                    }
                }
            }
        }
Exemple #3
0
        private HandlerCacheItem GetRegularSiteMap(HttpContextBase context)
        {
            bool forMobile = context.Request.Path.EndsWith("mobilesitemap.axd", StringComparison.OrdinalIgnoreCase);

            string cacheKey = forMobile ? "mobileSiteMap" : "siteMap";

            Log.Info("Generating {0}".FormatWith(cacheKey));

            HandlerCacheItem cacheItem;

            Cache.TryGet(cacheKey, out cacheItem);

            if (cacheItem == null)
            {
                XElement urlSet = new XElement(_ns + "urlset", new XAttribute("xmlns", _ns.ToString()));

                if (forMobile)
                {
                    urlSet.Add(new XAttribute(XNamespace.Xmlns + "mobile", _googleMobile.ToString()));
                }

                DateTime currentDate = SystemTime.Now();
                string   rootUrl     = Settings.RootUrl;

                AddStoriesInRegularSiteMap(context, forMobile, urlSet);
                AddPublishedPagesInRegularSiteMap(context, forMobile, urlSet, currentDate);
                AddUpcomingPagesInRegularSiteMap(context, forMobile, urlSet, currentDate);
                AddCategoryPagesInRegularSiteMap(context, forMobile, urlSet, currentDate);
                AddTagPagesInRegularSiteMap(context, forMobile, urlSet, currentDate);
                AddUserPagesInRegularSiteMap(context, forMobile, urlSet, currentDate);

                urlSet.Add(CreateEntry(context, rootUrl, "Submit", null, currentDate, SiteMapChangeFrequency.Monthly, SiteMapUpdatePriority.Low, forMobile));
                urlSet.Add(CreateEntry(context, rootUrl, "Faq", null, currentDate, SiteMapChangeFrequency.Monthly, SiteMapUpdatePriority.Low, forMobile));
                urlSet.Add(CreateEntry(context, rootUrl, "About", null, currentDate, SiteMapChangeFrequency.Monthly, SiteMapUpdatePriority.Low, forMobile));
                urlSet.Add(CreateEntry(context, rootUrl, "Contact", null, currentDate, SiteMapChangeFrequency.Monthly, SiteMapUpdatePriority.Low, forMobile));

                XDocument doc = new XDocument();
                doc.Add(urlSet);

                cacheItem = new HandlerCacheItem {
                    Content = doc.ToXml()
                };

                if ((CacheDurationInMinutes > 0) && !Cache.Contains(cacheKey))
                {
                    Cache.Set(cacheKey, cacheItem, SystemTime.Now().AddMinutes(CacheDurationInMinutes));
                }
            }

            Log.Info("{0} generated".FormatWith(cacheKey));

            return(cacheItem);
        }
Exemple #4
0
        private HandlerCacheItem GetNewsSiteMap(HttpContextBase context)
        {
            const string DateFormat = "yyyy-MM-ddThh:mm:ssZ";
            const string CacheKey   = "newsSitemap";

            Log.Info("Generating {0}".FormatWith(CacheKey));

            HandlerCacheItem cacheItem;

            Cache.TryGet(CacheKey, out cacheItem);

            if (cacheItem == null)
            {
                XElement urlSet = new XElement(_ns + "urlset", new XAttribute("xmlns", _ns.ToString()), new XAttribute(XNamespace.Xmlns + "news", _googleNews.ToString()));

                PagedResult <IStory> pagedResult = StoryRepository.FindPublished(0, 1000); // Google only supports 1000 story

                int i = 0;

                foreach (IStory story in pagedResult.Result)
                {
                    SiteMapUpdatePriority priority = (i < 50) ? SiteMapUpdatePriority.Critical : SiteMapUpdatePriority.Normal;

                    AddStoryInNewsSiteMap(context, urlSet, story, priority, DateFormat);

                    i += 1;
                }

                XDocument doc = new XDocument();
                doc.Add(urlSet);

                cacheItem = new HandlerCacheItem {
                    Content = doc.ToXml()
                };

                if ((CacheDurationInMinutes > 0) && (!Cache.Contains(CacheKey)))
                {
                    Cache.Set(CacheKey, cacheItem, SystemTime.Now().AddMinutes(CacheDurationInMinutes));
                }
            }

            Log.Info("{0} generated".FormatWith(CacheKey));

            return(cacheItem);
        }
        private HandlerCacheItem GetAsset(HttpContextBase context, AssetElement setting)
        {
            string           key = "cache:{0}".FormatWith(setting.Name);
            HandlerCacheItem asset;

            Cache.TryGet(key, out asset);

            if (asset == null)
            {
                string[] files = setting.Files.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                if (files.Length > 0)
                {
                    StringBuilder contentBuilder = new StringBuilder();

                    for (int i = 0; i < files.Length; i++)
                    {
                        string file        = context.Server.MapPath(Path.Combine(setting.Directory, files[i]));
                        string fileContent = FileReader.ReadAllText(file);

                        if (!string.IsNullOrEmpty(fileContent))
                        {
                            contentBuilder.AppendLine(fileContent);
                            contentBuilder.AppendLine();
                        }
                    }

                    string content = contentBuilder.ToString();

                    if (!string.IsNullOrEmpty(content))
                    {
                        asset = new HandlerCacheItem {
                            Content = content
                        };

                        if ((setting.CacheDurationInDays > 0) && (!Cache.Contains(key)))
                        {
                            Cache.Set(key, asset, SystemTime.Now().AddDays(setting.CacheDurationInDays));
                        }
                    }
                }
            }

            return(asset);
        }
Exemple #6
0
        public override void ProcessRequest(HttpContextBase context)
        {
            const string CacheKey = "openSearchDescription";

            XNamespace ns  = "http://a9.com/-/spec/opensearch/1.1/";
            XNamespace moz = "http://www.mozilla.org/2006/browser/search/";

            HandlerCacheItem cacheItem;

            Cache.TryGet(CacheKey, out cacheItem);

            if (cacheItem == null)
            {
                XElement openSearch = new XElement(
                    ns + "OpenSearchDescription",
                    new XAttribute("xmlns", ns.ToString()),
                    new XAttribute(XNamespace.Xmlns + "moz", moz.ToString()),
                    new XElement(ns + "ShortName", Settings.SiteTitle),
                    new XElement(ns + "Description", Settings.MetaDescription),
                    new XElement(ns + "LongName", "{0} Web Search".FormatWith(Settings.SiteTitle)));

                UrlHelper url = CreateUrlHelper(context);

                string rootUrl = Settings.RootUrl;
                string htmlUrl = url.RouteUrl("Search");
                string rssUrl  = url.RouteUrl("FeedSearch");
                string atomUrl = url.RouteUrl("FeedSearch", new { format = "Atom" });
                string tagUrl  = url.RouteUrl("SuggestTags");

                openSearch.Add(
                    new XElement(
                        ns + "Url",
                        new XAttribute("type", "text/html"),
                        new XAttribute("template", string.Concat("{0}{1}".FormatWith(rootUrl, htmlUrl), "?q={searchTerms}"))
                        ),
                    new XElement(
                        ns + "Url",
                        new XAttribute("type", "application/atom+xml"),
                        new XAttribute("template", string.Concat("{0}{1}".FormatWith(rootUrl, atomUrl), "/{searchTerms}"))
                        ),
                    new XElement(
                        ns + "Url",
                        new XAttribute("type", "application/rss+xml"),
                        new XAttribute("template", string.Concat("{0}{1}".FormatWith(rootUrl, rssUrl), "/{searchTerms}"))
                        ),
                    new XElement(
                        ns + "Url",
                        new XAttribute("type", "application/x-suggestions+json"),
                        new XAttribute("method", "GET"),
                        new XAttribute("template", string.Concat("{0}{1}".FormatWith(rootUrl, tagUrl), "?q={searchTerms}&client=browser"))
                        )
                    );

                openSearch.Add(new XElement(moz + "SearchForm", rootUrl));

                string tags = string.Join(" ", Settings.MetaKeywords.Split(','));

                openSearch.Add(
                    new XElement(ns + "Contact", Settings.WebmasterEmail),
                    new XElement(ns + "Tags", tags)
                    );

                openSearch.Add(
                    new XElement(ns + "Image", new XAttribute("height", "16"), new XAttribute("width", "16"), new XAttribute("type", "image/x-icon"), "{0}/Assets/Images/fav.ico".FormatWith(rootUrl)),
                    new XElement(ns + "Image", new XAttribute("height", "64"), new XAttribute("width", "64"), new XAttribute("type", "image/png"), "{0}/Assets/Images/fav.png".FormatWith(rootUrl)),
                    new XElement(ns + "Image", new XAttribute("type", "image/png"), "{0}/Assets/Images/logo2.png".FormatWith(rootUrl))
                    );

                openSearch.Add(
                    new XElement(ns + "Query", new XAttribute("role", "example"), new XAttribute("searchTerms", "asp.net")),
                    new XElement(ns + "Developer", "{0} Development Team".FormatWith(Settings.SiteTitle)),
                    new XElement(ns + "Attribution", "Search data Copyright (c) {0}".FormatWith(Settings.SiteTitle)),
                    new XElement(ns + "SyndicationRight", "open"),
                    new XElement(ns + "AdultContent", "false"),
                    new XElement(ns + "Language", "en-us"),
                    new XElement(ns + "OutputEncoding", "UTF-8"),
                    new XElement(ns + "InputEncoding", "UTF-8")
                    );

                XDocument doc = new XDocument();
                doc.Add(openSearch);

                cacheItem = new HandlerCacheItem {
                    Content = doc.ToXml()
                };

                if ((CacheDurationInDays > 0) && !Cache.Contains(CacheKey))
                {
                    Cache.Set(CacheKey, cacheItem, SystemTime.Now().AddDays(CacheDurationInDays));
                }
            }

            if (GenerateETag)
            {
                if (HandleIfNotModified(context, cacheItem.ETag))
                {
                    return;
                }
            }

            if (Compress)
            {
                context.CompressResponse();
            }

            HttpResponseBase response = context.Response;

            response.ContentType = "application/opensearchdescription+xml";
            response.Write(cacheItem.Content);

            if (CacheDurationInDays > 0)
            {
                if (GenerateETag)
                {
                    response.Cache.SetETag(cacheItem.ETag);
                }

                context.CacheResponseFor(TimeSpan.FromDays(CacheDurationInDays));
            }
        }
Exemple #7
0
        public override void ProcessRequest(HttpContextBase context)
        {
            const string CacheKey = "XrdsDescription";

            const string Xrds = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                "<xrds:XRDS xmlns:xrds=\"xri://$xrds\" xmlns:openid=\"http://openid.net/xmlns/1.0\" xmlns=\"xri://$xrd*($v*2.0)\">" +
                                "<XRD>" +
                                "<Service priority=\"1\">" +
                                "<Type>http://specs.openid.net/auth/2.0/return_to</Type>" +
                                "<URI>{0}</URI>" +
                                "</Service>" +
                                "</XRD>" +
                                "</xrds:XRDS>";

            HandlerCacheItem cacheItem;

            Cache.TryGet(CacheKey, out cacheItem);

            if (cacheItem == null)
            {
                UrlHelper urlHelper = CreateUrlHelper(context);
                string    url       = string.Concat(Settings.RootUrl, urlHelper.RouteUrl("OpenId"));
                string    xml       = Xrds.FormatWith(url);

                cacheItem = new HandlerCacheItem {
                    Content = xml
                };

                if ((CacheDurationInDays > 0) && !Cache.Contains(CacheKey))
                {
                    Cache.Set(CacheKey, cacheItem, SystemTime.Now().AddDays(CacheDurationInDays));
                }
            }

            if (GenerateETag)
            {
                if (HandleIfNotModified(context, cacheItem.ETag))
                {
                    return;
                }
            }

            if (Compress)
            {
                context.CompressResponse();
            }

            HttpResponseBase response = context.Response;

            response.ContentType = "application/xrds+xml";
            response.Write(cacheItem.Content);

            if (CacheDurationInDays > 0)
            {
                if (GenerateETag)
                {
                    response.Cache.SetETag(cacheItem.ETag);
                }

                context.CacheResponseFor(TimeSpan.FromDays(CacheDurationInDays));
            }

            Log.Info("Xrds Requested.");
        }