コード例 #1
0
ファイル: CdfHandler.cs プロジェクト: vnextcoder/dasblog-core
        public void ProcessRequest(HttpContext context)
        {
            SiteConfig          siteConfig     = SiteConfig.GetSiteConfig();
            ILoggingDataService loggingService = LoggingDataServiceFactory.GetService(SiteConfig.GetLogPathFromCurrentContext());
            IBlogDataService    dataService    = BlogDataServiceFactory.GetService(SiteConfig.GetContentPathFromCurrentContext(), loggingService);


            string referrer = context.Request.UrlReferrer != null?context.Request.UrlReferrer.AbsoluteUri:"";

            if (ReferralBlackList.IsBlockedReferrer(referrer))
            {
                if (siteConfig.EnableReferralUrlBlackList404s)
                {
                    context.Response.StatusCode = 404;
                    context.Response.End();
                    return;
                }
            }
            else
            {
                loggingService.AddReferral(
                    new LogDataItem(
                        context.Request.RawUrl,
                        referrer,
                        context.Request.UserAgent,
                        context.Request.UserHostName));
            }
            context.Response.ContentType = "application/x-cdf";

            //Create the XmlWriter around the Response
            XmlTextWriter xw = new XmlTextWriter(context.Response.Output);

            xw.Formatting = Formatting.Indented;

            EntryCollection entriesAll = dataService.GetEntriesForDay(
                DateTime.Now.ToUniversalTime(), new Util.UTCTimeZone(),
                null, siteConfig.RssDayCount, siteConfig.RssMainEntryCount, null);
            EntryCollection entries = new EntryCollection();

            foreach (Entry e in entriesAll)
            {
                if (e.IsPublic == true)
                {
                    entries.Add(e);
                }
            }
            entries.Sort(new EntrySorter());

            //Write out the boilerplate CDF
            xw.WriteStartDocument();

            xw.WriteStartElement("CHANNEL");
            xw.WriteAttributeString("HREF", SiteUtilities.GetBaseUrl(siteConfig));

            foreach (Entry current in entries)
            {
                xw.WriteAttributeString("LASTMOD",
                                        FormatLastMod(current.ModifiedLocalTime));
            }

            /*IEnumerator enumerator = entries.GetEnumerator();
             * if(enumerator.MoveNext())
             * {
             *      xw.WriteAttributeString("LASTMOD",
             *              FormatLastMod(((Entry)enumerator.Current).ModifiedLocalTime));
             * }*/
            xw.WriteAttributeString("LEVEL", "1");
            xw.WriteAttributeString("PRECACHE", "YES");

            xw.WriteElementString("TITLE", siteConfig.Title);
            xw.WriteElementString("ABSTRACT", siteConfig.Subtitle);

            foreach (Entry entry in entries)
            {
                xw.WriteStartElement("ITEM");
                xw.WriteAttributeString("HREF", SiteUtilities.GetPermaLinkUrl(siteConfig, (ITitledEntry)entry));
                xw.WriteAttributeString("LASTMOD", FormatLastMod(entry.ModifiedLocalTime));
                xw.WriteAttributeString("LEVEL", "1");
                xw.WriteAttributeString("PRECACHE", "YES");

                xw.WriteElementString("TITLE", entry.Title);

                if (entry.Description != null && entry.Description.Length > 0)
                {
                    xw.WriteElementString("ABSTRACT", entry.Description);
                }

                xw.WriteEndElement();
            }

            xw.WriteEndElement(); //channel
            xw.WriteEndDocument();
        }