protected ODataRequestMessageWrapper CreateRequest(EntityDescriptor entityDescriptor) { EntityStates state = entityDescriptor.State; Uri resourceUri = entityDescriptor.GetResourceUri(this.RequestInfo.BaseUriResolver, false); ClientEdmModel model = ClientEdmModel.GetModel(this.RequestInfo.MaxProtocolVersion); Version requestVersion = DetermineRequestVersion(model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())), state); string httpMethod = this.GetHttpMethod(state, ref requestVersion); ODataRequestMessageWrapper requestMessage = this.CreateRequestMessage(resourceUri, httpMethod); if (this.IsBatch) { requestMessage.SetHeader("Content-ID", entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture)); } if (EntityStates.Deleted != entityDescriptor.State) { requestMessage.SetHeader("Content-Type", "application/atom+xml"); } if ((EntityStates.Deleted == state) || (EntityStates.Modified == state)) { string latestETag = entityDescriptor.GetLatestETag(); if (latestETag != null) { requestMessage.SetHeader("If-Match", latestETag); if (!this.IsBatch) { requestMessage.AddHeadersToReset("If-Match"); } } } ApplyPreferences(requestMessage, httpMethod, this.RequestInfo.AddAndUpdateResponsePreference, ref requestVersion); WebUtil.SetOperationVersionHeaders(requestMessage, requestVersion, this.RequestInfo.MaxProtocolVersionAsVersion); return(requestMessage); }
/// <summary> /// Writes an entity reference link. /// </summary> /// <param name="binding">The link descriptor.</param> /// <param name="requestMessage">The request message used for writing the payload.</param> internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage) #endif { using (ODataMessageWriter messageWriter = Serializer.CreateMessageWriter(requestMessage, this.requestInfo, false /*isParameterPayload*/)) { EntityDescriptor targetResource = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target); Uri targetResourceEditLink; if (null != targetResource.GetLatestIdentity()) { // When we write the uri in the payload, we need to make sure that we write the edit // link in the payload, since the request uri is the edit link of the parent entity. // Think of a read/write service - since the uri is the target link to the parent entity // its better that we write the edit link of the child entity in the payload. targetResourceEditLink = targetResource.GetResourceUri(this.requestInfo.BaseUriResolver, false /*queryLink*/); } else { #if DEBUG Debug.Assert(isBatch, "we should be cross-referencing entities only in batch scenarios"); #endif targetResourceEditLink = UriUtil.CreateUri("$" + targetResource.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative); } ODataEntityReferenceLink referenceLink = new ODataEntityReferenceLink(); referenceLink.Url = targetResourceEditLink; messageWriter.WriteEntityReferenceLink(referenceLink); } }
protected Uri CreateRequestUri(EntityDescriptor sourceResource, LinkDescriptor binding) { Uri uri; LinkInfo linkInfo = null; if (sourceResource.TryGetLinkInfo(binding.SourceProperty, out linkInfo) && ((uri = linkInfo.AssociationLink) != null)) { if (binding.IsSourcePropertyCollection && (EntityStates.Deleted == binding.State)) { EntityDescriptor entityDescriptor = this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Target); uri = DataServiceContext.AppendKeysToUri(uri.AbsoluteUri, entityDescriptor.Entity, UriKind.Absolute, this.RequestInfo.MaxProtocolVersion); } return(uri); } return(Util.CreateUri(sourceResource.GetResourceUri(this.RequestInfo.BaseUriResolver, false), this.CreateRequestRelativeUri(binding))); }
internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage) { using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo)) { Uri resourceUri; EntityDescriptor entityDescriptor = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target); if (entityDescriptor.GetLatestIdentity() != null) { resourceUri = entityDescriptor.GetResourceUri(this.requestInfo.BaseUriResolver, false); } else { resourceUri = Util.CreateUri("$" + entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative); } ODataEntityReferenceLink link = new ODataEntityReferenceLink { Url = resourceUri }; writer.WriteEntityReferenceLink(link); } }
private ODataRequestMessageWrapper CheckAndProcessMediaEntryPost(EntityDescriptor entityDescriptor) { ClientEdmModel model = ClientEdmModel.GetModel(base.RequestInfo.MaxProtocolVersion); ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())); if (!clientTypeAnnotation.IsMediaLinkEntry && !entityDescriptor.IsMediaLinkEntry) { return(null); } if ((clientTypeAnnotation.MediaDataMember == null) && (entityDescriptor.SaveStream == null)) { throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_MLEWithoutSaveStream(clientTypeAnnotation.ElementTypeName)); } ODataRequestMessageWrapper mediaResourceRequest = null; if (clientTypeAnnotation.MediaDataMember != null) { string contentType = null; int length = 0; if (clientTypeAnnotation.MediaDataMember.MimeTypeProperty == null) { contentType = "application/octet-stream"; } else { object obj2 = clientTypeAnnotation.MediaDataMember.MimeTypeProperty.GetValue(entityDescriptor.Entity); string str2 = (obj2 != null) ? obj2.ToString() : null; if (string.IsNullOrEmpty(str2)) { throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_NoContentTypeForMediaLink(clientTypeAnnotation.ElementTypeName, clientTypeAnnotation.MediaDataMember.MimeTypeProperty.PropertyName)); } contentType = str2; } object propertyValue = clientTypeAnnotation.MediaDataMember.GetValue(entityDescriptor.Entity); if (propertyValue == null) { base.mediaResourceRequestStream = null; } else { byte[] bytes = propertyValue as byte[]; if (bytes == null) { string str3; Encoding encoding; HttpProcessUtility.ReadContentType(contentType, out str3, out encoding); if (encoding == null) { encoding = Encoding.UTF8; contentType = contentType + ";charset=UTF-8"; } bytes = encoding.GetBytes(ClientConvert.ToString(propertyValue)); } length = bytes.Length; base.mediaResourceRequestStream = new MemoryStream(bytes, 0, bytes.Length, false, true); } mediaResourceRequest = this.CreateMediaResourceRequest(entityDescriptor.GetResourceUri(base.RequestInfo.BaseUriResolver, false), "POST", Util.DataServiceVersion1, clientTypeAnnotation.MediaDataMember == null, true); mediaResourceRequest.SetHeader("Content-Length", length.ToString(CultureInfo.InvariantCulture)); mediaResourceRequest.SetHeader("Content-Type", contentType); mediaResourceRequest.AddHeadersToReset("Content-Length"); mediaResourceRequest.AddHeadersToReset("Content-Type"); } else { mediaResourceRequest = this.CreateMediaResourceRequest(entityDescriptor.GetResourceUri(base.RequestInfo.BaseUriResolver, false), "POST", Util.DataServiceVersion1, clientTypeAnnotation.MediaDataMember == null, true); this.SetupMediaResourceRequest(mediaResourceRequest, entityDescriptor.SaveStream, null); } entityDescriptor.State = EntityStates.Modified; return(mediaResourceRequest); }
private ODataRequestMessageWrapper CheckAndProcessMediaEntryPost(EntityDescriptor entityDescriptor) { ClientEdmModel model = ClientEdmModel.GetModel(base.RequestInfo.MaxProtocolVersion); ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())); if (!clientTypeAnnotation.IsMediaLinkEntry && !entityDescriptor.IsMediaLinkEntry) { return null; } if ((clientTypeAnnotation.MediaDataMember == null) && (entityDescriptor.SaveStream == null)) { throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_MLEWithoutSaveStream(clientTypeAnnotation.ElementTypeName)); } ODataRequestMessageWrapper mediaResourceRequest = null; if (clientTypeAnnotation.MediaDataMember != null) { string contentType = null; int length = 0; if (clientTypeAnnotation.MediaDataMember.MimeTypeProperty == null) { contentType = "application/octet-stream"; } else { object obj2 = clientTypeAnnotation.MediaDataMember.MimeTypeProperty.GetValue(entityDescriptor.Entity); string str2 = (obj2 != null) ? obj2.ToString() : null; if (string.IsNullOrEmpty(str2)) { throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_NoContentTypeForMediaLink(clientTypeAnnotation.ElementTypeName, clientTypeAnnotation.MediaDataMember.MimeTypeProperty.PropertyName)); } contentType = str2; } object propertyValue = clientTypeAnnotation.MediaDataMember.GetValue(entityDescriptor.Entity); if (propertyValue == null) { base.mediaResourceRequestStream = null; } else { byte[] bytes = propertyValue as byte[]; if (bytes == null) { string str3; Encoding encoding; HttpProcessUtility.ReadContentType(contentType, out str3, out encoding); if (encoding == null) { encoding = Encoding.UTF8; contentType = contentType + ";charset=UTF-8"; } bytes = encoding.GetBytes(ClientConvert.ToString(propertyValue)); } length = bytes.Length; base.mediaResourceRequestStream = new MemoryStream(bytes, 0, bytes.Length, false, true); } mediaResourceRequest = this.CreateMediaResourceRequest(entityDescriptor.GetResourceUri(base.RequestInfo.BaseUriResolver, false), "POST", Util.DataServiceVersion1, clientTypeAnnotation.MediaDataMember == null, true); mediaResourceRequest.SetHeader("Content-Length", length.ToString(CultureInfo.InvariantCulture)); mediaResourceRequest.SetHeader("Content-Type", contentType); mediaResourceRequest.AddHeadersToReset("Content-Length"); mediaResourceRequest.AddHeadersToReset("Content-Type"); } else { mediaResourceRequest = this.CreateMediaResourceRequest(entityDescriptor.GetResourceUri(base.RequestInfo.BaseUriResolver, false), "POST", Util.DataServiceVersion1, clientTypeAnnotation.MediaDataMember == null, true); this.SetupMediaResourceRequest(mediaResourceRequest, entityDescriptor.SaveStream, null); } entityDescriptor.State = EntityStates.Modified; return mediaResourceRequest; }
protected Uri CreateRequestUri(EntityDescriptor sourceResource, LinkDescriptor binding) { Uri uri; LinkInfo linkInfo = null; if (sourceResource.TryGetLinkInfo(binding.SourceProperty, out linkInfo) && ((uri = linkInfo.AssociationLink) != null)) { if (binding.IsSourcePropertyCollection && (EntityStates.Deleted == binding.State)) { EntityDescriptor entityDescriptor = this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Target); uri = DataServiceContext.AppendKeysToUri(uri.AbsoluteUri, entityDescriptor.Entity, UriKind.Absolute, this.RequestInfo.MaxProtocolVersion); } return uri; } return Util.CreateUri(sourceResource.GetResourceUri(this.RequestInfo.BaseUriResolver, false), this.CreateRequestRelativeUri(binding)); }
protected ODataRequestMessageWrapper CreateRequest(EntityDescriptor entityDescriptor) { EntityStates state = entityDescriptor.State; Uri resourceUri = entityDescriptor.GetResourceUri(this.RequestInfo.BaseUriResolver, false); ClientEdmModel model = ClientEdmModel.GetModel(this.RequestInfo.MaxProtocolVersion); Version requestVersion = DetermineRequestVersion(model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())), state); string httpMethod = this.GetHttpMethod(state, ref requestVersion); ODataRequestMessageWrapper requestMessage = this.CreateRequestMessage(resourceUri, httpMethod); if (this.IsBatch) { requestMessage.SetHeader("Content-ID", entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture)); } if (EntityStates.Deleted != entityDescriptor.State) { requestMessage.SetHeader("Content-Type", "application/atom+xml"); } if ((EntityStates.Deleted == state) || (EntityStates.Modified == state)) { string latestETag = entityDescriptor.GetLatestETag(); if (latestETag != null) { requestMessage.SetHeader("If-Match", latestETag); if (!this.IsBatch) { requestMessage.AddHeadersToReset("If-Match"); } } } ApplyPreferences(requestMessage, httpMethod, this.RequestInfo.AddAndUpdateResponsePreference, ref requestVersion); WebUtil.SetOperationVersionHeaders(requestMessage, requestVersion, this.RequestInfo.MaxProtocolVersionAsVersion); return requestMessage; }