PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/HttpHandlers/RatingHandler.cs

#
C# | 133 lines | 78 code | 19 blank | 36 comment | 14 complexity | 508c12897e51364725c6a7cd5445bb36 MD5 | raw file
  1namespace BlogEngine.Core.Web.HttpHandlers
  2{
  3    using System;
  4    using System.Web;
  5
  6    /// <summary>
  7    /// Receives and records all ratings comming in from the rating control.
  8    /// </summary>
  9    public class RatingHandler : IHttpHandler
 10    {
 11        #region Properties
 12
 13        /// <summary>
 14        ///     Gets a value indicating whether another request can use the <see cref = "T:System.Web.IHttpHandler"></see> instance.
 15        /// </summary>
 16        /// <value></value>
 17        /// <returns>true if the <see cref = "T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
 18        public bool IsReusable
 19        {
 20            get
 21            {
 22                return false;
 23            }
 24        }
 25
 26        #endregion
 27
 28        #region Implemented Interfaces
 29
 30        #region IHttpHandler
 31
 32        /// <summary>
 33        /// Enables processing of HTTP Web requests by a custom HttpHandler that 
 34        ///     implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
 35        /// </summary>
 36        /// <param name="context">
 37        /// An <see cref="T:System.Web.HttpContext"></see> 
 38        ///     object that provides references to the intrinsic server objects 
 39        ///     (for example, Request, Response, Session, and Server) used to service HTTP requests.
 40        /// </param>
 41        public void ProcessRequest(HttpContext context)
 42        {
 43
 44            if (!BlogSettings.Instance.EnableRating || !Security.IsAuthorizedTo(Rights.SubmitRatingsOnPosts))
 45            {
 46                throw new System.Security.SecurityException();
 47            }
 48            else
 49            {
 50                var rating = context.Request.QueryString["rating"];
 51                int rate;
 52                if (rating != null && int.TryParse(rating, out rate))
 53                {
 54                    var id = context.Request.QueryString["id"];
 55                    if (id != null && id.Length == 36 && rate > 0 && rate < 6)
 56                    {
 57                        try
 58                        {
 59                            if (HasRated(id))
 60                            {
 61                                context.Response.Write(string.Format("{0}HASRATED", rate));
 62                                context.Response.End();
 63                            }
 64                            else
 65                            {
 66                                var post = Post.GetPost(new Guid(id));
 67                                post.Rate(rate);
 68
 69                                SetCookie(id, context);
 70                                context.Response.Write(string.Format("{0}OK", rate));
 71                                context.Response.End();
 72                            }
 73
 74                        }
 75                        catch(Exception)
 76                        {
 77                           
 78                            // Something failed.
 79                        }
 80                    }
 81                }
 82                context.Response.Write("FAIL");
 83            }
 84        }
 85
 86        #endregion
 87
 88        #endregion
 89
 90        #region Methods
 91
 92        /// <summary>
 93        /// Determines whether the specified post id has rated.
 94        /// </summary>
 95        /// <param name="postId">The post id.</param>
 96        /// <returns>
 97        ///     <c>true</c> if the specified post id has rated; otherwise, <c>false</c>.
 98        /// </returns>
 99        private static bool HasRated(string postId)
100        {
101            // This seems like a bad idea. Someone without cookies disabled
102            // they could repeatedly rate a post. Also, if someone rates
103            // a lot of posts, it's going to continue to increase the size
104            // of their rating cookie, increasing bandwidth.
105            //
106            // -rossisdead 10/28/2010
107            var ratingCookie = HttpContext.Current.Request.Cookies["rating"];
108
109            if (ratingCookie != null)
110            {
111                return ratingCookie.Value.Contains(postId);
112            }
113
114            return false;
115        }
116
117        /// <summary>
118        /// Sets the cookie.
119        /// </summary>
120        /// <param name="id">The cookie id.</param>
121        /// <param name="context">The context.</param>
122        private static void SetCookie(string id, HttpContext context)
123        {
124            var cookie = context.Request.Cookies["rating"] ?? new HttpCookie("rating");
125
126            cookie.Expires = DateTime.Now.AddYears(2);
127            cookie.Value += id;
128            context.Response.Cookies.Add(cookie);
129        }
130
131        #endregion
132    }
133}