/// <summary> /// Gets the AtomEntryDocument from the specified collection and for the specified member resource. /// </summary> /// <param name="collectionName">The collection name.</param> /// <param name="memberResourceId">The Id of the member resource.</param> /// <returns>An AtomEntryDocument containing information for the member resource.</returns> private AtomEntryDocument GetAtomEntryDocument(string collectionName, string memberResourceId) { IAtomPubStoreReader storeReader = AtomPubStoreFactory.GetAtomPubStoreReader(base.BaseUri.OriginalString); SyndicationItem syndicationItem = storeReader.GetMember(collectionName, memberResourceId); AtomEntryDocument atomEntryDoc = new AtomEntryDocument(syndicationItem); return(atomEntryDoc); }
/// <summary> /// Gets the media resource for the specified member resource. /// </summary> /// <param name="collectionName">The collection name.</param> /// <param name="memberResourceId">The member resource Id.</param> /// <param name="content">A byte array to get media resource content.</param> private void GetMediaResource(string collectionName, string memberResourceId, out byte[] content) { Stream contentStream = new MemoryStream(); IAtomPubStoreReader storeReader = AtomPubStoreFactory.GetAtomPubStoreReader(base.BaseUri.OriginalString); storeReader.GetMedia(collectionName, memberResourceId, contentStream); contentStream.Seek(0, SeekOrigin.Begin); content = new byte[contentStream.Length]; contentStream.Read(content, 0, content.Length); contentStream.Close(); }
/// <summary> /// Gets the AtomPub Service Document for this instance of the AtomPub Service. /// </summary> /// <param name="context">HttpContext containing the request object.</param> /// <param name="baseUri">Base Uri for the specified request.</param> /// <returns>A string that contains the AtomPub Service Document.</returns> protected ServiceDocument GetServiceDocument(HttpContext context, Uri baseUri) { string[] collectionNames; // Get the list of collection names if (null != context.Cache[AtomPubConstants.CollectionTypesCacheKey]) { collectionNames = (string[])context.Cache[AtomPubConstants.CollectionTypesCacheKey]; } else { IAtomPubStoreReader storeReader = AtomPubStoreFactory.GetAtomPubStoreReader(base.BaseUri.OriginalString); collectionNames = storeReader.GetCollectionNames(); // Put the collection names in cache context.Cache.Insert(AtomPubConstants.CollectionTypesCacheKey, collectionNames, null, DateTime.UtcNow.AddMinutes(20), System.Web.Caching.Cache.NoSlidingExpiration); } ServiceDocument serviceDocument = new ServiceDocument(); List <ResourceCollectionInfo> collections = new List <ResourceCollectionInfo>(); foreach (string collectionType in collectionNames) { // Create collection Url based on resource type Uri collectionUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.Collection] .BindByPosition(baseUri, new string[] { collectionType }); // Create collection node and append to the list. string collectionTitle = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.CollectionTitle, collectionType); ResourceCollectionInfo collection = new ResourceCollectionInfo(collectionTitle, collectionUri); collection.Accepts.Add(AtomPubConstants.AtomEntryContentType); collection.Accepts.Add(AtomPubConstants.AcceptAll); collections.Add(collection); } Workspace defaultWorkspace = new Workspace(AtomPubConstants.DefaultWorkSpaceTitle, collections); serviceDocument.Workspaces.Add(defaultWorkspace); return(serviceDocument); }
/// <summary> /// Gets the Atom Feed for the specified collection. /// </summary> /// <param name="collectionName">The collection name.</param> /// <param name="pageNumber">A long value indicating the current page of the Feed Document.</param> /// <returns>A string that contains the Atom Feed.</returns> private string GetAtomFeed(string collectionName, long pageNumber) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (AtomPubConstants.FirstPageValue > pageNumber) { throw new ArgumentNullException("pageNumber"); } IAtomPubStoreReader storeReader = AtomPubStoreFactory.GetAtomPubStoreReader(base.BaseUri.OriginalString); // Count the number of resources long memberCount = storeReader.GetMembersCount(collectionName); // Count the number of pages. int pageSize = int.Parse(AtomPubConstants.FeedPageSize, CultureInfo.InvariantCulture); long numberOfPages = AtomPubConstants.FirstPageValue; if (0 < memberCount) { numberOfPages = memberCount / pageSize; if (0 != (memberCount % pageSize)) { numberOfPages++; // Increment pages if its not a perfect division. } // Find effective numberOfPages numberOfPages = AtomPubConstants.FirstPageValue + (numberOfPages - 1); // Check if page number is valid if (pageNumber > numberOfPages) { return(Properties.Resources.ATOMPUB_INVALID_PAGE_NUMBER); } } // Get the Members long skip = (pageNumber - 1) * pageSize; SyndicationFeed feed = storeReader.GetMembers(collectionName, skip, pageSize); #region Add First, Next, Prev and Last links // If number of pages are greater than FirstPageValue, then display page links. if (AtomPubConstants.FirstPageValue != numberOfPages) { // For second page, links will be as follows : // <link rel="first" // href="{BaseUri}?Page=1" /> // <link rel="previous" // href="{BaseUri}?Page=1" /> // <link rel="next" // href="{BaseUri}?Page=3" /> // <link rel="last" // href="{BaseUri}?Page=10" /> #region First NameValueCollection parameters = new NameValueCollection(); parameters.Add(AtomPubParameterType.CollectionName.ToString(), collectionName); parameters.Add(AtomPubParameterType.PageNo.ToString(), AtomPubConstants.FirstPageValue.ToString(CultureInfo.InvariantCulture)); Uri firstPageUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.CollectionWithPageNo] .BindByName(base.BaseUri, parameters); SyndicationLink firstPageLink = new SyndicationLink(); firstPageLink.RelationshipType = AtomPubConstants.First; firstPageLink.Uri = firstPageUri; feed.Links.Add(firstPageLink); #endregion #region Previous if (AtomPubConstants.FirstPageValue != pageNumber) { parameters = new NameValueCollection(); parameters.Add(AtomPubParameterType.CollectionName.ToString(), collectionName); parameters.Add(AtomPubParameterType.PageNo.ToString(), (pageNumber - 1).ToString(CultureInfo.InstalledUICulture)); Uri prevPageUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.CollectionWithPageNo] .BindByName(base.BaseUri, parameters); SyndicationLink prevPageLink = new SyndicationLink(); prevPageLink.RelationshipType = AtomPubConstants.Previous; prevPageLink.Uri = prevPageUri; feed.Links.Add(prevPageLink); } #endregion #region Next if (numberOfPages != pageNumber) { parameters = new NameValueCollection(); parameters.Add(AtomPubParameterType.CollectionName.ToString(), collectionName); parameters.Add(AtomPubParameterType.PageNo.ToString(), (pageNumber + 1).ToString(CultureInfo.InstalledUICulture)); Uri nextPageUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.CollectionWithPageNo] .BindByName(base.BaseUri, parameters); SyndicationLink nextPageLink = new SyndicationLink(); nextPageLink.RelationshipType = AtomPubConstants.Next; nextPageLink.Uri = nextPageUri; feed.Links.Add(nextPageLink); } #endregion #region Last { parameters = new NameValueCollection(); parameters.Add(AtomPubParameterType.CollectionName.ToString(), collectionName); parameters.Add(AtomPubParameterType.PageNo.ToString(), (numberOfPages).ToString(CultureInfo.InstalledUICulture)); Uri lastPageUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.CollectionWithPageNo] .BindByName(base.BaseUri, parameters); SyndicationLink lastPageLink = new SyndicationLink(); lastPageLink.RelationshipType = AtomPubConstants.Last; lastPageLink.Uri = lastPageUri; feed.Links.Add(lastPageLink); } #endregion } #endregion // Generate Feed document from Syndication Feed. return(GetFeedDocument(feed)); }