Esempio n. 1
0
        /// <summary>
        /// Creates an <see cref="ODataCollectionWriter"/> for the specified message and its stream.
        /// </summary>
        /// <param name="format">The serialization format to create the writer for.</param>
        /// <param name="encoding">The encoding to create the writer with.</param>
        /// <param name="stream">The response stream to write to.</param>
        /// <param name="settings">Writer settings to use.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>The newly created <see cref="ODataCollectionWriter"/> instance.</returns>
        /// <remarks>This is used to create the collection writer once we've obtained the stream from the response.</remarks>
        private static ODataCollectionWriter CreateCollectionWriter(
            ODataFormat format,
            Encoding encoding,
            Stream stream,
            ODataWriterSettings settings,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool synchronous)
        {
            if (settings.BaseUri != null && !settings.BaseUri.IsAbsoluteUri)
            {
                throw new ODataException(Strings.ODataWriter_BaseUriMustBeNullOrAbsolute(UriUtils.UriToString(settings.BaseUri)));
            }

            switch (format)
            {
            case ODataFormat.Json:
                return(new ODataJsonCollectionWriter(stream, settings, encoding, metadataProvider, synchronous));

            case ODataFormat.Atom:
                return(new ODataAtomCollectionWriter(stream, settings, encoding, metadataProvider, synchronous));

            case ODataFormat.Default:
                Debug.Assert(false, "Should never get here as content-type negotiation should not return Default format for collection.");
                throw new ODataException(Strings.ODataCollectionWriter_CannotCreateCollectionWriterForFormat(format.ToString()));

            default:
                throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataCollectionWriter_CreateCollectionWriter_UnreachableCodePath));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="odataWriterSettings">Configuration settings for the writer to create.</param>
        /// <param name="encoding">The encoding to use for writing.</param>
        /// <param name="writingResponse">True if the writer is to write a response payload; false if it's to write a request payload.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="writingFeed">True if the writer is created for writing a feed; false when it is created for writing an entry.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        internal ODataJsonWriter(
            Stream stream,
            ODataWriterSettings odataWriterSettings,
            Encoding encoding,
            bool writingResponse,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool writingFeed,
            bool synchronous)
            : base(
                odataWriterSettings.Version,
                odataWriterSettings.BaseUri,
                writingResponse,
                metadataProvider,
                writingFeed,
                synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");
            Debug.Assert(odataWriterSettings != null, "odataWriterSettings != null");

            this.outputStream = new AsyncBufferedStream(stream);
            this.textWriter   = new StreamWriter(this.outputStream, encoding);

            this.jsonWriter = new JsonWriter(this.textWriter, odataWriterSettings.Indent);
        }
Esempio n. 3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="settings">The <see cref="ODataWriterSettings"/> for the batch writer.</param>
        /// <param name="encoding">The encoding to use for writing.</param>
        /// <param name="writingResponse">A flag indicating whether we are writing a request or a response message.</param>
        /// <param name="batchBoundary">The boundary string for the batch structure itself.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        private ODataBatchWriter(
            Stream stream,
            ODataWriterSettings settings,
            Encoding encoding,
            bool writingResponse,
            string batchBoundary,
            bool synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");

            if (batchBoundary == null)
            {
                ExceptionUtils.CheckArgumentNotNull(batchBoundary, "batchBoundary");
            }

            this.settings            = settings;
            this.encoding            = encoding;
            this.outputStream        = stream;
            this.writingResponse     = writingResponse;
            this.batchBoundary       = batchBoundary;
            this.synchronous         = synchronous;
            this.asyncBufferedStream = new AsyncBufferedStream(stream, true);
            this.batchWriter         = new StreamWriter(this.asyncBufferedStream, this.encoding);
        }
Esempio n. 4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="odataWriterSettings">Configuration settings for the writer to create.</param>
        /// <param name="encoding">The encoding to use for writing.</param>
        /// <param name="writingResponse">True if the writer is to write a response payload; false if it's to write a request payload.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="writingFeed">True if the writer is created for writing a feed; false when it is created for writing an entry.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        internal ODataJsonWriter(
            Stream stream, 
            ODataWriterSettings odataWriterSettings, 
            Encoding encoding, 
            bool writingResponse,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool writingFeed,
            bool synchronous)
            : base(
                odataWriterSettings.Version, 
                odataWriterSettings.BaseUri, 
                writingResponse, 
                metadataProvider,
                writingFeed,
                synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");
            Debug.Assert(odataWriterSettings != null, "odataWriterSettings != null");

            this.outputStream = new AsyncBufferedStream(stream);
            this.textWriter = new StreamWriter(this.outputStream, encoding);

            this.jsonWriter = new JsonWriter(this.textWriter, odataWriterSettings.Indent);
        }
Esempio n. 5
0
        /// <summary>
        /// Given the Accept and the Accept-Charset headers of the request message computes the media type, encoding and <see cref="ODataFormat"/>
        /// to be used for the response message.
        /// </summary>
        /// <param name="settings">The writer settings to use for serializing the response payload.</param>
        /// <param name="payloadKind">The kind of payload to be serialized as part of the response message.</param>
        /// <param name="mediaType">The media type to be used in the response message.</param>
        /// <param name="encoding">The encoding to be used in the response message.</param>
        /// <returns>The <see cref="ODataFormat"/> used when serializing the response.</returns>
        internal static ODataFormat GetContentTypeFromSettings(
            ODataWriterSettings settings,
            ODataPayloadKind payloadKind,
            out MediaType mediaType,
            out Encoding encoding)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(settings != null, "settings != null");

            // compute format, media type and encoding
            ODataFormat format;

            if (settings.AcceptableMediaTypes != null)
            {
                mediaType = GetMediaType(settings.AcceptableMediaTypes, payloadKind, out format);
                encoding  = GetEncoding(settings.AcceptableCharsets, payloadKind, mediaType);
            }
            else
            {
                mediaType = GetDefaultMediaType(payloadKind, settings.Format, out format);
                encoding  = mediaType.SelectEncoding();
            }

            return(format);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates an Xml writer over the specified stream, with the provided settings and encoding.
        /// </summary>
        /// <param name="stream">The stream to create the XmlWriter over.</param>
        /// <param name="odataWriterSettings">The OData writer settings used to control the settings of the Xml writer.</param>
        /// <param name="encoding">The encoding used for writing.</param>
        /// <returns>An <see cref="XmlWriter"/> instance configured with the provided settings and encoding.</returns>
        internal static XmlWriter CreateXmlWriter(Stream stream, ODataWriterSettings odataWriterSettings, Encoding encoding)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");
            Debug.Assert(odataWriterSettings != null, "odataWriterSettings != null");

            XmlWriterSettings xmlWriterSettings = ODataAtomWriterUtils.CreateXmlWriterSettings(odataWriterSettings, encoding);

            return(XmlWriter.Create(stream, xmlWriterSettings));
        }
        /// <summary>
        /// Sets the 'DataServiceVersion' HTTP header on the message based on the protocol version specified in the settings.
        /// </summary>
        /// <param name="message">The message to set the data service version header on.</param>
        /// <param name="settings">The <see cref="ODataWriterSettings"/> determining the protocol version to use.</param>
        internal static void SetDataServiceVersion(ODataMessage message, ODataWriterSettings settings)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(message != null, "message != null");
            Debug.Assert(settings != null, "settings != null");

            // TODO: Bug 112260: What to do with the optional user agent string?
            string userAgentString = string.Empty;
            string dataServiceVersionString = settings.Version.VersionString() + ";" + userAgentString;
            message.SetHeader(ODataHttpHeaders.DataServiceVersion, dataServiceVersionString);
        }
Esempio n. 8
0
        /// <summary>
        /// Sets the 'DataServiceVersion' HTTP header on the message based on the protocol version specified in the settings.
        /// </summary>
        /// <param name="message">The message to set the data service version header on.</param>
        /// <param name="settings">The <see cref="ODataWriterSettings"/> determining the protocol version to use.</param>
        internal static void SetDataServiceVersion(ODataMessage message, ODataWriterSettings settings)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(message != null, "message != null");
            Debug.Assert(settings != null, "settings != null");

            // TODO: Bug 112260: What to do with the optional user agent string?
            string userAgentString          = string.Empty;
            string dataServiceVersionString = settings.Version.VersionString() + ";" + userAgentString;

            message.SetHeader(ODataHttpHeaders.DataServiceVersion, dataServiceVersionString);
        }
Esempio n. 9
0
        /// <summary>
        /// Create a func which creates an <see cref="ODataBatchWriter"/> for a given message and stream.
        /// </summary>
        /// <param name="message">The message to create the writer for.</param>
        /// <param name="settings">Configuration settings for the writer to create.</param>
        /// <param name="writingResponse">A flag indicating whether we are writing a request or a response message.</param>
        /// <param name="batchBoundary">The batch boundary string used when writing the batch payload.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>A func which creates the batch writer given a stream to write to.</returns>
        internal static Func <Stream, ODataBatchWriter> Create(
            ODataMessage message,
            ODataWriterSettings settings,
            bool writingResponse,
            string batchBoundary,
            bool synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            ExceptionUtils.CheckArgumentNotNull(message, "message");
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");
            ExceptionUtils.CheckArgumentNotNull(batchBoundary, "batchBoundary");

            return((stream) => CreateBatchWriter(batchBoundary, stream, settings, writingResponse, synchronous));
        }
Esempio n. 10
0
        /// <summary>
        /// Create a func which created an <see cref="ODataCollectionWriter"/> for a given request message and stream.
        /// </summary>
        /// <param name="message">The message to create the writer for.</param>
        /// <param name="settings">Configuration settings for the writer to create.</param>
        /// <param name="format">The OData format used for serialization of the payload.</param>
        /// <param name="encoding">The encoding used for serialization of the payload.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>A task returning an OData collection writer to write the payload of the request/response.</returns>
        internal static Func <Stream, ODataCollectionWriter> Create(
            ODataMessage message,
            ODataWriterSettings settings,
            ODataFormat format,
            Encoding encoding,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            ExceptionUtils.CheckArgumentNotNull(message, "message");
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");

            return((stream) => CreateCollectionWriter(format, encoding, stream, settings, metadataProvider, synchronous));
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="writerSettings">Configuration settings for the writer to create.</param>
        /// <param name="encoding">The encoding to use for writing.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        internal ODataAtomCollectionWriter(
            Stream stream, 
            ODataWriterSettings writerSettings, 
            Encoding encoding, 
            DataServiceMetadataProviderWrapper metadataProvider,
            bool synchronous)
            : base(writerSettings.Version, metadataProvider, synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");
            Debug.Assert(writerSettings != null, "writerSettings != null");

            this.outputStream = new AsyncBufferedStream(stream);
            this.writer = ODataAtomWriterUtils.CreateXmlWriter(this.outputStream, writerSettings, encoding);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="writerSettings">Configuration settings for the writer to create.</param>
        /// <param name="encoding">The encoding to use for writing.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        internal ODataAtomCollectionWriter(
            Stream stream,
            ODataWriterSettings writerSettings,
            Encoding encoding,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool synchronous)
            : base(writerSettings.Version, metadataProvider, synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");
            Debug.Assert(writerSettings != null, "writerSettings != null");

            this.outputStream = new AsyncBufferedStream(stream);
            this.writer       = ODataAtomWriterUtils.CreateXmlWriter(this.outputStream, writerSettings, encoding);
        }
Esempio n. 13
0
        /// <summary>
        /// Creates an <see cref="ODataBatchWriter"/> for the specified message and its stream.
        /// </summary>
        /// <param name="batchBoundary">The batch boundary string for this writer.</param>
        /// <param name="stream">The response stream to write to.</param>
        /// <param name="settings">Writer settings to use.</param>
        /// <param name="writingResponse">A flag indicating whether we are writing a request or a response message.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>The newly created <see cref="ODataBatchWriter"/> instance.</returns>
        private static ODataBatchWriter CreateBatchWriter(
            string batchBoundary,
            Stream stream,
            ODataWriterSettings settings,
            bool writingResponse,
            bool synchronous)
        {
            if (settings.BaseUri != null && !settings.BaseUri.IsAbsoluteUri)
            {
                throw new ODataException(Strings.ODataWriter_BaseUriMustBeNullOrAbsolute(UriUtils.UriToString(settings.BaseUri)));
            }

            // TODO: Bug 92529: how should we determine the encoding? Use the fixed UTF-8 encoding (this is what the product does)?
            Encoding encoding = MediaTypeUtils.EncodingUtf8NoPreamble;

            return(new ODataBatchWriter(stream, settings, encoding, writingResponse, batchBoundary, synchronous));
        }
Esempio n. 14
0
        public void Write(Stream stream, IEnumerable<Contact> contacts)
        {
            var message = new ODataStreamResponseMessage(stream);
            var settings = new ODataWriterSettings { Indent = true, CheckCharacters = false, BaseUri = new Uri(this.rootUri), Version = ODataVersion.V3 };
            settings.SetContentType(ODataFormat.Atom);
            var messageWriter = new ODataMessageWriter(message, settings);
            var writerTask = messageWriter.CreateODataFeedWriterAsync();
            writerTask.Wait();
            using (var writer = writerTask.Result)
            {
                writer.WriteStart(
                    new ODataFeed()
                    {
                        Count = contacts.Count(),
                        Id = "Contacts"
                    });

                foreach (var contact in contacts)
                {
                    var entry = new ODataEntry()
                    {
                        Id = string.Format("urn:Contacts(\"{0}\"", contact.ContactId),
                        EditLink = new Uri(string.Format("{1}", this.rootUri, contact.ContactId), UriKind.Relative),
                        TypeName = "Contacts.Contact",
                        Properties =
                            new List<ODataProperty>()
                                    {
                                        new ODataProperty() { Value = contact.ContactId, Name = "ContactId" },
                                        new ODataProperty() { Value = contact.Name, Name = "Name" },
                                        new ODataProperty() { Value = contact.Address, Name = "Address" },
                                        new ODataProperty() { Value = contact.City, Name = "City" },
                                        new ODataProperty() { Value = contact.State, Name = "State" },
                                        new ODataProperty() { Value = contact.Zip, Name = "Zip" },
                                        new ODataProperty() { Value = contact.Email, Name = "Email" },
                                        new ODataProperty() { Value = contact.Twitter, Name = "Twitter" }
                                    }
                    };
                    writer.WriteStart(entry);
                    writer.WriteEnd();
                }
                writer.WriteEnd();
                writer.FlushAsync().Wait();
            }
        }
        /// <summary>
        /// Creates a new XmlWriterSettings instance using the encoding.
        /// </summary>
        /// <param name="odataSettings">Configuration settings of the OData writer.</param>
        /// <param name="encoding"> Encoding to  use in the writer settings.</param>
        /// <returns>The Xml writer settings to use for this writer.</returns>
        internal static XmlWriterSettings CreateXmlWriterSettings(ODataWriterSettings odataSettings, Encoding encoding)
        {
            DebugUtils.CheckNoExternalCallers();

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.CheckCharacters = odataSettings.CheckCharacters;
            settings.ConformanceLevel = ConformanceLevel.Document;
            settings.OmitXmlDeclaration = false;
            if (encoding != null)
            {
                settings.Encoding = encoding;
            }

            settings.NewLineHandling = NewLineHandling.Entitize;

            settings.Indent = odataSettings.Indent;
            
            // we do not want to close the underlying stream when the OData writer is closed since we don't own the stream.
            settings.CloseOutput = false;

            return settings;
        }
Esempio n. 16
0
        /// <summary>
        /// Creates a new XmlWriterSettings instance using the encoding.
        /// </summary>
        /// <param name="odataSettings">Configuration settings of the OData writer.</param>
        /// <param name="encoding"> Encoding to  use in the writer settings.</param>
        /// <returns>The Xml writer settings to use for this writer.</returns>
        internal static XmlWriterSettings CreateXmlWriterSettings(ODataWriterSettings odataSettings, Encoding encoding)
        {
            DebugUtils.CheckNoExternalCallers();

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.CheckCharacters    = odataSettings.CheckCharacters;
            settings.ConformanceLevel   = ConformanceLevel.Document;
            settings.OmitXmlDeclaration = false;
            if (encoding != null)
            {
                settings.Encoding = encoding;
            }

            settings.NewLineHandling = NewLineHandling.Entitize;

            settings.Indent = odataSettings.Indent;

            // we do not want to close the underlying stream when the OData writer is closed since we don't own the stream.
            settings.CloseOutput = false;

            return(settings);
        }
        /// <summary>
        /// Creates an Xml writer over the specified stream, with the provided settings and encoding.
        /// </summary>
        /// <param name="stream">The stream to create the XmlWriter over.</param>
        /// <param name="odataWriterSettings">The OData writer settings used to control the settings of the Xml writer.</param>
        /// <param name="encoding">The encoding used for writing.</param>
        /// <returns>An <see cref="XmlWriter"/> instance configured with the provided settings and encoding.</returns>
        internal static XmlWriter CreateXmlWriter(Stream stream, ODataWriterSettings odataWriterSettings, Encoding encoding)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");
            Debug.Assert(odataWriterSettings != null, "odataWriterSettings != null");

            XmlWriterSettings xmlWriterSettings = ODataAtomWriterUtils.CreateXmlWriterSettings(odataWriterSettings, encoding);
            return XmlWriter.Create(stream, xmlWriterSettings);
        }
Esempio n. 18
0
        /// <summary>
        /// Create a func which creates an ODataWriter for a given request message and stream.
        /// </summary>
        /// <param name="message">The message to create the writer for.</param>
        /// <param name="settings">Configuration settings for the writer to create.</param>
        /// <param name="format">The OData format used for serialization of the payload.</param>
        /// <param name="encoding">The encoding used for serialization of the payload.</param>
        /// <param name="writingResponse">True if the message writing a response message; otherwise false.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="writingFeed">True when creating a writer to write a feed; false when creating a writer to write an entry.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>A func which creates the writer given a stream to write to.</returns>
        internal static Func<Stream, ODataWriter> Create(
            ODataMessage message,
            ODataWriterSettings settings,
            ODataFormat format,
            Encoding encoding,
            bool writingResponse,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool writingFeed,
            bool synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            ExceptionUtils.CheckArgumentNotNull(message, "message");
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");

            return (stream) => CreateWriter(format, encoding, stream, settings, writingResponse, metadataProvider, writingFeed, synchronous);
        }
Esempio n. 19
0
        /// <summary>
        /// Creates an <see cref="ODataBatchWriter"/> for the specified message and its stream.
        /// </summary>
        /// <param name="batchBoundary">The batch boundary string for this writer.</param>
        /// <param name="stream">The response stream to write to.</param>
        /// <param name="settings">Writer settings to use.</param>
        /// <param name="writingResponse">A flag indicating whether we are writing a request or a response message.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>The newly created <see cref="ODataBatchWriter"/> instance.</returns>
        private static ODataBatchWriter CreateBatchWriter(
            string batchBoundary, 
            Stream stream, 
            ODataWriterSettings settings, 
            bool writingResponse,
            bool synchronous)
        {
            if (settings.BaseUri != null && !settings.BaseUri.IsAbsoluteUri)
            {
                throw new ODataException(Strings.ODataWriter_BaseUriMustBeNullOrAbsolute(UriUtils.UriToString(settings.BaseUri)));
            }

            // TODO: Bug 92529: how should we determine the encoding? Use the fixed UTF-8 encoding (this is what the product does)?
            Encoding encoding = MediaTypeUtils.EncodingUtf8NoPreamble;
            return new ODataBatchWriter(stream, settings, encoding, writingResponse, batchBoundary, synchronous);
        }
Esempio n. 20
0
        /// <summary>
        /// Given the Accept and the Accept-Charset headers of the request message computes the media type, encoding and <see cref="ODataFormat"/>
        /// to be used for the response message.
        /// </summary>
        /// <param name="settings">The writer settings to use for serializing the response payload.</param>
        /// <param name="payloadKind">The kind of payload to be serialized as part of the response message.</param>
        /// <param name="mediaType">The media type to be used in the response message.</param>
        /// <param name="encoding">The encoding to be used in the response message.</param>
        /// <returns>The <see cref="ODataFormat"/> used when serializing the response.</returns>
        internal static ODataFormat GetContentTypeFromSettings(
            ODataWriterSettings settings,
            ODataPayloadKind payloadKind,
            out MediaType mediaType,
            out Encoding encoding)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(settings != null, "settings != null");

            // compute format, media type and encoding
            ODataFormat format;

            if (settings.AcceptableMediaTypes != null)
            {
                mediaType = GetMediaType(settings.AcceptableMediaTypes, payloadKind, out format);
                encoding = GetEncoding(settings.AcceptableCharsets, payloadKind, mediaType);
            }
            else
            {
                mediaType = GetDefaultMediaType(payloadKind, settings.Format, out format);
                encoding = mediaType.SelectEncoding();
            }

            return format;
        }
Esempio n. 21
0
 /// <summary>
 /// Creates a new ODataMessageWriter for the given request message and writer settings.
 /// </summary>
 /// <param name="requestMessage">The request message for which to create the writer.</param>
 /// <param name="settings">The writer settings to use for writing the message payload.</param>
 public ODataMessageWriter(IODataRequestMessage requestMessage, ODataWriterSettings settings)
     : this(requestMessage, settings, null)
 {
 }
Esempio n. 22
0
        /// <summary>
        /// Creates an <see cref="ODataWriter"/> for the specified message and its stream.
        /// </summary>
        /// <param name="format">The serialization format to create the writer for.</param>
        /// <param name="encoding">The encoding to create the writer with.</param>
        /// <param name="stream">The response stream to write to.</param>
        /// <param name="settings">Writer settings to use.</param>
        /// <param name="writingResponse">True if we are writing a response message; false for request messages.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        /// <param name="writingFeed">True when creating a writer to write a feed; false when creating a writer to write an entry.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>The newly created <see cref="ODataWriter"/> instance.</returns>
        /// <remarks>This is used to create the writer once we've obtained the stream from the response.</remarks>
        private static ODataWriter CreateWriter(
            ODataFormat format, 
            Encoding encoding,
            Stream stream, 
            ODataWriterSettings settings, 
            bool writingResponse,
            DataServiceMetadataProviderWrapper metadataProvider,
            bool writingFeed,
            bool synchronous)
        {
            if (settings.BaseUri != null && !settings.BaseUri.IsAbsoluteUri)
            {
                throw new ODataException(Strings.ODataWriter_BaseUriMustBeNullOrAbsolute(UriUtils.UriToString(settings.BaseUri)));
            }

            switch (format)
            {
                case ODataFormat.Json:
                    return new ODataJsonWriter(stream, settings, encoding, writingResponse, metadataProvider, writingFeed, synchronous);
                case ODataFormat.Atom:
                    return new ODataAtomWriter(stream, settings, encoding, writingResponse, metadataProvider, writingFeed, synchronous);
                case ODataFormat.Default:
                    Debug.Assert(false, "Should never get here as content-type negotiation should not return Default format for entry or feed.");
                    throw new ODataException(Strings.ODataWriter_CannotCreateWriterForFormat(format.ToString()));
                default:
                    throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataWriter_CreateWriter_UnreachableCodePath));
            }
        }
Esempio n. 23
0
        public void Write(Stream stream, IEnumerable <Contact> contacts)
        {
            var message  = new ODataStreamResponseMessage(stream);
            var settings = new ODataWriterSettings {
                Indent = true, CheckCharacters = false, BaseUri = new Uri(this.rootUri), Version = ODataVersion.V3
            };

            settings.SetContentType(ODataFormat.Atom);
            var messageWriter = new ODataMessageWriter(message, settings);
            var writerTask    = messageWriter.CreateODataFeedWriterAsync();

            writerTask.Wait();
            using (var writer = writerTask.Result)
            {
                writer.WriteStart(
                    new ODataFeed()
                {
                    Count = contacts.Count(),
                    Id    = "Contacts"
                });

                foreach (var contact in contacts)
                {
                    var entry = new ODataEntry()
                    {
                        Id         = string.Format("urn:Contacts(\"{0}\"", contact.ContactId),
                        EditLink   = new Uri(string.Format("{1}", this.rootUri, contact.ContactId), UriKind.Relative),
                        TypeName   = "Contacts.Contact",
                        Properties =
                            new List <ODataProperty>()
                        {
                            new ODataProperty()
                            {
                                Value = contact.ContactId, Name = "ContactId"
                            },
                            new ODataProperty()
                            {
                                Value = contact.Name, Name = "Name"
                            },
                            new ODataProperty()
                            {
                                Value = contact.Address, Name = "Address"
                            },
                            new ODataProperty()
                            {
                                Value = contact.City, Name = "City"
                            },
                            new ODataProperty()
                            {
                                Value = contact.State, Name = "State"
                            },
                            new ODataProperty()
                            {
                                Value = contact.Zip, Name = "Zip"
                            },
                            new ODataProperty()
                            {
                                Value = contact.Email, Name = "Email"
                            },
                            new ODataProperty()
                            {
                                Value = contact.Twitter, Name = "Twitter"
                            }
                        }
                    };
                    writer.WriteStart(entry);
                    writer.WriteEnd();
                }
                writer.WriteEnd();
                writer.FlushAsync().Wait();
            }
        }
Esempio n. 24
0
 /// <summary>
 /// Creates a new ODataMessageWriter for the given response message and writer settings.
 /// </summary>
 /// <param name="responseMessage">The response message for which to create the writer.</param>
 /// <param name="settings">The writer settings to use for writing the message payload.</param>
 public ODataMessageWriter(IODataResponseMessage responseMessage, ODataWriterSettings settings)
     : this(responseMessage, settings, null)
 {
 }
Esempio n. 25
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="settings">The <see cref="ODataWriterSettings"/> for the batch writer.</param>
        /// <param name="encoding">The encoding to use for writing.</param>
        /// <param name="writingResponse">A flag indicating whether we are writing a request or a response message.</param>
        /// <param name="batchBoundary">The boundary string for the batch structure itself.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        private ODataBatchWriter(
            Stream stream, 
            ODataWriterSettings settings, 
            Encoding encoding, 
            bool writingResponse, 
            string batchBoundary, 
            bool synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(stream != null, "stream != null");

            if (batchBoundary == null)
            {
                ExceptionUtils.CheckArgumentNotNull(batchBoundary, "batchBoundary");
            }

            this.settings = settings;
            this.encoding = encoding;
            this.outputStream = stream;
            this.writingResponse = writingResponse;
            this.batchBoundary = batchBoundary;
            this.synchronous = synchronous;
            this.asyncBufferedStream = new AsyncBufferedStream(stream, true);
            this.batchWriter = new StreamWriter(this.asyncBufferedStream, this.encoding);
        }
Esempio n. 26
0
        /// <summary>
        /// Creates a new ODataMessageWriter for the given request message and writer settings.
        /// </summary>
        /// <param name="requestMessage">The request message for which to create the writer.</param>
        /// <param name="settings">The writer settings to use for writing the message payload.</param>
        /// <param name="metadataProvider">The metadata provider to use.</param>
        public ODataMessageWriter(IODataRequestMessage requestMessage, ODataWriterSettings settings, IDataServiceMetadataProvider metadataProvider)
        {
            ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage");
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");

            ODataVersionChecker.CheckVersionSupported(settings.Version);

            this.writingResponse = false;
            this.message = new ODataRequestMessage(requestMessage);
            this.settings = settings;
            if (metadataProvider != null)
            {
                this.metadataProvider = new DataServiceMetadataProviderWrapper(metadataProvider);
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Create a func which creates an <see cref="ODataBatchWriter"/> for a given message and stream.
        /// </summary>
        /// <param name="message">The message to create the writer for.</param>
        /// <param name="settings">Configuration settings for the writer to create.</param>
        /// <param name="writingResponse">A flag indicating whether we are writing a request or a response message.</param>
        /// <param name="batchBoundary">The batch boundary string used when writing the batch payload.</param>
        /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param>
        /// <returns>A func which creates the batch writer given a stream to write to.</returns>
        internal static Func<Stream, ODataBatchWriter> Create(
            ODataMessage message,
            ODataWriterSettings settings,
            bool writingResponse,
            string batchBoundary,
            bool synchronous)
        {
            DebugUtils.CheckNoExternalCallers();
            ExceptionUtils.CheckArgumentNotNull(message, "message");
            ExceptionUtils.CheckArgumentNotNull(settings, "settings");
            ExceptionUtils.CheckArgumentNotNull(batchBoundary, "batchBoundary");

            return (stream) => CreateBatchWriter(batchBoundary, stream, settings, writingResponse, synchronous);
        }