private void TryAnnotateV2FeedPackage(ODataEntry entry, EntityInstanceContext entityInstanceContext)
        {
            var instance = entityInstanceContext.EntityInstance as V2FeedPackage;
            if (instance != null)
            {
                // Set Atom entry metadata
                var atomEntryMetadata = new AtomEntryMetadata();
                atomEntryMetadata.Title = instance.Id;
                if (!string.IsNullOrEmpty(instance.Authors))
                {
                    atomEntryMetadata.Authors = new[] { new AtomPersonMetadata { Name = instance.Authors } };
                }
                if (instance.LastUpdated > DateTime.MinValue)
                {
                    atomEntryMetadata.Updated = instance.LastUpdated;
                }
                if (!string.IsNullOrEmpty(instance.Summary))
                {
                    atomEntryMetadata.Summary = instance.Summary;
                }
                entry.SetAnnotation(atomEntryMetadata);

                // Add package download link
                entry.MediaResource = new ODataStreamReferenceValue
                {
                    ContentType = ContentType,
                    ReadLink = BuildLinkForStreamProperty("v2", instance.Id, instance.Version, entityInstanceContext.Request)
                };
            }
        }
Пример #2
0
 public static MaterializerEntry CreateEntry(ODataEntry entry, DataServiceProtocolVersion maxProtocolVersion)
 {
     MaterializerEntry annotation = new MaterializerEntry(entry, maxProtocolVersion);
     entry.SetAnnotation<MaterializerEntry>(annotation);
     if (entry.Id == null)
     {
         throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Deserialize_MissingIdElement);
     }
     annotation.EntityDescriptor.Identity = entry.Id;
     annotation.EntityDescriptor.EditLink = entry.EditLink;
     annotation.EntityDescriptor.SelfLink = entry.ReadLink;
     annotation.EntityDescriptor.ETag = entry.ETag;
     return annotation;
 }
Пример #3
0
        /// <summary>
        /// If an entity type name is found in the payload this method is called to apply it to the current scope.
        /// This method should be called even if the type name was not found in which case a null should be passed in.
        /// The method validates that some type will be available as the current entity type after it returns (if we are parsing using metadata).
        /// </summary>
        /// <param name="entityTypeNameFromPayload">The entity type name found in the payload or null if no type was specified in the payload.</param>
        protected void ApplyEntityTypeNameFromPayload(string entityTypeNameFromPayload)
        {
            Debug.Assert(
                this.scopes.Count > 0 && this.scopes.Peek().Item is ODataEntry,
                "Entity type can be applied only when in entry scope.");

            SerializationTypeNameAnnotation serializationTypeNameAnnotation;
            EdmTypeKind             targetTypeKind;
            IEdmEntityTypeReference targetEntityTypeReference =
                (IEdmEntityTypeReference)ReaderValidationUtils.ResolvePayloadTypeNameAndComputeTargetType(
                    EdmTypeKind.Entity,
                    /*defaultPrimitivePayloadType*/ null,
                    this.CurrentEntityType.ToTypeReference(),
                    entityTypeNameFromPayload,
                    this.inputContext.Model,
                    this.inputContext.MessageReaderSettings,
                    this.inputContext.Version,
                    () => EdmTypeKind.Entity,
                    out targetTypeKind,
                    out serializationTypeNameAnnotation);

            IEdmEntityType targetEntityType = null;
            ODataEntry     entry            = this.CurrentEntry;

            if (targetEntityTypeReference != null)
            {
                targetEntityType = targetEntityTypeReference.EntityDefinition();
                entry.TypeName   = targetEntityType.ODataFullName();

                if (serializationTypeNameAnnotation != null)
                {
                    entry.SetAnnotation(serializationTypeNameAnnotation);
                }
            }
            else if (entityTypeNameFromPayload != null)
            {
                entry.TypeName = entityTypeNameFromPayload;
            }

            // Set the current entity type since the type from payload might be more derived than
            // the expected one.
            this.CurrentEntityType = targetEntityType;
        }
Пример #4
0
        protected void ApplyEntityTypeNameFromPayload(string entityTypeNameFromPayload)
        {
            SerializationTypeNameAnnotation annotation;
            EdmTypeKind             kind;
            IEdmEntityTypeReference reference = (IEdmEntityTypeReference)ReaderValidationUtils.ResolvePayloadTypeNameAndComputeTargetType(EdmTypeKind.Entity, null, this.CurrentEntityType.ToTypeReference(), entityTypeNameFromPayload, this.inputContext.Model, this.inputContext.MessageReaderSettings, this.inputContext.Version, () => EdmTypeKind.Entity, out kind, out annotation);
            IEdmEntityType          type      = null;
            ODataEntry currentEntry           = this.CurrentEntry;

            if (reference != null)
            {
                type = reference.EntityDefinition();
                currentEntry.TypeName = type.ODataFullName();
                if (annotation != null)
                {
                    currentEntry.SetAnnotation <SerializationTypeNameAnnotation>(annotation);
                }
            }
            else if (entityTypeNameFromPayload != null)
            {
                currentEntry.TypeName = entityTypeNameFromPayload;
            }
            this.CurrentEntityType = type;
        }
        internal static void AddTypeNameAnnotationAsNeeded(ODataEntry entry, IEdmEntitySet entitySet,
            ODataMetadataLevel metadataLevel)
        {
            // ODataLib normally has the caller decide whether or not to serialize properties by leaving properties
            // null when values should not be serialized. The TypeName property is different and should always be
            // provided to ODataLib to enable model validation. A separate annotation is used to decide whether or not
            // to serialize the type name (a null value prevents serialization).

            // Note that this annotation should not be used for Atom or JSON verbose formats, as it will interfere with
            // the correct default behavior for those formats.

            Contract.Assert(entry != null);

            // Only add an annotation if we want to override ODataLib's default type name serialization behavior.
            if (ShouldAddTypeNameAnnotation(metadataLevel))
            {
                string typeName;

                // Provide the type name to serialize (or null to force it not to serialize).
                if (ShouldSuppressTypeNameSerialization(entry, entitySet, metadataLevel))
                {
                    typeName = null;
                }
                else
                {
                    typeName = entry.TypeName;
                }

                entry.SetAnnotation<SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation
                {
                    TypeName = typeName
                });
            }
        }
        private static void WriteOdataEntity(ITableEntity entity, TableOperationType operationType, OperationContext ctx, ODataWriter writer, TableRequestOptions options)
        {
            ODataEntry entry = new ODataEntry()
            {
                Properties = GetPropertiesWithKeys(entity, ctx, operationType, options),
                TypeName = "account.sometype"
            };

            entry.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = null });
            writer.WriteStart(entry);
            writer.WriteEnd();
            writer.Flush();
        }
Пример #7
0
        public void ConvertFeed(Uri relativeODataUri, Uri relativeSodaUri, JsonPayload jsonPayload,
		                        DateTimeOffset feedUpdateTime)
        {
            var jsonObject = jsonPayload.JsonObject;

            var entries = jsonObject.PropertyValue<JArray>("entries");
            var meta = jsonObject.PropertyValue<JObject>("meta");
            var view = meta.PropertyValue<JObject>("view");

            IList<string> fieldsToIgnore;
            var model = BuildModel(view, out fieldsToIgnore);

            var entitySet = model.EntityContainers.Single().EntitySets().Single();

            var settings = new ODataMessageWriterSettings
                           	{
                           		Indent = true,
                           	};

            using (var writer = new ODataMessageWriter(Message, settings, model))
            {
                var feedWriter = writer.CreateODataFeedWriter();

                var feed = new ODataFeed();

                feed.SetAnnotation(new AtomFeedMetadata
                                   	{
                                   		Updated = feedUpdateTime,
                                   	});

                feed.Id = new Uri(ODataEndpointUri, relativeODataUri.OriginalString).OriginalString;

                feedWriter.WriteStart(feed);
                foreach (var entry in entries.Cast<JObject>())
                {
                    var entryMetadata = new ODataEntry();
                    entryMetadata.Id = (string) ((JValue) entry.Property("id").Value).Value;
                    entryMetadata.TypeName = entitySet.ElementType.FullName();

                    entryMetadata.Properties = ConvertProperties(entry, fieldsToIgnore);

                    entryMetadata.SetAnnotation(new AtomEntryMetadata
                                                    {
                                                        Updated = ConvertDateTimeOffset(entry.PrimitivePropertyValue<long>("updated_at")),
                                                        Published = ConvertDateTimeOffset(entry.PrimitivePropertyValue<long>("created_at")),
                                                    });

                    feedWriter.WriteStart(entryMetadata);
                    feedWriter.WriteEnd();
                }

                feedWriter.WriteEnd();
            }
        }
        internal static void AddTypeNameAnnotationAsNeeded(ODataEntry entry, IEdmEntitySet entitySet,
            ODataMetadataLevel metadataLevel)
        {
            Contract.Assert(entry != null);

            if (ShouldAddTypeNameAnnotation(metadataLevel))
            {
                string typeName;

                if (ShouldSuppressTypeNameSerialization(entry, entitySet, metadataLevel))
                {
                    typeName = null;
                }
                else
                {
                    typeName = entry.TypeName;
                }

                entry.SetAnnotation<SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation
                {
                    TypeName = typeName
                });
            }
        }
Пример #9
0
 internal void WriteEntry(EntityDescriptor entityDescriptor, IEnumerable<LinkDescriptor> relatedLinks, ODataRequestMessageWrapper requestMessage)
 {
     ClientEdmModel model = ClientEdmModel.GetModel(this.requestInfo.MaxProtocolVersion);
     ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType()));
     using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo))
     {
         ODataWriter odataWriter = writer.CreateODataEntryWriter();
         ODataEntry entry = new ODataEntry();
         if (this.requestInfo.HasWritingEventHandlers)
         {
             entry.SetAnnotation<WritingEntityInfo>(new WritingEntityInfo(entityDescriptor.Entity, this.requestInfo));
         }
         string serverTypeName = this.requestInfo.GetServerTypeName(entityDescriptor);
         if (clientTypeAnnotation.ElementTypeName != serverTypeName)
         {
             SerializationTypeNameAnnotation annotation = new SerializationTypeNameAnnotation {
                 TypeName = serverTypeName
             };
             entry.SetAnnotation<SerializationTypeNameAnnotation>(annotation);
         }
         entry.TypeName = clientTypeAnnotation.ElementTypeName;
         if (EntityStates.Modified == entityDescriptor.State)
         {
             entry.Id = entityDescriptor.GetLatestIdentity();
         }
         if (entityDescriptor.IsMediaLinkEntry || clientTypeAnnotation.IsMediaLinkEntry)
         {
             entry.MediaResource = new ODataStreamReferenceValue();
         }
         odataWriter.WriteStart(entry);
         if (EntityStates.Added == entityDescriptor.State)
         {
             this.WriteNavigationLink(entityDescriptor, relatedLinks, odataWriter);
         }
         entry.Properties = this.PopulateProperties(clientTypeAnnotation, entityDescriptor.Entity, null);
         odataWriter.WriteEnd();
     }
 }
Пример #10
0
 private static XmlReader EntryXmlCustomizer(ODataEntry entry, XmlReader entryReader, Uri baseUri)
 {
     XElement payload = XElement.Load(entryReader.ReadSubtree(), LoadOptions.None);
     entryReader.Read();
     entry.SetAnnotation<ReadingEntityInfo>(new ReadingEntityInfo(payload, baseUri));
     XmlReader reader = payload.CreateReader();
     reader.Read();
     return reader;
 }
Пример #11
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();
 }