Beispiel #1
0
        /// <summary>Initializes a new instance of the <see cref="T:Microsoft.Data.OData.ODataMessageWriterSettings" /> class with specified settings.</summary>
        /// <param name="other">The specified settings.</param>
        public ODataMessageWriterSettings(ODataMessageWriterSettings other)
            : base(other)
        {
            ExceptionUtils.CheckArgumentNotNull(other, "other");

            this.acceptCharSets               = other.acceptCharSets;
            this.acceptMediaTypes             = other.acceptMediaTypes;
            this.BaseUri                      = other.BaseUri;
            this.DisableMessageStreamDisposal = other.DisableMessageStreamDisposal;
            this.format              = other.format;
            this.useFormat           = other.useFormat;
            this.Version             = other.Version;
            this.metadataDocumentUri = other.metadataDocumentUri;
            this.atomFormatStartEntryXmlCustomizationCallback = other.atomFormatStartEntryXmlCustomizationCallback;
            this.atomFormatEndEntryXmlCustomizationCallback   = other.atomFormatEndEntryXmlCustomizationCallback;
            this.JsonPCallback                    = other.JsonPCallback;
            this.shouldIncludeAnnotation          = other.shouldIncludeAnnotation;
            this.AutoComputePayloadMetadataInJson = other.AutoComputePayloadMetadataInJson;
            this.AutoGeneratedUrlsShouldPutKeyValueInDedicatedSegment = other.AutoGeneratedUrlsShouldPutKeyValueInDedicatedSegment;
            this.alwaysUseDefaultXmlNamespaceForRootElement           = other.alwaysUseDefaultXmlNamespaceForRootElement;
            this.UndeclaredPropertyBehaviorKinds = other.UndeclaredPropertyBehaviorKinds;

            // NOTE: writer behavior is immutable; copy by reference is ok.
            this.writerBehavior = other.writerBehavior;
        }
Beispiel #2
0
        /// <summary>
        /// Constructor to create default settings for OData writers.
        /// </summary>
        public ODataMessageWriterSettings()
        {
            // on writing the default value for 'CheckCharacters' is set to true so that we
            // produce valid Xml documents per default.
            this.CheckCharacters = true;

            // Create the default writer behavior
            this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
        }
        /// <summary>
        /// Constructor to create default settings for OData writers.
        /// </summary>
        public ODataMessageWriterSettings()
        {
            // on writing the default value for 'CheckCharacters' is set to true so that we 
            // produce valid Xml documents per default.
            this.CheckCharacters = true;

            // Create the default writer behavior
            this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
        }
Beispiel #4
0
 public void EnableWcfDataServicesClientBehavior(Func <ODataEntry, XmlWriter, XmlWriter> startEntryXmlCustomizationCallback, Action <ODataEntry, XmlWriter, XmlWriter> endEntryXmlCustomizationCallback, string odataNamespace, string typeScheme)
 {
     ExceptionUtils.CheckArgumentNotNull <string>(odataNamespace, "odataNamespace");
     ExceptionUtils.CheckArgumentNotNull <string>(typeScheme, "typeScheme");
     if (((startEntryXmlCustomizationCallback == null) && (endEntryXmlCustomizationCallback != null)) || ((startEntryXmlCustomizationCallback != null) && (endEntryXmlCustomizationCallback == null)))
     {
         throw new ODataException(Strings.ODataMessageWriterSettings_MessageWriterSettingsXmlCustomizationCallbacksMustBeSpecifiedBoth);
     }
     this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesClientBehavior(startEntryXmlCustomizationCallback, endEntryXmlCustomizationCallback, odataNamespace, typeScheme);
 }
 public void EnableWcfDataServicesClientBehavior(Func<ODataEntry, XmlWriter, XmlWriter> startEntryXmlCustomizationCallback, Action<ODataEntry, XmlWriter, XmlWriter> endEntryXmlCustomizationCallback, string odataNamespace, string typeScheme)
 {
     ExceptionUtils.CheckArgumentNotNull<string>(odataNamespace, "odataNamespace");
     ExceptionUtils.CheckArgumentNotNull<string>(typeScheme, "typeScheme");
     if (((startEntryXmlCustomizationCallback == null) && (endEntryXmlCustomizationCallback != null)) || ((startEntryXmlCustomizationCallback != null) && (endEntryXmlCustomizationCallback == null)))
     {
         throw new ODataException(Strings.ODataMessageWriterSettings_MessageWriterSettingsXmlCustomizationCallbacksMustBeSpecifiedBoth);
     }
     this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesClientBehavior(startEntryXmlCustomizationCallback, endEntryXmlCustomizationCallback, odataNamespace, typeScheme);
 }
Beispiel #6
0
 public ODataMessageWriterSettings(ODataMessageWriterSettings settings)
 {
     ExceptionUtils.CheckArgumentNotNull <ODataMessageWriterSettings>(settings, "settings");
     this.acceptCharSets               = settings.acceptCharSets;
     this.acceptMediaTypes             = settings.acceptMediaTypes;
     this.BaseUri                      = settings.BaseUri;
     this.CheckCharacters              = settings.CheckCharacters;
     this.DisableMessageStreamDisposal = settings.DisableMessageStreamDisposal;
     this.format         = settings.format;
     this.Indent         = settings.Indent;
     this.messageQuotas  = new ODataMessageQuotas(settings.MessageQuotas);
     this.useFormat      = settings.useFormat;
     this.Version        = settings.Version;
     this.writerBehavior = settings.writerBehavior;
 }
 public ODataMessageWriterSettings(ODataMessageWriterSettings settings)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataMessageWriterSettings>(settings, "settings");
     this.acceptCharSets = settings.acceptCharSets;
     this.acceptMediaTypes = settings.acceptMediaTypes;
     this.BaseUri = settings.BaseUri;
     this.CheckCharacters = settings.CheckCharacters;
     this.DisableMessageStreamDisposal = settings.DisableMessageStreamDisposal;
     this.format = settings.format;
     this.Indent = settings.Indent;
     this.messageQuotas = new ODataMessageQuotas(settings.MessageQuotas);
     this.useFormat = settings.useFormat;
     this.Version = settings.Version;
     this.writerBehavior = settings.writerBehavior;
 }
Beispiel #8
0
        public void EnableWcfDataServicesClientBehavior(
            Func <ODataEntry, XmlWriter, XmlWriter> startEntryXmlCustomizationCallback,
            Action <ODataEntry, XmlWriter, XmlWriter> endEntryXmlCustomizationCallback,
            string odataNamespace,
            string typeScheme)
        {
            ExceptionUtils.CheckArgumentNotNull(odataNamespace, "odataNamespace");
            ExceptionUtils.CheckArgumentNotNull(typeScheme, "typeScheme");

            this.SetAtomEntryXmlCustomization(startEntryXmlCustomizationCallback, endEntryXmlCustomizationCallback);

            this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesClientBehavior(
                odataNamespace,
                typeScheme);
        }
Beispiel #9
0
 private static string CreateDateTimeStringValue(object propertyValue, ODataWriterBehavior writerBehavior)
 {
     if (propertyValue == null)
     {
         propertyValue = DateTimeOffset.Now;
     }
     if (propertyValue is DateTime)
     {
         propertyValue = new DateTimeOffset((DateTime) propertyValue);
     }
     if (propertyValue is DateTimeOffset)
     {
         return ODataAtomConvert.ToAtomString((DateTimeOffset) propertyValue);
     }
     return EpmWriterUtils.GetPropertyValueAsText(propertyValue);
 }
Beispiel #10
0
 private static DateTimeOffset CreateDateTimeValue(object propertyValue, SyndicationItemProperty targetProperty, ODataWriterBehavior writerBehavior)
 {
     DateTimeOffset offset;
     DateTime time;
     if (propertyValue == null)
     {
         return DateTimeOffset.Now;
     }
     if (propertyValue is DateTimeOffset)
     {
         return (DateTimeOffset) propertyValue;
     }
     if (propertyValue is DateTime)
     {
         return new DateTimeOffset((DateTime) propertyValue);
     }
     string input = propertyValue as string;
     if (input == null)
     {
         DateTimeOffset offset2;
         try
         {
             offset2 = new DateTimeOffset(Convert.ToDateTime(propertyValue, CultureInfo.InvariantCulture));
         }
         catch (Exception exception)
         {
             if (!ExceptionUtils.IsCatchableExceptionType(exception))
             {
                 throw;
             }
             throw new ODataException(Microsoft.Data.OData.Strings.EpmSyndicationWriter_DateTimePropertyCanNotBeConverted(targetProperty.ToString()));
         }
         return offset2;
     }
     if (DateTimeOffset.TryParse(input, out offset))
     {
         return offset;
     }
     if (!DateTime.TryParse(input, out time))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.EpmSyndicationWriter_DateTimePropertyCanNotBeConverted(targetProperty.ToString()));
     }
     return new DateTimeOffset(time);
 }
Beispiel #11
0
        /// <summary>
        /// Copy constructor to create a copy of the settings for OData writers.
        /// </summary>
        /// <param name="settings">Settings to create a copy from</param>
        public ODataMessageWriterSettings(ODataMessageWriterSettings settings)
        {
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");

            this.acceptCharSets               = settings.acceptCharSets;
            this.acceptMediaTypes             = settings.acceptMediaTypes;
            this.BaseUri                      = settings.BaseUri;
            this.CheckCharacters              = settings.CheckCharacters;
            this.DisableMessageStreamDisposal = settings.DisableMessageStreamDisposal;
            this.format        = settings.format;
            this.Indent        = settings.Indent;
            this.messageQuotas = new ODataMessageQuotas(settings.MessageQuotas);
            this.useFormat     = settings.useFormat;
            this.Version       = settings.Version;
            this.atomFormatStartEntryXmlCustomizationCallback = settings.atomFormatStartEntryXmlCustomizationCallback;
            this.atomFormatEndEntryXmlCustomizationCallback   = settings.atomFormatEndEntryXmlCustomizationCallback;

            // NOTE: writer behavior is immutable; copy by reference is ok.
            this.writerBehavior = settings.writerBehavior;
        }
 /// <summary>
 /// Enables the default behavior of the OData library.
 /// </summary>
 public void EnableDefaultBehavior()
 {
     // We have to reset the ATOM entry XML customization since in the default behavior no atom entry customization is used.
     this.SetAtomEntryXmlCustomization(null, null);
     this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
 }
            /// <summary>
            /// Constructor to create a new entry scope.
            /// </summary>
            /// <param name="entry">The entry for the new scope.</param>
            /// <param name="skipWriting">true if the content of the scope to create should not be written.</param>
            /// <param name="writingResponse">true if we are writing a response, false if it's a request.</param>
            /// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
            internal EntryScope(ODataEntry entry, bool skipWriting, bool writingResponse, ODataWriterBehavior writerBehavior)
                : base(WriterState.Entry, entry, skipWriting)
            {
                DebugUtils.CheckNoExternalCallers();
                Debug.Assert(writerBehavior != null, "writerBehavior != null");

                if (entry != null)
                {
                    this.duplicatePropertyNamesChecker = new DuplicatePropertyNamesChecker(writerBehavior.AllowDuplicatePropertyNames, writingResponse);
                }
            }
Beispiel #14
0
        /// <summary>
        /// Validates that the expected property allows null value.
        /// </summary>
        /// <param name="expectedProperty">The metadata for the expected property.</param>
        /// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
        /// <param name="model">The model to use to get the data service version.</param>
        internal static void ValidateNullPropertyValue(IEdmProperty expectedProperty, ODataWriterBehavior writerBehavior, IEdmModel model)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(writerBehavior != null, "writerBehavior != null");
            Debug.Assert(model != null, "For null validation, model is required.");

            if (expectedProperty != null)
            {
                IEdmTypeReference propertyTypeReference = expectedProperty.Type;
                if (propertyTypeReference.IsNonEntityODataCollectionTypeKind())
                {
                    throw new ODataException(Strings.WriterValidationUtils_CollectionPropertiesMustNotHaveNullValue(expectedProperty.Name));
                }

                if (propertyTypeReference.IsODataPrimitiveTypeKind())
                {
                    // WCF DS allows null values for non-nullable primitive types, so we need to check for a knob which enables this behavior.
                    // See the description of ODataWriterBehavior.AllowNullValuesForNonNullablePrimitiveTypes for more details.
                    if (!propertyTypeReference.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
                    {
                        throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
                    }
                }
                else if (propertyTypeReference.IsStream())
                {
                    throw new ODataException(Strings.WriterValidationUtils_StreamPropertiesMustNotHaveNullValue(expectedProperty.Name));
                }
                else if (propertyTypeReference.IsODataComplexTypeKind())
                {
                    if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
                    {
                        IEdmComplexTypeReference complexTypeReference = propertyTypeReference.AsComplex();
                        if (!complexTypeReference.IsNullable)
                        {
                            throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
                        }
                    }
                }
            }
        }
Beispiel #15
0
 /// <summary>
 /// Enables the behavior of the WCF Data Services server.
 /// </summary>
 /// <param name="usesV1Provider">true if the server uses a V1 provider; otherwise false.</param>
 public void EnableWcfDataServicesServerBehavior(bool usesV1Provider)
 {
     // We have to reset the ATOM entry XML customization since in the server behavior no atom entry customization is used.
     this.SetAtomEntryXmlCustomization(null, null);
     this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesServerBehavior(usesV1Provider);
 }
 public void EnableDefaultBehavior()
 {
     this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
 }
 public ODataMessageWriterSettings()
 {
     this.CheckCharacters = true;
     this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
 }
Beispiel #18
0
 internal EntryScope(ODataEntry entry, bool skipWriting, bool writingResponse, ODataWriterBehavior writerBehavior) : base(ODataWriterCore.WriterState.Entry, entry, skipWriting)
 {
     if (entry != null)
     {
         this.duplicatePropertyNamesChecker = new Microsoft.Data.OData.DuplicatePropertyNamesChecker(writerBehavior.AllowDuplicatePropertyNames, writingResponse);
     }
 }
        /// <summary>
        /// Validates a null collection item against the expected type.
        /// </summary>
        /// <param name="expectedItemType">The expected item type or null if no expected item type exists.</param>
        /// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
        internal static void ValidateNullCollectionItem(IEdmTypeReference expectedItemType, ODataWriterBehavior writerBehavior)
        {
            DebugUtils.CheckNoExternalCallers();

            if (expectedItemType != null)
            {
                if (expectedItemType.IsODataPrimitiveTypeKind())
                {
                    // WCF DS allows null values for non-nullable primitive types, so we need to check for a knob which enables this behavior.
                    // See the description of ODataWriterBehavior.AllowNullValuesForNonNullablePrimitiveTypes for more details.
                    if (!expectedItemType.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
                    {
                        throw new ODataException(Strings.ValidationUtils_NullCollectionItemForNonNullableType(expectedItemType.ODataFullName()));
                    }
                }
            }
        }
 public void EnableWcfDataServicesServerBehavior(bool usesV1Provider)
 {
     this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesServerBehavior(usesV1Provider);
 }
Beispiel #21
0
 internal static void ValidateNullCollectionItem(IEdmTypeReference expectedItemType, ODataWriterBehavior writerBehavior)
 {
     if (((expectedItemType != null) && expectedItemType.IsODataPrimitiveTypeKind()) && (!expectedItemType.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ValidationUtils_NullCollectionItemForNonNullableType(expectedItemType.ODataFullName()));
     }
 }
 internal static void ValidateNullPropertyValue(IEdmProperty expectedProperty, ODataWriterBehavior writerBehavior, IEdmModel model)
 {
     if (expectedProperty != null)
     {
         IEdmTypeReference type = expectedProperty.Type;
         if (type.IsNonEntityODataCollectionTypeKind())
         {
             throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_CollectionPropertiesMustNotHaveNullValue(expectedProperty.Name));
         }
         if (type.IsODataPrimitiveTypeKind())
         {
             if (!type.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
             {
                 throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
             }
         }
         else
         {
             if (type.IsStream())
             {
                 throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_StreamPropertiesMustNotHaveNullValue(expectedProperty.Name));
             }
             if ((type.IsODataComplexTypeKind() && ValidationUtils.ShouldValidateComplexPropertyNullValue(model)) && !type.AsComplex().IsNullable)
             {
                 throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
             }
         }
     }
 }
Beispiel #23
0
        /// <summary>
        /// Validates a null collection item against the expected type.
        /// </summary>
        /// <param name="expectedItemType">The expected item type or null if no expected item type exists.</param>
        /// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
        internal static void ValidateNullCollectionItem(IEdmTypeReference expectedItemType, ODataWriterBehavior writerBehavior)
        {
            DebugUtils.CheckNoExternalCallers();

            if (expectedItemType != null)
            {
                if (expectedItemType.IsODataPrimitiveTypeKind())
                {
                    // WCF DS allows null values for non-nullable primitive types, so we need to check for a knob which enables this behavior.
                    // See the description of ODataWriterBehavior.AllowNullValuesForNonNullablePrimitiveTypes for more details.
                    if (!expectedItemType.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
                    {
                        throw new ODataException(Strings.ValidationUtils_NullCollectionItemForNonNullableType(expectedItemType.ODataFullName()));
                    }
                }
            }
        }
Beispiel #24
0
 internal static void ValidateNullPropertyValue(IEdmProperty expectedProperty, ODataWriterBehavior writerBehavior, IEdmModel model)
 {
     if (expectedProperty != null)
     {
         IEdmTypeReference type = expectedProperty.Type;
         if (type.IsNonEntityODataCollectionTypeKind())
         {
             throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_CollectionPropertiesMustNotHaveNullValue(expectedProperty.Name));
         }
         if (type.IsODataPrimitiveTypeKind())
         {
             if (!type.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
             {
                 throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
             }
         }
         else
         {
             if (type.IsStream())
             {
                 throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_StreamPropertiesMustNotHaveNullValue(expectedProperty.Name));
             }
             if ((type.IsODataComplexTypeKind() && ValidationUtils.ShouldValidateComplexPropertyNullValue(model)) && !type.AsComplex().IsNullable)
             {
                 throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
             }
         }
     }
 }
 /// <summary>
 /// Enables the behavior of the WCF Data Services server.
 /// </summary>
 /// <param name="usesV1Provider">true if the server uses a V1 provider; otherwise false.</param>
 public void EnableWcfDataServicesServerBehavior(bool usesV1Provider)
 {
     // We have to reset the ATOM entry XML customization since in the server behavior no atom entry customization is used.
     this.SetAtomEntryXmlCustomization(null, null);
     this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesServerBehavior(usesV1Provider);
 }
        public void EnableWcfDataServicesClientBehavior(
            Func<ODataEntry, XmlWriter, XmlWriter> startEntryXmlCustomizationCallback,
            Action<ODataEntry, XmlWriter, XmlWriter> endEntryXmlCustomizationCallback,
            string odataNamespace, 
            string typeScheme)
        {
            ExceptionUtils.CheckArgumentNotNull(odataNamespace, "odataNamespace");
            ExceptionUtils.CheckArgumentNotNull(typeScheme, "typeScheme");

            this.SetAtomEntryXmlCustomization(startEntryXmlCustomizationCallback, endEntryXmlCustomizationCallback);

            this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesClientBehavior(
                odataNamespace,
                typeScheme);
        }
Beispiel #27
0
 /// <summary>Initializes a new instance of the <see cref="T:Microsoft.Data.OData.ODataMessageWriterSettings" /> class with default settings. </summary>
 public ODataMessageWriterSettings()
     : base()
 {
     // Create the default writer behavior
     this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
 }
        /// <summary>
        /// Copy constructor to create a copy of the settings for OData writers.
        /// </summary>
        /// <param name="settings">Settings to create a copy from</param>
        public ODataMessageWriterSettings(ODataMessageWriterSettings settings)
        {
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");

            this.acceptCharSets = settings.acceptCharSets;
            this.acceptMediaTypes = settings.acceptMediaTypes;
            this.BaseUri = settings.BaseUri;
            this.CheckCharacters = settings.CheckCharacters;
            this.DisableMessageStreamDisposal = settings.DisableMessageStreamDisposal;
            this.format = settings.format;
            this.Indent = settings.Indent;
            this.messageQuotas = new ODataMessageQuotas(settings.MessageQuotas);
            this.useFormat = settings.useFormat;
            this.Version = settings.Version;
            this.atomFormatStartEntryXmlCustomizationCallback = settings.atomFormatStartEntryXmlCustomizationCallback;
            this.atomFormatEndEntryXmlCustomizationCallback = settings.atomFormatEndEntryXmlCustomizationCallback;

            // NOTE: writer behavior is immutable; copy by reference is ok.
            this.writerBehavior = settings.writerBehavior;
        }
Beispiel #29
0
 internal AtomEntryScope(ODataEntry entry, bool skipWriting, bool writingResponse, ODataWriterBehavior writerBehavior) : base(entry, skipWriting, writingResponse, writerBehavior)
 {
 }
Beispiel #30
0
 internal EntryScope(ODataEntry entry, bool skipWriting, bool writingResponse, ODataWriterBehavior writerBehavior) : base(ODataWriterCore.WriterState.Entry, entry, skipWriting)
 {
     if (entry != null)
     {
         this.duplicatePropertyNamesChecker = new Microsoft.Data.OData.DuplicatePropertyNamesChecker(writerBehavior.AllowDuplicatePropertyNames, writingResponse);
     }
 }
 /// <summary>
 /// Constructor to create a new entry scope.
 /// </summary>
 /// <param name="entry">The entry for the new scope.</param>
 /// <param name="skipWriting">true if the content of the scope to create should not be written.</param>
 /// <param name="writingResponse">true if we are writing a response, false if it's a request.</param>
 /// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
 internal AtomEntryScope(ODataEntry entry, bool skipWriting, bool writingResponse, ODataWriterBehavior writerBehavior)
     : base(entry, skipWriting, writingResponse, writerBehavior)
 {
     DebugUtils.CheckNoExternalCallers();
 }
        private static DateTimeOffset CreateDateTimeValue(object propertyValue, SyndicationItemProperty targetProperty, ODataWriterBehavior writerBehavior)
        {
            Debug.Assert(
                writerBehavior.FormatBehaviorKind != ODataBehaviorKind.WcfDataServicesClient,
                "CreateDateTimeValue should not be used in WCF DS client mode.");

            if (propertyValue == null)
            {
                return DateTimeOffset.Now;
            }

            if (propertyValue is DateTimeOffset)
            {
                return (DateTimeOffset)propertyValue;
            }

            if (propertyValue is DateTime)
            {
                // DateTimeOffset takes care of DateTimes of Unspecified kind so we won't end up 
                // with datetime without timezone info mapped to atom:updated or atom:published element.
                return new DateTimeOffset((DateTime)propertyValue);
            }

            string stringValue = propertyValue as string;
            if (stringValue != null)
            {
                DateTimeOffset date;
                if (!DateTimeOffset.TryParse(stringValue, out date))
                {
                    DateTime result;
                    if (!DateTime.TryParse(stringValue, out result))
                    {
                        throw new ODataException(o.Strings.EpmSyndicationWriter_DateTimePropertyCanNotBeConverted(targetProperty.ToString()));
                    }

                    return new DateTimeOffset(result);
                }

                return date;
            }

            try
            {
                return new DateTimeOffset(Convert.ToDateTime(propertyValue, CultureInfo.InvariantCulture));
            }
            catch (Exception e)
            {
                if (!ExceptionUtils.IsCatchableExceptionType(e))
                {
                    throw;
                }

                throw new ODataException(o.Strings.EpmSyndicationWriter_DateTimePropertyCanNotBeConverted(targetProperty.ToString()));
            }
        }
Beispiel #33
0
 /// <summary>
 /// Enables the default behavior of the OData library.
 /// </summary>
 public void EnableDefaultBehavior()
 {
     // We have to reset the ATOM entry XML customization since in the default behavior no atom entry customization is used.
     this.SetAtomEntryXmlCustomization(null, null);
     this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
 }
Beispiel #34
0
 public ODataMessageWriterSettings()
 {
     this.CheckCharacters = true;
     this.writerBehavior  = ODataWriterBehavior.DefaultBehavior;
 }
        private static string CreateDateTimeStringValue(object propertyValue, ODataWriterBehavior writerBehavior)
        {
            Debug.Assert(
                writerBehavior.FormatBehaviorKind == ODataBehaviorKind.WcfDataServicesClient,
                "CreateDateTimeStringValue should only be used in WCF DS client mode.");

            if (propertyValue == null)
            {
                propertyValue = DateTimeOffset.Now;
            }

            if (propertyValue is DateTime)
            {
                // DateTimeOffset takes care of DateTimes of Unspecified kind so we won't end up 
                // with datetime without timezone info mapped to atom:updated or atom:published element.
                propertyValue = new DateTimeOffset((DateTime)propertyValue);
            }

            // For DateTimeOffset values we need to use the ATOM format when translating them
            // to strings since the value will be used in ATOM metadata.
            if (propertyValue is DateTimeOffset)
            {
                return ODataAtomConvert.ToAtomString((DateTimeOffset)propertyValue);
            }

            return EpmWriterUtils.GetPropertyValueAsText(propertyValue);
        }
Beispiel #36
0
 public void EnableDefaultBehavior()
 {
     this.writerBehavior = ODataWriterBehavior.DefaultBehavior;
 }
Beispiel #37
0
 internal static void ValidateNullCollectionItem(IEdmTypeReference expectedItemType, ODataWriterBehavior writerBehavior)
 {
     if (((expectedItemType != null) && expectedItemType.IsODataPrimitiveTypeKind()) && (!expectedItemType.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.ValidationUtils_NullCollectionItemForNonNullableType(expectedItemType.ODataFullName()));
     }
 }
Beispiel #38
0
 public void EnableWcfDataServicesServerBehavior(bool usesV1Provider)
 {
     this.writerBehavior = ODataWriterBehavior.CreateWcfDataServicesServerBehavior(usesV1Provider);
 }
        /// <summary>
        /// Validates that the expected property allows null value.
        /// </summary>
        /// <param name="expectedProperty">The metadata for the expected property.</param>
        /// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
        /// <param name="model">The model to use to get the data service version.</param>
        internal static void ValidateNullPropertyValue(IEdmProperty expectedProperty, ODataWriterBehavior writerBehavior, IEdmModel model)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(writerBehavior != null, "writerBehavior != null");
            Debug.Assert(model != null, "For null validation, model is required.");

            if (expectedProperty != null)
            {
                IEdmTypeReference propertyTypeReference = expectedProperty.Type;
                if (propertyTypeReference.IsNonEntityODataCollectionTypeKind())
                {
                    throw new ODataException(Strings.WriterValidationUtils_CollectionPropertiesMustNotHaveNullValue(expectedProperty.Name));
                }

                if (propertyTypeReference.IsODataPrimitiveTypeKind())
                {
                    // WCF DS allows null values for non-nullable primitive types, so we need to check for a knob which enables this behavior.
                    // See the description of ODataWriterBehavior.AllowNullValuesForNonNullablePrimitiveTypes for more details.
                    if (!propertyTypeReference.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
                    {
                        throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
                    }
                }
                else if (propertyTypeReference.IsStream())
                {
                    throw new ODataException(Strings.WriterValidationUtils_StreamPropertiesMustNotHaveNullValue(expectedProperty.Name));
                }
                else if (propertyTypeReference.IsODataComplexTypeKind())
                {
                    if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
                    {
                        IEdmComplexTypeReference complexTypeReference = propertyTypeReference.AsComplex();
                        if (!complexTypeReference.IsNullable)
                        {
                            throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(expectedProperty.Name, expectedProperty.Type.ODataFullName()));
                        }
                    }
                }
            }
        }