comparison 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
comparison
equal deleted inserted replaced
98:4895116b8232 99:3027c64344bd
1 using System;
2 using System.Globalization;
3 using System.Security.Cryptography;
4 using System.ServiceModel;
5 using Agendas.Blog.Exceptions;
6 using Agendas.Blog.PortalSitefinity;
7
8 namespace Agendas.Blog.Impl
9 {
10 public class PostWriterWebServiceAdapter : IPostWriterWebService
11 {
12 private readonly PostWriterWebServiceSoapClient _service;
13
14 public PostWriterWebServiceAdapter()
15 {
16 var postWriterUrl = System.Configuration.ConfigurationManager.AppSettings["PublicadorBlog.PostWriterServiceUrl"];
17 if (string.IsNullOrEmpty(postWriterUrl))
18 throw new PostWriterServiceUrlNotFoundException();
19
20 _service = new PortalSitefinity.PostWriterWebServiceSoapClient(new BasicHttpBinding(), new EndpointAddress(postWriterUrl));
21 }
22
23 private static string buildToken(string key, DateTime time)
24 {
25 var aux = key + time.ToString(CultureInfo.InvariantCulture);
26
27 var enc = System.Text.Encoding.ASCII.GetEncoder();
28
29 var data = new byte[aux.Length];
30 enc.GetBytes(aux.ToCharArray(), 0, aux.Length, data, 0, true);
31
32 var md5 = new MD5CryptoServiceProvider();
33 var result = md5.ComputeHash(data);
34
35 return BitConverter.ToString(result).Replace("-", "").ToLower();
36 }
37
38 public void WriteBlogPost(string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate)
39 {
40 var securityToken = getSecurityToken();
41 var blogName = System.Configuration.ConfigurationManager.AppSettings["PublicadorBlog.BlogName"];
42 if (string.IsNullOrEmpty(blogName))
43 throw new BlogNameNotFoundException();
44
45 _service.WriteBlogPost(securityToken, blogName, postTitle, postHtmlContent, postAuthor, setPublicationDate);
46 }
47
48 private static string getSecurityToken()
49 {
50 var masterKey = System.Configuration.ConfigurationManager.AppSettings["PublicadorBlog.BlogWriterMasterKey"];
51 if (string.IsNullOrEmpty(masterKey))
52 throw new BlogWriterMasterKeyNotFoundException();
53
54 var now = DateTime.UtcNow;
55 now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, 0);
56
57 return buildToken(masterKey, now);
58 }
59 }
60 }