Ejemplo n.º 1
0
 private ODataBatchWriter CreateODataBatchWriterImplementation(string batchBoundary)
 {
     this.encoding = this.encoding ?? MediaTypeUtils.EncodingUtf8NoPreamble;
     ODataBatchWriter writer = new ODataBatchWriter(this, batchBoundary);
     this.outputInStreamErrorListener = writer;
     return writer;
 }
        /// <summary>
        /// Writes a single OData batch response.
        /// </summary>
        /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
        /// <param name="response">The response message.</param>
        public static async Task WriteMessageAsync(ODataBatchWriter writer, HttpResponseMessage response)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull("writer");
            }
            if (response == null)
            {
                throw Error.ArgumentNull("response");
            }

            ODataBatchOperationResponseMessage batchResponse = writer.CreateOperationResponseMessage();

            batchResponse.StatusCode = (int)response.StatusCode;

            foreach (KeyValuePair<string, IEnumerable<string>> header in response.Headers)
            {
                batchResponse.SetHeader(header.Key, String.Join(",", header.Value));
            }

            if (response.Content != null)
            {
                foreach (KeyValuePair<string, IEnumerable<string>> header in response.Content.Headers)
                {
                    batchResponse.SetHeader(header.Key, String.Join(",", header.Value));
                }

                using (Stream stream = batchResponse.GetStream())
                {
                    await response.Content.CopyToAsync(stream);
                }
            }
        }
Ejemplo n.º 3
0
        private ODataBatchWriter CreateODataBatchWriterImplementation(string batchBoundary)
        {
            this.encoding = this.encoding ?? MediaTypeUtils.EncodingUtf8NoPreamble;
            ODataBatchWriter writer = new ODataBatchWriter(this, batchBoundary);

            this.outputInStreamErrorListener = writer;
            return(writer);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a batch writer.
        /// </summary>
        /// <param name="batchBoundary">The boundary string for the batch structure itself.</param>
        /// <returns>The newly created batch writer.</returns>
        private ODataBatchWriter CreateODataBatchWriterImplementation(string batchBoundary)
        {
            // Batch writer needs the default encoding to not use the preamble.
            this.encoding = this.encoding ?? MediaTypeUtils.EncodingUtf8NoPreamble;
            ODataBatchWriter batchWriter = new ODataBatchWriter(this, batchBoundary);

            this.outputInStreamErrorListener = batchWriter;
            return(batchWriter);
        }
        /// <summary>
        /// Writes the response as an Operation.
        /// </summary>
        /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        public override Task WriteResponseAsync(ODataBatchWriter writer, CancellationToken cancellationToken)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull("writer");
            }

            return WriteMessageAsync(writer, Response, cancellationToken);
        }
        /// <summary>
        /// Writes the response as an Operation.
        /// </summary>
        /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
        public override Task WriteResponseAsync(ODataBatchWriter writer)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull("writer");
            }

            return WriteMessageAsync(writer, Response);
        }
Ejemplo n.º 7
0
#pragma warning disable 1998
        public override async Task StartBatchAsync()
        {
            _requestMessage = new ODataRequestMessage() { Url = _session.Settings.BaseUri };
            _messageWriter = new ODataMessageWriter(_requestMessage);
#if SILVERLIGHT
            _batchWriter = _messageWriter.CreateODataBatchWriter();
            _batchWriter.WriteStartBatch();
#else
            _batchWriter = await _messageWriter.CreateODataBatchWriterAsync();
            await _batchWriter.WriteStartBatchAsync();
#endif
        }
        /// <summary>
        /// Writes the responses as a ChangeSet.
        /// </summary>
        /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        public override async Task WriteResponseAsync(ODataBatchWriter writer, CancellationToken cancellationToken)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull("writer");
            }

            writer.WriteStartChangeset();

            foreach (HttpResponseMessage responseMessage in Responses)
            {
                await WriteMessageAsync(writer, responseMessage, cancellationToken);
            }

            writer.WriteEndChangeset();
        }
Ejemplo n.º 9
0
 internal static void HandleBatchInStreamError(IDataService service, Exception exception, ODataBatchWriter batchWriter, Stream responseStream)
 {
     string str;
     Encoding encoding;
     string str2;
     Version version;
     DataServiceHostWrapper host = (service.OperationContext == null) ? null : service.OperationContext.Host;
     TryGetResponseFormatForError(service, host, RequestDescription.DataServiceDefaultResponseVersion, out str, out encoding, out str2, out version);
     HandleExceptionArgs args = new HandleExceptionArgs(exception, false, str2, service.Configuration.UseVerboseErrors);
     service.InternalHandleException(args);
     batchWriter.Flush();
     using (XmlWriter writer = XmlUtil.CreateXmlWriterAndWriteProcessingInstruction(responseStream, encoding))
     {
         ODataError error = CreateODataErrorFromExceptionArgs(args);
         ErrorUtils.WriteXmlError(writer, error, args.UseVerboseErrors, 100);
     }
 }
Ejemplo n.º 10
0
        internal BatchServiceHost(Uri absoluteServiceUri, IODataRequestMessage operationMessage, string contentId, ODataBatchWriter writer, Version maxDataServiceVersion, Version minDataServiceVersion, Version dataServiceVersion) : this(writer)
        {
            this.absoluteServiceUri = absoluteServiceUri;
			this.absoluteRequestUri = RequestUriProcessor.GetAbsoluteUriFromReference(operationMessage.Url, absoluteServiceUri, dataServiceVersion);
            this.requestHttpMethod = operationMessage.Method;
            this.contentId = contentId;
            foreach (KeyValuePair<string, string> pair in operationMessage.Headers)
            {
                this.requestHeaders.Add(pair.Key, pair.Value);
            }
            if (string.IsNullOrEmpty(this.requestHeaders["MaxDataServiceVersion"]))
            {
                this.requestHeaders["MaxDataServiceVersion"] = maxDataServiceVersion.ToString();
            }
            if (string.IsNullOrEmpty(this.requestHeaders["MinDataServiceVersion"]))
            {
                this.requestHeaders["MinDataServiceVersion"] = minDataServiceVersion.ToString();
            }
            this.requestStream = operationMessage.GetStream();
        }
        private void createUpdateOperationRequestMessage(ref ODataBatchWriter writer, Change change, string tableName, string[] headers)
        {
            string id = this.getKeyType(tableName).IsString() ? "'" + change.id + "'" : change.id;
            Uri uri = new Uri(tableName + "(" + id + ")", UriKind.Relative);
            
            var updateOperaitonMessage = writer.CreateOperationRequestMessage("PATCH", uri);
            updateOperaitonMessage.SetHeader("Content-Type", "application/json");
             

            using (var operationMessageWriter = new ODataMessageWriter(updateOperaitonMessage))
            {
                var entityWriter = operationMessageWriter.CreateODataEntryWriter();
                var entry = new ODataEntry()
                {
                    Properties = createOperationProperties(change.data, tableName, headers),
                    TypeName = this.GetEntityTypeName(tableName)
                };
                entityWriter.WriteStart(entry);
                entityWriter.WriteEnd();
            }
        }
Ejemplo n.º 12
0
 internal static void HandleBatchOperationError(IDataService service, DataServiceHostWrapper host, Exception exception, ODataBatchWriter batchWriter, Stream responseStream, Version defaultResponseVersion)
 {
     string str;
     Encoding encoding;
     string str2;
     Version version;
     TryGetResponseFormatForError(service, host, defaultResponseVersion, out str, out encoding, out str2, out version);
     HandleExceptionArgs args = new HandleExceptionArgs(exception, false, str2, service.Configuration.UseVerboseErrors);
     service.InternalHandleException(args);
     Action<Stream> action = null;
     if (host != null)
     {
         host.ResponseVersion = version.ToString(2) + ";";
         host.ProcessException(args);
         action = ProcessBenignException(exception, service);
     }
     if (action == null)
     {
         ODataBatchOperationResponseMessage operationResponseMessage;
         if (host != null)
         {
             operationResponseMessage = host.BatchServiceHost.GetOperationResponseMessage();
             WebUtil.SetResponseHeadersForBatchRequests(operationResponseMessage, host.BatchServiceHost);
         }
         else
         {
             operationResponseMessage = batchWriter.CreateOperationResponseMessage();
             operationResponseMessage.StatusCode = args.ResponseStatusCode;
         }
         using (ODataMessageWriter writer = ResponseBodyWriter.CreateMessageWriter(null, service, version, operationResponseMessage, str, null))
         {
             SerializeODataError(args, writer, responseStream, encoding);
         }
     }
 }
Ejemplo n.º 13
0
 internal static ODataRequestMessageWrapper CreateBatchPartRequestMessage(ODataBatchWriter batchWriter, string method, Uri requestUrl, RequestInfo requestInfo)
 {
     return new ODataRequestMessageWrapper(batchWriter.CreateOperationRequestMessage(method, requestUrl), requestInfo) { IsBatchPartRequest = true };
 }
        private void createDeleteOperationRequestMessage(ref ODataBatchWriter writer, Change change, string tableName)
        {
            string id = this.getKeyType(tableName).IsString() ? "'" + change.id + "'" : change.id;
            Uri uri = new Uri(tableName + "(" + id + ")", UriKind.Relative);

            writer.CreateOperationRequestMessage("DELETE", uri);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Creates a batch writer.
 /// </summary>
 /// <param name="batchBoundary">The boundary string for the batch structure itself.</param>
 /// <returns>The newly created batch writer.</returns>
 private ODataBatchWriter CreateODataBatchWriterImplementation(string batchBoundary)
 {
     // Batch writer needs the default encoding to not use the preamble.
     this.encoding = this.encoding ?? MediaTypeUtils.EncodingUtf8NoPreamble;
     ODataBatchWriter batchWriter = new ODataBatchWriter(this, batchBoundary);
     this.outputInStreamErrorListener = batchWriter;
     return batchWriter;
 }
 /// <summary>
 /// Writes the response.
 /// </summary>
 /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 public abstract Task WriteResponseAsync(ODataBatchWriter writer, CancellationToken cancellationToken);
 /// <summary>
 /// Writes a single OData batch response.
 /// </summary>
 /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
 /// <param name="response">The response message.</param>
 /// <returns>A task object representing writing the given batch response using the given writer.</returns>
 public static Task WriteMessageAsync(ODataBatchWriter writer, HttpResponseMessage response)
 {
     return WriteMessageAsync(writer, response, CancellationToken.None);
 }
Ejemplo n.º 18
0
 private ODataRequestMessageWrapper GenerateBatchRequest()
 {
     if ((base.ChangedEntries.Count == 0) && (this.Queries == null))
     {
         base.SetCompleted();
         return null;
     }
     ODataRequestMessageWrapper requestMessage = this.CreateBatchRequest();
     using (ODataMessageWriter writer = Serializer.CreateMessageWriter(requestMessage, base.RequestInfo))
     {
         ODataUtils.SetHeadersForPayload(writer, ODataPayloadKind.Batch);
         requestMessage.FireSendingRequest2(null);
         this.batchWriter = writer.CreateODataBatchWriter();
         this.batchWriter.WriteStartBatch();
         if (this.Queries != null)
         {
             for (int i = 0; i < this.Queries.Length; i++)
             {
                 Uri requestUri = base.RequestInfo.BaseUriResolver.CreateAbsoluteUriIfNeeded(this.Queries[i].QueryComponents(base.RequestInfo.MaxProtocolVersion).Uri);
                 ODataRequestMessageWrapper wrapper2 = this.CreateRequestMessage(requestUri, "GET");
                 Version requestVersion = this.Queries[i].QueryComponents(base.RequestInfo.MaxProtocolVersion).Version;
                 WebUtil.SetOperationVersionHeaders(wrapper2, requestVersion, base.RequestInfo.MaxProtocolVersionAsVersion);
                 wrapper2.FireSendingRequest2(null);
             }
         }
         else if (0 < base.ChangedEntries.Count)
         {
             this.batchWriter.WriteStartChangeset();
             ClientEdmModel model = ClientEdmModel.GetModel(base.RequestInfo.MaxProtocolVersion);
             for (int j = 0; j < base.ChangedEntries.Count; j++)
             {
                 Descriptor descriptor = base.ChangedEntries[j];
                 if (!descriptor.ContentGeneratedForSave)
                 {
                     ODataRequestMessageWrapper wrapper3;
                     EntityDescriptor entityDescriptor = descriptor as EntityDescriptor;
                     if (descriptor.DescriptorKind == DescriptorKind.Entity)
                     {
                         if (entityDescriptor.State != EntityStates.Added)
                         {
                             if (((entityDescriptor.State == EntityStates.Unchanged) || (entityDescriptor.State == EntityStates.Modified)) && (entityDescriptor.SaveStream != null))
                             {
                                 throw System.Data.Services.Client.Error.NotSupported(System.Data.Services.Client.Strings.Context_BatchNotSupportedForMediaLink);
                             }
                         }
                         else if (model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())).IsMediaLinkEntry || entityDescriptor.IsMediaLinkEntry)
                         {
                             throw System.Data.Services.Client.Error.NotSupported(System.Data.Services.Client.Strings.Context_BatchNotSupportedForMediaLink);
                         }
                     }
                     else if (descriptor.DescriptorKind == DescriptorKind.NamedStream)
                     {
                         throw System.Data.Services.Client.Error.NotSupported(System.Data.Services.Client.Strings.Context_BatchNotSupportedForNamedStreams);
                     }
                     if (descriptor.DescriptorKind == DescriptorKind.Entity)
                     {
                         wrapper3 = base.CreateRequest(entityDescriptor);
                     }
                     else
                     {
                         wrapper3 = base.CreateRequest((LinkDescriptor) descriptor);
                     }
                     wrapper3.FireSendingRequest2(descriptor);
                     base.CreateChangeData(j, wrapper3);
                 }
             }
             this.batchWriter.WriteEndChangeset();
         }
         this.batchWriter.WriteEndBatch();
         this.batchWriter.Flush();
     }
     return requestMessage;
 }
 /// <summary>
 /// Writes the response.
 /// </summary>
 /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
 public abstract Task WriteResponseAsync(ODataBatchWriter writer);
Ejemplo n.º 20
0
 internal BatchServiceHost(ODataBatchWriter writer)
 {
     this.writer = writer;
     this.requestHeaders = new WebHeaderCollection();
     this.responseHeaders = new WebHeaderCollection();
 }