internal static AtomLinkMetadata MergeLinkMetadata(AtomLinkMetadata metadata, string relation, Uri href, string title, string mediaType)
 {
     AtomLinkMetadata metadata2 = new AtomLinkMetadata(metadata);
     string strB = metadata2.Relation;
     if (strB != null)
     {
         if (string.CompareOrdinal(relation, strB) != 0)
         {
             throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkRelationsMustMatch(relation, strB));
         }
     }
     else
     {
         metadata2.Relation = relation;
     }
     if (href != null)
     {
         Uri uri = metadata2.Href;
         if (uri != null)
         {
             if (!href.Equals(uri))
             {
                 throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkHrefsMustMatch(href, uri));
             }
         }
         else
         {
             metadata2.Href = href;
         }
     }
     if (title != null)
     {
         string str2 = metadata2.Title;
         if (str2 != null)
         {
             if (string.CompareOrdinal(title, str2) != 0)
             {
                 throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkTitlesMustMatch(title, str2));
             }
         }
         else
         {
             metadata2.Title = title;
         }
     }
     if (mediaType != null)
     {
         string str3 = metadata2.MediaType;
         if (str3 == null)
         {
             metadata2.MediaType = mediaType;
             return metadata2;
         }
         if (!HttpUtils.CompareMediaTypeNames(mediaType, str3))
         {
             throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkMediaTypesMustMatch(mediaType, str3));
         }
     }
     return metadata2;
 }
        /// <summary>
        /// Write the metadata for an OData association link; makes sure any duplicate of the link's values duplicated in metadata are equal.
        /// </summary>
        /// <param name="associationLink">The association link for which to write the metadata.</param>
        /// <param name="owningType">The <see cref="IEdmEntityType"/> instance the association link is defined on.</param>
        /// <param name="duplicatePropertyNamesChecker">The checker instance for duplicate property names.</param>
        /// <param name="projectedProperties">Set of projected properties, or null if all properties should be written.</param>
        internal void WriteAssociationLink(
            ODataAssociationLink associationLink,
            IEdmEntityType owningType,
            DuplicatePropertyNamesChecker duplicatePropertyNamesChecker,
            ProjectedPropertiesAnnotation projectedProperties)
        {
            DebugUtils.CheckNoExternalCallers();

            ValidationUtils.ValidateAssociationLinkNotNull(associationLink);
            string associationLinkName = associationLink.Name;

            if (projectedProperties.ShouldSkipProperty(associationLinkName))
            {
                return;
            }

            this.ValidateAssociationLink(associationLink, owningType);
            duplicatePropertyNamesChecker.CheckForDuplicateAssociationLinkNames(associationLink);

            AtomLinkMetadata linkMetadata       = associationLink.GetAnnotation <AtomLinkMetadata>();
            string           linkRelation       = AtomUtils.ComputeODataAssociationLinkRelation(associationLink);
            AtomLinkMetadata mergedLinkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(linkMetadata, linkRelation, associationLink.Url, associationLinkName, MimeConstants.MimeApplicationXml);

            this.atomEntryMetadataSerializer.WriteAtomLink(mergedLinkMetadata, null /* etag*/);
        }
Esempio n. 3
0
 private void WriteReadOrEditLink(Uri link, AtomLinkMetadata linkMetadata, string linkRelation)
 {
     if (link != null)
     {
         AtomLinkMetadata metadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(linkMetadata, linkRelation, link, null, null);
         this.atomEntryMetadataSerializer.WriteAtomLink(metadata, null);
     }
 }
Esempio n. 4
0
 internal static void AddLinkToFeedMetadata(AtomFeedMetadata feedMetadata, AtomLinkMetadata linkMetadata)
 {
     if (object.ReferenceEquals(feedMetadata.Links, EmptyLinksList))
     {
         feedMetadata.Links = new ReadOnlyEnumerable<AtomLinkMetadata>();
     }
     ReaderUtils.GetSourceListOfEnumerable<AtomLinkMetadata>(feedMetadata.Links, "Links").Add(linkMetadata);
 }
 internal void WriteAtomLinkAttributes(AtomLinkMetadata linkMetadata, string etag)
 {
     string href = (linkMetadata.Href == null) ? null : base.UriToUrlAttributeValue(linkMetadata.Href);
     this.WriteAtomLinkMetadataAttributes(linkMetadata.Relation, href, linkMetadata.HrefLang, linkMetadata.Title, linkMetadata.MediaType, linkMetadata.Length);
     if (etag != null)
     {
         ODataAtomWriterUtils.WriteETag(base.XmlWriter, etag);
     }
 }
        /// <summary>
        /// Writes a stream property to the ATOM payload
        /// </summary>
        /// <param name="streamProperty">The stream property to create the payload for.</param>
        /// <param name="owningType">The <see cref="IEdmEntityType"/> instance for which the stream property defined on.</param>
        /// <param name="duplicatePropertyNamesChecker">The checker instance for duplicate property names.</param>
        /// <param name="projectedProperties">Set of projected properties, or null if all properties should be written.</param>
        internal void WriteStreamProperty(
            ODataProperty streamProperty,
            IEdmEntityType owningType,
            DuplicatePropertyNamesChecker duplicatePropertyNamesChecker,
            ProjectedPropertiesAnnotation projectedProperties)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(streamProperty != null, "Stream property must not be null.");
            Debug.Assert(streamProperty.Value != null, "The media resource of the stream property must not be null.");

            WriterValidationUtils.ValidatePropertyNotNull(streamProperty);
            string propertyName = streamProperty.Name;

            if (projectedProperties.ShouldSkipProperty(propertyName))
            {
                return;
            }

            WriterValidationUtils.ValidatePropertyName(propertyName);
            duplicatePropertyNamesChecker.CheckForDuplicatePropertyNames(streamProperty);
            IEdmProperty edmProperty = WriterValidationUtils.ValidatePropertyDefined(streamProperty.Name, owningType, this.MessageWriterSettings.UndeclaredPropertyBehaviorKinds);

            WriterValidationUtils.ValidateStreamReferenceProperty(streamProperty, edmProperty, this.Version, this.WritingResponse);
            ODataStreamReferenceValue streamReferenceValue = (ODataStreamReferenceValue)streamProperty.Value;

            WriterValidationUtils.ValidateStreamReferenceValue(streamReferenceValue, false /*isDefaultStream*/);
            if (owningType != null && owningType.IsOpen && edmProperty == null)
            {
                ValidationUtils.ValidateOpenPropertyValue(streamProperty.Name, streamReferenceValue, this.MessageWriterSettings.UndeclaredPropertyBehaviorKinds);
            }

            AtomStreamReferenceMetadata streamReferenceMetadata = streamReferenceValue.GetAnnotation <AtomStreamReferenceMetadata>();
            string contentType = streamReferenceValue.ContentType;
            string linkTitle   = streamProperty.Name;

            Uri readLink = streamReferenceValue.ReadLink;

            if (readLink != null)
            {
                string readLinkRelation = AtomUtils.ComputeStreamPropertyRelation(streamProperty, false);

                AtomLinkMetadata readLinkMetadata = streamReferenceMetadata == null ? null : streamReferenceMetadata.SelfLink;
                AtomLinkMetadata mergedMetadata   = ODataAtomWriterMetadataUtils.MergeLinkMetadata(readLinkMetadata, readLinkRelation, readLink, linkTitle, contentType);
                this.atomEntryMetadataSerializer.WriteAtomLink(mergedMetadata, null /* etag */);
            }

            Uri editLink = streamReferenceValue.EditLink;

            if (editLink != null)
            {
                string editLinkRelation = AtomUtils.ComputeStreamPropertyRelation(streamProperty, true);

                AtomLinkMetadata editLinkMetadata = streamReferenceMetadata == null ? null : streamReferenceMetadata.EditLink;
                AtomLinkMetadata mergedMetadata   = ODataAtomWriterMetadataUtils.MergeLinkMetadata(editLinkMetadata, editLinkRelation, editLink, linkTitle, contentType);
                this.atomEntryMetadataSerializer.WriteAtomLink(mergedMetadata, streamReferenceValue.ETag);
            }
        }
        internal AtomLinkMetadata ReadAtomLinkElementInFeed(string relation, string hrefStringValue)
        {
            AtomLinkMetadata metadata = new AtomLinkMetadata {
                Relation = relation,
                Href     = (hrefStringValue == null) ? null : base.ProcessUriFromPayload(hrefStringValue, base.XmlReader.XmlBaseUri)
            };

            while (base.XmlReader.MoveToNextAttribute())
            {
                if (base.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    switch (base.XmlReader.LocalName)
                    {
                    case "type":
                        metadata.MediaType = base.XmlReader.Value;
                        break;

                    case "hreflang":
                        metadata.HrefLang = base.XmlReader.Value;
                        break;

                    case "title":
                        metadata.Title = base.XmlReader.Value;
                        break;

                    case "length":
                    {
                        int    num;
                        string s = base.XmlReader.Value;
                        if (!int.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out num))
                        {
                            throw new ODataException(Strings.EpmSyndicationWriter_InvalidLinkLengthValue(s));
                        }
                        metadata.Length = new int?(num);
                        break;
                    }

                    case "rel":
                        if (metadata.Relation == null)
                        {
                            metadata.Relation = base.XmlReader.Value;
                        }
                        break;

                    case "href":
                        if (metadata.Href == null)
                        {
                            metadata.Href = base.ProcessUriFromPayload(base.XmlReader.Value, base.XmlReader.XmlBaseUri);
                        }
                        break;
                    }
                }
            }
            base.XmlReader.Skip();
            return(metadata);
        }
        internal void WriteAtomLinkAttributes(AtomLinkMetadata linkMetadata, string etag)
        {
            string href = (linkMetadata.Href == null) ? null : base.UriToUrlAttributeValue(linkMetadata.Href);

            this.WriteAtomLinkMetadataAttributes(linkMetadata.Relation, href, linkMetadata.HrefLang, linkMetadata.Title, linkMetadata.MediaType, linkMetadata.Length);
            if (etag != null)
            {
                ODataAtomWriterUtils.WriteETag(base.XmlWriter, etag);
            }
        }
Esempio n. 9
0
        internal void WriteNavigationLinkStart(ODataNavigationLink navigationLink, Uri navigationLinkUrlOverride)
        {
            base.XmlWriter.WriteStartElement("", "link", "http://www.w3.org/2005/Atom");
            string           relation     = AtomUtils.ComputeODataNavigationLinkRelation(navigationLink);
            string           mediaType    = AtomUtils.ComputeODataNavigationLinkType(navigationLink);
            string           name         = navigationLink.Name;
            Uri              href         = navigationLinkUrlOverride ?? navigationLink.Url;
            AtomLinkMetadata linkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(navigationLink.GetAnnotation <AtomLinkMetadata>(), relation, href, name, mediaType);

            this.atomEntryMetadataSerializer.WriteAtomLinkAttributes(linkMetadata, null);
        }
Esempio n. 10
0
 public static AtomLinkMetadata Atom(this ODataAssociationLink associationLink)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataAssociationLink>(associationLink, "associationLink");
     AtomLinkMetadata annotation = associationLink.GetAnnotation<AtomLinkMetadata>();
     if (annotation == null)
     {
         annotation = new AtomLinkMetadata();
         associationLink.SetAnnotation<AtomLinkMetadata>(annotation);
     }
     return annotation;
 }
Esempio n. 11
0
        /// <summary>
        /// Writes the edit link element for an entry.
        /// </summary>
        /// <param name="editLink">The edit link URL.</param>
        /// <param name="entryMetadata">The ATOM entry metatadata for the current entry.</param>
        internal void WriteEntryEditLink(Uri editLink, AtomEntryMetadata entryMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(editLink != null, "editLink != null");

            // we allow additional link metadata to specify the title, type, hreflang or length of the link
            AtomLinkMetadata editLinkMetadata = entryMetadata == null ? null : entryMetadata.EditLink;

            // <link rel="edit" href="LinkHRef" .../>
            this.WriteReadOrEditLink(editLink, editLinkMetadata, AtomConstants.AtomEditRelationAttributeValue);
        }
Esempio n. 12
0
        internal void WriteFeedNextPageLink(ODataFeed feed)
        {
            Uri nextPageLink = feed.NextPageLink;

            if (nextPageLink != null)
            {
                AtomFeedMetadata annotation   = feed.GetAnnotation <AtomFeedMetadata>();
                AtomLinkMetadata linkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata((annotation == null) ? null : annotation.NextPageLink, "next", nextPageLink, null, null);
                this.atomFeedMetadataSerializer.WriteAtomLink(linkMetadata, null);
            }
        }
        internal AtomLinkMetadata ReadAtomLinkElementInFeed(string relation, string hrefStringValue)
        {
            AtomLinkMetadata metadata = new AtomLinkMetadata {
                Relation = relation,
                Href = (hrefStringValue == null) ? null : base.ProcessUriFromPayload(hrefStringValue, base.XmlReader.XmlBaseUri)
            };
            while (base.XmlReader.MoveToNextAttribute())
            {
                if (base.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    switch (base.XmlReader.LocalName)
                    {
                        case "type":
                            metadata.MediaType = base.XmlReader.Value;
                            break;

                        case "hreflang":
                            metadata.HrefLang = base.XmlReader.Value;
                            break;

                        case "title":
                            metadata.Title = base.XmlReader.Value;
                            break;

                        case "length":
                        {
                            int num;
                            string s = base.XmlReader.Value;
                            if (!int.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out num))
                            {
                                throw new ODataException(Strings.EpmSyndicationWriter_InvalidLinkLengthValue(s));
                            }
                            metadata.Length = new int?(num);
                            break;
                        }
                        case "rel":
                            if (metadata.Relation == null)
                            {
                                metadata.Relation = base.XmlReader.Value;
                            }
                            break;

                        case "href":
                            if (metadata.Href == null)
                            {
                                metadata.Href = base.ProcessUriFromPayload(base.XmlReader.Value, base.XmlReader.XmlBaseUri);
                            }
                            break;
                    }
                }
            }
            base.XmlReader.Skip();
            return metadata;
        }
Esempio n. 14
0
        public static AtomLinkMetadata Atom(this ODataAssociationLink associationLink)
        {
            ExceptionUtils.CheckArgumentNotNull <ODataAssociationLink>(associationLink, "associationLink");
            AtomLinkMetadata annotation = associationLink.GetAnnotation <AtomLinkMetadata>();

            if (annotation == null)
            {
                annotation = new AtomLinkMetadata();
                associationLink.SetAnnotation <AtomLinkMetadata>(annotation);
            }
            return(annotation);
        }
Esempio n. 15
0
        /// <summary>
        /// Reads an atom:link element into the Links collection of feed metadata (i.e., links that are not special to the OData protocol).
        /// </summary>
        /// <param name="atomFeedMetadata">The feed metadata to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element (atom:link) - the atom:link element to read.
        /// Post-Condition: Any                             - the node after the atom:link element which was read.
        /// </remarks>
        private void ReadLinkElementIntoLinksCollection(AtomFeedMetadata atomFeedMetadata)
        {
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(
                this.XmlReader.LocalName == AtomConstants.AtomLinkElementName && this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace,
                "Only atom:link elements can be read by this method.");

            // By sending nulls to ReadAtomLinkElementInFeed(), the method will read and store values for href and rel from the wire (inside of using parameter overrides).
            AtomLinkMetadata linkMetadata = this.ReadAtomLinkElementInFeed(null, null);

            atomFeedMetadata.AddLink(linkMetadata);
        }
Esempio n. 16
0
        internal void WriteEntryMediaEditLink(ODataStreamReferenceValue mediaResource)
        {
            Uri editLink = mediaResource.EditLink;

            if (editLink != null)
            {
                AtomStreamReferenceMetadata annotation   = mediaResource.GetAnnotation <AtomStreamReferenceMetadata>();
                AtomLinkMetadata            metadata     = (annotation == null) ? null : annotation.EditLink;
                AtomLinkMetadata            linkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(metadata, "edit-media", editLink, null, null);
                this.atomEntryMetadataSerializer.WriteAtomLink(linkMetadata, mediaResource.ETag);
            }
        }
Esempio n. 17
0
 internal AtomLinkMetadata(AtomLinkMetadata other)
 {
     if (other != null)
     {
         this.Relation = other.Relation;
         this.Href = other.Href;
         this.HrefLang = other.HrefLang;
         this.Title = other.Title;
         this.MediaType = other.MediaType;
         this.Length = other.Length;
         this.hrefFromEpm = other.hrefFromEpm;
     }
 }
Esempio n. 18
0
 internal void WriteAssociationLink(ODataAssociationLink associationLink, IEdmEntityType owningType, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, ProjectedPropertiesAnnotation projectedProperties)
 {
     ValidationUtils.ValidateAssociationLinkNotNull(associationLink);
     if (!projectedProperties.ShouldSkipProperty(associationLink.Name))
     {
         base.ValidateAssociationLink(associationLink, owningType);
         duplicatePropertyNamesChecker.CheckForDuplicatePropertyNames(associationLink);
         AtomLinkMetadata annotation   = associationLink.GetAnnotation <AtomLinkMetadata>();
         string           relation     = AtomUtils.ComputeODataAssociationLinkRelation(associationLink);
         AtomLinkMetadata linkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(annotation, relation, associationLink.Url, associationLink.Name, "application/xml");
         this.atomEntryMetadataSerializer.WriteAtomLink(linkMetadata, null);
     }
 }
Esempio n. 19
0
 internal AtomLinkMetadata(AtomLinkMetadata other)
 {
     if (other != null)
     {
         this.Relation    = other.Relation;
         this.Href        = other.Href;
         this.HrefLang    = other.HrefLang;
         this.Title       = other.Title;
         this.MediaType   = other.MediaType;
         this.Length      = other.Length;
         this.hrefFromEpm = other.hrefFromEpm;
     }
 }
Esempio n. 20
0
        public static AtomLinkMetadata Atom(this ODataNavigationLink navigationLink)
        {
            ExceptionUtils.CheckArgumentNotNull(navigationLink, "navigationLink");

            AtomLinkMetadata linkMetadata = navigationLink.GetAnnotation<AtomLinkMetadata>();
            if (linkMetadata == null)
            {
                linkMetadata = new AtomLinkMetadata();
                navigationLink.SetAnnotation(linkMetadata);
            }

            return linkMetadata;
        }
        /// <summary>
        /// Writes a feed link.
        /// </summary>
        /// <param name="feed">The feed that contains the link.</param>
        /// <param name="relation">Relation attribute of the link.</param>
        /// <param name="href">href attribute of the link.</param>
        /// <param name="getLinkMetadata">Function to get the AtomLinkMetadata for the feed link.</param>
        internal void WriteFeedLink(ODataFeed feed, string relation, Uri href, Func <AtomFeedMetadata, AtomLinkMetadata> getLinkMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            AtomFeedMetadata feedMetadata = feed.GetAnnotation <AtomFeedMetadata>();
            AtomLinkMetadata mergedLink   = ODataAtomWriterMetadataUtils.MergeLinkMetadata(
                getLinkMetadata(feedMetadata),
                relation,
                href,
                null,     /*title*/
                null /*mediaType*/);

            this.atomFeedMetadataSerializer.WriteAtomLink(mergedLink, null);
        }
        /// <summary>
        /// Write the metadata of a link in ATOM format
        /// </summary>
        /// <param name="linkMetadata">The link metadata to write.</param>
        /// <param name="etag">The (optional) ETag for a link.</param>
        internal void WriteAtomLinkAttributes(AtomLinkMetadata linkMetadata, string etag)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(linkMetadata != null, "Link metadata must not be null.");

            string linkHref = linkMetadata.Href == null ? null : this.UriToUrlAttributeValue(linkMetadata.Href);

            this.WriteAtomLinkMetadataAttributes(linkMetadata.Relation, linkHref, linkMetadata.HrefLang, linkMetadata.Title, linkMetadata.MediaType, linkMetadata.Length);

            if (etag != null)
            {
                ODataAtomWriterUtils.WriteETag(this.XmlWriter, etag);
            }
        }
Esempio n. 23
0
        public static AtomLinkMetadata Atom(this ODataNavigationLink navigationLink)
        {
            ExceptionUtils.CheckArgumentNotNull(navigationLink, "navigationLink");

            AtomLinkMetadata linkMetadata = navigationLink.GetAnnotation <AtomLinkMetadata>();

            if (linkMetadata == null)
            {
                linkMetadata = new AtomLinkMetadata();
                navigationLink.SetAnnotation(linkMetadata);
            }

            return(linkMetadata);
        }
        /// <summary>
        /// Write the metadata of a link in ATOM format
        /// </summary>
        /// <param name="linkMetadata">The link metadata to write.</param>
        /// <param name="etag">The (optional) ETag for a link.</param>
        internal void WriteAtomLink(AtomLinkMetadata linkMetadata, string etag)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(linkMetadata != null, "Link metadata must not be null.");

            // <atom:link ...
            this.XmlWriter.WriteStartElement(
                AtomConstants.AtomNamespacePrefix,
                AtomConstants.AtomLinkElementName,
                AtomConstants.AtomNamespace);

            // write the attributes of the link
            this.WriteAtomLinkAttributes(linkMetadata, etag);

            // </atom:link>
            this.XmlWriter.WriteEndElement();
        }
Esempio n. 25
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="other">The <see cref="AtomLinkMetadata"/> instance to copy the values from; can be null.</param>
        internal AtomLinkMetadata(AtomLinkMetadata other)
        {
            DebugUtils.CheckNoExternalCallers();

            if (other == null)
            {
                return;
            }

            this.Relation = other.Relation;
            this.Href = other.Href;
            this.HrefLang = other.HrefLang;
            this.Title = other.Title;
            this.MediaType = other.MediaType;
            this.Length = other.Length;
            this.hrefFromEpm = other.hrefFromEpm;
        }
Esempio n. 26
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="other">The <see cref="AtomLinkMetadata"/> instance to copy the values from; can be null.</param>
        internal AtomLinkMetadata(AtomLinkMetadata other)
        {
            DebugUtils.CheckNoExternalCallers();

            if (other == null)
            {
                return;
            }

            this.Relation    = other.Relation;
            this.Href        = other.Href;
            this.HrefLang    = other.HrefLang;
            this.Title       = other.Title;
            this.MediaType   = other.MediaType;
            this.Length      = other.Length;
            this.hrefFromEpm = other.hrefFromEpm;
        }
Esempio n. 27
0
        /// <summary>
        /// Writes the next page link for a feed.
        /// </summary>
        /// <param name="feed">The feed to write the next page link for.</param>
        internal void WriteFeedNextPageLink(ODataFeed feed)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(feed != null, "feed != null");

            Uri nextPageLink = feed.NextPageLink;

            if (nextPageLink != null)
            {
                // <atom:link rel="next" href="next-page-link" />
                AtomFeedMetadata feedMetadata = feed.GetAnnotation <AtomFeedMetadata>();
                AtomLinkMetadata mergedLink   = ODataAtomWriterMetadataUtils.MergeLinkMetadata(
                    feedMetadata == null ? null : feedMetadata.NextPageLink,
                    AtomConstants.AtomNextRelationAttributeValue,
                    nextPageLink,
                    null, /*title*/
                    null /*mediaType*/);
                this.atomFeedMetadataSerializer.WriteAtomLink(mergedLink, null);
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Writes the navigation link's start element and atom metadata.
        /// </summary>
        /// <param name="navigationLink">The navigation link to write.</param>
        /// <param name="navigationLinkUrlOverride">Url to use for the navigation link. If this is specified the Url property on the <paramref name="navigationLink"/>
        /// will be ignored. If this parameter is null, the Url from the navigation link is used.</param>
        internal void WriteNavigationLinkStart(ODataNavigationLink navigationLink, Uri navigationLinkUrlOverride)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(navigationLink != null, "navigationLink != null");
            Debug.Assert(!string.IsNullOrEmpty(navigationLink.Name), "The navigation link name was not verified yet.");
            Debug.Assert(navigationLink.Url != null, "The navigation link Url was not verified yet.");
            Debug.Assert(navigationLink.IsCollection.HasValue, "navigationLink.IsCollection.HasValue");

            // <atom:link>
            this.XmlWriter.WriteStartElement(AtomConstants.AtomNamespacePrefix, AtomConstants.AtomLinkElementName, AtomConstants.AtomNamespace);

            string linkRelation = AtomUtils.ComputeODataNavigationLinkRelation(navigationLink);
            string linkType     = AtomUtils.ComputeODataNavigationLinkType(navigationLink);
            string linkTitle    = navigationLink.Name;

            Uri navigationLinkUrl           = navigationLinkUrlOverride ?? navigationLink.Url;
            AtomLinkMetadata linkMetadata   = navigationLink.GetAnnotation <AtomLinkMetadata>();
            AtomLinkMetadata mergedMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(linkMetadata, linkRelation, navigationLinkUrl, linkTitle, linkType);

            this.atomEntryMetadataSerializer.WriteAtomLinkAttributes(mergedMetadata, null /* etag */);
        }
Esempio n. 29
0
        /// <summary>
        /// Writes the edit-media link for an entry.
        /// </summary>
        /// <param name="mediaResource">The media resource representing the MR of the entry to write.</param>
        internal void WriteEntryMediaEditLink(ODataStreamReferenceValue mediaResource)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(mediaResource != null, "mediaResource != null");

            Uri mediaEditLink = mediaResource.EditLink;

            Debug.Assert(mediaEditLink != null || mediaResource.ETag == null, "The default stream edit link and etag should have been validated by now.");
            if (mediaEditLink != null)
            {
                AtomStreamReferenceMetadata streamReferenceMetadata = mediaResource.GetAnnotation <AtomStreamReferenceMetadata>();
                AtomLinkMetadata            mediaEditMetadata       = streamReferenceMetadata == null ? null : streamReferenceMetadata.EditLink;
                AtomLinkMetadata            mergedLinkMetadata      =
                    ODataAtomWriterMetadataUtils.MergeLinkMetadata(
                        mediaEditMetadata,
                        AtomConstants.AtomEditMediaRelationAttributeValue,
                        mediaEditLink,
                        null /* title */,
                        null /* mediaType */);

                this.atomEntryMetadataSerializer.WriteAtomLink(mergedLinkMetadata, mediaResource.ETag);
            }
        }
Esempio n. 30
0
        internal void WriteStreamProperty(ODataProperty streamProperty, IEdmEntityType owningType, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, ProjectedPropertiesAnnotation projectedProperties)
        {
            WriterValidationUtils.ValidatePropertyNotNull(streamProperty);
            string name = streamProperty.Name;

            if (!projectedProperties.ShouldSkipProperty(name))
            {
                WriterValidationUtils.ValidateProperty(streamProperty);
                duplicatePropertyNamesChecker.CheckForDuplicatePropertyNames(streamProperty);
                IEdmProperty edmProperty = WriterValidationUtils.ValidatePropertyDefined(streamProperty.Name, owningType);
                WriterValidationUtils.ValidateStreamReferenceProperty(streamProperty, edmProperty, base.Version, base.WritingResponse);
                ODataStreamReferenceValue value2 = (ODataStreamReferenceValue)streamProperty.Value;
                if (((owningType != null) && owningType.IsOpen) && (edmProperty == null))
                {
                    ValidationUtils.ValidateOpenPropertyValue(streamProperty.Name, value2);
                }
                AtomStreamReferenceMetadata annotation = value2.GetAnnotation <AtomStreamReferenceMetadata>();
                string contentType = value2.ContentType;
                string title       = streamProperty.Name;
                Uri    readLink    = value2.ReadLink;
                if (readLink != null)
                {
                    string           relation     = AtomUtils.ComputeStreamPropertyRelation(streamProperty, false);
                    AtomLinkMetadata metadata     = (annotation == null) ? null : annotation.SelfLink;
                    AtomLinkMetadata linkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(metadata, relation, readLink, title, contentType);
                    this.atomEntryMetadataSerializer.WriteAtomLink(linkMetadata, null);
                }
                Uri editLink = value2.EditLink;
                if (editLink != null)
                {
                    string           str5      = AtomUtils.ComputeStreamPropertyRelation(streamProperty, true);
                    AtomLinkMetadata metadata4 = (annotation == null) ? null : annotation.EditLink;
                    AtomLinkMetadata metadata5 = ODataAtomWriterMetadataUtils.MergeLinkMetadata(metadata4, str5, editLink, title, contentType);
                    this.atomEntryMetadataSerializer.WriteAtomLink(metadata5, value2.ETag);
                }
            }
        }
        /// <summary>
        /// Adds a new link to feed metadata.
        /// </summary>
        /// <param name="feedMetadata">The feed metadata to add the link to.</param>
        /// <param name="linkMetadata">The link metadata to add.</param>
        internal static void AddLinkToFeedMetadata(AtomFeedMetadata feedMetadata, AtomLinkMetadata linkMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(feedMetadata != null, "feedMetadata != null");
            Debug.Assert(linkMetadata != null, "linkMetadata != null");

            if (object.ReferenceEquals(feedMetadata.Links, EmptyLinksList))
            {
                feedMetadata.Links = new ReadOnlyEnumerable<AtomLinkMetadata>();
            }

            ReaderUtils.GetSourceListOfEnumerable(feedMetadata.Links, "Links").Add(linkMetadata);
        }
Esempio n. 32
0
 private void WriteFeedElements(IExpandedResult expanded, IEnumerator elements, ResourceType expectedType, string title, Uri relativeUri, Uri absoluteUri, bool hasMoved, bool topLevel)
 {
     ODataFeed feed = new ODataFeed {
         Id = absoluteUri.AbsoluteUri
     };
     AtomFeedMetadata annotation = new AtomFeedMetadata();
     feed.SetAnnotation<AtomFeedMetadata>(annotation);
     AtomTextConstruct construct = new AtomTextConstruct {
         Text = title
     };
     annotation.Title = construct;
     AtomLinkMetadata metadata2 = new AtomLinkMetadata {
         Href = relativeUri,
         Title = title
     };
     annotation.SelfLink = metadata2;
     bool flag = false;
     if (topLevel && (base.RequestDescription.CountOption == RequestQueryCountOption.Inline))
     {
         flag = this.contentFormat == ODataFormat.VerboseJson;
         if (!flag)
         {
             feed.Count = new long?(base.RequestDescription.CountValue);
         }
     }
     this.odataWriter.WriteStart(feed);
     try
     {
         object lastObject = null;
         IExpandedResult skipTokenExpandedResult = null;
         while (hasMoved)
         {
             object current = elements.Current;
             IExpandedResult skipToken = base.GetSkipToken(expanded);
             if (current != null)
             {
                 IExpandedResult result3 = current as IExpandedResult;
                 if (result3 != null)
                 {
                     expanded = result3;
                     current = Serializer.GetExpandedElement(expanded);
                     skipToken = base.GetSkipToken(expanded);
                 }
                 this.WriteEntry(expanded, current, true, expectedType);
             }
             hasMoved = elements.MoveNext();
             lastObject = current;
             skipTokenExpandedResult = skipToken;
         }
         if (flag)
         {
             feed.Count = new long?(base.RequestDescription.CountValue);
         }
         if (base.NeedNextPageLink(elements))
         {
             feed.NextPageLink = base.GetNextLinkUri(lastObject, skipTokenExpandedResult, absoluteUri);
         }
     }
     finally
     {
         if (!topLevel)
         {
             WebUtil.Dispose(elements);
         }
     }
     this.odataWriter.WriteEnd();
 }
Esempio n. 33
0
 internal void WriteAtomLink(AtomLinkMetadata linkMetadata, string etag)
 {
     base.XmlWriter.WriteStartElement("", "link", "http://www.w3.org/2005/Atom");
     this.WriteAtomLinkAttributes(linkMetadata, etag);
     base.XmlWriter.WriteEndElement();
 }
        internal void WriteFeedMetadata(AtomFeedMetadata feedMetadata, ODataFeed feed, string updatedTime, out bool authorWritten)
        {
            string textContent = (feed == null) ? feedMetadata.SourceId : feed.Id;

            base.WriteElementWithTextContent("", "id", "http://www.w3.org/2005/Atom", textContent);
            base.WriteTextConstruct("", "title", "http://www.w3.org/2005/Atom", feedMetadata.Title);
            if (feedMetadata.Subtitle != null)
            {
                base.WriteTextConstruct("", "subtitle", "http://www.w3.org/2005/Atom", feedMetadata.Subtitle);
            }
            string str2 = feedMetadata.Updated.HasValue ? ODataAtomConvert.ToAtomString(feedMetadata.Updated.Value) : updatedTime;

            base.WriteElementWithTextContent("", "updated", "http://www.w3.org/2005/Atom", str2);
            AtomLinkMetadata selfLink = feedMetadata.SelfLink;

            if (selfLink != null)
            {
                AtomLinkMetadata linkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(selfLink, "self", null, null, null);
                base.WriteAtomLink(linkMetadata, null);
            }
            IEnumerable <AtomLinkMetadata> links = feedMetadata.Links;

            if (links != null)
            {
                foreach (AtomLinkMetadata metadata3 in links)
                {
                    base.WriteAtomLink(metadata3, null);
                }
            }
            IEnumerable <AtomCategoryMetadata> categories = feedMetadata.Categories;

            if (categories != null)
            {
                foreach (AtomCategoryMetadata metadata4 in categories)
                {
                    base.WriteCategory(metadata4);
                }
            }
            Uri logo = feedMetadata.Logo;

            if (logo != null)
            {
                base.WriteElementWithTextContent("", "logo", "http://www.w3.org/2005/Atom", base.UriToUrlAttributeValue(logo));
            }
            if (feedMetadata.Rights != null)
            {
                base.WriteTextConstruct("", "rights", "http://www.w3.org/2005/Atom", feedMetadata.Rights);
            }
            IEnumerable <AtomPersonMetadata> contributors = feedMetadata.Contributors;

            if (contributors != null)
            {
                foreach (AtomPersonMetadata metadata5 in contributors)
                {
                    base.XmlWriter.WriteStartElement("", "contributor", "http://www.w3.org/2005/Atom");
                    base.WritePersonMetadata(metadata5);
                    base.XmlWriter.WriteEndElement();
                }
            }
            AtomGeneratorMetadata generator = feedMetadata.Generator;

            if (generator != null)
            {
                base.XmlWriter.WriteStartElement("", "generator", "http://www.w3.org/2005/Atom");
                if (generator.Uri != null)
                {
                    base.XmlWriter.WriteAttributeString("uri", base.UriToUrlAttributeValue(generator.Uri));
                }
                if (!string.IsNullOrEmpty(generator.Version))
                {
                    base.XmlWriter.WriteAttributeString("version", generator.Version);
                }
                ODataAtomWriterUtils.WriteString(base.XmlWriter, generator.Name);
                base.XmlWriter.WriteEndElement();
            }
            Uri icon = feedMetadata.Icon;

            if (icon != null)
            {
                base.WriteElementWithTextContent("", "icon", "http://www.w3.org/2005/Atom", base.UriToUrlAttributeValue(icon));
            }
            IEnumerable <AtomPersonMetadata> authors = feedMetadata.Authors;

            authorWritten = false;
            if (authors != null)
            {
                foreach (AtomPersonMetadata metadata7 in authors)
                {
                    authorWritten = true;
                    base.XmlWriter.WriteStartElement("", "author", "http://www.w3.org/2005/Atom");
                    base.WritePersonMetadata(metadata7);
                    base.XmlWriter.WriteEndElement();
                }
            }
        }
        /// <summary>
        /// Reads the atom:link element in the entry content.
        /// </summary>
        /// <param name="relation">The value of the rel attribute for the link element.</param>
        /// <param name="hrefStringValue">The value of the href attribute for the link element.</param>
        /// <returns>An <see cref="AtomLinkMetadata"/> instance storing the information about this link, or null if link info doesn't need to be stored.</returns>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element (atom:link) - the atom:link element to read.
        /// Post-Condition: XmlNodeType.Element (atom:link) - the atom:link element which was read.
        /// </remarks>
        internal AtomLinkMetadata ReadAtomLinkElementInEntryContent(string relation, string hrefStringValue)
        {
            DebugUtils.CheckNoExternalCallers();
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(
                this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace && this.XmlReader.LocalName == AtomConstants.AtomLinkElementName,
                "Only atom:link element can be read by this method.");

            AtomLinkMetadata linkMetadata = null;

            if (this.ReadAtomMetadata)
            {
                linkMetadata = new AtomLinkMetadata();
                linkMetadata.Relation = relation;
                if (this.ReadAtomMetadata)
                {
                    linkMetadata.Href = hrefStringValue == null ? null : this.ProcessUriFromPayload(hrefStringValue, this.XmlReader.XmlBaseUri);
                }

                // Read the attributes
                while (this.XmlReader.MoveToNextAttribute())
                {
                    if (this.XmlReader.NamespaceEquals(this.EmptyNamespace))
                    {
                        // Note that it's OK to store values which we don't validate in any way even if we might not need them.
                        // The EPM reader will ignore them if they're not needed and the fact that we don't validate them means that there are no observable differences
                        // if we store them. It keeps the code simpler (less ifs).
                        switch (this.XmlReader.LocalName)
                        {
                            case AtomConstants.AtomLinkTypeAttributeName:
                                linkMetadata.MediaType = this.XmlReader.Value;
                                break;
                            case AtomConstants.AtomLinkHrefLangAttributeName:
                                linkMetadata.HrefLang = this.XmlReader.Value;
                                break;
                            case AtomConstants.AtomLinkTitleAttributeName:
                                linkMetadata.Title = this.XmlReader.Value;
                                break;
                            case AtomConstants.AtomLinkLengthAttributeName:
                                // We must NOT try to parse the value into a number if we don't need it (either ATOM metadata or EPM)
                                if (this.ReadAtomMetadata)
                                {
                                    string lengthStringValue = this.XmlReader.Value;
                                    int length;
                                    if (int.TryParse(lengthStringValue, NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out length))
                                    {
                                        linkMetadata.Length = length;
                                    }
                                    else
                                    {
                                        throw new ODataException(Strings.EpmSyndicationWriter_InvalidLinkLengthValue(lengthStringValue));
                                    }
                                }

                                break;
                            default:
                                // Ignore all other attributes.
                                break;
                        }
                    }
                }
            }

            this.XmlReader.MoveToElement();

            return linkMetadata;
        }
Esempio n. 36
0
        internal void WriteEntryEditLink(Uri editLink, AtomEntryMetadata entryMetadata)
        {
            AtomLinkMetadata linkMetadata = (entryMetadata == null) ? null : entryMetadata.EditLink;

            this.WriteReadOrEditLink(editLink, linkMetadata, "edit");
        }
 internal AtomLinkMetadata ReadAtomLinkElementInEntryContent(string relation, string hrefStringValue)
 {
     AtomLinkMetadata metadata = null;
     if (base.ReadAtomMetadata)
     {
         metadata = new AtomLinkMetadata {
             Relation = relation
         };
         if (base.ReadAtomMetadata)
         {
             metadata.Href = (hrefStringValue == null) ? null : base.ProcessUriFromPayload(hrefStringValue, base.XmlReader.XmlBaseUri);
         }
         while (base.XmlReader.MoveToNextAttribute())
         {
             string str2;
             if (base.XmlReader.NamespaceEquals(this.EmptyNamespace) && ((str2 = base.XmlReader.LocalName) != null))
             {
                 if (!(str2 == "type"))
                 {
                     if (str2 == "hreflang")
                     {
                         goto Label_00B9;
                     }
                     if (str2 == "title")
                     {
                         goto Label_00CC;
                     }
                     if (str2 == "length")
                     {
                         goto Label_00DF;
                     }
                 }
                 else
                 {
                     metadata.MediaType = base.XmlReader.Value;
                 }
             }
             continue;
         Label_00B9:
             metadata.HrefLang = base.XmlReader.Value;
             continue;
         Label_00CC:
             metadata.Title = base.XmlReader.Value;
             continue;
         Label_00DF:
             if (base.ReadAtomMetadata)
             {
                 int num;
                 string s = base.XmlReader.Value;
                 if (!int.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out num))
                 {
                     throw new ODataException(Microsoft.Data.OData.Strings.EpmSyndicationWriter_InvalidLinkLengthValue(s));
                 }
                 metadata.Length = new int?(num);
             }
         }
     }
     base.XmlReader.MoveToElement();
     return metadata;
 }
        private void ReadLinkElementIntoLinksCollection(AtomFeedMetadata atomFeedMetadata)
        {
            AtomLinkMetadata linkMetadata = this.ReadAtomLinkElementInFeed(null, null);

            AtomMetadataReaderUtils.AddLinkToFeedMetadata(atomFeedMetadata, linkMetadata);
        }
        /// <summary>
        /// Creates a new <see cref="AtomLinkMetadata"/> instance by merging the given
        /// <paramref name="metadata"/> (if any) with the specified <paramref name="href"/>,
        /// <paramref name="relation"/> and (optional) <paramref name="title"/>.
        /// </summary>
        /// <param name="metadata">The metadata to merge with the <paramref name="href"/>, <paramref name="relation"/> and (optional) <paramref name="title"/>.</param>
        /// <param name="relation">The relation to use in the merged metadata.</param>
        /// <param name="href">The href to use in the merged metadata.</param>
        /// <param name="title">The (optional) title to use in the merged metadata.</param>
        /// <param name="mediaType">The (optional) media type to use in the merged metadata.</param>
        /// <returns>A new <see cref="AtomLinkMetadata"/> instance created by merging all the arguments.</returns>
        /// <remarks>
        /// If the <paramref name="metadata"/> already holds values for <paramref name="href"/>,
        /// <paramref name="relation"/>, <paramref name="title"/>, or <paramref name="mediaType"/> this method validates that they
        /// are the same as the ones specified in the method arguments.
        /// </remarks>
        internal static AtomLinkMetadata MergeLinkMetadata(
            AtomLinkMetadata metadata, 
            string relation,
            Uri href,
            string title,
            string mediaType)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(relation != null, "relation != null");

            AtomLinkMetadata mergedMetadata = new AtomLinkMetadata(metadata);

            // set the relation
            string metadataRelation = mergedMetadata.Relation;
            if (metadataRelation != null)
            {
                // validate that the relations are the same
                if (string.CompareOrdinal(relation, metadataRelation) != 0)
                {
                    throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkRelationsMustMatch(relation, metadataRelation));
                }
            }
            else
            {
                mergedMetadata.Relation = relation;
            }

            // set the href if it was specified
            if (href != null)
            {
                Uri metadataHref = mergedMetadata.Href;
                if (metadataHref != null)
                {
                    // validate that the hrefs are the same
                    if (!href.Equals(metadataHref))
                    {
                        throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkHrefsMustMatch(href, metadataHref));
                    }
                }
                else
                {
                    mergedMetadata.Href = href;
                }
            }

            // set the title if it was specified
            if (title != null)
            {
                string metadataTitle = mergedMetadata.Title;
                if (metadataTitle != null)
                {
                    // validate that the relations are the same
                    if (string.CompareOrdinal(title, metadataTitle) != 0)
                    {
                        throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkTitlesMustMatch(title, metadataTitle));
                    }
                }
                else
                {
                    mergedMetadata.Title = title;
                }
            }

            // set the content type if it was specified
            if (mediaType != null)
            {
                string metadataMediaType = mergedMetadata.MediaType;
                if (metadataMediaType != null)
                {
                    // validate that the relations are the same
                    if (!HttpUtils.CompareMediaTypeNames(mediaType, metadataMediaType))
                    {
                        throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkMediaTypesMustMatch(mediaType, metadataMediaType));
                    }
                }
                else
                {
                    mergedMetadata.MediaType = mediaType;
                }
            }

            return mergedMetadata;
        }
Esempio n. 40
0
        internal void WriteEntryReadLink(Uri readLink, AtomEntryMetadata entryMetadata)
        {
            AtomLinkMetadata linkMetadata = (entryMetadata == null) ? null : entryMetadata.SelfLink;

            this.WriteReadOrEditLink(readLink, linkMetadata, "self");
        }
Esempio n. 41
0
        /// <summary>
        /// Creates a new <see cref="AtomLinkMetadata"/> instance by merging the given
        /// <paramref name="metadata"/> (if any) with the specified <paramref name="href"/>,
        /// <paramref name="relation"/> and (optional) <paramref name="title"/>.
        /// </summary>
        /// <param name="metadata">The metadata to merge with the <paramref name="href"/>, <paramref name="relation"/> and (optional) <paramref name="title"/>.</param>
        /// <param name="relation">The relation to use in the merged metadata.</param>
        /// <param name="href">The href to use in the merged metadata.</param>
        /// <param name="title">The (optional) title to use in the merged metadata.</param>
        /// <param name="mediaType">The (optional) media type to use in the merged metadata.</param>
        /// <returns>A new <see cref="AtomLinkMetadata"/> instance created by merging all the arguments.</returns>
        /// <remarks>
        /// If the <paramref name="metadata"/> already holds values for <paramref name="href"/>,
        /// <paramref name="relation"/>, <paramref name="title"/>, or <paramref name="mediaType"/> this method validates that they
        /// are the same as the ones specified in the method arguments.
        /// </remarks>
        internal static AtomLinkMetadata MergeLinkMetadata(
            AtomLinkMetadata metadata,
            string relation,
            Uri href,
            string title,
            string mediaType)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(relation != null, "relation != null");

            AtomLinkMetadata mergedMetadata = new AtomLinkMetadata(metadata);

            // set the relation
            string metadataRelation = mergedMetadata.Relation;

            if (metadataRelation != null)
            {
                // validate that the relations are the same
                if (string.CompareOrdinal(relation, metadataRelation) != 0)
                {
                    throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkRelationsMustMatch(relation, metadataRelation));
                }
            }
            else
            {
                mergedMetadata.Relation = relation;
            }

            // set the href if it was specified
            if (href != null)
            {
                Uri metadataHref = mergedMetadata.Href;
                if (metadataHref != null)
                {
                    // validate that the hrefs are the same
                    if (!href.Equals(metadataHref))
                    {
                        throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkHrefsMustMatch(href, metadataHref));
                    }
                }
                else
                {
                    mergedMetadata.Href = href;
                }
            }

            // set the title if it was specified
            if (title != null)
            {
                string metadataTitle = mergedMetadata.Title;
                if (metadataTitle != null)
                {
                    // validate that the relations are the same
                    if (string.CompareOrdinal(title, metadataTitle) != 0)
                    {
                        throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkTitlesMustMatch(title, metadataTitle));
                    }
                }
                else
                {
                    mergedMetadata.Title = title;
                }
            }

            // set the content type if it was specified
            if (mediaType != null)
            {
                string metadataMediaType = mergedMetadata.MediaType;
                if (metadataMediaType != null)
                {
                    // validate that the relations are the same
                    if (!HttpUtils.CompareMediaTypeNames(mediaType, metadataMediaType))
                    {
                        throw new ODataException(Strings.ODataAtomWriterMetadataUtils_LinkMediaTypesMustMatch(mediaType, metadataMediaType));
                    }
                }
                else
                {
                    mergedMetadata.MediaType = mediaType;
                }
            }

            return(mergedMetadata);
        }
        /// <summary>
        /// Write the metadata of a link in ATOM format
        /// </summary>
        /// <param name="linkMetadata">The link metadata to write.</param>
        /// <param name="etag">The (optional) ETag for a link.</param>
        internal void WriteAtomLinkAttributes(AtomLinkMetadata linkMetadata, string etag)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(linkMetadata != null, "Link metadata must not be null.");

            string linkHref = linkMetadata.Href == null ? null : this.UriToUrlAttributeValue(linkMetadata.Href);

            this.WriteAtomLinkMetadataAttributes(linkMetadata.Relation, linkHref, linkMetadata.HrefLang, linkMetadata.Title, linkMetadata.MediaType, linkMetadata.Length);

            if (etag != null)
            {
                ODataAtomWriterUtils.WriteETag(this.XmlWriter, etag);
            }
        }
        /// <summary>
        /// Writes the self or edit link.
        /// </summary>
        /// <param name="link">Uri object for the link.</param>
        /// <param name="linkMetadata">The atom link metadata for the link to specify title, type, hreflang and length of the link.</param>
        /// <param name="linkRelation">Relationship value. Either "edit" or "self".</param>
        private void WriteReadOrEditLink(
            Uri link,
            AtomLinkMetadata linkMetadata,
            string linkRelation)
        {
            if (link != null)
            {
                AtomLinkMetadata mergedLinkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(
                    linkMetadata,
                    linkRelation,
                    link,
                    null /* title */,
                    null /* media type */);

                this.atomEntryMetadataSerializer.WriteAtomLink(mergedLinkMetadata, null /* etag */);
            }
        }
Esempio n. 44
0
 private void WriteEntry(IExpandedResult expanded, object element, bool resourceInstanceInFeed, ResourceType expectedType)
 {
     Uri uri;
     Func<ProjectionNode, bool> predicate = null;
     base.IncrementSegmentResultCount();
     ODataEntry entry = new ODataEntry();
     AtomEntryMetadata annotation = new AtomEntryMetadata();
     entry.SetAnnotation<AtomEntryMetadata>(annotation);
     string name = expectedType.Name;
     ResourceType actualResourceType = WebUtil.GetNonPrimitiveResourceType(base.Provider, element);
     if (actualResourceType.ResourceTypeKind != ResourceTypeKind.EntityType)
     {
         throw new DataServiceException(500, System.Data.Services.Strings.BadProvider_InconsistentEntityOrComplexTypeUsage(actualResourceType.FullName));
     }
     Uri absoluteUri = Serializer.GetIdAndEditLink(element, actualResourceType, base.Provider, base.CurrentContainer, base.AbsoluteServiceUri, out uri);
     Uri relativeUri = new Uri(absoluteUri.AbsoluteUri.Substring(base.AbsoluteServiceUri.AbsoluteUri.Length), UriKind.Relative);
     entry.MediaResource = this.GetMediaResource(element, actualResourceType, name, relativeUri);
     entry.TypeName = actualResourceType.FullName;
     entry.Id = uri.AbsoluteUri;
     entry.EditLink = relativeUri;
     AtomLinkMetadata metadata2 = new AtomLinkMetadata {
         Title = name
     };
     annotation.EditLink = metadata2;
     string eTagValue = base.GetETagValue(element, actualResourceType);
     if (eTagValue != null)
     {
         entry.ETag = eTagValue;
     }
     IEnumerable<ProjectionNode> projections = base.GetProjections();
     if (projections != null)
     {
         if (predicate == null)
         {
             predicate = projectionNode => projectionNode.TargetResourceType.IsAssignableFrom(actualResourceType);
         }
         projections = projections.Where<ProjectionNode>(predicate);
         entry.SetAnnotation<ProjectedPropertiesAnnotation>(new ProjectedPropertiesAnnotation(from p in projections select p.PropertyName));
     }
     entry.AssociationLinks = this.GetEntityAssociationLinks(actualResourceType, relativeUri, projections);
     this.PopulateODataOperations(element, resourceInstanceInFeed, entry, actualResourceType);
     this.odataWriter.WriteStart(entry);
     this.WriteNavigationProperties(expanded, element, resourceInstanceInFeed, actualResourceType, absoluteUri, relativeUri, projections);
     entry.Properties = this.GetEntityProperties(element, actualResourceType, relativeUri, projections);
     this.odataWriter.WriteEnd();
 }
        /// <summary>
        /// Write the metadata of a link in ATOM format
        /// </summary>
        /// <param name="linkMetadata">The link metadata to write.</param>
        /// <param name="etag">The (optional) ETag for a link.</param>
        internal void WriteAtomLink(AtomLinkMetadata linkMetadata, string etag)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(linkMetadata != null, "Link metadata must not be null.");

            // <atom:link ...
            this.XmlWriter.WriteStartElement(
                AtomConstants.AtomNamespacePrefix,
                AtomConstants.AtomLinkElementName,
                AtomConstants.AtomNamespace);

            // write the attributes of the link
            this.WriteAtomLinkAttributes(linkMetadata, etag);

            // </atom:link>
            this.XmlWriter.WriteEndElement();
        }
 internal void WriteAtomLink(AtomLinkMetadata linkMetadata, string etag)
 {
     base.XmlWriter.WriteStartElement("", "link", "http://www.w3.org/2005/Atom");
     this.WriteAtomLinkAttributes(linkMetadata, etag);
     base.XmlWriter.WriteEndElement();
 }
Esempio n. 47
0
        /// <summary>
        /// Reads the atom:link element and returns a new ATOM link metadata object.
        /// </summary>
        /// <param name="relation">The value of the rel attribute for the link element.</param>
        /// <param name="hrefStringValue">The value of the href attribute for the link element.</param>
        /// <returns>An <see cref="AtomLinkMetadata"/> instance storing the information about this link.</returns>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element (atom:link) - the atom:link element to read.
        /// Post-Condition: Any                             - the node after the ATOM element which was read.
        /// </remarks>
        internal AtomLinkMetadata ReadAtomLinkElementInFeed(string relation, string hrefStringValue)
        {
            DebugUtils.CheckNoExternalCallers();
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(
                this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace && this.XmlReader.LocalName == AtomConstants.AtomLinkElementName,
                "Only atom:link element can be read by this method.");

            AtomLinkMetadata linkMetadata = new AtomLinkMetadata
            {
                Relation = relation,
                Href     = hrefStringValue == null ? null : this.ProcessUriFromPayload(hrefStringValue, this.XmlReader.XmlBaseUri)
            };

            // Read the attributes
            while (this.XmlReader.MoveToNextAttribute())
            {
                if (this.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    switch (this.XmlReader.LocalName)
                    {
                    case AtomConstants.AtomLinkTypeAttributeName:
                        linkMetadata.MediaType = this.XmlReader.Value;
                        break;

                    case AtomConstants.AtomLinkHrefLangAttributeName:
                        linkMetadata.HrefLang = this.XmlReader.Value;
                        break;

                    case AtomConstants.AtomLinkTitleAttributeName:
                        linkMetadata.Title = this.XmlReader.Value;
                        break;

                    case AtomConstants.AtomLinkLengthAttributeName:
                        string lengthStringValue = this.XmlReader.Value;
                        int    length;
                        if (int.TryParse(lengthStringValue, NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out length))
                        {
                            linkMetadata.Length = length;
                        }
                        else
                        {
                            throw new ODataException(Strings.EpmSyndicationWriter_InvalidLinkLengthValue(lengthStringValue));
                        }

                        break;

                    case AtomConstants.AtomLinkRelationAttributeName:
                        // Only store this rel value if linkMetadata.Relation was not set yet.
                        // Note: The value supplied via the parameter "relation" takes priority
                        //       over what we read in the XML at this point.  This is because
                        //       of situations such as having an IANA namespace prefix on the rel
                        //       value (in which case we don't store the literal rel value as it
                        //       appears in the xml, but just the part after the IANA prefix).
                        if (linkMetadata.Relation == null)
                        {
                            linkMetadata.Relation = this.XmlReader.Value;
                        }

                        break;

                    case AtomConstants.AtomLinkHrefAttributeName:
                        // Only store the href value if linkMetadata.Href was not set yet.
                        if (linkMetadata.Href == null)
                        {
                            linkMetadata.Href = this.ProcessUriFromPayload(this.XmlReader.Value, this.XmlReader.XmlBaseUri);
                        }

                        break;

                    default:
                        // Ignore all other attributes.
                        break;
                    }
                }
            }

            this.XmlReader.Skip();

            return(linkMetadata);
        }
        /// <summary>
        /// Reads the atom:link element and returns a new ATOM link metadata object.
        /// </summary>
        /// <param name="relation">The value of the rel attribute for the link element.</param>
        /// <param name="hrefStringValue">The value of the href attribute for the link element.</param>
        /// <returns>An <see cref="AtomLinkMetadata"/> instance storing the information about this link.</returns>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element (atom:link) - the atom:link element to read.
        /// Post-Condition: Any                             - the node after the ATOM element which was read.
        /// </remarks>
        internal AtomLinkMetadata ReadAtomLinkElementInFeed(string relation, string hrefStringValue)
        {
            DebugUtils.CheckNoExternalCallers();
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(
                this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace && this.XmlReader.LocalName == AtomConstants.AtomLinkElementName,
                "Only atom:link element can be read by this method.");

            AtomLinkMetadata linkMetadata = new AtomLinkMetadata
            {
                Relation = relation,
                Href = hrefStringValue == null ? null : this.ProcessUriFromPayload(hrefStringValue, this.XmlReader.XmlBaseUri)
            };

            // Read the attributes
            while (this.XmlReader.MoveToNextAttribute())
            {
                if (this.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    switch (this.XmlReader.LocalName)
                    {
                        case AtomConstants.AtomLinkTypeAttributeName:
                            linkMetadata.MediaType = this.XmlReader.Value;
                            break;
                        case AtomConstants.AtomLinkHrefLangAttributeName:
                            linkMetadata.HrefLang = this.XmlReader.Value;
                            break;
                        case AtomConstants.AtomLinkTitleAttributeName:
                            linkMetadata.Title = this.XmlReader.Value;
                            break;
                        case AtomConstants.AtomLinkLengthAttributeName:
                            string lengthStringValue = this.XmlReader.Value;
                            int length;
                            if (int.TryParse(lengthStringValue, NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out length))
                            {
                                linkMetadata.Length = length;
                            }
                            else
                            {
                                throw new ODataException(Strings.EpmSyndicationWriter_InvalidLinkLengthValue(lengthStringValue));
                            }

                            break;
                        case AtomConstants.AtomLinkRelationAttributeName:
                            // Only store this rel value if linkMetadata.Relation was not set yet.
                            // Note: The value supplied via the parameter "relation" takes priority 
                            //       over what we read in the XML at this point.  This is because
                            //       of situations such as having an IANA namespace prefix on the rel
                            //       value (in which case we don't store the literal rel value as it 
                            //       appears in the xml, but just the part after the IANA prefix).
                            if (linkMetadata.Relation == null)
                            {
                                linkMetadata.Relation = this.XmlReader.Value;
                            }

                            break;
                        case AtomConstants.AtomLinkHrefAttributeName:
                            // Only store the href value if linkMetadata.Href was not set yet.
                            if (linkMetadata.Href == null)
                            {
                                linkMetadata.Href = this.ProcessUriFromPayload(this.XmlReader.Value, this.XmlReader.XmlBaseUri);
                            }

                            break;
                        default:
                            // Ignore all other attributes.
                            break;
                    }
                }
            }
            
            this.XmlReader.Skip();

            return linkMetadata;
        }
        internal void WriteFeedMetadata(AtomFeedMetadata feedMetadata, ODataFeed feed, string updatedTime, out bool authorWritten)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(feedMetadata != null, "Feed metadata must not be null!");

            // <atom:id>text</atom:id>
            // NOTE: this is the Id of the feed. For a regular feed this is stored on the feed itself;
            // if used in the context of an <atom:source> element it is stored in metadata
            Debug.Assert(feed == null || !string.IsNullOrEmpty(feed.Id), "The feed Id should have been validated by now.");
            string id = feed == null ? feedMetadata.SourceId : feed.Id;

            this.WriteElementWithTextContent(
                AtomConstants.AtomNamespacePrefix,
                AtomConstants.AtomIdElementName,
                AtomConstants.AtomNamespace,
                id);

            // <atom:title>text</atom:title>
            // NOTE: write an empty element if no title is specified since the element is required
            this.WriteTextConstruct(AtomConstants.AtomNamespacePrefix, AtomConstants.AtomTitleElementName, AtomConstants.AtomNamespace, feedMetadata.Title);

            if (feedMetadata.Subtitle != null)
            {
                // <atom:subtitle>text</atom:subtitle>
                this.WriteTextConstruct(AtomConstants.AtomNamespacePrefix, AtomConstants.AtomSubtitleElementName, AtomConstants.AtomNamespace, feedMetadata.Subtitle);
            }

            // <atom:updated>date</atom:updated>
            // NOTE: the <updated> element is required and if not specified we use a single 'default/current' date/time for the whole payload.
            string updated = feedMetadata.Updated.HasValue ? ODataAtomConvert.ToAtomString(feedMetadata.Updated.Value) : updatedTime;

            this.WriteElementWithTextContent(
                AtomConstants.AtomNamespacePrefix,
                AtomConstants.AtomUpdatedElementName,
                AtomConstants.AtomNamespace,
                updated);

            AtomLinkMetadata selfLinkMetadata = feedMetadata.SelfLink;

            if (selfLinkMetadata != null)
            {
                AtomLinkMetadata mergedSelfLinkMetadata = ODataAtomWriterMetadataUtils.MergeLinkMetadata(
                    selfLinkMetadata,
                    AtomConstants.AtomSelfRelationAttributeValue,
                    null /* href */,
                    null /* title */,
                    null /* media type */);
                this.WriteAtomLink(mergedSelfLinkMetadata, null /*etag*/);
            }

            IEnumerable <AtomLinkMetadata> links = feedMetadata.Links;

            if (links != null)
            {
                foreach (AtomLinkMetadata link in links)
                {
                    // DeltaLink is written from ODataFeed, so it shouldn't be written again from AtomFeedMetadata.
                    if (link.Relation != AtomConstants.AtomDeltaRelationAttributeValue)
                    {
                        // <atom:link>...</atom:link>
                        this.WriteAtomLink(link, null /* etag */);
                    }
                }
            }

            IEnumerable <AtomCategoryMetadata> categories = feedMetadata.Categories;

            if (categories != null)
            {
                foreach (AtomCategoryMetadata category in categories)
                {
                    // <atom:category term="..." scheme="..." label="..."></atom:category>
                    this.WriteCategory(category);
                }
            }

            Uri logo = feedMetadata.Logo;

            if (logo != null)
            {
                // <atom:logo>Uri</atom:logo>
                this.WriteElementWithTextContent(
                    AtomConstants.AtomNamespacePrefix,
                    AtomConstants.AtomLogoElementName,
                    AtomConstants.AtomNamespace,
                    this.UriToUrlAttributeValue(logo));
            }

            if (feedMetadata.Rights != null)
            {
                // <atom:rights>rights</atom:rights>
                this.WriteTextConstruct(
                    AtomConstants.AtomNamespacePrefix,
                    AtomConstants.AtomRightsElementName,
                    AtomConstants.AtomNamespace,
                    feedMetadata.Rights);
            }

            IEnumerable <AtomPersonMetadata> contributors = feedMetadata.Contributors;

            if (contributors != null)
            {
                foreach (AtomPersonMetadata contributor in contributors)
                {
                    // <atom:contributor>contributor data</atom:contributor>
                    this.XmlWriter.WriteStartElement(AtomConstants.AtomNamespacePrefix, AtomConstants.AtomContributorElementName, AtomConstants.AtomNamespace);
                    this.WritePersonMetadata(contributor);
                    this.XmlWriter.WriteEndElement();
                }
            }

            AtomGeneratorMetadata generator = feedMetadata.Generator;

            if (generator != null)
            {
                // <atom:generator uri="..." version="...">name</atom:generator>
                this.XmlWriter.WriteStartElement(AtomConstants.AtomNamespacePrefix, AtomConstants.AtomGeneratorElementName, AtomConstants.AtomNamespace);
                if (generator.Uri != null)
                {
                    this.XmlWriter.WriteAttributeString(AtomConstants.AtomGeneratorUriAttributeName, this.UriToUrlAttributeValue(generator.Uri));
                }

                if (!string.IsNullOrEmpty(generator.Version))
                {
                    this.XmlWriter.WriteAttributeString(AtomConstants.AtomGeneratorVersionAttributeName, generator.Version);
                }

                ODataAtomWriterUtils.WriteString(this.XmlWriter, generator.Name);
                this.XmlWriter.WriteEndElement();
            }

            Uri icon = feedMetadata.Icon;

            if (icon != null)
            {
                // <atom:icon>Uri</atom:icon>
                this.WriteElementWithTextContent(
                    AtomConstants.AtomNamespacePrefix,
                    AtomConstants.AtomIconElementName,
                    AtomConstants.AtomNamespace,
                    this.UriToUrlAttributeValue(icon));
            }

            IEnumerable <AtomPersonMetadata> authors = feedMetadata.Authors;

            authorWritten = false;
            if (authors != null)
            {
                foreach (AtomPersonMetadata author in authors)
                {
                    // <atom:author>author data</atom:author>
                    authorWritten = true;
                    this.XmlWriter.WriteStartElement(AtomConstants.AtomNamespacePrefix, AtomConstants.AtomAuthorElementName, AtomConstants.AtomNamespace);
                    this.WritePersonMetadata(author);
                    this.XmlWriter.WriteEndElement();
                }
            }
        }
Esempio n. 50
0
        private ODataStreamReferenceValue GetMediaResource(object element, ResourceType entityResourceType, string title, Uri relativeUri)
        {
            ODataStreamReferenceValue value2 = null;
            if (entityResourceType.IsMediaLinkEntry)
            {
                string str;
                Uri uri;
                string str2;
                base.Service.StreamProvider.GetStreamDescription(element, null, base.Service.OperationContext, out str, out uri, out str2);
				Uri uri2 = RequestUriProcessor.AppendEscapedSegment(relativeUri, "$value");
                value2 = new ODataStreamReferenceValue {
                    EditLink = uri2,
                    ContentType = str2,
                    ReadLink = uri ?? uri2
                };
                AtomStreamReferenceMetadata metadata2 = new AtomStreamReferenceMetadata();
                AtomLinkMetadata metadata3 = new AtomLinkMetadata {
                    Title = title
                };
                metadata2.EditLink = metadata3;
                AtomStreamReferenceMetadata annotation = metadata2;
                value2.SetAnnotation<AtomStreamReferenceMetadata>(annotation);
                if (!string.IsNullOrEmpty(str))
                {
                    value2.ETag = str;
                }
            }
            return value2;
        }