diff Agendas/trunk/src/Agendas.Blog/Impl/PostWriterWebServiceAdapter.cs @ 99:3027c64344bd

agregado de llamadas a web service para crear posts en el blog de la comunidad agregado de tests sobre el publicador de blogs agregado de tests sobre el adapter del web service
author jorge.rowies
date Sat, 04 Jun 2011 22:33:05 -0300
parents
children 1d820f17fc75
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Blog/Impl/PostWriterWebServiceAdapter.cs	Sat Jun 04 22:33:05 2011 -0300
@@ -0,0 +1,60 @@
+using System;
+using System.Globalization;
+using System.Security.Cryptography;
+using System.ServiceModel;
+using Agendas.Blog.Exceptions;
+using Agendas.Blog.PortalSitefinity;
+
+namespace Agendas.Blog.Impl
+{
+  public class PostWriterWebServiceAdapter : IPostWriterWebService
+  {
+    private readonly PostWriterWebServiceSoapClient _service;
+
+    public PostWriterWebServiceAdapter()
+    {
+      var postWriterUrl = System.Configuration.ConfigurationManager.AppSettings["PublicadorBlog.PostWriterServiceUrl"];
+      if (string.IsNullOrEmpty(postWriterUrl))
+        throw new PostWriterServiceUrlNotFoundException();
+
+      _service = new PortalSitefinity.PostWriterWebServiceSoapClient(new BasicHttpBinding(), new EndpointAddress(postWriterUrl));
+    }
+
+    private static 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();
+    }
+
+    public void WriteBlogPost(string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate)
+    {
+      var securityToken = getSecurityToken();
+      var blogName = System.Configuration.ConfigurationManager.AppSettings["PublicadorBlog.BlogName"];
+      if (string.IsNullOrEmpty(blogName))
+        throw new BlogNameNotFoundException();
+
+      _service.WriteBlogPost(securityToken, blogName, postTitle, postHtmlContent, postAuthor, setPublicationDate);
+    }
+
+    private static string getSecurityToken()
+    {
+      var masterKey = System.Configuration.ConfigurationManager.AppSettings["PublicadorBlog.BlogWriterMasterKey"];
+      if (string.IsNullOrEmpty(masterKey))
+        throw new BlogWriterMasterKeyNotFoundException();
+
+      var now = DateTime.UtcNow;
+      now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, 0);
+
+      return buildToken(masterKey, now);
+    }
+  }
+}
\ No newline at end of file