/// <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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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>
        /// Reads a stream property link in an atom:entry.
        /// </summary>
        /// <param name="entryState">The reader entry state for the entry being read.</param>
        /// <param name="streamPropertyName">The name of the stream property that is being read.</param>
        /// <param name="linkRelation">The rel attribute value for the link.</param>
        /// <param name="linkHRef">The href attribute value for the link (or null if the href attribute was not present).</param>
        /// <param name="editLink">true if we are reading an edit link; otherwise false.</param>
        /// <returns>true if the stream property link was read; otherwise false.</returns>
        /// <remarks>
        /// Pre-Condition:   XmlNodeType.Element atom:link  - The atom:link element to read.
        /// Post-Condition:  Any                            - The node after the atom:link element if the link was read by this method.
        ///                  XmlNodeType.Element atom:link  - The atom:link element to read if the link was not read by this method.
        /// </remarks>
        private bool ReadStreamPropertyLinkInEntry(IODataAtomReaderEntryState entryState, string streamPropertyName, string linkRelation, string linkHRef, bool editLink)
        {
            if (!this.ReadingResponse || this.Version < ODataVersion.V3)
            {
                return false;
            }

            if (streamPropertyName.Length == 0)
            {
                throw new ODataException(o.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithEmptyName);
            }

            ODataStreamReferenceValue streamReferenceValue = this.GetNewOrExistingStreamPropertyValue(entryState, streamPropertyName);
            Debug.Assert(streamReferenceValue != null, "streamReferenceValue != null");

            AtomStreamReferenceMetadata atomStreamMetadata = null;
            if (this.ReadAtomMetadata)
            {
                // First, check if there is an existing metadata annotation on the stream reference value.
                atomStreamMetadata = streamReferenceValue.GetAnnotation<AtomStreamReferenceMetadata>();

                // If not, create a new metadata annotation.
                if (atomStreamMetadata == null)
                {
                    atomStreamMetadata = new AtomStreamReferenceMetadata();
                    streamReferenceValue.SetAnnotation(atomStreamMetadata);
                }
            }

            // set the edit-link or the read-link
            Uri href = linkHRef == null ? null : this.ProcessUriFromPayload(linkHRef, this.XmlReader.XmlBaseUri);

            if (editLink)
            {
                // edit-link
                if (streamReferenceValue.EditLink != null)
                {
                    throw new ODataException(o.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithMultipleEditLinks(streamPropertyName));
                }

                streamReferenceValue.EditLink = href;

                if (this.ReadAtomMetadata)
                {
                    atomStreamMetadata.EditLink = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
                }
            }
            else
            {
                // read-link
                if (streamReferenceValue.ReadLink != null)
                {
                    throw new ODataException(o.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithMultipleReadLinks(streamPropertyName));
                }

                streamReferenceValue.ReadLink = href;

                if (this.ReadAtomMetadata)
                {
                    atomStreamMetadata.SelfLink = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
                }
            }

            // set the ContentType
            string contentType = this.XmlReader.GetAttribute(this.AtomTypeAttributeName, this.EmptyNamespace);

            if (contentType != null && streamReferenceValue.ContentType != null)
            {
                if (!HttpUtils.CompareMediaTypeNames(contentType, streamReferenceValue.ContentType))
                {
                    throw new ODataException(o.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithMultipleContentTypes(streamPropertyName));
                }
            }

            streamReferenceValue.ContentType = contentType;

            // set the ETag
            if (editLink)
            {
                string etag = this.XmlReader.GetAttribute(this.ODataETagAttributeName, this.XmlReader.ODataMetadataNamespace);
                streamReferenceValue.ETag = etag;
            }

            this.XmlReader.Skip();
            return true;
        }
 private bool TryReadAtomStandardRelationLinkInEntry(IODataAtomReaderEntryState entryState, string linkRelation, string linkHRef)
 {
     if (string.CompareOrdinal(linkRelation, "edit") == 0)
     {
         if (entryState.HasEditLink && base.AtomInputContext.UseDefaultFormatBehavior)
         {
             throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_MultipleLinksInEntry("edit"));
         }
         if ((linkHRef != null) && (!base.AtomInputContext.UseClientFormatBehavior || !entryState.HasEditLink))
         {
             entryState.Entry.EditLink = base.ProcessUriFromPayload(linkHRef, base.XmlReader.XmlBaseUri);
         }
         if (this.ReadAtomMetadata)
         {
             entryState.AtomEntryMetadata.EditLink = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
         }
         entryState.HasEditLink = true;
         base.XmlReader.Skip();
         return true;
     }
     if (string.CompareOrdinal(linkRelation, "self") == 0)
     {
         if (entryState.HasReadLink && base.AtomInputContext.UseDefaultFormatBehavior)
         {
             throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_MultipleLinksInEntry("self"));
         }
         if ((linkHRef != null) && (!base.AtomInputContext.UseClientFormatBehavior || !entryState.HasReadLink))
         {
             entryState.Entry.ReadLink = base.ProcessUriFromPayload(linkHRef, base.XmlReader.XmlBaseUri);
         }
         if (this.ReadAtomMetadata)
         {
             entryState.AtomEntryMetadata.SelfLink = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
         }
         entryState.HasReadLink = true;
         base.XmlReader.Skip();
         return true;
     }
     if (string.CompareOrdinal(linkRelation, "edit-media") != 0)
     {
         return false;
     }
     if (entryState.HasEditMediaLink && base.AtomInputContext.UseDefaultFormatBehavior)
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_MultipleLinksInEntry("edit-media"));
     }
     if (!base.AtomInputContext.UseClientFormatBehavior || !entryState.HasEditMediaLink)
     {
         EnsureMediaResource(entryState, !base.UseClientFormatBehavior);
         ODataEntry entry = entryState.Entry;
         if (linkHRef != null)
         {
             entry.MediaResource.EditLink = base.ProcessUriFromPayload(linkHRef, base.XmlReader.XmlBaseUri);
         }
         string attribute = base.XmlReader.GetAttribute(this.ODataETagAttributeName, base.XmlReader.ODataMetadataNamespace);
         if (attribute != null)
         {
             entry.MediaResource.ETag = attribute;
         }
         if (this.ReadAtomMetadata)
         {
             AtomLinkMetadata metadata = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
             AtomStreamReferenceMetadata annotation = new AtomStreamReferenceMetadata {
                 EditLink = metadata
             };
             entry.MediaResource.SetAnnotation<AtomStreamReferenceMetadata>(annotation);
         }
     }
     entryState.HasEditMediaLink = true;
     base.XmlReader.Skip();
     return true;
 }
 private bool ReadStreamPropertyLinkInEntry(IODataAtomReaderEntryState entryState, string streamPropertyName, string linkRelation, string linkHRef, bool editLink)
 {
     if (!base.ReadingResponse || (base.Version < ODataVersion.V3))
     {
         return false;
     }
     if (streamPropertyName.Length == 0)
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithEmptyName);
     }
     ODataStreamReferenceValue newOrExistingStreamPropertyValue = this.GetNewOrExistingStreamPropertyValue(entryState, streamPropertyName);
     AtomStreamReferenceMetadata annotation = null;
     if (this.ReadAtomMetadata)
     {
         annotation = newOrExistingStreamPropertyValue.GetAnnotation<AtomStreamReferenceMetadata>();
         if (annotation == null)
         {
             annotation = new AtomStreamReferenceMetadata();
             newOrExistingStreamPropertyValue.SetAnnotation<AtomStreamReferenceMetadata>(annotation);
         }
     }
     Uri uri = (linkHRef == null) ? null : base.ProcessUriFromPayload(linkHRef, base.XmlReader.XmlBaseUri);
     if (editLink)
     {
         if (newOrExistingStreamPropertyValue.EditLink != null)
         {
             throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithMultipleEditLinks(streamPropertyName));
         }
         newOrExistingStreamPropertyValue.EditLink = uri;
         if (this.ReadAtomMetadata)
         {
             annotation.EditLink = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
         }
     }
     else
     {
         if (newOrExistingStreamPropertyValue.ReadLink != null)
         {
             throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithMultipleReadLinks(streamPropertyName));
         }
         newOrExistingStreamPropertyValue.ReadLink = uri;
         if (this.ReadAtomMetadata)
         {
             annotation.SelfLink = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef);
         }
     }
     string attribute = base.XmlReader.GetAttribute(base.AtomTypeAttributeName, base.EmptyNamespace);
     if (((attribute != null) && (newOrExistingStreamPropertyValue.ContentType != null)) && !HttpUtils.CompareMediaTypeNames(attribute, newOrExistingStreamPropertyValue.ContentType))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomEntryAndFeedDeserializer_StreamPropertyWithMultipleContentTypes(streamPropertyName));
     }
     newOrExistingStreamPropertyValue.ContentType = attribute;
     if (editLink)
     {
         string str2 = base.XmlReader.GetAttribute(this.ODataETagAttributeName, base.XmlReader.ODataMetadataNamespace);
         newOrExistingStreamPropertyValue.ETag = str2;
     }
     base.XmlReader.Skip();
     return true;
 }
Exemplo n.º 8
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;
        }