Ejemplo n.º 1
0
 public void ParseThenBind()
 {
     var            serviceBaseUri = new Uri("http://server/service/");
     var            queryUri       = new Uri(serviceBaseUri, "Customers(1)");
     var            syntacticTree  = SyntacticTree.ParseUri(queryUri, serviceBaseUri);
     var            model          = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetServiceModel();
     MetadataBinder binder         = new MetadataBinder(model);
     var            semanticTree   = binder.BindQuery(syntacticTree);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses the specified query into a query descriptor.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns>The parsed query.</returns>
        internal static SyntacticTree ParseQuery(string query)
        {
            Uri baseUri = new Uri("http://odata.org/test/");

            if (query.StartsWith("/"))
            {
                query = query.Substring(1);
            }

            return(SyntacticTree.ParseUri(new Uri(baseUri, query), baseUri));
        }
Ejemplo n.º 3
0
        public static string GetOperationName(Uri requestUri, Uri baseAddress)
        {
            try
            {
                // remove the query part as they are irrelevant here and we dont want to fail parsing them.
                Uri               requestUriWithoutQuerypart = new Uri(requestUri.GetLeftPart(UriPartial.Path));
                SyntacticTree     syntacticTree = SyntacticTree.ParseUri(requestUriWithoutQuerypart, baseAddress);
                SegmentQueryToken lastSegment   = syntacticTree.Path as SegmentQueryToken;
                if (lastSegment != null && !String.IsNullOrEmpty(lastSegment.Name))
                {
                    return(lastSegment.Name);
                }
            }
            catch (Exception)
            {
            }

            return(null);
        }
Ejemplo n.º 4
0
        public void UriCheckTests()
        {
            this.Assert.ExpectedException <ODataException>(
                () => SyntacticTree.ParseUri(new Uri("http://www.odata.org/base1/foo"), new Uri("http://www.odata.org/base2/")),
                "The URI 'http://www.odata.org/base1/foo' is not valid because it is not based on 'http://www.odata.org/base2/'.",
                "The operation should fail if the request URI is not relative to the specified base.");

            // Starting with .Net 4.5, Uri.UnescapeDataString does not throw an exception on invalid sequence. It just does not unescape something
            // that it does not understand. Hence disabling these tests.
            //this.Assert.ExpectedException<ODataException>(
            //    () => SyntacticTree.ParseUri(new Uri("http://www.odata.org/base1/foo%ZZ%%/", true), new Uri("http://www.odata.org/base1/")),
            //    "Bad Request: there was an error in the query syntax.",
            //    "Wrong escape sequence should cause ODataException.");

            // Test the what will be a default max depth which should be 800
            string longUri = "http://www.odata.org/";

            for (int i = 0; i < 801; i++)
            {
                longUri += "seg/";
            }

            this.Assert.ExpectedException <ODataException>(
                () => SyntacticTree.ParseUri(new Uri(longUri), new Uri("http://www.odata.org/")),
                "Too many segments in URI.",
                "Recursion limit should reject long URI.");

            // Test the explicit max depth
            // Test short max depth
            longUri = "http://www.odata.org/";
            for (int i = 0; i < 11; i++)
            {
                longUri += "seg/";
            }

            this.Assert.ExpectedException <ODataException>(
                () => SyntacticTree.ParseUri(new Uri(longUri), new Uri("http://www.odata.org/"), 10),
                "Too many segments in URI.",
                "Recursion limit should reject long URI.");
        }
Ejemplo n.º 5
0
        private bool TryReadSearchFilter(HttpRequestBase request, out SearchFilter searchFilter)
        {
            var odataQuery = SyntacticTree.ParseUri(new Uri(SiteRoot + request.RawUrl), new Uri(SiteRoot));

            var keywordPath = odataQuery.Path as KeywordSegmentQueryToken;

            searchFilter = new SearchFilter
            {
                // HACK: The way the default paging works is WCF attempts to read up to the MaxPageSize elements. If it finds as many, it'll assume there
                // are more elements to be paged and generate a continuation link. Consequently we'll always ask to pull MaxPageSize elements so WCF generates the
                // link for us and then allow it to do a Take on the results. The alternative to do is roll our IDataServicePagingProvider, but we run into
                // issues since we need to manage state over concurrent requests. This seems like an easier solution.
                Take          = MaxPageSize,
                Skip          = odataQuery.Skip ?? 0,
                CountOnly     = keywordPath != null && keywordPath.Keyword == KeywordKind.Count,
                SortDirection = SortDirection.Ascending
            };

            var filterProperty = odataQuery.Filter as PropertyAccessQueryToken;

            if (filterProperty == null ||
                !(filterProperty.Name.Equals("IsLatestVersion", StringComparison.Ordinal) ||
                  filterProperty.Name.Equals("IsAbsoluteLatestVersion", StringComparison.Ordinal)))
            {
                // We'll only use the index if we the query searches for latest \ latest-stable packages
                return(false);
            }

            var orderBy = odataQuery.OrderByTokens.FirstOrDefault();

            if (orderBy == null || orderBy.Expression == null)
            {
                searchFilter.SortProperty = SortProperty.Relevance;
            }
            else if (orderBy.Expression.Kind == QueryTokenKind.PropertyAccess)
            {
                var propertyAccess = (PropertyAccessQueryToken)orderBy.Expression;
                if (propertyAccess.Name.Equals("DownloadCount", StringComparison.Ordinal))
                {
                    searchFilter.SortProperty = SortProperty.DownloadCount;
                }
                else if (propertyAccess.Name.Equals("Published", StringComparison.Ordinal))
                {
                    searchFilter.SortProperty = SortProperty.Recent;
                }
                else if (propertyAccess.Name.Equals("Id", StringComparison.Ordinal))
                {
                    searchFilter.SortProperty = SortProperty.DisplayName;
                }
                else
                {
                    Debug.WriteLine("Order by clause {0} is unsupported", propertyAccess.Name);
                    return(false);
                }
            }
            else if (orderBy.Expression.Kind == QueryTokenKind.FunctionCall)
            {
                var functionCall = (FunctionCallQueryToken)orderBy.Expression;
                if (functionCall.Name.Equals("concat", StringComparison.OrdinalIgnoreCase))
                {
                    // We'll assume this is concat(Title, Id)
                    searchFilter.SortProperty  = SortProperty.DisplayName;
                    searchFilter.SortDirection = orderBy.Direction == OrderByDirection.Descending ? SortDirection.Descending : SortDirection.Ascending;
                }
                else
                {
                    Debug.WriteLine("Order by clause {0} is unsupported", functionCall.Name);
                    return(false);
                }
            }
            else
            {
                Debug.WriteLine("Order by clause {0} is unsupported", orderBy.Expression.Kind);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 6
0
 public void CanBuildSyntacticTree()
 {
     var serviceBaseUri = new Uri("http://server/service/");
     var queryUri       = new Uri(serviceBaseUri, "Customers(1)");
     var syntacticTree  = SyntacticTree.ParseUri(queryUri, serviceBaseUri);
 }