/// <summary>Constructor.</summary>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageReaderSettings">Configuration settings of the OData reader.</param>
        public ODataMetadataInputContext(
            ODataMessageInfo messageInfo,
            ODataMessageReaderSettings messageReaderSettings)
            : base(ODataFormat.Metadata, messageInfo, messageReaderSettings)
        {
            Debug.Assert(messageInfo.MessageStream != null, "messageInfo.MessageStream != null");

            try
            {
                // Which encoding do we use when reading XML payloads
                this.baseXmlReader = ODataMetadataReaderUtils.CreateXmlReader(messageInfo.MessageStream, messageInfo.Encoding, messageReaderSettings);

                // We use the buffering reader here only for in-stream error detection (not for buffering).
                this.xmlReader = new BufferingXmlReader(
                    this.baseXmlReader,
                    /*parentXmlReader*/ null,
                    messageReaderSettings.BaseUri,
                    /*disableXmlBase*/ false,
                    messageReaderSettings.MessageQuotas.MaxNestingDepth);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e))
                {
                    messageInfo.MessageStream.Dispose();
                }

                throw;
            }
        }
Ejemplo n.º 2
0
 protected override void DisposeImplementation()
 {
     try
     {
         if (this.baseXmlReader != null)
         {
             this.baseXmlReader.Dispose();
         }
     }
     finally
     {
         this.baseXmlReader = null;
         this.xmlReader     = null;
     }
 }
Ejemplo n.º 3
0
 private ODataMetadataInputContext(ODataFormat format, Stream messageStream, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver) : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull <ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull <ODataMessageReaderSettings>(messageReaderSettings, "messageReaderSettings");
     try
     {
         this.baseXmlReader = ODataAtomReaderUtils.CreateXmlReader(messageStream, encoding, messageReaderSettings);
         this.xmlReader     = new BufferingXmlReader(this.baseXmlReader, null, messageReaderSettings.BaseUri, false, messageReaderSettings.MessageQuotas.MaxNestingDepth, messageReaderSettings.ReaderBehavior.ODataNamespace);
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (messageStream != null))
         {
             messageStream.Dispose();
         }
         throw;
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Attempts to read the current element as an annotation element.
        /// </summary>
        /// <param name="annotation">If this method returned true, this is the instance annotation information from the parsed element.</param>
        /// <returns>true if the element was an annotation element, false if it wasn't.</returns>
        /// <remarks>
        /// Pre-Condition:   XmlNodeType.Element    - The element to read.
        /// Post-Condition:  XmlNodeType.EndElement - The end tag of the element (if the element was a non-empty annotation element).
        ///                  XmlNodeType.Element    - The same element as the pre-condition if this method returned false, or an empty annotation element.
        /// </remarks>
        internal bool TryReadAnnotation(out AtomInstanceAnnotation annotation)
        {
            BufferingXmlReader xmlReader = this.inputContext.XmlReader;

            Debug.Assert(xmlReader != null, "xmlReader != null");
            Debug.Assert(xmlReader.NodeType == XmlNodeType.Element, "xmlReader must be positioned on an Element");

            annotation = null;

            if (this.propertyAndValueDeserializer.MessageReaderSettings.ShouldIncludeAnnotation != null &&
                xmlReader.NamespaceEquals(this.odataMetadataNamespace) &&
                xmlReader.LocalNameEquals(this.attributeElementName))
            {
                annotation = AtomInstanceAnnotation.CreateFrom(this.inputContext, this.propertyAndValueDeserializer);
            }

            return(annotation != null);
        }
Ejemplo n.º 5
0
        /// <summary>Constructor.</summary>
        /// <param name="format">The format for this input context.</param>
        /// <param name="messageStream">The stream to read data from.</param>
        /// <param name="encoding">The encoding to use to read the input.</param>
        /// <param name="messageReaderSettings">Configuration settings of the OData reader.</param>
        /// <param name="version">The OData protocol version to be used for reading the payload.</param>
        /// <param name="readingResponse">true if reading a response message; otherwise false.</param>
        /// <param name="synchronous">true if the input should be read synchronously; false if it should be read asynchronously.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs read from the payload.</param>
        internal ODataMetadataInputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(messageStream != null, "stream != null");

            ExceptionUtils.CheckArgumentNotNull(format, "format");
            ExceptionUtils.CheckArgumentNotNull(messageReaderSettings, "messageReaderSettings");

            try
            {
                this.baseXmlReader = ODataAtomReaderUtils.CreateXmlReader(messageStream, encoding, messageReaderSettings);

                // We use the buffering reader here only for in-stream error detection (not for buffering).
                this.xmlReader = new BufferingXmlReader(
                    this.baseXmlReader,
                    /*parentXmlReader*/ null,
                    messageReaderSettings.BaseUri,
                    /*disableXmlBase*/ false,
                    messageReaderSettings.MessageQuotas.MaxNestingDepth,
                    messageReaderSettings.ReaderBehavior.ODataNamespace);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
                {
                    messageStream.Dispose();
                }

                throw;
            }
        }
        /// <summary>
        /// Perform the actual cleanup work.
        /// </summary>
        /// <param name="disposing">If 'true' this method is called from user code; if 'false' it is called by the runtime.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                try
                {
                    if (this.baseXmlReader != null)
                    {
                        ((IDisposable)this.baseXmlReader).Dispose();
                    }
                }
                finally
                {
                    this.baseXmlReader = null;
                    this.xmlReader     = null;
                }
            }

            base.Dispose(disposing);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new ATOM annotation parser.
        /// </summary>
        /// <param name="inputContext">The input context this annotation reader should use to read annotation elements.</param>
        /// <param name="propertyAndValueDeserializer">The property and value deserializer to use to read the value of an annotation element.</param>
        internal ODataAtomAnnotationReader(ODataAtomInputContext inputContext, ODataAtomPropertyAndValueDeserializer propertyAndValueDeserializer)
        {
            this.inputContext = inputContext;
            this.propertyAndValueDeserializer = propertyAndValueDeserializer;
            BufferingXmlReader xmlReader = this.inputContext.XmlReader;

            Debug.Assert(xmlReader != null, "xmlReader != null");
            Debug.Assert(xmlReader.NameTable != null, "xmlReader.NameTable != null");
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationTargetAttribute);
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationTermAttribute);
            xmlReader.NameTable.Add(AtomConstants.AtomTypeAttributeName);
            xmlReader.NameTable.Add(AtomConstants.ODataNullAttributeName);
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationStringAttribute);
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationBoolAttribute);
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationDecimalAttribute);
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationIntAttribute);
            xmlReader.NameTable.Add(AtomConstants.ODataAnnotationFloatAttribute);
            this.odataMetadataNamespace = xmlReader.NameTable.Add(AtomConstants.ODataMetadataNamespace);
            this.attributeElementName   = xmlReader.NameTable.Add(AtomConstants.ODataAnnotationElementName);
        }
Ejemplo n.º 8
0
        internal ODataAtomInputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
        {
            Debug.Assert(messageStream != null, "stream != null");

            try
            {
                ExceptionUtils.CheckArgumentNotNull(format, "format");
                ExceptionUtils.CheckArgumentNotNull(messageReaderSettings, "messageReaderSettings");

                this.baseXmlReader = ODataAtomReaderUtils.CreateXmlReader(messageStream, encoding, messageReaderSettings);

                // For WCF DS Server behavior we need to turn off xml:base processing for V1/V2 back compat.
                this.xmlReader = new BufferingXmlReader(
                    this.baseXmlReader,
                    /*parentXmlReader*/ null,
                    messageReaderSettings.BaseUri,
                    /*disableXmlBase*/ false,
                    messageReaderSettings.MessageQuotas.MaxNestingDepth);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
                {
                    messageStream.Dispose();
                }

                throw;
            }
        }
        /// <summary>
        /// Reads the content of an error element.
        /// </summary>
        /// <param name="xmlReader">The Xml reader to read the error payload from.</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of recursive internalexception elements to allow.</param>
        /// <returns>The <see cref="ODataError"/> representing the error.</returns>
        /// <remarks>
        /// This method is used to read top-level errors as well as in-stream errors (from inside the buffering Xml reader).
        /// Pre-Condition:  XmlNodeType.Element   - The m:error start element.
        /// Post-Condition: XmlNodeType.EndElement - The m:error end-element.
        ///                 XmlNodeType.Element    - The empty m:error start element.
        /// </remarks>
        internal static ODataError ReadErrorElement(BufferingXmlReader xmlReader, int maxInnerErrorDepth)
        {
            Debug.Assert(xmlReader != null, "this.XmlReader != null");
            Debug.Assert(xmlReader.NodeType == XmlNodeType.Element, "xmlReader.NodeType == XmlNodeType.Element");
            Debug.Assert(xmlReader.LocalName == AtomConstants.ODataErrorElementName, "Expected reader to be positioned on <m:error> element.");
            Debug.Assert(xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace), "this.XmlReader.NamespaceEquals(atomizedMetadataNamespace)");

            ODataError error = new ODataError();
            DuplicateErrorElementPropertyBitMask elementsReadBitmask = DuplicateErrorElementPropertyBitMask.None;

            if (!xmlReader.IsEmptyElement)
            {
                // Move to the first child node of the element.
                xmlReader.Read();

                do
                {
                    switch (xmlReader.NodeType)
                    {
                    case XmlNodeType.EndElement:
                        // end of the <m:error> element
                        continue;

                    case XmlNodeType.Element:
                        if (xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace))
                        {
                            switch (xmlReader.LocalName)
                            {
                            // <m:code>
                            case AtomConstants.ODataErrorCodeElementName:
                                VerifyErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateErrorElementPropertyBitMask.Code,
                                    AtomConstants.ODataErrorCodeElementName);
                                error.ErrorCode = xmlReader.ReadElementValue();
                                continue;

                            // <m:message >
                            case AtomConstants.ODataErrorMessageElementName:
                                VerifyErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateErrorElementPropertyBitMask.Message,
                                    AtomConstants.ODataErrorMessageElementName);
                                error.Message = xmlReader.ReadElementValue();
                                continue;

                            // <m:innererror>
                            case AtomConstants.ODataInnerErrorElementName:
                                VerifyErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateErrorElementPropertyBitMask.InnerError,
                                    AtomConstants.ODataInnerErrorElementName);
                                error.InnerError = ReadInnerErrorElement(xmlReader, 0 /* recursionDepth */, maxInnerErrorDepth);
                                continue;

                            default:
                                break;
                            }
                        }

                        break;

                    default:
                        break;
                    }

                    xmlReader.Skip();
                }while (xmlReader.NodeType != XmlNodeType.EndElement);
            }

            return(error);
        }
        /// <summary>
        /// Reads the content of an inner error element.
        /// </summary>
        /// <param name="xmlReader">The (buffering) Xml reader to read the error payload from.</param>
        /// <param name="recursionDepth">The number of times this function has been called recursively.</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of recursive internalexception elements to allow.</param>
        /// <returns>The <see cref="ODataInnerError"/> representing the inner error.</returns>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - the m:innererror or m:internalexception element
        /// Post-Condition: Any                 - the node after the m:innererror/m:internalexception end element or the node after the empty m:innererror/m:internalexception element node.
        /// </remarks>
        private static ODataInnerError ReadInnerErrorElement(BufferingXmlReader xmlReader, int recursionDepth, int maxInnerErrorDepth)
        {
            Debug.Assert(xmlReader != null, "this.XmlReader != null");
            Debug.Assert(xmlReader.NodeType == XmlNodeType.Element, "xmlReader.NodeType == XmlNodeType.Element");
            Debug.Assert(
                xmlReader.LocalName == AtomConstants.ODataInnerErrorElementName ||
                xmlReader.LocalName == AtomConstants.ODataInnerErrorInnerErrorElementName,
                "Expected reader to be positioned on 'm:innererror' or 'm:internalexception' element.");
            Debug.Assert(xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace), "this.XmlReader.NamespaceEquals(this.ODataMetadataNamespace)");

            ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, maxInnerErrorDepth);

            ODataInnerError innerError = new ODataInnerError();
            DuplicateInnerErrorElementPropertyBitMask elementsReadBitmask = DuplicateInnerErrorElementPropertyBitMask.None;

            if (!xmlReader.IsEmptyElement)
            {
                // Move to the first child node of the element.
                xmlReader.Read();

                do
                {
                    switch (xmlReader.NodeType)
                    {
                    case XmlNodeType.EndElement:
                        // end of the <m:innererror> or <m:internalexception> element
                        continue;

                    case XmlNodeType.Element:
                        if (xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace))
                        {
                            switch (xmlReader.LocalName)
                            {
                            // <m:message>
                            case AtomConstants.ODataInnerErrorMessageElementName:
                                VerifyInnerErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateInnerErrorElementPropertyBitMask.Message,
                                    AtomConstants.ODataInnerErrorMessageElementName);
                                innerError.Message = xmlReader.ReadElementValue();
                                continue;

                            // <m:type>
                            case AtomConstants.ODataInnerErrorTypeElementName:
                                VerifyInnerErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateInnerErrorElementPropertyBitMask.TypeName,
                                    AtomConstants.ODataInnerErrorTypeElementName);
                                innerError.TypeName = xmlReader.ReadElementValue();
                                continue;

                            // <m:stacktrace>
                            case AtomConstants.ODataInnerErrorStackTraceElementName:
                                VerifyInnerErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateInnerErrorElementPropertyBitMask.StackTrace,
                                    AtomConstants.ODataInnerErrorStackTraceElementName);
                                innerError.StackTrace = xmlReader.ReadElementValue();
                                continue;

                            // <m:internalexception>
                            case AtomConstants.ODataInnerErrorInnerErrorElementName:
                                VerifyInnerErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateInnerErrorElementPropertyBitMask.InternalException,
                                    AtomConstants.ODataInnerErrorInnerErrorElementName);
                                innerError.InnerError = ReadInnerErrorElement(xmlReader, recursionDepth, maxInnerErrorDepth);
                                continue;

                            default:
                                break;
                            }
                        }

                        break;

                    default:
                        break;
                    }

                    xmlReader.Skip();
                }while (xmlReader.NodeType != XmlNodeType.EndElement);
            }

            // Read over the end element, or empty start element.
            xmlReader.Read();

            return(innerError);
        }
        internal static void AssertBuffering(this BufferingXmlReader bufferedXmlReader)
        {
#if DEBUG
            Debug.Assert(bufferedXmlReader.IsBuffering, "bufferedXmlReader.IsBuffering");
#endif
        }