protected Message CreateResponseMessage(SyncSerializationFormat serializationFormat, SyncWriter oDataWriter) { var bodyWriter = new DelegateBodyWriter(WriteResponse, oDataWriter); Message message = Message.CreateMessage(MessageVersion.None, String.Empty, bodyWriter); switch (serializationFormat) { case SyncSerializationFormat.ODataAtom: message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Xml)); break; case SyncSerializationFormat.ODataJson: message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json)); break; } var property = new HttpResponseMessageProperty { StatusCode = HttpStatusCode.OK }; property.Headers[HttpResponseHeader.ContentType] = WebUtil.GetContentType(serializationFormat); // Copy the SyncOperationContext's ResponseHeaders if present if (this._operationContext != null) { property.Headers.Add(this._operationContext.ResponseHeaders); } message.Properties.Add(HttpResponseMessageProperty.Name, property); return message; }
/// <summary> /// Callback for the Upload HttpWebRequest.beginGetRequestStream /// </summary> /// <param name="asyncResult">IAsyncResult object</param> void OnUploadGetRequestStreamCompleted(IAsyncResult asyncResult) { AsyncArgsWrapper wrapper = asyncResult.AsyncState as AsyncArgsWrapper; try { Stream requestStream = wrapper.WebRequest.EndGetRequestStream(asyncResult); // Create a SyncWriter to write the contents this._syncWriter = (base.SerializationFormat == SerializationFormat.ODataAtom) ? (SyncWriter)new ODataAtomWriter(base.BaseUri) : (SyncWriter)new ODataJsonWriter(base.BaseUri); this._syncWriter.StartFeed(wrapper.CacheRequest.IsLastBatch, wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); foreach (IOfflineEntity entity in wrapper.CacheRequest.Changes) { // Skip tombstones that dont have a ID element. if (entity.ServiceMetadata.IsTombstone && string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { continue; } string tempId = null; // Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty if (string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { if (wrapper.TempIdToEntityMapping == null) { wrapper.TempIdToEntityMapping = new Dictionary<string, IOfflineEntity>(); } tempId = Guid.NewGuid().ToString(); wrapper.TempIdToEntityMapping.Add(tempId, entity); } this._syncWriter.AddItem(entity, tempId); } if (base.SerializationFormat == SerializationFormat.ODataAtom) { this._syncWriter.WriteFeed(XmlWriter.Create(requestStream)); } else { this._syncWriter.WriteFeed(JsonReaderWriterFactory.CreateJsonWriter(requestStream)); } requestStream.Flush(); requestStream.Close(); if (this._beforeRequestHandler != null) { // Invoke user code and wait for them to call back us when they are done with the input request this._workerManager.PostProgress(wrapper.WorkerRequest, this.FirePreRequestHandler, wrapper); } else { this.GetWebResponse(wrapper); } } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } wrapper.Error = e; this._workerManager.CompleteWorkRequest(wrapper.WorkerRequest, wrapper); } }
/// <summary> /// Callback for the Download HttpWebRequest.beginGetRequestStream /// </summary> /// <param name="asyncResult">IAsyncResult object</param> void OnDownloadGetRequestStreamCompleted(IAsyncResult asyncResult) { AsyncArgsWrapper wrapper = asyncResult.AsyncState as AsyncArgsWrapper; try { Stream requestStream = wrapper.WebRequest.EndGetRequestStream(asyncResult); // Create a SyncWriter to write the contents this._syncWriter = (base.SerializationFormat == SerializationFormat.ODataAtom) ? (SyncWriter)new ODataAtomWriter(base.BaseUri) : (SyncWriter)new ODataJsonWriter(base.BaseUri); this._syncWriter.StartFeed(wrapper.CacheRequest.IsLastBatch, wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); if (base.SerializationFormat == SerializationFormat.ODataAtom) { this._syncWriter.WriteFeed(XmlWriter.Create(requestStream)); } else { this._syncWriter.WriteFeed(JsonReaderWriterFactory.CreateJsonWriter(requestStream)); } requestStream.Flush(); requestStream.Close(); if (this._beforeRequestHandler != null) { // Invoke user code and wait for them to call back us when they are done with the input request this._workerManager.PostProgress(wrapper.WorkerRequest, this.FirePreRequestHandler, wrapper); } else { this.GetWebResponse(wrapper); } } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } wrapper.Error = e; this._workerManager.CompleteWorkRequest(wrapper.WorkerRequest, wrapper); } }
internal DelegateBodyWriter(Action<XmlDictionaryWriter, SyncWriter> writer, SyncWriter syncWriter) : base(false) { _writerAction = writer; _syncWriter = syncWriter; }
/// <summary> /// Delegate passed into the custom body writer to form the outgoing response. /// </summary> /// <param name="writer"></param> /// <param name="syncWriter"></param> private static void WriteResponse(XmlDictionaryWriter writer, SyncWriter syncWriter) { try { syncWriter.WriteFeed(writer); } catch (Exception exception) { // An exception at this point seems to be unrecoverable but ideally we should not hit exceptions since we are only // writing to the XmlDictionaryWriter. SyncServiceTracer.TraceError("Exception in WriteResponse method. Details: {0}", WebUtil.GetExceptionMessage(exception)); } }
NSData CreateRequestBody() { if (ApplicationContext.Current.Settings.BitMobileFormatterDisabled) _syncWriter = new ODataAtomWriter(BaseUri); else _syncWriter = new BMWriter(BaseUri); _syncWriter.StartFeed(_wrapper.CacheRequest.IsLastBatch, _wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); var requestStream = new MemoryStream(); if (_wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) { foreach (IOfflineEntity entity in _wrapper.CacheRequest.Changes) { var ientity = (IEntity)entity; // Skip tombstones that dont have a ID element. if (entity.ServiceMetadata.IsTombstone && string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { if (ientity != null) LogManager.Logger.SyncUpload(ientity.EntityType, true); continue; } string tempId = null; // Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty if (string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { if (_wrapper.TempIdToEntityMapping == null) { _wrapper.TempIdToEntityMapping = new Dictionary<string, IOfflineEntity>(); } tempId = Guid.NewGuid().ToString(); _wrapper.TempIdToEntityMapping.Add(tempId, entity); } _syncWriter.AddItem(entity, tempId); if (ientity != null) LogManager.Logger.SyncUpload(ientity.EntityType); } } _syncWriter.WriteFeed(SerializationFormat == SerializationFormat.ODataAtom ? XmlWriter.Create(requestStream) : JsonReaderWriterFactory.CreateJsonWriter(requestStream)); string result; requestStream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(requestStream)) { result = reader.ReadToEnd(); } requestStream.Flush(); requestStream.Close(); var dataString = new NSString(result); return dataString.DataUsingEncoding(NSStringEncoding.UTF8); }
NSData CreateRequestBody () { _syncWriter = (SyncWriter)new ODataAtomWriter (base.BaseUri); _syncWriter.StartFeed (_wrapper.CacheRequest.IsLastBatch, _wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); MemoryStream requestStream = new MemoryStream (); if (_wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) { foreach (IOfflineEntity entity in _wrapper.CacheRequest.Changes) { // Skip tombstones that dont have a ID element. if (entity.ServiceMetadata.IsTombstone && string.IsNullOrEmpty (entity.ServiceMetadata.Id)) { continue; } string tempId = null; // Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty if (string.IsNullOrEmpty (entity.ServiceMetadata.Id)) { if (_wrapper.TempIdToEntityMapping == null) { _wrapper.TempIdToEntityMapping = new Dictionary<string, IOfflineEntity> (); } tempId = Guid.NewGuid ().ToString (); _wrapper.TempIdToEntityMapping.Add (tempId, entity); } _syncWriter.AddItem (entity, tempId); } } if (base.SerializationFormat == SerializationFormat.ODataAtom) { _syncWriter.WriteFeed (XmlWriter.Create (requestStream)); } else { _syncWriter.WriteFeed (JsonReaderWriterFactory.CreateJsonWriter (requestStream)); } string result = ""; requestStream.Seek (0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader (requestStream)) { result = reader.ReadToEnd (); } requestStream.Flush (); requestStream.Close (); NSString dataString = new NSString (result); return dataString.DataUsingEncoding (NSStringEncoding.UTF8); }