/// <summary>
        /// Get a new URI string by adding <paramref name="nextSegment"/> to the original one.
        /// </summary>
        /// <param name="nextSegment">Name of the action.</param>
        /// <returns>The new URI string.</returns>
        public virtual string AppendRequestUri(string nextSegment)
        {
            Uri requestUri = this.RequestUri;

            return(UriUtil.UriToString(requestUri).Replace(requestUri.AbsolutePath, requestUri.AbsolutePath + UriHelper.FORWARDSLASH + nextSegment));
        }
示例#2
0
 /// <summary>
 /// Get a new URI string by adding <paramref name="nextSegment"/> to the original one.
 /// </summary>
 /// <param name="nextSegment">Name of the action.</param>
 /// <returns>The new URI string.</returns>
 public string AppendRequestUri(string nextSegment)
 {
     return(UriUtil.UriToString(this.RequestUri).Replace(this.RequestUri.AbsolutePath, this.RequestUri.AbsolutePath + UriHelper.FORWARDSLASH + nextSegment));
 }
示例#3
0
        /// <summary>
        /// Get a new URI path string by adding <paramref name="nextSegment"/> to the original one.
        /// </summary>
        /// <param name="nextSegment">The next segment to add to path.</param>
        /// <returns>The new URI path string.</returns>
        public string GetPath(string nextSegment)
        {
            string resourcePath = UriUtil.UriToString(this.RequestUri).Substring(UriUtil.UriToString(this.Context.BaseUri).Length);

            return(nextSegment == null ? resourcePath : resourcePath + UriHelper.FORWARDSLASH + nextSegment);
        }
示例#4
0
        /// <summary>
        /// Enumerates through the list of URI operation parameters and creates a new Uri with the uri operation parameters written as query string of the new Uri.
        /// </summary>
        /// <param name="requestUri">The Uri used to construct the new Uri.</param>
        /// <param name="operationParameters">The non-empty list of uri parameters which will be converted to query string.</param>
        /// <returns>Uri containing the uri parameters as query string.</returns>
        internal Uri WriteUriOperationParametersToUri(Uri requestUri, List <UriOperationParameter> operationParameters)
        {
            Debug.Assert(operationParameters != null && operationParameters.Any(), "OperationParameters was null or empty");
            Debug.Assert(requestUri != null, "request_uri != null");

            UriBuilder    uriBuilder  = new UriBuilder(requestUri);
            StringBuilder pathBuilder = new StringBuilder();

            pathBuilder.Append(uriBuilder.Path);
            string lastSeg = uriBuilder.Path.Substring(uriBuilder.Path.LastIndexOf('/') + 1);

            StringBuilder queryBuilder = new StringBuilder();
            String        uriString    = UriUtil.UriToString(uriBuilder.Uri);

            if (!string.IsNullOrEmpty(uriBuilder.Query))
            {
                Debug.Assert(uriBuilder.Query[0] == UriHelper.QUESTIONMARK, "uriBuilder.Query[0] == UriHelper.QUESTIONMARK");

                // Don't append the '?', as later when we call setter on the Query, the '?' will be automatically added.
                queryBuilder.Append(uriBuilder.Query.Substring(1));
                queryBuilder.Append(UriHelper.AMPERSAND);
            }

            if (!lastSeg.Contains(Char.ToString(UriHelper.ATSIGN)))
            {
                pathBuilder.Append(UriHelper.LEFTPAREN);
            }
            else
            {
                if (pathBuilder.ToString().EndsWith(Char.ToString(UriHelper.RIGHTPAREN), StringComparison.OrdinalIgnoreCase))
                {
                    pathBuilder.Remove(pathBuilder.Length - 1, 1);
                    pathBuilder.Append(UriHelper.COMMA);
                }
            }

            foreach (UriOperationParameter op in operationParameters)
            {
                Debug.Assert(op != null, "op != null");
                Debug.Assert(!string.IsNullOrEmpty(op.Name), "!string.IsNullOrEmpty(op.ParameterName)");

                string paramName = op.Name.Trim();

                // if the parameter name is an alias, make sure that the URI contains it.
                if (paramName.StartsWith(Char.ToString(UriHelper.ATSIGN), StringComparison.OrdinalIgnoreCase) && !uriString.Contains(paramName))
                {
                    throw new DataServiceRequestException(Strings.Serializer_UriDoesNotContainParameterAlias(op.Name));
                }

                if (paramName.StartsWith(Char.ToString(UriHelper.ATSIGN), StringComparison.OrdinalIgnoreCase))
                {
                    // name=value&
                    queryBuilder.Append(paramName);
                    queryBuilder.Append(UriHelper.EQUALSSIGN);
                    queryBuilder.Append(this.ConvertToEscapedUriValue(paramName, op.Value));
                    queryBuilder.Append(UriHelper.AMPERSAND);
                }

                string value = this.ConvertToEscapedUriValue(paramName, op.Value);

                // non-primitive value, use alias.
                if (!UriHelper.IsPrimitiveValue(value))
                {
                    // name = @name
                    pathBuilder.Append(paramName);
                    pathBuilder.Append(UriHelper.EQUALSSIGN);
                    pathBuilder.Append(UriHelper.ENCODEDATSIGN);
                    pathBuilder.Append(paramName);
                    pathBuilder.Append(UriHelper.COMMA);

                    // @name = value&
                    queryBuilder.Append(UriHelper.ENCODEDATSIGN);
                    queryBuilder.Append(paramName);
                    queryBuilder.Append(UriHelper.EQUALSSIGN);
                    queryBuilder.Append(value);
                    queryBuilder.Append(UriHelper.AMPERSAND);
                }
                else
                {
                    // primitive value, do not use alias.
                    pathBuilder.Append(paramName);
                    pathBuilder.Append(UriHelper.EQUALSSIGN);
                    pathBuilder.Append(value);
                    pathBuilder.Append(UriHelper.COMMA);
                }
            }

            // remove the last extra comma.
            if (pathBuilder.ToString().EndsWith(Char.ToString(UriHelper.COMMA), StringComparison.OrdinalIgnoreCase))
            {
                Debug.Assert(pathBuilder.ToString().EndsWith(Char.ToString(UriHelper.COMMA), StringComparison.OrdinalIgnoreCase), "Uri was expected to end with an ampersand.");
                pathBuilder.Remove(pathBuilder.Length - 1, 1);
            }

            pathBuilder.Append(UriHelper.RIGHTPAREN);

            // remove the last extra ampersand.
            if (queryBuilder.ToString().EndsWith(Char.ToString(UriHelper.AMPERSAND), StringComparison.OrdinalIgnoreCase))
            {
                Debug.Assert(queryBuilder.ToString().EndsWith(Char.ToString(UriHelper.AMPERSAND), StringComparison.OrdinalIgnoreCase), "Uri was expected to end with an ampersand.");
                queryBuilder.Remove(queryBuilder.Length - 1, 1);
            }

            uriBuilder.Path  = pathBuilder.ToString();
            uriBuilder.Query = queryBuilder.ToString();

            return(uriBuilder.Uri);
        }
示例#5
0
        /// <summary>
        /// Writes a navigation link.
        /// </summary>
        /// <param name="entityDescriptor">The entity</param>
        /// <param name="relatedLinks">The links related to the entity</param>
        /// <param name="odataWriter">The ODataWriter used to write the navigation link.</param>
        internal void WriteNestedResourceInfo(EntityDescriptor entityDescriptor, IEnumerable <LinkDescriptor> relatedLinks, ODataWriterWrapper odataWriter)
        {
            // TODO: create instance of odatawriter.
            // TODO: send clientType once, so that we don't need entity descriptor
            Debug.Assert(EntityStates.Added == entityDescriptor.State, "entity not added state");

            Dictionary <string, List <LinkDescriptor> > groupRelatedLinks = new Dictionary <string, List <LinkDescriptor> >(EqualityComparer <string> .Default);

            foreach (LinkDescriptor end in relatedLinks)
            {
                List <LinkDescriptor> linkDescriptorsList = null;
                if (!groupRelatedLinks.TryGetValue(end.SourceProperty, out linkDescriptorsList))
                {
                    linkDescriptorsList = new List <LinkDescriptor>();
                    groupRelatedLinks.Add(end.SourceProperty, linkDescriptorsList);
                }

                linkDescriptorsList.Add(end);
            }

            ClientTypeAnnotation clientType = null;

            foreach (var grlinks in groupRelatedLinks)
            {
                if (clientType == null)
                {
                    ClientEdmModel model = this.requestInfo.Model;
                    clientType = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType()));
                }

                bool isCollection = clientType.GetProperty(grlinks.Key, UndeclaredPropertyBehavior.ThrowException).IsEntityCollection;
                bool started      = false;

                foreach (LinkDescriptor end in grlinks.Value)
                {
                    Debug.Assert(!end.ContentGeneratedForSave, "already saved link");
                    end.ContentGeneratedForSave = true;
                    Debug.Assert(end.Target != null, "null is DELETE");

                    ODataNestedResourceInfo navigationLink = new ODataNestedResourceInfo();
                    navigationLink.Url = this.requestInfo.EntityTracker.GetEntityDescriptor(end.Target).GetLatestEditLink();
                    Debug.Assert(Uri.IsWellFormedUriString(UriUtil.UriToString(navigationLink.Url), UriKind.Absolute), "Uri.IsWellFormedUriString(targetEditLink, UriKind.Absolute)");

                    navigationLink.IsCollection = isCollection;
                    navigationLink.Name         = grlinks.Key;

                    if (!started)
                    {
                        odataWriter.WriteNestedResourceInfoStart(navigationLink);
                        started = true;
                    }

                    odataWriter.WriteNestedResourceInfoStart(navigationLink, end.Source, end.Target);
                    odataWriter.WriteEntityReferenceLink(new ODataEntityReferenceLink()
                    {
                        Url = navigationLink.Url
                    }, end.Source, end.Target);
                    odataWriter.WriteNestedResourceInfoEnd(navigationLink, end.Source, end.Target);
                }

                odataWriter.WriteNestedResourceInfoEnd();
            }
        }
示例#6
0
 /// <summary>
 /// Convert an instance of primitive type to string
 /// </summary>
 /// <param name="instance">The instance</param>
 /// <returns>The string representation of the instance</returns>
 internal override string ToString(object instance)
 {
     return(UriUtil.UriToString((Uri)instance));
 }