Beispiel #1
0
        /// <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>
        /// Deletes a media resource using the information present in the HttpContext.
        /// </summary>
        /// <param name="context">The HttpContext for the incoming HTTP request.</param>
        /// <returns>True if the operation succeeds, False otherwise.</returns>
        private bool DeleteMedia(HttpContext context)
        {
            // Gets the atom request template match result to get requested collection name.
            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName);
            string memberId       = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id);

            IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString);

            return(atomPubStoreWriter.DeleteMedia(collectionName, memberId));
        }
Beispiel #3
0
        /// <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();
        }
Beispiel #4
0
        /// <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>
        /// Creates a member resource using the information present in the HttpContext.
        /// </summary>
        /// <param name="context">The HttpContext for the incoming HTTP request.</param>
        /// <returns>An AtomEntryDocument corresponding to the newly created member resource.</returns>
        private AtomEntryDocument CreateMember(HttpContext context)
        {
            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri,
                                                                             AtomPubParameterType.CollectionName);

            //Get AtomEntryDocument for request to update the member information.
            AtomEntryDocument atomEntry = new AtomEntryDocument(context.Request.InputStream);

            IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString);
            SyndicationItem     item = atomPubStoreWriter.CreateMember(collectionName, atomEntry);

            // Create atom entry document from syndication item.
            return(new AtomEntryDocument(item));
        }
Beispiel #6
0
        /// <summary>
        /// Updates a member resource using the information present in the HttpContext.
        /// </summary>
        /// <param name="context">The HttpContext for the incoming HTTP request.</param>
        /// <returns>An AtomEntryDocument corresponding to the updated member resource.</returns>
        private AtomEntryDocument UpdateMember(HttpContext context)
        {
            // Gets the atom request template match result to get requested collection name.
            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName);
            string memberId       = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id);

            IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString);

            //Get AtomEntryDocument for request to update the member information.
            AtomEntryDocument atomDocument = new AtomEntryDocument(context.Request.InputStream);

            SyndicationItem item = atomPubStoreWriter.UpdateMemberInfo(collectionName, memberId, atomDocument);

            // Get AtomEntryDocument for response.
            return(new AtomEntryDocument(item));
        }
Beispiel #7
0
        /// <summary>
        /// Updates a media resource using the information present in the HttpContext.
        /// </summary>
        /// <param name="context">The HttpContext for the incoming HTTP request.</param>
        /// <returns>An AtomEntryDocument corresponding to the updated media resource.</returns>
        private AtomEntryDocument UpdateMedia(HttpContext context)
        {
            // Gets the atom request template match result to get requested collection name.
            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName);
            string memberId       = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id);

            IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString);

            // Get byte array to update media.
            BinaryReader reader = new BinaryReader(context.Request.InputStream);

            byte[] media = new byte[context.Request.InputStream.Length];
            reader.Read(media, 0, media.Length);

            SyndicationItem item = atomPubStoreWriter.UpdateMedia(collectionName, memberId, context.Request.ContentType, media);

            // Get AtomEntryDocument for response.
            return(new AtomEntryDocument(item));
        }
        /// <summary>
        /// Creates a media resource using the information present in the HttpContext.
        /// </summary>
        /// <param name="context">The HttpContext for the incoming HTTP request.</param>
        /// <returns>An AtomEntryDocument corresponding to the newly created media resource.</returns>
        private AtomEntryDocument CreateMedia(HttpContext context)
        {
            SyndicationItem item = null;

            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context,
                                                                             base.BaseUri,
                                                                             AtomPubParameterType.CollectionName);

            // Get byte array to update media.
            BinaryReader reader = new BinaryReader(context.Request.InputStream);

            byte[] media = new byte[context.Request.InputStream.Length];
            reader.Read(media, 0, media.Length);

            string fileExtention = string.Empty;

            if (context.Request.Headers.AllKeys.Contains("Content-Disposition"))
            {
                fileExtention = AtomPubHelper.GetFileExtentionFromContentDisposition(
                    context.Request.Headers["Content-Disposition"]);
            }

            IAtomPubStoreWriter swordStoreWriter = AtomPubStoreFactory.GetSwordStoreWriter();

            item = swordStoreWriter.CreateMedia(collectionName, context.Request.ContentType, media, fileExtention);

            AtomEntryDocument atomEntryDocument = null;

            if (null != item)
            {
                // Add <sword:treatment>Successfully created a {Collection Name}</sword:treatment> element.
                SwordPostProcessor.AddTreatmentElement(collectionName, item);

                // Create atom entry document from syndication item.
                atomEntryDocument = new AtomEntryDocument(item);
            }

            return(atomEntryDocument);
        }
        /// <summary>
        /// Creates a media resource using the information present in the HttpContext.
        /// </summary>
        /// <param name="context">The HttpContext for the incoming HTTP request.</param>
        /// <returns>An AtomEntryDocument corresponding to the newly created media resource.</returns>
        private AtomEntryDocument CreateMedia(HttpContext context)
        {
            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri,
                                                                             AtomPubParameterType.CollectionName);

            string fileExtention = string.Empty;

            if (context.Request.Headers.AllKeys.Contains("Content-Disposition"))
            {
                fileExtention = AtomPubHelper.GetFileExtentionFromContentDisposition(
                    context.Request.Headers["Content-Disposition"]);
            }
            // Get byte array to update media.
            BinaryReader reader = new BinaryReader(context.Request.InputStream);

            byte[] media = new byte[context.Request.InputStream.Length];
            reader.Read(media, 0, media.Length);

            IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString);
            SyndicationItem     item = atomPubStoreWriter.CreateMedia(collectionName, context.Request.ContentType, media, fileExtention);

            // Create atom entry document from syndication item.
            return(new AtomEntryDocument(item));
        }
Beispiel #10
0
        /// <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));
        }