Example #1
0
        /// <summary>
        /// Clones the current stream descriptor data
        /// </summary>
        /// <param name="clonedEntityDescriptor">The entity descriptor that will contain the result of this clone</param>
        /// <returns>A clone of the current stream descriptor data</returns>
        public StreamDescriptorData Clone(EntityDescriptorData clonedEntityDescriptor)
        {
            var clone = new StreamDescriptorData(clonedEntityDescriptor, this.Name);

            clone.State       = this.State;
            clone.ChangeOrder = this.ChangeOrder;

            clone.ContentType = this.ContentType;
            clone.ETag        = this.ETag;

            if (this.EditLink != null)
            {
                clone.EditLink = new Uri(this.EditLink.OriginalString, UriKind.RelativeOrAbsolute);
            }

            if (this.SelfLink != null)
            {
                clone.SelfLink = new Uri(this.SelfLink.OriginalString, UriKind.RelativeOrAbsolute);
            }

            if (this.SaveStream != null)
            {
                clone.SaveStream = this.SaveStream.Clone();
            }

            return(clone);
        }
        /// <summary>
        /// Sets the etag
        /// </summary>
        /// <param name="streamDescriptorData">The stream descriptor data.</param>
        /// <param name="value">The etag to set.</param>
        /// <returns><see cref="StreamDescriptorData"/> on which etag has been set.</returns>
        public static StreamDescriptorData SetETag(this StreamDescriptorData streamDescriptorData, string value)
        {
            ExceptionUtilities.CheckArgumentNotNull(streamDescriptorData, "streamDescriptorData");

            streamDescriptorData.ETag = value;

            return(streamDescriptorData);
        }
        /// <summary>
        /// Sets the content type
        /// </summary>
        /// <param name="streamDescriptorData">The stream descriptor data.</param>
        /// <param name="contentType">The content type.</param>
        /// <returns><see cref="StreamDescriptorData"/> on which content type has been set.</returns>
        public static StreamDescriptorData SetContentType(this StreamDescriptorData streamDescriptorData, string contentType)
        {
            ExceptionUtilities.CheckArgumentNotNull(streamDescriptorData, "streamDescriptorData");

            streamDescriptorData.ContentType = contentType;

            return(streamDescriptorData);
        }
        /// <summary>
        /// Sets the self link.
        /// </summary>
        /// <param name="streamDescriptorData">The stream descriptor data.</param>
        /// <param name="selfLink">The self link.</param>
        /// <returns><see cref="StreamDescriptorData"/> on which self link has been set.</returns>
        public static StreamDescriptorData SetSelfLink(this StreamDescriptorData streamDescriptorData, Uri selfLink)
        {
            ExceptionUtilities.CheckArgumentNotNull(streamDescriptorData, "streamDescriptorData");

            streamDescriptorData.SelfLink = selfLink;

            return(streamDescriptorData);
        }
Example #5
0
 private void InitializeDefaultStreamDescriptor()
 {
     if (this.defaultStreamDescriptor == null)
     {
         this.defaultStreamDescriptor       = new StreamDescriptorData(this);
         this.defaultStreamDescriptor.State = EntityStates.Unchanged;
     }
 }
        /// <summary>
        /// Updates the stream decriptor data with values from response headers
        /// </summary>
        /// <param name="streamDescriptorData">The descriptor data to update</param>
        /// <param name="headers">The response headers</param>
        public static void UpdateFromHeaders(this StreamDescriptorData streamDescriptorData, IDictionary <string, string> headers)
        {
            ExceptionUtilities.CheckArgumentNotNull(streamDescriptorData, "streamDescriptorData");
            ExceptionUtilities.CheckArgumentNotNull(headers, "headers");

            string etag;

            headers.TryGetValue(HttpHeaders.ETag, out etag);
            streamDescriptorData.SetETag(etag);
        }
Example #7
0
        /// <summary>
        /// Creates a new stream descriptor data and adds it to this entity descriptor data
        /// </summary>
        /// <param name="state">The state of the stream descriptor data</param>
        /// <param name="changeOrder">The change order for the stream descriptor data</param>
        /// <param name="name">The name of the stream</param>
        /// <returns>The stream descriptor data</returns>
        public StreamDescriptorData CreateStreamDescriptorData(EntityStates state, long changeOrder, string name)
        {
            ExceptionUtilities.CheckArgumentNotNull(name, "name");
            ExceptionUtilities.Assert(!this.streamDescriptors.Any(s => s.Name == name), "Stream descriptor data with name '{0}' already exists on entity descriptor data: {1}", name, this);

            var streamData = new StreamDescriptorData(this, name);

            this.contextData.ChangeStateAndChangeOrder(streamData, state, changeOrder);

            this.streamDescriptors.Add(streamData);

            return(streamData);
        }
        /// <summary>
        /// Tries to get the stream descriptor data with the given name for the given entity
        /// </summary>
        /// <param name="entity">The entity to get the stream descriptor for</param>
        /// <param name="name">The name of the stream descriptor</param>
        /// <param name="streamData">The stream descriptor data</param>
        /// <returns>True if the stream descriptor data was found, otherwise false</returns>
        public bool TryGetStreamDescriptorData(object entity, string name, out StreamDescriptorData streamData)
        {
            ExceptionUtilities.CheckArgumentNotNull(entity, "entity");
            ExceptionUtilities.CheckArgumentNotNull(name, "name");

            streamData = null;
            EntityDescriptorData entityData;
            if (!this.TryGetEntityDescriptorData(entity, out entityData))
            {
                streamData = null;
                return false;
            }

            var found = entityData.StreamDescriptors.Where(n => n.Name == name).ToList();
            ExceptionUtilities.Assert(found.Count <= 1, "Multiple stream descriptors found with name '{0}'. Entity = {1}", name, entityData);
            if (found.Count < 1)
            {
                return false;
            }

            streamData = found[0];
            return true;
        }
        private ExpectedClientRequest CreateSaveStreamRequest(StreamDescriptorData streamDescriptorData, Uri requestUri)
        {
            HttpVerb verb;
            if (streamDescriptorData.State == EntityStates.Added)
            {
                verb = HttpVerb.Post;
            }
            else
            {
                ExceptionUtilities.Assert(streamDescriptorData.State == EntityStates.Modified, "Stream descriptor state should only be added or modified. State was: '{0}'", streamDescriptorData.State);
                verb = HttpVerb.Put;
            }

            var request = new ExpectedClientRequest() { Verb = verb, Uri = requestUri };
            foreach (var header in streamDescriptorData.SaveStream.Headers)
            {
                request.Headers.Add(header);
            }

            request.Body = new PrimitiveValue(null, streamDescriptorData.SaveStream.StreamLogger.GetAllBytesRead());
            return request;
        }
        private ExpectedClientRequest CreateStreamUpdateRequest(StreamDescriptorData streamDescriptorData, SaveChangesOptions options)
        {
            var request = this.CreateSaveStreamRequest(streamDescriptorData, streamDescriptorData.EditLink);
            request.Headers[HttpHeaders.IfMatch] = streamDescriptorData.ETag;
            request.Headers[HttpHeaders.Prefer] = null;
            request.Headers[HttpHeaders.DataServiceVersion] = ToClientHeaderFormat(DataServiceProtocolVersion.V4);
            this.SetDefaultAcceptHeader(request, options);
            SetContentTypeHeaderForStream(request, streamDescriptorData);

            string hintString = @"Stream update\r\n{{\r\n  Descriptor = {0}\r\n\r\n}}";
            request.DebugHintString = string.Format(CultureInfo.InvariantCulture, hintString, streamDescriptorData);

            return request;
        }
        private ExpectedClientRequest CreateStreamInsertRequest(DataServiceContextData contextData, StreamDescriptorData streamDescriptorData, SaveChangesOptions options)
        {
            ExceptionUtilities.Assert(streamDescriptorData.Name == null, "Can only be used for media-resources");

            var insertUri = GetEntityInsertUri(contextData, streamDescriptorData.EntityDescriptor);

            var request = this.CreateSaveStreamRequest(streamDescriptorData, insertUri);
            
            string preference = contextData.AddAndUpdateResponsePreference.ToHeaderValue();
            var dsv = GetDataServiceVersion(request.Verb, preference);

            request.Headers[HttpHeaders.DataServiceVersion] = ToClientHeaderFormat(dsv);
            request.Headers[HttpHeaders.IfMatch] = null;
            request.Headers[HttpHeaders.Prefer] = preference;

            this.SetDefaultAcceptHeader(request, options);
            SetContentTypeHeaderForStream(request, streamDescriptorData);

            string hintString = @"Stream insert\r\n{{\r\n  Descriptor = {0}\r\n\r\n}}";
            request.DebugHintString = string.Format(CultureInfo.InvariantCulture, hintString, streamDescriptorData);

            return request;
        }
        private static void SetContentTypeHeaderForStream(ExpectedClientRequest request, StreamDescriptorData streamDescriptorData)
        {
            request.Headers[HttpHeaders.ContentType] = streamDescriptorData.ContentType;
            if (streamDescriptorData.ContentType == null)
            {
                request.Headers[HttpHeaders.ContentType] = MimeTypes.Any;
            }

            foreach (var header in streamDescriptorData.SaveStream.Headers)
            {
                request.Headers[header.Key] = header.Value;
            }

            request.Headers[HttpHeaders.Accept] = DefaultAccept;
        }
 private void InitializeDefaultStreamDescriptor()
 {
     if (this.defaultStreamDescriptor == null)
     {
         this.defaultStreamDescriptor = new StreamDescriptorData(this);
         this.defaultStreamDescriptor.State = EntityStates.Unchanged;
     }
 }
        /// <summary>
        /// Creates a new stream descriptor data and adds it to this entity descriptor data
        /// </summary>
        /// <param name="state">The state of the stream descriptor data</param>
        /// <param name="changeOrder">The change order for the stream descriptor data</param>
        /// <param name="name">The name of the stream</param>
        /// <returns>The stream descriptor data</returns>
        public StreamDescriptorData CreateStreamDescriptorData(EntityStates state, long changeOrder, string name)
        {
            ExceptionUtilities.CheckArgumentNotNull(name, "name");
            ExceptionUtilities.Assert(!this.streamDescriptors.Any(s => s.Name == name), "Stream descriptor data with name '{0}' already exists on entity descriptor data: {1}", name, this);

            var streamData = new StreamDescriptorData(this, name);
            this.contextData.ChangeStateAndChangeOrder(streamData, state, changeOrder);
            
            this.streamDescriptors.Add(streamData);

            return streamData;
        }
 private void VerifyStreamClosed(StreamDescriptorData streamDescriptor)
 {
     var saveStream = streamDescriptor.SaveStream;
     if (saveStream != null)
     {
         this.parent.Assert.IsTrue(saveStream.StreamLogger.IsEndOfStream, string.Format(CultureInfo.InvariantCulture, "Stream was not read to the end: {0}", streamDescriptor));
         this.parent.Assert.AreEqual(
             saveStream.CloseStream, 
             saveStream.StreamLogger.IsClosed, 
             string.Format(CultureInfo.InvariantCulture, "Save stream should/should not have been closed: {0}", streamDescriptor));
         streamDescriptor.SaveStream = null;
     }
 }
        /// <summary>
        /// Clones the current stream descriptor data
        /// </summary>
        /// <param name="clonedEntityDescriptor">The entity descriptor that will contain the result of this clone</param>
        /// <returns>A clone of the current stream descriptor data</returns>
        public StreamDescriptorData Clone(EntityDescriptorData clonedEntityDescriptor)
        {
            var clone = new StreamDescriptorData(clonedEntityDescriptor, this.Name);
            clone.State = this.State;
            clone.ChangeOrder = this.ChangeOrder;
            
            clone.ContentType = this.ContentType;
            clone.ETag = this.ETag;
            
            if (this.EditLink != null)
            {
                clone.EditLink = new Uri(this.EditLink.OriginalString, UriKind.RelativeOrAbsolute);
            }

            if (this.SelfLink != null)
            {
                clone.SelfLink = new Uri(this.SelfLink.OriginalString, UriKind.RelativeOrAbsolute);
            }

            if (this.SaveStream != null)
            {
                clone.SaveStream = this.SaveStream.Clone();
            }

            return clone;
        }