Exemplo n.º 1
0
        /// <summary>
        /// Instantiate an <see cref="ODataBatchOperationRequestMessage"/> instance.
        /// </summary>
        /// <param name="streamCreatorFunc">The function for stream creation.</param>
        /// <param name="method">The HTTP method used for this request message.</param>
        /// <param name="requestUri">The request Url for this request message.</param>
        /// <param name="headers">The headers for this request message.</param>
        /// <param name="contentId">The contentId of this request message.</param>
        /// <param name="groupId">The group id that this request belongs to. Can be null.</param>
        /// <param name="dependsOnRequestIds">
        /// The prerequisite request Ids of this request message that could be specified by caller
        /// explicitly.
        /// </param>
        /// <param name="dependsOnIdsValidationRequired">
        /// Whether the <code>dependsOnIds</code> value needs to be validated.</param>
        /// <returns>The <see cref="ODataBatchOperationRequestMessage"/> instance.</returns>
        protected ODataBatchOperationRequestMessage BuildOperationRequestMessage(
            Func <Stream> streamCreatorFunc,
            string method,
            Uri requestUri,
            ODataBatchOperationHeaders headers,
            string contentId,
            string groupId,
            IEnumerable <string> dependsOnRequestIds,
            bool dependsOnIdsValidationRequired)
        {
            if (dependsOnRequestIds != null && dependsOnIdsValidationRequired)
            {
                foreach (string id in dependsOnRequestIds)
                {
                    if (!this.payloadUriConverter.ContainsContentId(id))
                    {
                        throw new ODataException(Strings.ODataBatchReader_DependsOnIdNotFound(id, contentId));
                    }
                }
            }

            Uri uri = ODataBatchUtils.CreateOperationRequestUri(
                requestUri, this.inputContext.MessageReaderSettings.BaseUri, this.payloadUriConverter);

            ODataBatchUtils.ValidateReferenceUri(requestUri, dependsOnRequestIds,
                                                 this.inputContext.MessageReaderSettings.BaseUri);

            return(new ODataBatchOperationRequestMessage(streamCreatorFunc, method, uri, headers, this,
                                                         contentId, this.payloadUriConverter, /*writing*/ false, this.container, dependsOnRequestIds, groupId));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor. Creates a request message for an operation of a batch request.
        /// </summary>
        /// <param name="contentStreamCreatorFunc">A function to create the content stream.</param>
        /// <param name="method">The HTTP method used for this request message.</param>
        /// <param name="requestUrl">The request Url for this request message.</param>
        /// <param name="headers">The headers for this request message.</param>
        /// <param name="operationListener">Listener interface to be notified of operation changes.</param>
        /// <param name="contentId">The content-ID for the operation request message.</param>
        /// <param name="payloadUriConverter">The optional URL converter to perform custom URL conversion for URLs written to the payload.</param>
        /// <param name="writing">true if the request message is being written; false when it is read.</param>
        /// <param name="container">The dependency injection container to get related services.</param>
        /// <param name="dependsOnIds">
        /// Request or group Ids that current request has dependency on. Values are added to a new list.
        /// Empty list will be created if value is null.
        /// </param>
        /// <param name="groupId">Value for the group id that current request belongs to. Can be null.</param>
        internal ODataBatchOperationRequestMessage(
            Func <Stream> contentStreamCreatorFunc,
            string method,
            Uri requestUrl,
            ODataBatchOperationHeaders headers,
            IODataBatchOperationListener operationListener,
            string contentId,
            IODataPayloadUriConverter payloadUriConverter,
            bool writing,
            IServiceProvider container,
            IEnumerable <string> dependsOnIds,
            string groupId)
        {
            Debug.Assert(contentStreamCreatorFunc != null, "contentStreamCreatorFunc != null");
            Debug.Assert(operationListener != null, "operationListener != null");
            Debug.Assert(payloadUriConverter != null, "payloadUriConverter != null");

            this.Method    = method;
            this.Url       = requestUrl;
            this.ContentId = contentId;
            this.groupId   = groupId;

            this.message   = new ODataBatchOperationMessage(contentStreamCreatorFunc, headers, operationListener, payloadUriConverter, writing);
            this.Container = container;

            this.dependsOnIds = dependsOnIds == null
                ? new List <string>()
                : new List <string>(dependsOnIds);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads the headers of a batch part or an operation.
        /// </summary>
        /// <returns>A dictionary of header names to header values; never null.</returns>
        internal ODataBatchOperationHeaders ReadHeaders()
        {
            Debug.Assert(this.batchEncoding != null, "Batch encoding should have been established on first call to SkipToBoundary.");

            // [Astoria-ODataLib-Integration] WCF DS Batch reader compares header names in batch part using case insensitive comparison, ODataLib is case sensitive
            ODataBatchOperationHeaders headers = new ODataBatchOperationHeaders();

            // Read all the headers
            string headerLine = this.ReadLine();

            while (headerLine != null && headerLine.Length > 0)
            {
                string headerName, headerValue;
                ValidateHeaderLine(headerLine, out headerName, out headerValue);

                if (headers.ContainsKeyOrdinal(headerName))
                {
                    throw new ODataException(Strings.ODataBatchReaderStream_DuplicateHeaderFound(headerName));
                }

                headers.Add(headerName, headerValue);
                headerLine = this.ReadLine();
            }

            return(headers);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads the headers of a part.
        /// </summary>
        /// <param name="contentId">Content-ID read from changeset header, null if changeset part detected</param>
        /// <returns>true if the start of a changeset part was detected; otherwise false.</returns>
        internal bool ProcessPartHeader(out string contentId)
        {
            Debug.Assert(this.batchEncoding != null, "Batch encoding should have been established on first call to SkipToBoundary.");

            bool isChangeSetPart;
            ODataBatchOperationHeaders headers = this.ReadPartHeaders(out isChangeSetPart);

            contentId = null;

            if (isChangeSetPart)
            {
                // determine the changeset boundary and the changeset encoding from the content type header
                this.DetermineChangesetBoundaryAndEncoding(headers[ODataConstants.ContentTypeHeader]);

                if (this.changesetEncoding == null)
                {
                    // NOTE: No changeset encoding was specified in the changeset's content type header.
                    //       Determine the changeset encoding from the first bytes in the changeset.
                    // NOTE: We do not have to skip over the potential preamble of the encoding
                    //       because the batch reader will skip over everything (incl. the preamble)
                    //       until it finds the first changeset (or batch) boundary
                    this.changesetEncoding = this.DetectEncoding();
                }

                // Verify that we only allow single byte encodings and UTF-8 for now.
                ReaderValidationUtils.ValidateEncodingSupportedInBatch(this.changesetEncoding);
            }
            else if (this.ChangeSetBoundary != null)
            {
                headers.TryGetValue(ODataConstants.ContentIdHeader, out contentId);
            }

            return(isChangeSetPart);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the cached <see cref="ODataBatchOperationRequestMessage"/> for reading the content of an operation
        /// in a batch request.
        /// </summary>
        /// <returns>The message that can be used to read the content of the batch request operation from.</returns>
        private ODataBatchOperationRequestMessage CreateOperationRequestMessageImplementation()
        {
            this.operationState = OperationState.MessageCreated;

            string requestLine = this.batchStream.ReadFirstNonEmptyLine();

            string httpMethod;
            Uri    requestUri;

            this.ParseRequestLine(requestLine, out httpMethod, out requestUri);

            // Read all headers and create the request message
            ODataBatchOperationHeaders headers = this.batchStream.ReadHeaders();

            if (this.batchStream.ChangeSetBoundary != null)
            {
                if (this.allowLegacyContentIdBehaviour)
                {
                    // Add a potential Content-ID header to the URL resolver so that it will be available
                    // to subsequent operations.
                    string contentId;
                    if (this.contentIdToAddOnNextRead == null && headers.TryGetValue(ODataConstants.ContentIdHeader, out contentId))
                    {
                        if (contentId != null && this.payloadUriConverter.ContainsContentId(contentId))
                        {
                            throw new ODataException(Strings.ODataBatchReader_DuplicateContentIDsNotAllowed(contentId));
                        }

                        this.contentIdToAddOnNextRead = contentId;
                    }
                }

                if (this.contentIdToAddOnNextRead == null)
                {
                    throw new ODataException(Strings.ODataBatchOperationHeaderDictionary_KeyNotFound(ODataConstants.ContentIdHeader));
                }
            }

            ODataBatchOperationRequestMessage requestMessage = ODataBatchOperationRequestMessage.CreateReadMessage(
                this.batchStream,
                httpMethod,
                requestUri,
                headers,
                /*operationListener*/ this,
                this.contentIdToAddOnNextRead,
                this.payloadUriConverter,
                this.container);

            return(requestMessage);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Constructor. Base class constructor to create a message for an operation of a batch request/response.
        /// </summary>
        /// <param name="contentStreamCreatorFunc">A function to retrieve the content stream for this batch operation message.</param>
        /// <param name="headers">The headers of the batch operation message.</param>
        /// <param name="operationListener">Listener interface to be notified of part changes.</param>
        /// <param name="payloadUriConverter">The URL resolver to perform custom URL resolution for URLs read or written from/to the payload.</param>
        /// <param name="writing">true if the request message is being written; false when it is read.</param>
        internal ODataBatchOperationMessage(
            Func <Stream> contentStreamCreatorFunc,
            ODataBatchOperationHeaders headers,
            IODataStreamListener operationListener,
            IODataPayloadUriConverter payloadUriConverter,
            bool writing)
            : base(writing, /*disableMessageStreamDisposal*/ false, /*maxMessageSize*/ -1)
        {
            Debug.Assert(contentStreamCreatorFunc != null, "contentStreamCreatorFunc != null");
            Debug.Assert(operationListener != null, "operationListener != null");

            this.contentStreamCreatorFunc = contentStreamCreatorFunc;
            this.operationListener        = operationListener;
            this.headers             = headers;
            this.payloadUriConverter = payloadUriConverter;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="contentStreamCreatorFunc">A function to retrieve the content stream for this batch operation message.</param>
        /// <param name="headers">The headers of the batch operation message.</param>
        /// <param name="operationListener">Listener interface to be notified of part changes.</param>
        /// <param name="contentId">The content-ID for the operation response message.</param>
        /// <param name="payloadUriConverter">The optional URL converter to perform custom URL conversion for URLs written to the payload.</param>
        /// <param name="writing">true if the request message is being written; false when it is read.</param>
        /// <param name="container">The dependency injection container to get related services.</param>
        private ODataBatchOperationResponseMessage(
            Func <Stream> contentStreamCreatorFunc,
            ODataBatchOperationHeaders headers,
            IODataBatchOperationListener operationListener,
            string contentId,
            IODataPayloadUriConverter payloadUriConverter,
            bool writing,
            IServiceProvider container)
        {
            Debug.Assert(contentStreamCreatorFunc != null, "contentStreamCreatorFunc != null");
            Debug.Assert(operationListener != null, "operationListener != null");

            this.message   = new ODataBatchOperationMessage(contentStreamCreatorFunc, headers, operationListener, payloadUriConverter, writing);
            this.ContentId = contentId;
            this.Container = container;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Instantiate an <see cref="ODataBatchOperationResponseMessage"/> instance and set the status code.
        /// </summary>
        /// <param name="streamCreatorFunc">The function for stream creation.</param>
        /// <param name="statusCode">The status code for the response.</param>
        /// <param name="headers">The headers for this response message.</param>
        /// <param name="contentId">The contentId of this request message.</param>
        /// <param name="groupId">The groupId of this request message.</param>
        /// <returns>The <see cref="ODataBatchOperationResponseMessage"/> instance.</returns>
        protected ODataBatchOperationResponseMessage BuildOperationResponseMessage(
            Func <Stream> streamCreatorFunc,
            int statusCode,
            ODataBatchOperationHeaders headers,
            string contentId,
            string groupId)
        {
            ODataBatchOperationResponseMessage responseMessage = new ODataBatchOperationResponseMessage(
                streamCreatorFunc, headers, this,
                contentId,
                this.PayloadUriConverter.BatchMessagePayloadUriConverter, /*writing*/ false, this.container, groupId)
            {
                StatusCode = statusCode
            };

            return(responseMessage);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns the cached <see cref="ODataBatchOperationRequestMessage"/> for reading the content of an operation
        /// in a batch request.
        /// </summary>
        /// <returns>The message that can be used to read the content of the batch request operation from.</returns>
        private ODataBatchOperationResponseMessage CreateOperationResponseMessageImplementation()
        {
            this.operationState = OperationState.MessageCreated;

            string responseLine = this.batchStream.ReadFirstNonEmptyLine();

            int statusCode = this.ParseResponseLine(responseLine);

            // Read all headers and create the response message
            ODataBatchOperationHeaders headers = this.batchStream.ReadHeaders();

            if (this.batchStream.ChangeSetBoundary != null)
            {
                if (this.allowLegacyContentIdBehaviour)
                {
                    // Add a potential Content-ID header to the URL resolver so that it will be available
                    // to subsequent operations.
                    string contentId;
                    if (this.contentIdToAddOnNextRead == null && headers.TryGetValue(ODataConstants.ContentIdHeader, out contentId))
                    {
                        if (contentId != null && this.payloadUriConverter.ContainsContentId(contentId))
                        {
                            throw new ODataException(Strings.ODataBatchReader_DuplicateContentIDsNotAllowed(contentId));
                        }

                        this.contentIdToAddOnNextRead = contentId;
                    }
                }
            }

            // In responses we don't need to use our batch URL resolver, since there are no cross referencing URLs
            // so use the URL resolver from the batch message instead.
            ODataBatchOperationResponseMessage responseMessage = ODataBatchOperationResponseMessage.CreateReadMessage(
                this.batchStream,
                statusCode,
                headers,
                this.contentIdToAddOnNextRead,
                /*operationListener*/ this,
                this.payloadUriConverter.BatchMessagePayloadUriConverter,
                this.container);

            //// NOTE: Content-IDs for cross referencing are only supported in request messages; in responses
            ////       we allow a Content-ID header but don't process it (i.e., don't add the content ID to the URL resolver).

            return(responseMessage);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates an operation request message that can be used to read the operation content from.
        /// </summary>
        /// <param name="batchReaderStream">The batch stream underyling the operation response message.</param>
        /// <param name="method">The HTTP method to use for the message to create.</param>
        /// <param name="requestUrl">The request URL for the message to create.</param>
        /// <param name="headers">The headers to use for the operation request message.</param>
        /// <param name="operationListener">The operation listener.</param>
        /// <param name="contentId">The content-ID for the operation request message.</param>
        /// <param name="payloadUriConverter">The (optional) URL converter for the message to create.</param>
        /// <param name="container">The dependency injection container to get related services.</param>
        /// <returns>An <see cref="ODataBatchOperationRequestMessage"/> to read the request content from.</returns>
        internal static ODataBatchOperationRequestMessage CreateReadMessage(
            ODataBatchReaderStream batchReaderStream,
            string method,
            Uri requestUrl,
            ODataBatchOperationHeaders headers,
            IODataBatchOperationListener operationListener,
            string contentId,
            IODataPayloadUriConverter payloadUriConverter,
            IServiceProvider container)
        {
            Debug.Assert(batchReaderStream != null, "batchReaderStream != null");
            Debug.Assert(operationListener != null, "operationListener != null");

            Func <Stream> streamCreatorFunc = () => ODataBatchUtils.CreateBatchOperationReadStream(batchReaderStream, headers, operationListener);

            return(new ODataBatchOperationRequestMessage(streamCreatorFunc, method, requestUrl, headers, operationListener, contentId, payloadUriConverter, /*writing*/ false, container));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Validates the headers that have been read for a part.
        /// </summary>
        /// <param name="headers">The set of headers to validate.</param>
        /// <param name="isChangeSetPart">true if the headers indicate a changset part; otherwise false.</param>
        /// <returns>The set of validated headers.</returns>
        /// <remarks>
        /// An operation part is required to have content type 'application/http' and content transfer
        /// encoding 'binary'. A changeset is required to have content type 'multipart/mixed'.
        /// Note that we allow additional headers for batch parts; clients of the library can choose
        /// to be more strict.
        /// </remarks>
        private ODataBatchOperationHeaders ValidatePartHeaders(ODataBatchOperationHeaders headers, out bool isChangeSetPart)
        {
            string contentType;

            if (!headers.TryGetValue(ODataConstants.ContentTypeHeader, out contentType))
            {
                throw new ODataException(Strings.ODataBatchReaderStream_MissingContentTypeHeader);
            }

            if (MediaTypeUtils.MediaTypeAndSubtypeAreEqual(contentType, MimeConstants.MimeApplicationHttp))
            {
                isChangeSetPart = false;

                // An operation part is required to have application/http content type and
                // binary content transfer encoding.
                string transferEncoding;
                if (!headers.TryGetValue(ODataConstants.ContentTransferEncoding, out transferEncoding) ||
                    string.Compare(transferEncoding, ODataConstants.BatchContentTransferEncoding, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new ODataException(Strings.ODataBatchReaderStream_MissingOrInvalidContentEncodingHeader(
                                                 ODataConstants.ContentTransferEncoding,
                                                 ODataConstants.BatchContentTransferEncoding));
                }
            }
            else if (MediaTypeUtils.MediaTypeStartsWithTypeAndSubtype(contentType, MimeConstants.MimeMultipartMixed))
            {
                isChangeSetPart = true;

                if (this.changesetBoundary != null)
                {
                    // Nested changesets are not supported
                    throw new ODataException(Strings.ODataBatchReaderStream_NestedChangesetsAreNotSupported);
                }
            }
            else
            {
                throw new ODataException(Strings.ODataBatchReaderStream_InvalidContentTypeSpecified(
                                             ODataConstants.ContentTypeHeader,
                                             contentType,
                                             MimeConstants.MimeMultipartMixed,
                                             MimeConstants.MimeApplicationHttp));
            }

            return(headers);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates an operation response message that can be used to read the operation content from.
        /// </summary>
        /// <param name="batchReaderStream">The batch stream underyling the operation response message.</param>
        /// <param name="statusCode">The status code to use for the operation response message.</param>
        /// <param name="headers">The headers to use for the operation response message.</param>
        /// <param name="contentId">The content-ID for the operation response message.</param>
        /// <param name="operationListener">The operation listener.</param>
        /// <param name="payloadUriConverter">The (optional) URL converter for the message to create.</param>
        /// <param name="container">The dependency injection container to get related services.</param>
        /// <returns>An <see cref="ODataBatchOperationResponseMessage"/> that can be used to read the operation content.</returns>
        internal static ODataBatchOperationResponseMessage CreateReadMessage(
            ODataBatchReaderStream batchReaderStream,
            int statusCode,
            ODataBatchOperationHeaders headers,
            string contentId,
            IODataBatchOperationListener operationListener,
            IODataPayloadUriConverter payloadUriConverter,
            IServiceProvider container)
        {
            Debug.Assert(batchReaderStream != null, "batchReaderStream != null");
            Debug.Assert(operationListener != null, "operationListener != null");

            Func <Stream> streamCreatorFunc = () => ODataBatchUtils.CreateBatchOperationReadStream(batchReaderStream, headers, operationListener);
            ODataBatchOperationResponseMessage responseMessage =
                new ODataBatchOperationResponseMessage(streamCreatorFunc, headers, operationListener, contentId, payloadUriConverter, /*writing*/ false, container);

            responseMessage.statusCode = statusCode;
            return(responseMessage);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Sets the value of an HTTP header of this operation.
        /// </summary>
        /// <param name="headerName">The name of the header to set.</param>
        /// <param name="headerValue">The value of the HTTP header or 'null' if the header should be removed.</param>
        public override void SetHeader(string headerName, string headerValue)
        {
            this.VerifyNotCompleted();
            this.VerifyCanSetHeader();

            if (headerValue == null)
            {
                if (this.headers != null)
                {
                    this.headers.Remove(headerName);
                }
            }
            else
            {
                if (this.headers == null)
                {
                    this.headers = new ODataBatchOperationHeaders();
                }

                this.headers[headerName] = headerValue;
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Creates a batch operation stream from the specified batch stream.
        /// </summary>
        /// <param name="batchReaderStream">The batch stream to create the operation read stream for.</param>
        /// <param name="headers">The headers of the current part; based on the header we create different, optimized stream implementations.</param>
        /// <param name="operationListener">The operation listener to be passed to the newly created read stream.</param>
        /// <returns>A new <see cref="ODataReadStream"/> instance.</returns>
        internal static ODataReadStream CreateBatchOperationReadStream(
            ODataBatchReaderStream batchReaderStream,
            ODataBatchOperationHeaders headers,
            IODataStreamListener operationListener)
        {
            Debug.Assert(batchReaderStream != null, "batchReaderStream != null");
            Debug.Assert(operationListener != null, "operationListener != null");

            // See whether we have a Content-Length header
            string contentLengthValue;

            if (headers.TryGetValue(ODataConstants.ContentLengthHeader, out contentLengthValue))
            {
                int length = Int32.Parse(contentLengthValue, CultureInfo.InvariantCulture);
                if (length < 0)
                {
                    throw new ODataException(Strings.ODataBatchReaderStream_InvalidContentLengthSpecified(contentLengthValue));
                }

                return(ODataReadStream.Create(batchReaderStream, operationListener, length));
            }

            return(ODataReadStream.Create(batchReaderStream, operationListener));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Reads and validates the headers of a batch part.
        /// </summary>
        /// <param name="isChangeSetPart">true if the headers indicate a changset part; otherwise false.</param>
        /// <returns>A dictionary of header names to header values; never null.</returns>
        private ODataBatchOperationHeaders ReadPartHeaders(out bool isChangeSetPart)
        {
            ODataBatchOperationHeaders partHeaders = this.ReadHeaders();

            return(this.ValidatePartHeaders(partHeaders, out isChangeSetPart));
        }