changeset 98:4895116b8232

subida del web service para escribir posts en el blog del portal
author jorge.rowies
date Sat, 04 Jun 2011 14:10:21 -0300
parents 24e9488ac152
children 3027c64344bd
files PostWriterWebService blog/PostWriterWebService.asmx
diffstat 1 files changed, 93 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PostWriterWebService blog/PostWriterWebService.asmx	Sat Jun 04 14:10:21 2011 -0300
@@ -0,0 +1,93 @@
+<%@ 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);
+    }
+  }
+}
\ No newline at end of file