Esempio n. 1
0
        private void ProcessContentType(params ODataPayloadKind[] payloadKinds)
        {
            MediaType type;
            string    contentTypeHeader = this.GetContentTypeHeader();

            this.format = MediaTypeUtils.GetFormatFromContentType(contentTypeHeader, payloadKinds, this.MediaTypeResolver, out type, out this.encoding, out this.readerPayloadKind, out this.batchBoundary);
        }
Esempio n. 2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageStream">The message stream to write the payload to.</param>
        /// <param name="encoding">The encoding to use for the payload.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="synchronous">true if the output should be written synchronously; false if it should be written asynchronously.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        internal ODataMetadataOutputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(messageStream != null, "messageStream != null");
            Debug.Assert(synchronous, "Metadata output context is only supported in synchronous operations.");

            try
            {
                this.messageOutputStream = messageStream;
                this.xmlWriter           = ODataAtomWriterUtils.CreateXmlWriter(messageStream, messageWriterSettings, encoding);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the output context.
                if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
                {
                    messageStream.Dispose();
                }

                throw;
            }
        }
        /// <summary>
        /// Create a metadata input context.
        /// </summary>
        /// <param name="format">The format for this input context.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <returns>The newly created input context.</returns>
        internal static ODataInputContext Create(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");

            Stream messageStream = message.GetStream();

            return(new ODataMetadataInputContext(
                       format,
                       messageStream,
                       encoding,
                       messageReaderSettings,
                       version,
                       readingResponse,
                       true,
                       model,
                       urlResolver));
        }
Esempio n. 4
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;
        }
 public void SetContentType(ODataFormat payloadFormat)
 {
     this.acceptCharSets = null;
     this.acceptMediaTypes = null;
     this.format = payloadFormat;
     this.useFormat = true;
 }
Esempio n. 6
0
        /// <summary>
        /// Create metadata output context.
        /// </summary>
        /// <param name="format">The format to create the output context for.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        /// <returns>The newly created output context.</returns>
        internal static ODataOutputContext Create(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(format == ODataFormat.Metadata, "This method only supports the Metadata format.");
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageWriterSettings != null, "messageWriterSettings != null");

            Stream messageStream = message.GetStream();

            return(new ODataMetadataOutputContext(
                       format,
                       messageStream,
                       encoding,
                       messageWriterSettings,
                       writingResponse,
                       true,
                       model,
                       urlResolver));
        }
Esempio n. 7
0
 private ODataAtomOutputContext(ODataFormat format, Stream messageStream, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver) : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
 {
     try
     {
         Stream asynchronousOutputStream;
         this.messageOutputStream = messageStream;
         if (synchronous)
         {
             asynchronousOutputStream = messageStream;
         }
         else
         {
             this.asynchronousOutputStream = new AsyncBufferedStream(messageStream);
             asynchronousOutputStream = this.asynchronousOutputStream;
         }
         this.xmlRootWriter = ODataAtomWriterUtils.CreateXmlWriter(asynchronousOutputStream, messageWriterSettings, encoding);
         this.xmlWriter = this.xmlRootWriter;
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (messageStream != null))
         {
             messageStream.Dispose();
         }
         throw;
     }
 }
Esempio n. 8
0
        internal static ODataFormat GetFormatFromContentType(string contentTypeHeader, ODataPayloadKind[] supportedPayloadKinds, MediaTypeResolver mediaTypeResolver, out MediaType mediaType, out Encoding encoding, out ODataPayloadKind selectedPayloadKind, out string batchBoundary)
        {
            ODataFormat format = GetFormatFromContentType(contentTypeHeader, supportedPayloadKinds, mediaTypeResolver, out mediaType, out encoding, out selectedPayloadKind);

            if (selectedPayloadKind == ODataPayloadKind.Batch)
            {
                KeyValuePair <string, string> pair = new KeyValuePair <string, string>();
                IEnumerable <KeyValuePair <string, string> > parameters = mediaType.Parameters;
                if (parameters != null)
                {
                    bool flag = false;
                    foreach (KeyValuePair <string, string> pair2 in from p in parameters
                             where HttpUtils.CompareMediaTypeParameterNames("boundary", p.Key)
                             select p)
                    {
                        if (flag)
                        {
                            throw new ODataException(Microsoft.Data.OData.Strings.MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads(contentTypeHeader, "boundary"));
                        }
                        pair = pair2;
                        flag = true;
                    }
                }
                if (pair.Key == null)
                {
                    throw new ODataException(Microsoft.Data.OData.Strings.MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads(contentTypeHeader, "boundary"));
                }
                batchBoundary = pair.Value;
                ValidationUtils.ValidateBoundaryString(batchBoundary);
                return(format);
            }
            batchBoundary = null;
            return(format);
        }
 public void SetContentType(string acceptableMediaTypes, string acceptableCharSets)
 {
     this.acceptMediaTypes = acceptableMediaTypes;
     this.acceptCharSets = acceptableCharSets;
     this.format = null;
     this.useFormat = false;
 }
Esempio n. 10
0
 /// <summary>
 /// Sets the acceptable media types and character sets from which the content type will be computed when writing the payload.
 /// </summary>
 /// <param name="acceptableMediaTypes">
 /// The acceptable media types used to determine the content type of the message.
 /// This is a comma separated list of content types as specified in RFC 2616, Section 14.1
 /// </param>
 /// <param name="acceptableCharSets">
 /// The acceptable charsets to use to the determine the encoding of the message.
 /// This is a comma separated list of charsets as specified in RFC 2616, Section 14.2
 /// </param>
 /// <remarks>Calling this method replaces any previously set content-type settings.</remarks>
 public void SetContentType(string acceptableMediaTypes, string acceptableCharSets)
 {
     this.acceptMediaTypes = acceptableMediaTypes;
     this.acceptCharSets   = acceptableCharSets;
     this.format           = null;
     this.useFormat        = false;
 }
Esempio n. 11
0
 /// <summary>
 /// Sets the format to be used when writing the payload. This will automatically set a compatible
 /// content type header.
 /// </summary>
 /// <param name="payloadFormat">The format to use for writing the payload.</param>
 /// <remarks>Calling this method replaces any previously set content-type settings.</remarks>
 public void SetContentType(ODataFormat payloadFormat)
 {
     this.acceptCharSets   = null;
     this.acceptMediaTypes = null;
     this.format           = payloadFormat;
     this.useFormat        = true;
 }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageStream">The message stream to write the payload to.</param>
        /// <param name="encoding">The encoding to use for the payload.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="synchronous">true if the output should be written synchronously; false if it should be written asynchronously.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        private ODataRawOutputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
        {
            try
            {
                this.messageOutputStream = messageStream;
                this.encoding = encoding;

                if (synchronous)
                {
                    this.outputStream = messageStream;
                }
                else
                {
                    this.asynchronousOutputStream = new AsyncBufferedStream(messageStream);
                    this.outputStream = this.asynchronousOutputStream;
                }
            }
            catch
            {
                messageStream.Dispose();
                throw;
            }
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageStream">The message stream to write the payload to.</param>
        /// <param name="encoding">The encoding to use for the payload.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="synchronous">true if the output should be written synchronously; false if it should be written asynchronously.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        private ODataMetadataOutputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
        {
            Debug.Assert(synchronous, "Metadata output context is only supported in synchronous operations.");

            try
            {
                this.messageOutputStream = messageStream;
                this.xmlWriter = ODataAtomWriterUtils.CreateXmlWriter(messageStream, messageWriterSettings, encoding);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the output context.
                if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
                {
                    messageStream.Dispose();
                }

                throw;
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageStream">The message stream to write the payload to.</param>
        /// <param name="encoding">The encoding to use for the payload.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="synchronous">true if the output should be written synchronously; false if it should be written asynchronously.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        internal ODataRawOutputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(messageStream != null, "messageStream != null");

            try
            {
                this.messageOutputStream = messageStream;
                this.encoding            = encoding;

                if (synchronous)
                {
                    this.outputStream = messageStream;
                }
                else
                {
                    this.asynchronousOutputStream = new AsyncBufferedStream(messageStream);
                    this.outputStream             = this.asynchronousOutputStream;
                }
            }
            catch
            {
                messageStream.Dispose();
                throw;
            }
        }
Esempio n. 15
0
 private ODataJsonOutputContext(ODataFormat format, Stream messageStream, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver) : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
 {
     try
     {
         Stream asynchronousOutputStream;
         this.messageOutputStream = messageStream;
         if (synchronous)
         {
             asynchronousOutputStream = messageStream;
         }
         else
         {
             this.asynchronousOutputStream = new AsyncBufferedStream(messageStream);
             asynchronousOutputStream = this.asynchronousOutputStream;
         }
         this.textWriter = new StreamWriter(asynchronousOutputStream, encoding);
         this.jsonWriter = new Microsoft.Data.OData.Json.JsonWriter(this.textWriter, messageWriterSettings.Indent);
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (messageStream != null))
         {
             messageStream.Dispose();
         }
         throw;
     }
 }
Esempio n. 16
0
 private static Action<Stream> CreateErrorSerializer(ODataFormat responseFormat, HandleExceptionArgs args, Encoding encoding)
 {
     ErrorHandler handler = new ErrorHandler(args, encoding);
     if (responseFormat == ODataFormat.VerboseJson)
     {
         return new Action<Stream>(handler.SerializeJsonErrorToStream);
     }
     return new Action<Stream>(handler.SerializeXmlErrorToStream);
 }
Esempio n. 17
0
        /// <summary>
        /// Asynchronously creates an instance of the input context for the specified format.
        /// </summary>
        /// <param name="format">The format to create the context for.</param>
        /// <param name="readerPayloadKind">The <see cref="ODataPayloadKind"/> to read.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <returns>Task which when completed returned the newly created input context.</returns>
        internal static Task <ODataInputContext> CreateInputContextAsync(
            ODataFormat format,
            ODataPayloadKind readerPayloadKind,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(format != ODataFormat.Metadata, "Async reading of metadata documents is not supported.");
            Debug.Assert(readerPayloadKind != ODataPayloadKind.MetadataDocument, "Async reading of metadata documents is not supported.");

            if (format == ODataFormat.Atom)
            {
                return(ODataAtomInputContext.CreateAsync(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver));
            }

            if (format == ODataFormat.VerboseJson)
            {
                return(ODataJsonInputContext.CreateAsync(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver));
            }

            if (format == ODataFormat.Batch || format == ODataFormat.RawValue)
            {
                return(ODataRawInputContext.CreateAsync(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver,
                           readerPayloadKind));
            }

            throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataInputContext_CreateInputContext_UnrecognizedFormat));
        }
Esempio n. 18
0
 internal ResponseBodyWriter(bool hasMoved, IDataService service, IEnumerator queryResults, RequestDescription requestDescription, IODataResponseMessage responseMessage, ODataPayloadKind payloadKind)
 {
     this.hasMoved = hasMoved;
     this.service = service;
     this.queryResults = queryResults;
     this.requestDescription = requestDescription;
     this.responseMessage = responseMessage;
     this.payloadKind = payloadKind;
     this.encoding = HttpProcessUtility.EncodingFromAcceptCharset(this.service.OperationContext.Host.RequestAcceptCharSet);
     if ((((payloadKind == ODataPayloadKind.Entry) || (payloadKind == ODataPayloadKind.Feed)) || ((payloadKind == ODataPayloadKind.Property) || (payloadKind == ODataPayloadKind.Collection))) || (((payloadKind == ODataPayloadKind.EntityReferenceLink) || (payloadKind == ODataPayloadKind.EntityReferenceLinks)) || (((payloadKind == ODataPayloadKind.Error) || (payloadKind == ODataPayloadKind.ServiceDocument)) || (payloadKind == ODataPayloadKind.Parameter))))
     {
         DataServiceHostWrapper host = service.OperationContext.Host;
         if (WebUtil.GetEffectiveMaxResponseVersion(service.Configuration.DataServiceBehavior.MaxProtocolVersion, host.RequestMaxVersion) > RequestDescription.Version2Dot0)
         {
             bool isEntityOrFeed = (payloadKind == ODataPayloadKind.Entry) || (payloadKind == ODataPayloadKind.Feed);
             if (WebUtil.ResponseMediaTypeWouldBeJsonLight(host.RequestAccept, isEntityOrFeed))
             {
                 requestDescription.VerifyAndRaiseResponseVersion(RequestDescription.Version3Dot0, service);
                 host.ResponseVersion = RequestDescription.Version3Dot0.ToString() + ";";
             }
         }
     }
     if (this.requestDescription.TargetKind == RequestTargetKind.MediaResource)
     {
         this.mediaResourceStream = service.StreamProvider.GetReadStream(this.queryResults.Current, RequestDescription.GetStreamProperty(this.requestDescription), this.service.OperationContext);
     }
     else if (payloadKind != ODataPayloadKind.BinaryValue)
     {
         string requestAcceptCharSet = this.service.OperationContext.Host.RequestAcceptCharSet;
         if (string.IsNullOrEmpty(requestAcceptCharSet) || (requestAcceptCharSet == "*"))
         {
             requestAcceptCharSet = "UTF-8";
         }
         if ((payloadKind == ODataPayloadKind.Value) && !string.IsNullOrEmpty(this.requestDescription.MimeType))
         {
             this.messageWriter = CreateMessageWriter(this.AbsoluteServiceUri, this.service, this.requestDescription.ActualResponseVersion, responseMessage, ODataFormat.RawValue);
         }
         else
         {
             this.messageWriter = CreateMessageWriter(this.AbsoluteServiceUri, this.service, this.requestDescription.ActualResponseVersion, responseMessage, this.service.OperationContext.Host.RequestAccept, requestAcceptCharSet);
         }
         try
         {
             this.contentFormat = ODataUtils.SetHeadersForPayload(this.messageWriter, payloadKind);
             if ((payloadKind == ODataPayloadKind.Value) && !string.IsNullOrEmpty(this.requestDescription.MimeType))
             {
                 responseMessage.SetHeader("Content-Type", this.requestDescription.MimeType);
             }
         }
         catch (ODataContentTypeException exception)
         {
             throw new DataServiceException(0x19f, null, System.Data.Services.Strings.DataServiceException_UnsupportedMediaType, null, exception);
         }
         string headerValue = this.requestDescription.ResponseVersion.ToString() + ";";
         responseMessage.SetHeader("DataServiceVersion", headerValue);
     }
 }
Esempio n. 19
0
 protected ODataOutputContext(ODataFormat format, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull<ODataMessageWriterSettings>(messageWriterSettings, "messageWriterSettings");
     this.format = format;
     this.messageWriterSettings = messageWriterSettings;
     this.writingResponse = writingResponse;
     this.synchronous = synchronous;
     this.model = model;
     this.urlResolver = urlResolver;
 }
Esempio n. 20
0
 protected ODataOutputContext(ODataFormat format, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull <ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull <ODataMessageWriterSettings>(messageWriterSettings, "messageWriterSettings");
     this.format = format;
     this.messageWriterSettings = messageWriterSettings;
     this.writingResponse       = writingResponse;
     this.synchronous           = synchronous;
     this.model       = model;
     this.urlResolver = urlResolver;
 }
Esempio n. 21
0
 protected ODataInputContext(ODataFormat format, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull <ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull <ODataMessageReaderSettings>(messageReaderSettings, "messageReaderSettings");
     this.format = format;
     this.messageReaderSettings = messageReaderSettings;
     this.version         = version;
     this.readingResponse = readingResponse;
     this.synchronous     = synchronous;
     this.model           = model;
     this.urlResolver     = urlResolver;
 }
Esempio n. 22
0
 protected ODataInputContext(ODataFormat format, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull<ODataMessageReaderSettings>(messageReaderSettings, "messageReaderSettings");
     this.format = format;
     this.messageReaderSettings = messageReaderSettings;
     this.version = version;
     this.readingResponse = readingResponse;
     this.synchronous = synchronous;
     this.model = model;
     this.urlResolver = urlResolver;
 }
Esempio n. 23
0
        /// <summary>
        /// Creates an instance of the output context for the specified format.
        /// </summary>
        /// <param name="format">The format to create the context for.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        /// <returns>Task which represents the pending create operation.</returns>
        internal static Task <ODataOutputContext> CreateOutputContextAsync(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();

            if (format == ODataFormat.Atom)
            {
                return(ODataAtomOutputContext.CreateAsync(
                           format,
                           message,
                           encoding,
                           messageWriterSettings,
                           writingResponse,
                           model,
                           urlResolver));
            }

            if (format == ODataFormat.VerboseJson)
            {
                return(ODataJsonOutputContext.CreateAsync(
                           format,
                           message,
                           encoding,
                           messageWriterSettings,
                           writingResponse,
                           model,
                           urlResolver));
            }

            //// Metadata output is only supported in synchronous operation.

            if (format == ODataFormat.Batch || format == ODataFormat.RawValue)
            {
                return(ODataRawOutputContext.CreateAsync(
                           format,
                           message,
                           encoding,
                           messageWriterSettings,
                           writingResponse,
                           model,
                           urlResolver));
            }

            throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataOutputContext_CreateOutputContext_UnrecognizedFormat));
        }
Esempio n. 24
0
        /// <summary>
        /// Determine the <see cref="ODataFormat"/> to use for the given <paramref name="contentTypeHeader"/>. If no supported content type
        /// is found an exception is thrown.
        /// </summary>
        /// <param name="contentTypeHeader">The name of the content type to be checked.</param>
        /// <param name="supportedPayloadKinds">All possiblel kinds of payload that can be read with this content type.</param>
        /// <param name="mediaTypeResolver">The media type resolver to use when interpreting the content type.</param>
        /// <param name="mediaType">The media type parsed from the <paramref name="contentTypeHeader"/>.</param>
        /// <param name="encoding">The encoding from the content type or the default encoding for the <paramref name="mediaType" />.</param>
        /// <param name="selectedPayloadKind">
        /// The payload kind that was selected form the list of <paramref name="supportedPayloadKinds"/> for the
        /// specified <paramref name="contentTypeHeader"/>.
        /// </param>
        /// <param name="batchBoundary">The batch boundary read from the content type for batch payloads; otherwise null.</param>
        /// <returns>The <see cref="ODataFormat"/> for the <paramref name="contentTypeHeader"/>.</returns>
        internal static ODataFormat GetFormatFromContentType(
            string contentTypeHeader,
            ODataPayloadKind[] supportedPayloadKinds,
            MediaTypeResolver mediaTypeResolver,
            out MediaType mediaType,
            out Encoding encoding,
            out ODataPayloadKind selectedPayloadKind,
            out string batchBoundary)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(!supportedPayloadKinds.Contains(ODataPayloadKind.Unsupported), "!supportedPayloadKinds.Contains(ODataPayloadKind.Unsupported)");

            ODataFormat format = GetFormatFromContentType(contentTypeHeader, supportedPayloadKinds, mediaTypeResolver, out mediaType, out encoding, out selectedPayloadKind);

            // for batch payloads, read the batch boundary from the content type header; this is the only
            // content type parameter we support (and that is required for batch payloads)
            if (selectedPayloadKind == ODataPayloadKind.Batch)
            {
                KeyValuePair <string, string> boundaryPair = default(KeyValuePair <string, string>);
                IEnumerable <KeyValuePair <string, string> > parameters = mediaType.Parameters;
                if (parameters != null)
                {
                    bool boundaryPairFound = false;
                    foreach (KeyValuePair <string, string> pair in parameters.Where(p => HttpUtils.CompareMediaTypeParameterNames(ODataConstants.HttpMultipartBoundary, p.Key)))
                    {
                        if (boundaryPairFound)
                        {
                            throw new ODataException(Strings.MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads(contentTypeHeader, ODataConstants.HttpMultipartBoundary));
                        }

                        boundaryPair      = pair;
                        boundaryPairFound = true;
                    }
                }

                if (boundaryPair.Key == null)
                {
                    throw new ODataException(Strings.MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads(contentTypeHeader, ODataConstants.HttpMultipartBoundary));
                }

                batchBoundary = boundaryPair.Value;
                ValidationUtils.ValidateBoundaryString(batchBoundary);
            }
            else
            {
                batchBoundary = null;
            }

            return(format);
        }
Esempio n. 25
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;
 }
Esempio n. 26
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;
 }
Esempio n. 27
0
 internal static Task <ODataInputContext> CreateInputContextAsync(ODataFormat format, ODataPayloadKind readerPayloadKind, ODataMessage message, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, IEdmModel model, IODataUrlResolver urlResolver)
 {
     if (format == ODataFormat.Atom)
     {
         return(ODataAtomInputContext.CreateAsync(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver));
     }
     if (format == ODataFormat.VerboseJson)
     {
         return(ODataJsonInputContext.CreateAsync(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver));
     }
     if ((format != ODataFormat.Batch) && (format != ODataFormat.RawValue))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataInputContext_CreateInputContext_UnrecognizedFormat));
     }
     return(ODataRawInputContext.CreateAsync(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver, readerPayloadKind));
 }
Esempio n. 28
0
 private ODataMetadataOutputContext(ODataFormat format, Stream messageStream, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver) : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
 {
     try
     {
         this.messageOutputStream = messageStream;
         this.xmlWriter           = ODataAtomWriterUtils.CreateXmlWriter(messageStream, messageWriterSettings, encoding);
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (messageStream != null))
         {
             messageStream.Dispose();
         }
         throw;
     }
 }
Esempio n. 29
0
 internal static Task <ODataOutputContext> CreateOutputContextAsync(ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, IEdmModel model, IODataUrlResolver urlResolver)
 {
     if (format == ODataFormat.Atom)
     {
         return(ODataAtomOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver));
     }
     if (format == ODataFormat.VerboseJson)
     {
         return(ODataJsonOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver));
     }
     if ((format != ODataFormat.Batch) && (format != ODataFormat.RawValue))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataOutputContext_CreateOutputContext_UnrecognizedFormat));
     }
     return(ODataRawOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver));
 }
Esempio n. 30
0
 internal ODataJsonInputContext(ODataFormat format, TextReader reader, 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.textReader = reader;
         this.jsonReader = new BufferingJsonReader(this.textReader, base.UseServerFormatBehavior, messageReaderSettings.MessageQuotas.MaxNestingDepth);
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (reader != null))
         {
             reader.Dispose();
         }
         throw;
     }
 }
Esempio n. 31
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;
     }
 }
Esempio n. 32
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;
     }
 }
Esempio n. 33
0
 private ODataRawInputContext(ODataFormat format, System.IO.Stream messageStream, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver, ODataPayloadKind readerPayloadKind) : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull <ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull <ODataMessageReaderSettings>(messageReaderSettings, "messageReaderSettings");
     try
     {
         this.stream            = messageStream;
         this.encoding          = encoding;
         this.readerPayloadKind = readerPayloadKind;
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (messageStream != null))
         {
             messageStream.Dispose();
         }
         throw;
     }
 }
Esempio n. 34
0
 private ODataRawInputContext(ODataFormat format, System.IO.Stream messageStream, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver, ODataPayloadKind readerPayloadKind) : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataFormat>(format, "format");
     ExceptionUtils.CheckArgumentNotNull<ODataMessageReaderSettings>(messageReaderSettings, "messageReaderSettings");
     try
     {
         this.stream = messageStream;
         this.encoding = encoding;
         this.readerPayloadKind = readerPayloadKind;
     }
     catch (Exception exception)
     {
         if (ExceptionUtils.IsCatchableExceptionType(exception) && (messageStream != null))
         {
             messageStream.Dispose();
         }
         throw;
     }
 }
Esempio n. 35
0
        /// <summary>
        /// Gets the default media type for a given payload kind in a given format.
        /// </summary>
        /// <param name="supportedMediaTypes">An array of supported media types and formats.</param>
        /// <param name="specifiedFormat">The user-specified format in which to write the payload (can be null).</param>
        /// <param name="actualFormat">The default format for the specified payload kind</param>
        /// <returns>The default media type for the given payload kind and format.</returns>
        private static MediaType GetDefaultMediaType(
            MediaTypeWithFormat[] supportedMediaTypes,
            ODataFormat specifiedFormat,
            out ODataFormat actualFormat)
        {
            for (int i = 0; i < supportedMediaTypes.Length; ++i)
            {
                // NOTE: the supportedMediaTypes are sorted (desc) by format and media type; so the
                //       default format and media type is the first entry in the array
                MediaTypeWithFormat supportedMediaType = supportedMediaTypes[i];
                if (specifiedFormat == null || supportedMediaType.Format == specifiedFormat)
                {
                    actualFormat = supportedMediaType.Format;
                    return(supportedMediaType.MediaType);
                }
            }

            throw new ODataException(Strings.ODataUtils_DidNotFindDefaultMediaType(specifiedFormat));
        }
Esempio n. 36
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>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 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)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(messageStream != null, "stream != null");

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

            try
            {
                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*/ this.UseServerFormatBehavior && this.Version < ODataVersion.V3,
                    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;
            }
        }
Esempio n. 38
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>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>
        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)
        {
            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;
            }
        }
Esempio n. 40
0
 private ODataRawOutputContext(ODataFormat format, Stream messageStream, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver) : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
 {
     try
     {
         this.messageOutputStream = messageStream;
         this.encoding            = encoding;
         if (synchronous)
         {
             this.outputStream = messageStream;
         }
         else
         {
             this.asynchronousOutputStream = new AsyncBufferedStream(messageStream);
             this.outputStream             = this.asynchronousOutputStream;
         }
     }
     catch
     {
         messageStream.Dispose();
         throw;
     }
 }
Esempio n. 41
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>
        /// <param name="readerPayloadKind">The <see cref="ODataPayloadKind"/> to read.</param>
        internal ODataRawInputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver,
            ODataPayloadKind readerPayloadKind)
            : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(messageStream != null, "stream != null");
            Debug.Assert(readerPayloadKind != ODataPayloadKind.Unsupported, "readerPayloadKind != ODataPayloadKind.Unsupported");

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

            try
            {
                this.stream            = messageStream;
                this.encoding          = encoding;
                this.readerPayloadKind = readerPayloadKind;
            }
            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>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>
        /// <param name="readerPayloadKind">The <see cref="ODataPayloadKind"/> to read.</param>
        private ODataRawInputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver,
            ODataPayloadKind readerPayloadKind)
            : base(format, messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
        {
            Debug.Assert(messageStream != null, "stream != null");
            Debug.Assert(readerPayloadKind != ODataPayloadKind.Unsupported, "readerPayloadKind != ODataPayloadKind.Unsupported");

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

            try
            { 
                this.stream = messageStream;
                this.encoding = encoding;
                this.readerPayloadKind = readerPayloadKind;
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
                {
                    messageStream.Dispose();
                }

                throw;
            }
        }
        public ODataResponseContext(IODataRequestMessage requestMessage, ODataFormat format, ODataVersion version, Uri baseAddress, string serviceOperationName)
        {
            if (requestMessage == null)
            {
                throw Error.ArgumentNull("requestMessage");
            }

            if (baseAddress == null)
            {
                throw Error.ArgumentNull("baseAddress");
            }

            if (String.IsNullOrEmpty(serviceOperationName))
            {
                throw Error.ArgumentNullOrEmpty("serviceOperationName");
            }

            _requestMessage = requestMessage;
            ODataFormat = format;
            ODataVersion = version;
            _baseAddress = baseAddress;
            _serviceOperationName = serviceOperationName;
            IsIndented = true;
        }
Esempio n. 44
0
 internal static ODataInputContext CreateInputContext(ODataFormat format, ODataPayloadKind readerPayloadKind, ODataMessage message, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, IEdmModel model, IODataUrlResolver urlResolver)
 {
     if (format == ODataFormat.Atom)
     {
         return ODataAtomInputContext.Create(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver);
     }
     if (format == ODataFormat.VerboseJson)
     {
         return ODataJsonInputContext.Create(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver);
     }
     if (format == ODataFormat.Metadata)
     {
         return ODataMetadataInputContext.Create(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver);
     }
     if ((format != ODataFormat.Batch) && (format != ODataFormat.RawValue))
     {
         throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataInputContext_CreateInputContext_UnrecognizedFormat));
     }
     return ODataRawInputContext.Create(format, message, encoding, messageReaderSettings, version, readingResponse, model, urlResolver, readerPayloadKind);
 }
Esempio n. 45
0
 internal ODataJsonInputContext(ODataFormat format, Stream messageStream, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, bool synchronous, IEdmModel model, IODataUrlResolver urlResolver) : this(format, CreateTextReaderForMessageStreamConstructor(messageStream, encoding), messageReaderSettings, version, readingResponse, synchronous, model, urlResolver)
 {
 }
Esempio n. 46
0
        /// <summary>
        /// Creates an instance of the input context for the specified format.
        /// </summary>
        /// <param name="format">The format to create the context for.</param>
        /// <param name="readerPayloadKind">The <see cref="ODataPayloadKind"/> to read.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <returns>The newly created input context.</returns>
        internal static ODataInputContext CreateInputContext(
            ODataFormat format,
            ODataPayloadKind readerPayloadKind,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();

            if (format == ODataFormat.Atom)
            {
                return(ODataAtomInputContext.Create(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver));
            }

            if (format == ODataFormat.VerboseJson)
            {
                return(ODataJsonInputContext.Create(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver));
            }

            if (format == ODataFormat.Metadata)
            {
                return(ODataMetadataInputContext.Create(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver));
            }

            if (format == ODataFormat.Batch || format == ODataFormat.RawValue)
            {
                return(ODataRawInputContext.Create(
                           format,
                           message,
                           encoding,
                           messageReaderSettings,
                           version,
                           readingResponse,
                           model,
                           urlResolver,
                           readerPayloadKind));
            }

            throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataInputContext_CreateInputContext_UnrecognizedFormat));
        }
Esempio n. 47
0
 private static ODataMessageWriterSettings CreateMessageWriterSettings(Uri serviceUri, DataServiceProviderWrapper provider, Version responseVersion, bool disableDisposeStream, ODataFormat format)
 {
     ODataMessageWriterSettings settings = new ODataMessageWriterSettings {
         BaseUri = serviceUri,
         Version = new ODataVersion?(CommonUtil.ConvertToODataVersion(responseVersion)),
         Indent = false,
         CheckCharacters = false,
         DisableMessageStreamDisposal = disableDisposeStream
     };
     settings.EnableWcfDataServicesServerBehavior(provider.IsV1Provider);
     settings.SetContentType(format);
     return settings;
 }
        private IEnumerable<KeyValuePair<string, string>> GetResponseMessageHeaders(Type graphType, ODataFormat odataFormat, ODataVersion version)
        {
            IODataResponseMessage responseMessage = new ODataMessageWrapper();

            ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings()
            {
                BaseUri = new Uri(ODataFormatterConstants.DefaultNamespace),
                Version = version,
                Indent = false
            };
            writerSettings.SetContentType(odataFormat);
            using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage, writerSettings))
            {
                ODataSerializer serializer = ODataSerializerProvider.GetODataPayloadSerializer(graphType);

                // get the OData specific headers for the payloadkind
                ODataUtils.SetHeadersForPayload(messageWriter, serializer.ODataPayloadKind);
            }

            return responseMessage.Headers;
        }
        /// <summary>
        /// Create RAW input context.
        /// </summary>
        /// <param name="format">The format for the input context.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <param name="readerPayloadKind">The <see cref="ODataPayloadKind"/> to read.</param>
        /// <returns>The newly created input context.</returns>
        internal static ODataInputContext Create(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver,
            ODataPayloadKind readerPayloadKind)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(format == ODataFormat.RawValue || format == ODataFormat.Batch, "This method only supports the RAW value or batch format.");
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");
            Debug.Assert(readerPayloadKind != ODataPayloadKind.Unsupported, "readerPayloadKind != ODataPayloadKind.Unsupported");

            Stream messageStream = message.GetStream();
            return new ODataRawInputContext(
                format,
                messageStream,
                encoding,
                messageReaderSettings,
                version,
                readingResponse,
                true,
                model,
                urlResolver,
                readerPayloadKind);
        }
        /// <summary>
        /// Create ATOM input context.
        /// </summary>
        /// <param name="format">The format for the input context.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <returns>The newly created input context.</returns>
        internal static ODataInputContext Create(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");

            Stream messageStream = message.GetStream();
            return new ODataAtomInputContext(
                format,
                messageStream,
                encoding,
                messageReaderSettings,
                version,
                readingResponse,
                true,
                model,
                urlResolver);
        }
Esempio n. 51
0
 internal static ODataInputContext Create(ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, IEdmModel model, IODataUrlResolver urlResolver, ODataPayloadKind readerPayloadKind)
 {
     return new ODataRawInputContext(format, message.GetStream(), encoding, messageReaderSettings, version, readingResponse, true, model, urlResolver, readerPayloadKind);
 }
        /// <summary>
        /// Asynchronously create ATOM input context.
        /// </summary>
        /// <param name="format">The format for the input context.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <returns>Task which when completed returns the newly create input context.</returns>
        internal static Task<ODataInputContext> CreateAsync(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");

            return message.GetStreamAsync()
                .FollowOnSuccessWith(
                    (streamTask) => (ODataInputContext)new ODataAtomInputContext(
                        format,
                        streamTask.Result,
                        encoding,
                        messageReaderSettings,
                        version,
                        readingResponse,
                        false,
                        model,
                        urlResolver));
        }
Esempio n. 53
0
 internal static ODataInputContext Create(ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, IEdmModel model, IODataUrlResolver urlResolver)
 {
     return(new ODataMetadataInputContext(format, message.GetStream(), encoding, messageReaderSettings, version, readingResponse, true, model, urlResolver));
 }
        /// <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>
        /// Asynchronously create RAW input context.
        /// </summary>
        /// <param name="format">The format for the input context.</param>
        /// <param name="message">The message to use.</param>
        /// <param name="encoding">The encoding to use.</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="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>
        /// <param name="readerPayloadKind">The <see cref="ODataPayloadKind"/> to read.</param>
        /// <returns>Task which when completed returns the newly create input context.</returns>
        internal static Task<ODataInputContext> CreateAsync(
            ODataFormat format,
            ODataMessage message,
            Encoding encoding,
            ODataMessageReaderSettings messageReaderSettings,
            ODataVersion version,
            bool readingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver,
            ODataPayloadKind readerPayloadKind)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(format == ODataFormat.RawValue || format == ODataFormat.Batch, "This method only supports the RAW value or batch format.");
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");
            Debug.Assert(readerPayloadKind != ODataPayloadKind.Unsupported, "readerPayloadKind != ODataPayloadKind.Unsupported");

            // TODO: Note that this will buffer the entire input. We need this for batch and for raw values since they
            // both use TextReader - verify that this is really what we want to do.
            return message.GetStreamAsync()
                .FollowOnSuccessWith(
                    (streamTask) => (ODataInputContext)new ODataRawInputContext(
                        format,
                        streamTask.Result,
                        encoding,
                        messageReaderSettings,
                        version,
                        readingResponse,
                        false,
                        model,
                        urlResolver,
                        readerPayloadKind));
        }
Esempio n. 56
0
 private static MediaType GetDefaultMediaType(MediaTypeWithFormat[] supportedMediaTypes, ODataFormat specifiedFormat, out ODataFormat actualFormat)
 {
     for (int i = 0; i < supportedMediaTypes.Length; i++)
     {
         MediaTypeWithFormat format = supportedMediaTypes[i];
         if ((specifiedFormat == null) || (format.Format == specifiedFormat))
         {
             actualFormat = format.Format;
             return(format.MediaType);
         }
     }
     throw new ODataException(Microsoft.Data.OData.Strings.ODataUtils_DidNotFindDefaultMediaType(specifiedFormat));
 }
Esempio n. 57
0
        /// <summary>
        /// Inserts the specified media type before the first occurrence of <paramref name="beforeFormat"/>.
        /// </summary>
        /// <param name="mediaTypeList">The media type list to insert into.</param>
        /// <param name="mediaTypeToInsert">The media type to insert.</param>
        /// <param name="beforeFormat">The format of the media type before which <paramref name="mediaTypeToInsert"/> should be inserted.</param>
        private static void AddMediaTypeEntryOrdered(IList <MediaTypeWithFormat> mediaTypeList, MediaTypeWithFormat mediaTypeToInsert, ODataFormat beforeFormat)
        {
            Debug.Assert(mediaTypeList != null, "mediaTypeList != null");
            Debug.Assert(mediaTypeToInsert != null, "mediaTypeToInsert != null");
            Debug.Assert(beforeFormat != null, "beforeFormat != null");

            // Go through the list and find the first entry that has the specified format to insert before.
            for (int i = 0; i < mediaTypeList.Count; ++i)
            {
                if (mediaTypeList[i].Format == beforeFormat)
                {
                    mediaTypeList.Insert(i, mediaTypeToInsert);
                    return;
                }
            }

            mediaTypeList.Add(mediaTypeToInsert);
        }
Esempio n. 58
0
 internal static ODataMessageWriter CreateMessageWriter(Uri serviceUri, IDataService dataService, Version responseVersion, IODataResponseMessage responseMessage, ODataFormat format)
 {
     return new ODataMessageWriter(responseMessage, CreateMessageWriterSettings(serviceUri, dataService.Provider, responseVersion, responseMessage is System.Data.Services.ODataResponseMessage, format), (dataService.OperationContext == null) ? null : dataService.Provider.GetMetadataModel(dataService.OperationContext));
 }
Esempio n. 59
0
 internal static Task<ODataInputContext> CreateAsync(ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageReaderSettings messageReaderSettings, ODataVersion version, bool readingResponse, IEdmModel model, IODataUrlResolver urlResolver, ODataPayloadKind readerPayloadKind)
 {
     return message.GetStreamAsync().FollowOnSuccessWith<System.IO.Stream, ODataInputContext>(streamTask => new ODataRawInputContext(format, streamTask.Result, encoding, messageReaderSettings, version, readingResponse, false, model, urlResolver, readerPayloadKind));
 }
Esempio n. 60
0
 /// <summary>
 /// Inserts the given media type/format for all the payload kinds which support JSON before the first media type with the given format.
 /// </summary>
 /// <param name="mediaTypeWithFormat">The media type/format pair to add.</param>
 /// <param name="beforeFormat">The format of the media type before which <paramref name="mediaTypeWithFormat"/> should be inserted.</param>
 private void InsertForJsonPayloadKinds(MediaTypeWithFormat mediaTypeWithFormat, ODataFormat beforeFormat)
 {
     foreach (ODataPayloadKind kind in JsonPayloadKinds)
     {
         AddMediaTypeEntryOrdered(this.mediaTypesForPayloadKind[(int)kind], mediaTypeWithFormat, beforeFormat);
     }
 }