view PostWriterWebService blog/PostWriterWebService.asmx @ 281:6382e35625c5

Cambiandos espacios por tabuladores, etc.
author juanjose.montesdeocaarbos
date Mon, 26 Dec 2011 13:48:21 -0300
parents 4895116b8232
children
line wrap: on
line source

<%@ WebService Language="C#" Class="PostWriterWebService" %>

using System;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Telerik.Blogs;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class PostWriterWebService  : System.Web.Services.WebService {

  [WebMethod]
  public void WriteBlogPost(string securityToken, string blogName, string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate)
  {
    checkSecurityToken(securityToken);
    writePost(blogName, postTitle, postHtmlContent, postAuthor, setPublicationDate);
  }
  
  private void checkSecurityToken(string securityToken)
  {
    if (string.IsNullOrEmpty(securityToken)) throw new ArgumentNullException("securityToken");

    var now = DateTime.UtcNow;
    now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, 0);

    var masterKey = System.Configuration.ConfigurationManager.AppSettings["BlogWriterMasterKey"];
    if (string.IsNullOrEmpty(masterKey))
      throw new Exception("Master key not found");

    var token1 = buildToken(masterKey, now.AddMinutes(-1));
    var token2 = buildToken(masterKey, now);
    var token3 = buildToken(masterKey, now.AddMinutes(1));

    if (!securityToken.Equals(token1) && !securityToken.Equals(token2) && !securityToken.Equals(token3))
      throw new Exception("Invalid security token");
  }

  private string buildToken(string key, DateTime time)
  {
    var aux = key + time.ToString(CultureInfo.InvariantCulture);

    var enc = System.Text.Encoding.ASCII.GetEncoder();

    var data = new byte[aux.Length];
    enc.GetBytes(aux.ToCharArray(), 0, aux.Length, data, 0, true);

    var md5 = new MD5CryptoServiceProvider();
    var result = md5.ComputeHash(data);

    return BitConverter.ToString(result).Replace("-", "").ToLower();
  }

  private void writePost(string blogName, string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate)
  {
    if (string.IsNullOrEmpty(blogName)) throw new ArgumentNullException("blogName");
    if (string.IsNullOrEmpty(postTitle)) throw new ArgumentNullException("postTitle");
    if (string.IsNullOrEmpty(postHtmlContent)) throw new ArgumentNullException("postHtmlContent");
    if (string.IsNullOrEmpty(postAuthor)) throw new ArgumentNullException("postAuthor");

    try
    {
      var manager = new BlogManager();

      var blogs = manager.GetBlogs();
      var blog = (blogs.Cast<IBlog>()).SingleOrDefault(b => b.Name.Equals(blogName));
      
      if (blog == null)
        throw new Exception(string.Format("Blog '{0}' no encontrado", blogName));

      var postContent = manager.Content.CreateContent("text/html");

      postContent.ParentID = blog.ID;
      postContent.Content = postHtmlContent;
      postContent.SetMetaData("Author", postAuthor);
      postContent.SetMetaData("Title", postTitle);
      if (setPublicationDate)
        postContent.SetMetaData("Publication_Date", DateTime.Now);

      manager.Content.SaveContent(postContent);
    }
    catch (Exception e)
    {
      throw new Exception("Ocurrio un error creando el post", e);
    }
  }
}