BatchServiceHost.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataWeb / Server / System / Data / Services / BatchServiceHost.cs / 1 / BatchServiceHost.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides an internal implementation for IDataServiceHost to keep track of states
//      for batch operations 
//  
//
// @owner  [....] 
//---------------------------------------------------------------------

namespace System.Data.Services
{ 
    #region Namespaces.
 
    using System; 
    using System.Collections.Specialized;
    using System.Diagnostics; 
    using System.IO;
    using System.Web;

    #endregion Namespaces. 

    ///  
    /// Keeps track of the request and response headers for each 
    /// operation in the batch
    ///  
    internal class BatchServiceHost : IDataServiceHost
    {
        #region Private fields.
 
        /// Response separator string.
        private readonly string boundary; 
 
        /// Request headers.
        private readonly CachedRequestParams requestParams; 

        /// Request Stream.
        private readonly Stream requestStream;
 
        /// Content Id for this operation.
        private readonly string contentId; 
 
        /// Output writer.
        private readonly StreamWriter writer; 

        /// List of query parameters as specified in the request uri.
        private NameValueCollection queryParameters;
 
        /// The value for the 'Allow' response header.
        private string responseAllowHeader; 
 
        /// Value of the response Cache-Control header.
        private string responseCacheControl; 

        /// Value of the response Content-Type header.
        private string responseContentType;
 
        /// Value of the response ETag header.
        private string responseETag; 
 
        /// Value of the response Location header.
        private string responseLocation; 

        /// Value of the response StatusCode header.
        private int responseStatusCode;
 
        /// The value for the DataServiceVersion response header.
        private string responseVersion; 
 
        #endregion Private fields.
 
        #region Constructors.

        /// 
        /// Initializes a new dummy host for the batch request. 
        /// This host represents a single operation in the batch.
        ///  
        /// cached request headers. 
        /// Request stream (possibly null, eg for a DELETE operation).
        /// content id for the given operation host. 
        /// Response separator string.
        /// Output writer.
        internal BatchServiceHost(CachedRequestParams requestParams, Stream requestStream, string contentId, string boundary, StreamWriter writer)
        { 
            Debug.Assert(requestParams != null, "requestParams != null");
            Debug.Assert(boundary != null, "boundary != null"); 
            Debug.Assert(writer != null, "writer != null"); 
            this.requestParams = requestParams;
            this.requestStream = requestStream; 
            this.contentId = contentId;
            this.boundary = boundary;
            this.writer = writer;
        } 

        ///  
        /// Initializes a host for error scenarios - something to which we can write the response header values 
        /// and write them to the underlying stream.
        ///  
        /// Response separator string.
        /// Output writer.
        internal BatchServiceHost(string boundary, StreamWriter writer)
        { 
            Debug.Assert(boundary != null, "boundary != null");
            Debug.Assert(writer != null, "writer != null"); 
            this.boundary = boundary; 
            this.writer = writer;
        } 

        #endregion Constructors.

        #region Properties. 

        /// Gets the absolute URI to the resource upon which to apply the request. 
        Uri IDataServiceHost.AbsoluteRequestUri 
        {
            get 
            {
                if (this.requestParams != null)
                {
                    return this.requestParams.AbsoluteRequestUri; 
                }
 
                return null; 
            }
        } 

        /// Gets the absolute URI to the service.
        Uri IDataServiceHost.AbsoluteServiceUri
        { 
            get
            { 
                if (this.requestParams != null) 
                {
                    return this.requestParams.AbsoluteServiceUri; 
                }

                return null;
            } 
        }
 
        ///  
        /// Gets the character set encoding that the client requested,
        /// possibly null. 
        /// 
        string IDataServiceHost.RequestAccept
        {
            get 
            {
                if (this.requestParams != null) 
                { 
                    return this.requestParams.Accept;
                } 

                return null;
            }
        } 

        ///  
        /// Gets the character set encoding that the client requested, 
        /// possibly null.
        ///  
        string IDataServiceHost.RequestAcceptCharSet
        {
            get
            { 
                if (this.requestParams != null)
                { 
                    return this.requestParams.AcceptCharset; 
                }
 
                return null;
            }
        }
 
        /// Gets the HTTP MIME type of the input stream.
        string IDataServiceHost.RequestContentType 
        { 
            get
            { 
                if (this.requestParams != null)
                {
                    return this.requestParams.ContentType;
                } 

                return null; 
            } 
        }
 
        /// 
        /// Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client.
        /// 
        string IDataServiceHost.RequestHttpMethod 
        {
            get 
            { 
                if (this.requestParams != null)
                { 
                    return this.requestParams.HttpMethod;
                }

                return null; 
            }
        } 
 
        /// Gets the value of the If-Match header from the request made
        string IDataServiceHost.RequestIfMatch 
        {
            get
            {
                if (this.requestParams != null) 
                {
                    return this.requestParams.IfMatch; 
                } 

                return null; 
            }
        }

        /// Gets the value of the If-None-Match header from the request made 
        string IDataServiceHost.RequestIfNoneMatch
        { 
            get 
            {
                if (this.requestParams != null) 
                {
                    return this.requestParams.IfNoneMatch;
                }
 
                return null;
            } 
        } 

        /// Gets the value for the MaxDataServiceVersion request header. 
        string IDataServiceHost.RequestMaxVersion
        {
            get
            { 
                if (this.requestParams != null)
                { 
                    return this.requestParams.MaxVersion; 
                }
 
                return null;
            }
        }
 
        /// Gets the value for the DataServiceVersion request header.
        string IDataServiceHost.RequestVersion 
        { 
            get
            { 
                if (this.requestParams != null)
                {
                    return this.requestParams.Version;
                } 

                return null; 
            } 
        }
 
        /// Gets or sets the Cache-Control header on the response.
        string IDataServiceHost.ResponseCacheControl
        {
            get { return this.responseCacheControl; } 
            set { this.responseCacheControl = value; }
        } 
 
        /// Gets or sets the HTTP MIME type of the output stream.
        string IDataServiceHost.ResponseContentType 
        {
            get { return this.responseContentType; }
            set { this.responseContentType = value; }
        } 

        /// Gets/Sets the value of the ETag header on the outgoing response 
        string IDataServiceHost.ResponseETag 
        {
            get { return this.responseETag; } 
            set { this.responseETag = value; }
        }

        /// Gets or sets the Location header on the response. 
        string IDataServiceHost.ResponseLocation
        { 
            get { return this.responseLocation; } 
            set { this.responseLocation = value; }
        } 

        /// 
        /// Gets/Sets the status code for the request made.
        ///  
        int IDataServiceHost.ResponseStatusCode
        { 
            get { return this.responseStatusCode; } 
            set { this.responseStatusCode = value; }
        } 

        /// 
        /// Gets the  to be written to send a response
        /// to the client. 
        /// 
        Stream IDataServiceHost.ResponseStream 
        { 
            get
            { 
                // There is a batch stream for writing requests for batch operations.
                // Hence this method should never be called.
                throw Error.NotSupported();
            } 
        }
 
        /// Gets or sets the value for the DataServiceVersion response header. 
        string IDataServiceHost.ResponseVersion
        { 
            get { return this.responseVersion; }
            set { this.responseVersion = value; }
        }
 
        /// 
        /// Gets the  from which the request data can be read from 
        /// to the client. 
        /// 
        Stream IDataServiceHost.RequestStream 
        {
            [DebuggerStepThrough]
            get { return this.requestStream; }
        } 

        /// Response separator string. 
        internal string BoundaryString 
        {
            get { return this.boundary; } 
        }

        ///  returns all the cached request headers 
        internal CachedRequestParams RequestParams 
        {
            get { return this.requestParams; } 
        } 

        /// The value for the 'Allow' response header. 
        internal string ResponseAllowHeader
        {
            get { return this.responseAllowHeader; }
        } 

        ///  
        /// Gets/Sets the content id as specified in the batch request. 
        /// This same value is written out in the response headers also to allow mapping requests on the client.
        ///  
        internal string ContentId
        {
            get { return this.contentId; }
        } 

        /// Output writer. 
        internal StreamWriter Writer 
        {
            get { return this.writer; } 
        }

        #endregion Properties.
 
        #region Methods.
 
        /// Gets the value for the specified item in the request query string. 
        /// Item to return.
        ///  
        /// The value for the specified item in the request query string;
        /// null if  is not found.
        /// 
        string IDataServiceHost.GetQueryStringItem(string item) 
        {
            this.GetUriAndQueryParameters(); 
 
            string[] result = this.queryParameters.GetValues(item);
            if (result == null || result.Length == 0) 
            {
                return null;
            }
            else if (result.Length == 1) 
            {
                return result[0]; 
            } 
            else
            { 
                throw DataServiceException.CreateBadRequestError(
                    Strings.DataServiceHost_MoreThanOneQueryParameterSpecifiedWithTheGivenName(item, this.requestParams.AbsoluteRequestUri));
            }
        } 

        /// Method to handle a data service exception during processing. 
        /// Exception handling description. 
        void IDataServiceHost.ProcessException(HandleExceptionArgs args)
        { 
            // This would typically set headers on the host.
            Debug.Assert(args != null, "args != null");
            Debug.Assert(WebUtil.IsCatchableExceptionType(args.Exception), "WebUtil.IsCatchableExceptionType(args.Exception)");
            this.responseStatusCode = args.ResponseStatusCode; 
            this.responseContentType = args.ResponseContentType;
            this.responseAllowHeader = args.ResponseAllowHeader; 
 
            // Only write the headers if the response is not written
            if (!args.ResponseWritten) 
            {
                System.Data.Services.Serializers.BatchWriter.WriteBoundaryAndHeaders(this.writer, this, this.boundary);
            }
        } 

        ///  
        /// Given the request uri, parse the uri and query parameters and cache them 
        /// 
        private void GetUriAndQueryParameters() 
        {
            if (this.queryParameters == null)
            {
                Debug.Assert(this.queryParameters == null, "this.queryParameters == null"); 
                this.queryParameters = HttpUtility.ParseQueryString(this.requestParams.AbsoluteRequestUri.Query);
            } 
        } 

        #endregion Methods. 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides an internal implementation for IDataServiceHost to keep track of states
//      for batch operations 
//  
//
// @owner  [....] 
//---------------------------------------------------------------------

namespace System.Data.Services
{ 
    #region Namespaces.
 
    using System; 
    using System.Collections.Specialized;
    using System.Diagnostics; 
    using System.IO;
    using System.Web;

    #endregion Namespaces. 

    ///  
    /// Keeps track of the request and response headers for each 
    /// operation in the batch
    ///  
    internal class BatchServiceHost : IDataServiceHost
    {
        #region Private fields.
 
        /// Response separator string.
        private readonly string boundary; 
 
        /// Request headers.
        private readonly CachedRequestParams requestParams; 

        /// Request Stream.
        private readonly Stream requestStream;
 
        /// Content Id for this operation.
        private readonly string contentId; 
 
        /// Output writer.
        private readonly StreamWriter writer; 

        /// List of query parameters as specified in the request uri.
        private NameValueCollection queryParameters;
 
        /// The value for the 'Allow' response header.
        private string responseAllowHeader; 
 
        /// Value of the response Cache-Control header.
        private string responseCacheControl; 

        /// Value of the response Content-Type header.
        private string responseContentType;
 
        /// Value of the response ETag header.
        private string responseETag; 
 
        /// Value of the response Location header.
        private string responseLocation; 

        /// Value of the response StatusCode header.
        private int responseStatusCode;
 
        /// The value for the DataServiceVersion response header.
        private string responseVersion; 
 
        #endregion Private fields.
 
        #region Constructors.

        /// 
        /// Initializes a new dummy host for the batch request. 
        /// This host represents a single operation in the batch.
        ///  
        /// cached request headers. 
        /// Request stream (possibly null, eg for a DELETE operation).
        /// content id for the given operation host. 
        /// Response separator string.
        /// Output writer.
        internal BatchServiceHost(CachedRequestParams requestParams, Stream requestStream, string contentId, string boundary, StreamWriter writer)
        { 
            Debug.Assert(requestParams != null, "requestParams != null");
            Debug.Assert(boundary != null, "boundary != null"); 
            Debug.Assert(writer != null, "writer != null"); 
            this.requestParams = requestParams;
            this.requestStream = requestStream; 
            this.contentId = contentId;
            this.boundary = boundary;
            this.writer = writer;
        } 

        ///  
        /// Initializes a host for error scenarios - something to which we can write the response header values 
        /// and write them to the underlying stream.
        ///  
        /// Response separator string.
        /// Output writer.
        internal BatchServiceHost(string boundary, StreamWriter writer)
        { 
            Debug.Assert(boundary != null, "boundary != null");
            Debug.Assert(writer != null, "writer != null"); 
            this.boundary = boundary; 
            this.writer = writer;
        } 

        #endregion Constructors.

        #region Properties. 

        /// Gets the absolute URI to the resource upon which to apply the request. 
        Uri IDataServiceHost.AbsoluteRequestUri 
        {
            get 
            {
                if (this.requestParams != null)
                {
                    return this.requestParams.AbsoluteRequestUri; 
                }
 
                return null; 
            }
        } 

        /// Gets the absolute URI to the service.
        Uri IDataServiceHost.AbsoluteServiceUri
        { 
            get
            { 
                if (this.requestParams != null) 
                {
                    return this.requestParams.AbsoluteServiceUri; 
                }

                return null;
            } 
        }
 
        ///  
        /// Gets the character set encoding that the client requested,
        /// possibly null. 
        /// 
        string IDataServiceHost.RequestAccept
        {
            get 
            {
                if (this.requestParams != null) 
                { 
                    return this.requestParams.Accept;
                } 

                return null;
            }
        } 

        ///  
        /// Gets the character set encoding that the client requested, 
        /// possibly null.
        ///  
        string IDataServiceHost.RequestAcceptCharSet
        {
            get
            { 
                if (this.requestParams != null)
                { 
                    return this.requestParams.AcceptCharset; 
                }
 
                return null;
            }
        }
 
        /// Gets the HTTP MIME type of the input stream.
        string IDataServiceHost.RequestContentType 
        { 
            get
            { 
                if (this.requestParams != null)
                {
                    return this.requestParams.ContentType;
                } 

                return null; 
            } 
        }
 
        /// 
        /// Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client.
        /// 
        string IDataServiceHost.RequestHttpMethod 
        {
            get 
            { 
                if (this.requestParams != null)
                { 
                    return this.requestParams.HttpMethod;
                }

                return null; 
            }
        } 
 
        /// Gets the value of the If-Match header from the request made
        string IDataServiceHost.RequestIfMatch 
        {
            get
            {
                if (this.requestParams != null) 
                {
                    return this.requestParams.IfMatch; 
                } 

                return null; 
            }
        }

        /// Gets the value of the If-None-Match header from the request made 
        string IDataServiceHost.RequestIfNoneMatch
        { 
            get 
            {
                if (this.requestParams != null) 
                {
                    return this.requestParams.IfNoneMatch;
                }
 
                return null;
            } 
        } 

        /// Gets the value for the MaxDataServiceVersion request header. 
        string IDataServiceHost.RequestMaxVersion
        {
            get
            { 
                if (this.requestParams != null)
                { 
                    return this.requestParams.MaxVersion; 
                }
 
                return null;
            }
        }
 
        /// Gets the value for the DataServiceVersion request header.
        string IDataServiceHost.RequestVersion 
        { 
            get
            { 
                if (this.requestParams != null)
                {
                    return this.requestParams.Version;
                } 

                return null; 
            } 
        }
 
        /// Gets or sets the Cache-Control header on the response.
        string IDataServiceHost.ResponseCacheControl
        {
            get { return this.responseCacheControl; } 
            set { this.responseCacheControl = value; }
        } 
 
        /// Gets or sets the HTTP MIME type of the output stream.
        string IDataServiceHost.ResponseContentType 
        {
            get { return this.responseContentType; }
            set { this.responseContentType = value; }
        } 

        /// Gets/Sets the value of the ETag header on the outgoing response 
        string IDataServiceHost.ResponseETag 
        {
            get { return this.responseETag; } 
            set { this.responseETag = value; }
        }

        /// Gets or sets the Location header on the response. 
        string IDataServiceHost.ResponseLocation
        { 
            get { return this.responseLocation; } 
            set { this.responseLocation = value; }
        } 

        /// 
        /// Gets/Sets the status code for the request made.
        ///  
        int IDataServiceHost.ResponseStatusCode
        { 
            get { return this.responseStatusCode; } 
            set { this.responseStatusCode = value; }
        } 

        /// 
        /// Gets the  to be written to send a response
        /// to the client. 
        /// 
        Stream IDataServiceHost.ResponseStream 
        { 
            get
            { 
                // There is a batch stream for writing requests for batch operations.
                // Hence this method should never be called.
                throw Error.NotSupported();
            } 
        }
 
        /// Gets or sets the value for the DataServiceVersion response header. 
        string IDataServiceHost.ResponseVersion
        { 
            get { return this.responseVersion; }
            set { this.responseVersion = value; }
        }
 
        /// 
        /// Gets the  from which the request data can be read from 
        /// to the client. 
        /// 
        Stream IDataServiceHost.RequestStream 
        {
            [DebuggerStepThrough]
            get { return this.requestStream; }
        } 

        /// Response separator string. 
        internal string BoundaryString 
        {
            get { return this.boundary; } 
        }

        ///  returns all the cached request headers 
        internal CachedRequestParams RequestParams 
        {
            get { return this.requestParams; } 
        } 

        /// The value for the 'Allow' response header. 
        internal string ResponseAllowHeader
        {
            get { return this.responseAllowHeader; }
        } 

        ///  
        /// Gets/Sets the content id as specified in the batch request. 
        /// This same value is written out in the response headers also to allow mapping requests on the client.
        ///  
        internal string ContentId
        {
            get { return this.contentId; }
        } 

        /// Output writer. 
        internal StreamWriter Writer 
        {
            get { return this.writer; } 
        }

        #endregion Properties.
 
        #region Methods.
 
        /// Gets the value for the specified item in the request query string. 
        /// Item to return.
        ///  
        /// The value for the specified item in the request query string;
        /// null if  is not found.
        /// 
        string IDataServiceHost.GetQueryStringItem(string item) 
        {
            this.GetUriAndQueryParameters(); 
 
            string[] result = this.queryParameters.GetValues(item);
            if (result == null || result.Length == 0) 
            {
                return null;
            }
            else if (result.Length == 1) 
            {
                return result[0]; 
            } 
            else
            { 
                throw DataServiceException.CreateBadRequestError(
                    Strings.DataServiceHost_MoreThanOneQueryParameterSpecifiedWithTheGivenName(item, this.requestParams.AbsoluteRequestUri));
            }
        } 

        /// Method to handle a data service exception during processing. 
        /// Exception handling description. 
        void IDataServiceHost.ProcessException(HandleExceptionArgs args)
        { 
            // This would typically set headers on the host.
            Debug.Assert(args != null, "args != null");
            Debug.Assert(WebUtil.IsCatchableExceptionType(args.Exception), "WebUtil.IsCatchableExceptionType(args.Exception)");
            this.responseStatusCode = args.ResponseStatusCode; 
            this.responseContentType = args.ResponseContentType;
            this.responseAllowHeader = args.ResponseAllowHeader; 
 
            // Only write the headers if the response is not written
            if (!args.ResponseWritten) 
            {
                System.Data.Services.Serializers.BatchWriter.WriteBoundaryAndHeaders(this.writer, this, this.boundary);
            }
        } 

        ///  
        /// Given the request uri, parse the uri and query parameters and cache them 
        /// 
        private void GetUriAndQueryParameters() 
        {
            if (this.queryParameters == null)
            {
                Debug.Assert(this.queryParameters == null, "this.queryParameters == null"); 
                this.queryParameters = HttpUtility.ParseQueryString(this.requestParams.AbsoluteRequestUri.Query);
            } 
        } 

        #endregion Methods. 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK