annotate PostWriterWebService blog/PostWriterWebService.asmx @ 262:b87a4875b2b6

ControllerTests: Refactoring.
author juanjose.montesdeocaarbos
date Sat, 22 Oct 2011 14:08:30 -0300
parents 4895116b8232
children
rev   line source
98
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
1 <%@ WebService Language="C#" Class="PostWriterWebService" %>
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
2
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
3 using System;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
4 using System.Configuration;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
5 using System.Globalization;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
6 using System.Linq;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
7 using System.Security.Cryptography;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
8 using System.Web;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
9 using System.Web.Services;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
10 using System.Web.Services.Protocols;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
11 using Telerik.Blogs;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
12
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
13 [WebService(Namespace = "http://tempuri.org/")]
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
14 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
15 // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
16 // [System.Web.Script.Services.ScriptService]
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
17 public class PostWriterWebService : System.Web.Services.WebService {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
18
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
19 [WebMethod]
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
20 public void WriteBlogPost(string securityToken, string blogName, string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
21 {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
22 checkSecurityToken(securityToken);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
23 writePost(blogName, postTitle, postHtmlContent, postAuthor, setPublicationDate);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
24 }
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
25
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
26 private void checkSecurityToken(string securityToken)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
27 {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
28 if (string.IsNullOrEmpty(securityToken)) throw new ArgumentNullException("securityToken");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
29
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
30 var now = DateTime.UtcNow;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
31 now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, 0);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
32
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
33 var masterKey = System.Configuration.ConfigurationManager.AppSettings["BlogWriterMasterKey"];
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
34 if (string.IsNullOrEmpty(masterKey))
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
35 throw new Exception("Master key not found");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
36
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
37 var token1 = buildToken(masterKey, now.AddMinutes(-1));
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
38 var token2 = buildToken(masterKey, now);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
39 var token3 = buildToken(masterKey, now.AddMinutes(1));
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
40
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
41 if (!securityToken.Equals(token1) && !securityToken.Equals(token2) && !securityToken.Equals(token3))
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
42 throw new Exception("Invalid security token");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
43 }
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
44
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
45 private string buildToken(string key, DateTime time)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
46 {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
47 var aux = key + time.ToString(CultureInfo.InvariantCulture);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
48
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
49 var enc = System.Text.Encoding.ASCII.GetEncoder();
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
50
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
51 var data = new byte[aux.Length];
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
52 enc.GetBytes(aux.ToCharArray(), 0, aux.Length, data, 0, true);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
53
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
54 var md5 = new MD5CryptoServiceProvider();
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
55 var result = md5.ComputeHash(data);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
56
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
57 return BitConverter.ToString(result).Replace("-", "").ToLower();
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
58 }
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
59
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
60 private void writePost(string blogName, string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
61 {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
62 if (string.IsNullOrEmpty(blogName)) throw new ArgumentNullException("blogName");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
63 if (string.IsNullOrEmpty(postTitle)) throw new ArgumentNullException("postTitle");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
64 if (string.IsNullOrEmpty(postHtmlContent)) throw new ArgumentNullException("postHtmlContent");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
65 if (string.IsNullOrEmpty(postAuthor)) throw new ArgumentNullException("postAuthor");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
66
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
67 try
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
68 {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
69 var manager = new BlogManager();
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
70
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
71 var blogs = manager.GetBlogs();
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
72 var blog = (blogs.Cast<IBlog>()).SingleOrDefault(b => b.Name.Equals(blogName));
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
73
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
74 if (blog == null)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
75 throw new Exception(string.Format("Blog '{0}' no encontrado", blogName));
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
76
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
77 var postContent = manager.Content.CreateContent("text/html");
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
78
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
79 postContent.ParentID = blog.ID;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
80 postContent.Content = postHtmlContent;
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
81 postContent.SetMetaData("Author", postAuthor);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
82 postContent.SetMetaData("Title", postTitle);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
83 if (setPublicationDate)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
84 postContent.SetMetaData("Publication_Date", DateTime.Now);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
85
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
86 manager.Content.SaveContent(postContent);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
87 }
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
88 catch (Exception e)
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
89 {
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
90 throw new Exception("Ocurrio un error creando el post", e);
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
91 }
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
92 }
4895116b8232 subida del web service para escribir posts en el blog del portal
jorge.rowies
parents:
diff changeset
93 }