public SyndicationFeedResult(SyndicationFeed feed) { this.ContentType = "application/rss+xml"; using (StringWriter stringWriter = new Utf8StringWriter()) { using (XmlWriter writer = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, ConformanceLevel = ConformanceLevel.Document, CheckCharacters = true, NamespaceHandling = NamespaceHandling.OmitDuplicates, OmitXmlDeclaration = false, Encoding = Encoding.UTF8, })) { Rss20FeedFormatter formatter = feed.GetRss20Formatter(serializeExtensionsAsAtom: false); formatter.PreserveAttributeExtensions = true; formatter.PreserveAttributeExtensions = true; formatter.WriteTo(writer); } this.Content = stringWriter.ToString(); } }
public string SerializeFeed(SyndicationFeed feed, SyndicationFormat format) { using (var stream = new MemoryStream()) { var writer = XmlWriter.Create(stream); if (format == SyndicationFormat.Atom) { feed.GetAtom10Formatter().WriteTo(writer); } else { feed.GetRss20Formatter().WriteTo(writer); } writer.Flush(); stream.Position = 0; var reader = new StreamReader(stream, Encoding.UTF8); return reader.ReadToEnd(); } }
public HttpResponseMessage GetPosts(string id, HttpRequestMessage request, int pageIndex = 1, int pageSize = 10) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); try { IPostsService postsService = ObjectFactory.GetInstance<IPostsService>(); var posts = postsService.GetAllFromBlog(String.Format("blogs/{0}", id), pageIndex, pageSize); if (posts != null) { if (this.ClientAcceptsMediaType("text/html", request)) { var postsHtml = posts.GeneratePostsHtml(); response.Content = new ObjectContent<string>(postsHtml, "text/html"); } else { SyndicationFeed blogPostsFeed = new SyndicationFeed { Title = new TextSyndicationContent( String.Format("Blog {0} posts", id)), LastUpdatedTime = new DateTimeOffset(DateTime.Now) }; blogPostsFeed.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); List<SyndicationItem> itemList = new List<SyndicationItem>(); blogPostsFeed.Items = itemList; foreach (var post in posts) { SyndicationItem item = new SyndicationItem { Id = post.Id, LastUpdatedTime = post.updated, PublishDate = post.published, Title = new TextSyndicationContent(post.title) }; item.Links.Add(SyndicationLink.CreateSelfLink(new Uri(String.Format("{0}/{1}/{2}", this.serviceURI, post.blogId, post.Id)))); item.Links.Add(SyndicationLink.CreateAlternateLink(request.RequestUri, "text/html")); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}", this.serviceURI, post.blogId)), "service.blog", "Parent blog", "application/atom+xml;type=feed", 0)); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}/{2}", this.serviceURI, post.blogId, post.Id)), "service.edit", "Edit post", "application/atom+xml;type=feed", 0)); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}/{2}/{3}", this.serviceURI, post.blogId, post.Id, "service.comments")), "comments", "Post comments", "application/atom+xml;type=feed", 0)); var pagingLinks = this.BuildPagingLinks(postsService.Count(), pageIndex, pageSize, request.RequestUri); foreach (var link in pagingLinks) { item.Links.Add(link); } item.Authors.Add(new SyndicationPerson(string.Empty, post.author, string.Empty)); item.Content = SyndicationContent.CreatePlaintextContent(post.content); itemList.Add(item); } SyndicationFeedFormatter formatter = null; if (this.ClientAcceptsMediaType("application/atom+xml", request)) { formatter = blogPostsFeed.GetAtom10Formatter(); } else { if (this.ClientAcceptsMediaType("application/rss+xml", request)) { formatter = blogPostsFeed.GetRss20Formatter(); } } response.Content = new ObjectContent(typeof(SyndicationFeedFormatter), formatter); } } else { response.StatusCode = HttpStatusCode.NoContent; } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return response; }
public HttpResponseMessage GetPost(string blogId, string id, HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); try { IPostsService postsService = ObjectFactory.GetInstance<IPostsService>(); var post = postsService.Get(String.Format("posts/{0}", id)); var etag = request.Headers.IfNoneMatch.FirstOrDefault(); if (etag != null && etag.Tag == post.etag) { response.StatusCode = HttpStatusCode.NotModified; } else { if (post != null) { if (this.ClientAcceptsMediaType("text/html", request)) { response.Content = new ObjectContent<string>(post.ToHtml(), "text/html"); } else { SyndicationFeed postFeed = new SyndicationFeed { Title = new TextSyndicationContent("Single Post"), LastUpdatedTime = new DateTimeOffset(DateTime.Now) }; postFeed.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); SyndicationItem item = new SyndicationItem(); List<SyndicationItem> itemList = new List<SyndicationItem> {item}; postFeed.Items = itemList; item.Id = post.Id; item.LastUpdatedTime = post.updated; item.PublishDate = post.published; item.Title = new TextSyndicationContent(post.title); item.Content = new TextSyndicationContent(post.content); item.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); item.Links.Add(SyndicationLink.CreateAlternateLink(request.RequestUri, "text/html")); item.Links.Add(new SyndicationLink(request.RequestUri, "service.edit", "Edit Post", "application/atom+xml;type=feed", 0)); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/blogs/{1}/{2}/{3}", this.serviceURI, blogId, post.Id, "service.comments")), "comments", "Post comments", "application/atom+xml;type=feed", 0)); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/blogs/{1}", this.serviceURI, blogId)), "service.blog", "Parent blog", "application/atom+xml;type=feed", 0)); item.Authors.Add(new SyndicationPerson(string.Empty, post.author, string.Empty)); SyndicationFeedFormatter formatter = null; if (this.ClientAcceptsMediaType("application/atom+xml", request)) { formatter = postFeed.GetAtom10Formatter(); } else { if (this.ClientAcceptsMediaType("application/rss+xml", request)) { formatter = postFeed.GetRss20Formatter(); } } response.Content = new ObjectContent(typeof(SyndicationFeedFormatter), formatter); } } else { response.StatusCode = HttpStatusCode.NotFound; } } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return response; }
public HttpResponseMessage GetBlogTagCloud(string id, HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); try { IBlogsService blogsService = ObjectFactory.GetInstance<IBlogsService>(); var blog = blogsService.Get(String.Format("blogs/{0}", id)); var tagCloud = blogsService.GetTagCloud(String.Format("blogs/{0}", id)); if (blog != null) { SyndicationFeed blogFeed = new SyndicationFeed { Title = new TextSyndicationContent("Blog tag cloud"), LastUpdatedTime = new DateTimeOffset(DateTime.Now) }; blogFeed.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); SyndicationItem item = new SyndicationItem(); List<SyndicationItem> itemList = new List<SyndicationItem> {item}; blogFeed.Items = itemList; item.Id = blog.Id; item.LastUpdatedTime = blog.updated; item.PublishDate = blog.published; item.Title = new TextSyndicationContent("Blog tag cloud"); item.Content = SyndicationContent.CreatePlaintextContent(BuildtagCloud(tagCloud)); item.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); SyndicationFeedFormatter formatter = null; if (this.ClientAcceptsMediaType("application/atom+xml", request)) { formatter = blogFeed.GetAtom10Formatter(); } else { if (this.ClientAcceptsMediaType("application/rss+xml", request)) { formatter = blogFeed.GetRss20Formatter(); } } response.Content = new ObjectContent(typeof(SyndicationFeedFormatter), formatter); } else { response.StatusCode = HttpStatusCode.NotFound; } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return response; }
public HttpResponseMessage GetBlogs(HttpRequestMessage request, int pageIndex = 1, int pageSize = 10) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); try { IBlogsService blogsService = ObjectFactory.GetInstance<IBlogsService>(); List<Blog> blogs = blogsService.GetAll(pageIndex, pageSize); if (blogs != null) { if (this.ClientAcceptsMediaType("text/html", request)) { var blogsHtml = blogs.GenerateBlogsHtml(); response.Content = new ObjectContent<string>(blogsHtml, "text/html"); } else { SyndicationFeed blogsFeed = new SyndicationFeed { Title = new TextSyndicationContent("Blogs List"), LastUpdatedTime = new DateTimeOffset(DateTime.Now) }; blogsFeed.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); List<SyndicationItem> itemList = new List<SyndicationItem>(); blogsFeed.Items = itemList; foreach (var blog in blogs) { SyndicationItem item = new SyndicationItem { Id = blog.Id, LastUpdatedTime = blog.updated, PublishDate = blog.published, Title = new TextSyndicationContent(blog.name), Summary = new TextSyndicationContent(blog.description) }; item.Links.Add(SyndicationLink.CreateSelfLink(new Uri(String.Format("{0}/{1}", this.serviceURI, blog.Id)))); item.Links.Add(SyndicationLink.CreateAlternateLink(request.RequestUri, "text/html")); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}", this.serviceURI, blog.Id)), "service.edit", "Edit Blog", "application/atom+xml;type=feed", 0)); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}/posts", this.serviceURI, blog.Id)), "service.posts", "Blog posts", "application/atom+xml;type=feed", 0)); var pagingLinks = this.BuildPagingLinks(blogsService.Count(), pageIndex, pageSize, request.RequestUri); foreach (var link in pagingLinks) { item.Links.Add(link); } item.Authors.Add(new SyndicationPerson(string.Empty, blog.author, string.Empty)); itemList.Add(item); } SyndicationFeedFormatter formatter = null; if (this.ClientAcceptsMediaType("application/atom+xml", request)) { formatter = blogsFeed.GetAtom10Formatter(); } else { if (this.ClientAcceptsMediaType("application/rss+xml", request)) { formatter = blogsFeed.GetRss20Formatter(); } } response.Content = new ObjectContent(typeof(SyndicationFeedFormatter), formatter); } } else { response.StatusCode = HttpStatusCode.NoContent; } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return response; }