Esempio n. 1
0
        private StreamData GenerateStreamData(EntityInstance entityInstance, XmlBaseAnnotation baseAddressAnnotation, QueryProperty streamProperty)
        {
            string editLinkValue;
            string contentType;
            string etag;
            if (streamProperty.Name == AstoriaQueryStreamType.DefaultStreamPropertyName)
            {
                editLinkValue = entityInstance.StreamEditLink;
                contentType = entityInstance.StreamContentType;
                etag = entityInstance.StreamETag;
            }
            else
            {
                var namedStream = entityInstance.Properties.OfType<NamedStreamInstance>().SingleOrDefault(p => p.Name == streamProperty.Name);
                ExceptionUtilities.CheckObjectNotNull(namedStream, "Could not find name stream '{0}' in entity payload", streamProperty.Name);
                editLinkValue = namedStream.EditLink;
                contentType = namedStream.EditLinkContentType;
                etag = namedStream.ETag;
            }

            if (contentType == null)
            {
                // TODO: add atom/json?
                contentType = this.Random.ChooseFrom(new[] { MimeTypes.TextPlain, MimeTypes.ApplicationOctetStream, MimeTypes.ApplicationHttp });
            }

            var streamData = new StreamData()
            {
                ContentType = contentType,
                ETag = etag,
                Content = this.Random.NextBytes(this.Random.Next(100)),
            };

            if (editLinkValue != null)
            {
                var editLink = new Uri(editLinkValue, UriKind.RelativeOrAbsolute);
                if (!editLink.IsAbsoluteUri)
                {
                    ExceptionUtilities.CheckObjectNotNull(baseAddressAnnotation, "Cannot construct absolute uri for edit link because xml:base annotation was missing");
                    editLink = new Uri(new Uri(baseAddressAnnotation.Value), editLinkValue);
                }

                streamData.EditLink = editLink.AbsoluteUri;
            }

            return streamData;
        }
        /// <summary>
        /// Gets the absolute uri for the given link href value
        /// </summary>
        /// <param name="baseUri">The context base uri</param>
        /// <param name="xmlBase">The base uri annotation from the payload</param>
        /// <param name="hrefValue">The href value for the link</param>
        /// <returns>The absolute uri for the link, or a relative uri if it cannot be determined</returns>
        internal static Uri GetAbsoluteUriForLink(Uri baseUri, XmlBaseAnnotation xmlBase, string hrefValue)
        {
            var absoluteUri = new Uri(hrefValue, UriKind.RelativeOrAbsolute);
            if (!absoluteUri.IsAbsoluteUri)
            {
                string root;
                if (xmlBase != null)
                {
                    root = xmlBase.Value;
                }
                else if (baseUri != null)
                {
                    ExceptionUtilities.Assert(baseUri.IsAbsoluteUri, "baseUri was not an absolute uri. It was: '{0}'", baseUri.OriginalString);
                    root = baseUri.AbsoluteUri;
                }
                else
                {
                    // TODO: need to be able to read xml:base from feed, cannot currently.
                    // just return a relative uri for now
                    return absoluteUri;
                }

                absoluteUri = new Uri(UriHelpers.ConcatenateUriSegments(root, hrefValue));
            }

            return absoluteUri;
        }