PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/SendPings.cs

#
C# | 113 lines | 60 code | 18 blank | 35 comment | 9 complexity | abcfc11a8a0e093748ca71dd056ccf0e MD5 | raw file
  1#region using
  2
  3using System;
  4using System.Threading;
  5
  6using BlogEngine.Core;
  7using BlogEngine.Core.Ping;
  8using BlogEngine.Core.Web.Controls;
  9using BlogEngine.Core.Web.Extensions;
 10using System.Collections.Specialized;
 11using BlogEngine.Core.Providers;
 12
 13#endregion
 14
 15/// <summary>
 16/// Pings all the ping services specified on the 
 17///     PingServices admin page and send track- and pingbacks
 18/// </summary>
 19[Extension("Pings all the ping services specified on the PingServices admin page and send track- and pingbacks", "1.3",
 20    "BlogEngine.NET")]
 21public class SendPings
 22{
 23    #region Constructors and Destructors
 24
 25    /// <summary>
 26    /// Initializes static members of the <see cref="SendPings"/> class. 
 27    ///     Hooks up an event handler to the Post.Saved event.
 28    /// </summary>
 29    static SendPings()
 30    {
 31        Post.Saved += PostSaved;
 32        Page.Saved += PostSaved;
 33    }
 34
 35    #endregion
 36
 37    #region Methods
 38
 39    /// <summary>
 40    /// Executes the pings from the new thread.
 41    /// </summary>
 42    /// <param name="item">
 43    /// The publishable item.
 44    /// </param>
 45    /// <param name="itemUrl">
 46    /// The item Url.
 47    /// </param>
 48    private static void Ping(IPublishable item, Uri itemUrl)
 49    {
 50        try
 51        {
 52            Thread.Sleep(2000);
 53
 54            // Ping the specified ping services.
 55            PingService.Send(itemUrl);
 56
 57            // Send trackbacks and pingbacks.
 58            if (!BlogSettings.Instance.EnableTrackBackSend && !BlogSettings.Instance.EnablePingBackSend)
 59            {
 60                return;
 61            }
 62
 63            if (item.Content.ToUpperInvariant().Contains("\"HTTP"))
 64            {
 65                Manager.Send(item, itemUrl);
 66            }
 67        }
 68        catch (Exception)
 69        {
 70            // We need to catch this exception so the application doesn't get killed.
 71        }
 72    }
 73
 74    /// <summary>
 75    /// Handles the Saved event of the Post control.
 76    /// Sends the pings in a new thread.
 77    ///     <remarks>
 78    /// It opens a new thread and executes the pings from there,
 79    ///         because it takes some time to complete.
 80    ///     </remarks>
 81    /// </summary>
 82    /// <param name="sender">The source of the event.</param>
 83    /// <param name="e">The <see cref="BlogEngine.Core.SavedEventArgs"/> instance containing the event data.</param>
 84    private static void PostSaved(object sender, SavedEventArgs e)
 85    {
 86        if (!ExtensionManager.ExtensionEnabled("SendPings"))
 87            return;
 88
 89        if (e.Action == SaveAction.None || e.Action == SaveAction.Delete)
 90            return;
 91
 92        var item = (IPublishable)sender;
 93        if (!item.IsVisibleToPublic)
 94            return;
 95        
 96        var url = item.AbsoluteLink;
 97
 98        // Need blogSettings to pass to Ping since the current blog instance won't
 99        // be detectable once in a BG thread.
100        Guid blogId = Blog.CurrentInstance.Id;
101        ThreadPool.QueueUserWorkItem(state =>
102            {
103                // because HttpContext is not available within this BG thread
104                // needed to determine the current blog instance,
105                // set override value here.
106                Blog.InstanceIdOverride = blogId;
107
108                Ping(item, url);
109            });
110    }
111
112    #endregion
113}