Beispiel #1
0
 internal static void CheckSyntaxValid(bool valid)
 {
     if (!valid)
     {
         throw DataServiceException.CreateSyntaxError();
     }
 }
Beispiel #2
0
 internal static void CheckResourceNotCollectionForOpenProperty(ResourceType resourceType, string propertyName)
 {
     if ((resourceType.ResourceTypeKind == ResourceTypeKind.Collection) || (resourceType.ResourceTypeKind == ResourceTypeKind.EntityCollection))
     {
         throw DataServiceException.CreateSyntaxError(System.Data.Services.Strings.InvalidUri_OpenPropertiesCannotBeCollection(propertyName));
     }
 }
Beispiel #3
0
        string IDataServiceHost.GetQueryStringItem(string item)
        {
            WebUtil.CheckArgumentNull <string>(item, "item");
            NameValueCollection queryParameters = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;

            string[] values = queryParameters.GetValues(item);
            if ((values == null) || (values.Length == 0))
            {
                string str = null;
                foreach (string str2 in queryParameters.Keys)
                {
                    if ((str2 != null) && StringComparer.OrdinalIgnoreCase.Equals(str2.Trim(), item))
                    {
                        if (str != null)
                        {
                            throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_AmbiguousItemName(item, str, str2));
                        }
                        str    = str2;
                        values = queryParameters.GetValues(str2);
                    }
                }
                if ((values == null) || (values.Length == 0))
                {
                    return(null);
                }
            }
            if (values.Length != 1)
            {
                throw DataServiceException.CreateSyntaxError();
            }
            return(values[0]);
        }
Beispiel #4
0
        private bool ReadSkipOrTopArgument(string queryItem, out int count)
        {
            string queryStringItem = this.service.OperationContext.Host.GetQueryStringItem(queryItem);

            if (string.IsNullOrEmpty(queryStringItem))
            {
                count = 0;
                return(false);
            }
            if (!int.TryParse(queryStringItem, NumberStyles.Integer, CultureInfo.InvariantCulture, out count))
            {
                throw DataServiceException.CreateSyntaxError(System.Data.Services.Strings.RequestQueryProcessor_IncorrectArgumentFormat(queryItem, queryStringItem));
            }
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Checks query result.
        /// </summary>
        /// <param name="result">Query result to be checked.</param>
        /// <param name="segmentInfo">Segment details for the <paramref name="result"/>.</param>
        internal static void CheckQueryResult(object result, SegmentInfo segmentInfo)
        {
            // e.g. /Customers(4) - if there is a direct reference to an entity, it should not be null.
            // e.g. $value also must not be null, since you are dereferencing the values
            // Any other case, having null is fine
            if (segmentInfo.IsDirectReference && result == null)
            {
                throw DataServiceException.CreateResourceNotFound(segmentInfo.Identifier);
            }

            IEnumerable enumerable;

            if (segmentInfo.TargetKind == RequestTargetKind.OpenProperty &&
                WebUtil.IsElementIEnumerable(result, out enumerable))
            {
                throw DataServiceException.CreateSyntaxError(
                          Strings.InvalidUri_OpenPropertiesCannotBeCollection(segmentInfo.Identifier));
            }
        }
Beispiel #6
0
        /// <summary>Gets the value for the specified item in the request query string.</summary>
        /// <param name="item">Item to return.</param>
        /// <returns>
        /// The value for the specified item in the request query string;
        /// null if <paramref name="item"/> is not found.
        /// </returns>
        string IDataServiceHost.GetQueryStringItem(string item)
        {
            Debug.Assert(item != null, "item != null");
            Debug.Assert(item.Trim() == item, "item.Trim() == item - otherwise, there are leading/trailing spaces in the name");

            System.Collections.Specialized.NameValueCollection collection = this.operationContext.IncomingRequest.UriTemplateMatch.QueryParameters;
            string[] values = collection.GetValues(item);
            if (values == null || values.Length == 0)
            {
                // Do a scan of arguments ignoring whitespace (SQLBUDT #555944).
                string keyFound = null;
                foreach (string key in collection.Keys)
                {
                    if (key != null && StringComparer.OrdinalIgnoreCase.Equals(key.Trim(), item))
                    {
                        if (keyFound != null)
                        {
                            throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_AmbiguousItemName(item, keyFound, key));
                        }

                        keyFound = key;
                        values   = collection.GetValues(key);
                    }
                }

                if (values == null || values.Length == 0)
                {
                    return(null);
                }
            }

            Debug.Assert(values != null && values.Length > 0, "values != null && values.Length > 0 - otherwise we should have returned already");
            if (values.Length == 1)
            {
                return(values[0]);
            }
            else
            {
                throw DataServiceException.CreateSyntaxError();
            }
        }
Beispiel #7
0
 private void ApplyProjectionsToExpandTree(List <List <string> > selectPathsAsText)
 {
     for (int i = selectPathsAsText.Count - 1; i >= 0; i--)
     {
         List <string>          list = selectPathsAsText[i];
         ExpandedProjectionNode rootProjectionNode = this.GetRootProjectionNode();
         ResourceType           type = null;
         for (int j = 0; j < list.Count; j++)
         {
             bool   flag2;
             string containerQualifiedName = list[j];
             bool   lastPathSegment        = j == (list.Count - 1);
             rootProjectionNode.ProjectionFound = true;
             if (containerQualifiedName == "*")
             {
                 rootProjectionNode.ProjectAllImmediateProperties = true;
                 break;
             }
             if (this.service.Provider.GetNameFromContainerQualifiedName(containerQualifiedName, out flag2) == "*")
             {
                 rootProjectionNode.ProjectAllImmediateOperations = true;
                 break;
             }
             ResourceType     previousSegmentResourceType = type ?? rootProjectionNode.ResourceType;
             ResourceProperty property = previousSegmentResourceType.TryResolvePropertyName(containerQualifiedName);
             if (property == null)
             {
                 type = WebUtil.ResolveTypeIdentifier(this.service.Provider, containerQualifiedName, previousSegmentResourceType, type != null);
                 if (type != null)
                 {
                     this.description.VerifyProtocolVersion(RequestDescription.Version3Dot0, this.service);
                     continue;
                 }
                 Func <OperationWrapper, bool> predicate = null;
                 string           serviceActionName      = this.service.Provider.GetNameFromContainerQualifiedName(containerQualifiedName, out flag2);
                 OperationWrapper operation = null;
                 if (!previousSegmentResourceType.IsOpenType || flag2)
                 {
                     if (predicate == null)
                     {
                         predicate = o => o.Name == serviceActionName;
                     }
                     operation = this.service.ActionProvider.GetServiceActionsByBindingParameterType(this.service.OperationContext, previousSegmentResourceType).SingleOrDefault <OperationWrapper>(predicate);
                 }
                 if (operation != null)
                 {
                     rootProjectionNode.AddOperation(operation);
                     if (!lastPathSegment)
                     {
                         throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.RequestQueryProcessor_ServiceActionMustBeLastSegmentInSelect(containerQualifiedName));
                     }
                     continue;
                 }
                 if (!previousSegmentResourceType.IsOpenType)
                 {
                     throw DataServiceException.CreateSyntaxError(System.Data.Services.Strings.RequestUriProcessor_PropertyNotFound(previousSegmentResourceType.FullName, containerQualifiedName));
                 }
                 if (!lastPathSegment)
                 {
                     throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.OpenNavigationPropertiesNotSupportedOnOpenTypes(containerQualifiedName));
                 }
             }
             rootProjectionNode = ApplyProjectionForProperty(rootProjectionNode, containerQualifiedName, property, previousSegmentResourceType, lastPathSegment);
             type = null;
         }
         if (type != null)
         {
             throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.RequestQueryProcessor_QueryParametersPathCannotEndInTypeIdentifier("$select", type.FullName));
         }
     }
 }
Beispiel #8
0
        private ExpandSegmentCollection CheckSingleExpandPath(List <string> path)
        {
            ResourceType            targetResourceType = this.description.TargetResourceType;
            ResourceSetWrapper      targetContainer    = this.description.LastSegmentInfo.TargetContainer;
            ExpandSegmentCollection segments           = new ExpandSegmentCollection(path.Count);
            bool flag = false;
            bool previousSegmentIsTypeSegment = false;

            for (int i = 0; i < path.Count; i++)
            {
                string propertyName                     = path[i];
                ResourcePropertyKind stream             = ResourcePropertyKind.Stream;
                ResourceProperty     navigationProperty = targetResourceType.TryResolvePropertyName(propertyName, stream);
                if (navigationProperty == null)
                {
                    ResourceType type2 = WebUtil.ResolveTypeIdentifier(this.service.Provider, propertyName, targetResourceType, previousSegmentIsTypeSegment);
                    if (type2 == null)
                    {
                        if (targetResourceType.IsOpenType)
                        {
                            throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.OpenNavigationPropertiesNotSupportedOnOpenTypes(propertyName));
                        }
                        throw DataServiceException.CreateSyntaxError(System.Data.Services.Strings.RequestUriProcessor_PropertyNotFound(targetResourceType.FullName, propertyName));
                    }
                    this.description.VerifyProtocolVersion(RequestDescription.Version3Dot0, this.service);
                    targetResourceType           = type2;
                    previousSegmentIsTypeSegment = true;
                }
                else
                {
                    previousSegmentIsTypeSegment = false;
                    if (navigationProperty.TypeKind == ResourceTypeKind.EntityType)
                    {
                        targetContainer = this.service.Provider.GetContainer(targetContainer, targetResourceType, navigationProperty);
                        if (targetContainer == null)
                        {
                            throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.BadRequest_InvalidPropertyNameSpecified(navigationProperty.Name, targetResourceType.FullName));
                        }
                        bool singleResult = navigationProperty.Kind == ResourcePropertyKind.ResourceReference;
                        DataServiceConfiguration.CheckResourceRightsForRead(targetContainer, singleResult);
                        Expression filter = DataServiceConfiguration.ComposeQueryInterceptors(this.service, targetContainer);
                        if (((targetContainer.PageSize != 0) && !singleResult) && !this.IsCustomPaged)
                        {
                            OrderingInfo        orderingInfo = new OrderingInfo(true);
                            ParameterExpression expression   = Expression.Parameter(targetContainer.ResourceType.InstanceType, "p");
                            foreach (ResourceProperty property2 in targetContainer.GetKeyPropertiesForOrderBy())
                            {
                                Expression expression3;
                                if (property2.CanReflectOnInstanceTypeProperty)
                                {
                                    expression3 = Expression.Property(expression, targetContainer.ResourceType.GetPropertyInfo(property2));
                                }
                                else
                                {
                                    expression3 = Expression.Convert(Expression.Call(null, DataServiceProviderMethods.GetValueMethodInfo, expression, Expression.Constant(property2)), property2.Type);
                                }
                                orderingInfo.Add(new OrderingExpression(Expression.Lambda(expression3, new ParameterExpression[] { expression }), true));
                            }
                            segments.Add(new ExpandSegment(navigationProperty.Name, filter, targetContainer.PageSize, targetContainer, targetResourceType, navigationProperty, orderingInfo));
                            this.description.VerifyProtocolVersion(RequestDescription.Version2Dot0, this.service);
                            this.description.VerifyAndRaiseResponseVersion(RequestDescription.Version2Dot0, this.service);
                        }
                        else
                        {
                            if (!singleResult && this.IsCustomPaged)
                            {
                                this.CheckAndApplyCustomPaging(null);
                            }
                            segments.Add(new ExpandSegment(navigationProperty.Name, filter, this.service.Configuration.MaxResultsPerCollection, targetContainer, targetResourceType, navigationProperty, null));
                        }
                        this.description.UpdateAndCheckEpmFeatureVersion(targetContainer, this.service);
                        this.description.UpdateVersions(this.service.OperationContext.Host.RequestAccept, targetContainer, this.service);
                        flag = false;
                        targetResourceType = navigationProperty.ResourceType;
                    }
                    else
                    {
                        flag = true;
                    }
                }
            }
            if (previousSegmentIsTypeSegment)
            {
                throw DataServiceException.CreateBadRequestError(System.Data.Services.Strings.RequestQueryProcessor_QueryParametersPathCannotEndInTypeIdentifier("$expand", targetResourceType.FullName));
            }
            if (!flag)
            {
                return(segments);
            }
            return(null);
        }