Ejemplo n.º 1
0
        private void SetupMediaResourceRequest(ODataRequestMessageWrapper mediaResourceRequest, DataServiceSaveStream saveStream, string etag)
        {
            IEnumerable <string> keys;

            base.mediaResourceRequestStream = saveStream.Stream;
            WebUtil.ApplyHeadersToRequest(saveStream.Args.Headers, mediaResourceRequest, true);
            Dictionary <string, string> headers = saveStream.Args.Headers;

            if (headers.ContainsKey("Accept"))
            {
                keys = new List <string>(headers.Count - 1);
                foreach (string str in headers.Keys)
                {
                    if (str != "Accept")
                    {
                        ((List <string>)keys).Add(str);
                    }
                }
            }
            else
            {
                keys = headers.Keys;
            }
            mediaResourceRequest.AddHeadersToReset(keys);
            if (etag != null)
            {
                mediaResourceRequest.SetHeader("If-Match", etag);
                mediaResourceRequest.AddHeadersToReset("If-Match");
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
 private void SetupMediaResourceRequest(ODataRequestMessageWrapper mediaResourceRequest, DataServiceSaveStream saveStream, string etag)
 {
     IEnumerable<string> keys;
     base.mediaResourceRequestStream = saveStream.Stream;
     WebUtil.ApplyHeadersToRequest(saveStream.Args.Headers, mediaResourceRequest, true);
     Dictionary<string, string> headers = saveStream.Args.Headers;
     if (headers.ContainsKey("Accept"))
     {
         keys = new List<string>(headers.Count - 1);
         foreach (string str in headers.Keys)
         {
             if (str != "Accept")
             {
                 ((List<string>) keys).Add(str);
             }
         }
     }
     else
     {
         keys = headers.Keys;
     }
     mediaResourceRequest.AddHeadersToReset(keys);
     if (etag != null)
     {
         mediaResourceRequest.SetHeader("If-Match", etag);
         mediaResourceRequest.AddHeadersToReset("If-Match");
     }
 }