/// <summary> /// If the provider implements IConcurrencyProvider, then this method passes the etag values /// to the provider, otherwise compares the etag itself. /// </summary> /// <param name="resourceCookie">etag values for the given resource.</param> /// <param name="container">container for the given resource.</param> internal void SetETagValues(object resourceCookie, ResourceSetWrapper container) { Debug.Assert(resourceCookie != null, "resourceCookie != null"); Debug.Assert(container != null, "container != null"); AstoriaRequestMessage host = this.service.OperationContext.RequestMessage; Debug.Assert(String.IsNullOrEmpty(host.GetRequestIfNoneMatchHeader()), "IfNoneMatch header cannot be specified for Update/Delete operations"); // Resolve the cookie first to the actual resource type object actualEntity = this.ResolveResource(resourceCookie); Debug.Assert(actualEntity != null, "actualEntity != null"); ResourceType resourceType = WebUtil.GetNonPrimitiveResourceType(this.service.Provider, actualEntity); Debug.Assert(resourceType != null, "resourceType != null"); IList <ResourceProperty> etagProperties = this.service.Provider.GetETagProperties(container.Name, resourceType); if (etagProperties.Count == 0) { if (!String.IsNullOrEmpty(host.GetRequestIfMatchHeader())) { throw DataServiceException.CreateBadRequestError(Strings.Serializer_NoETagPropertiesForType); } // If the type has no etag properties, then we do not need to do any etag checks return; } // If the provider implements IConcurrencyProvider, then we need to call the provider // and pass the etag values. Else, we need to compare the etag values ourselves. IDataServiceUpdateProvider concurrencyProvider = this.updateProvider as IDataServiceUpdateProvider; if (concurrencyProvider != null) { bool?checkForEquality = null; IEnumerable <KeyValuePair <string, object> > etagValues; if (!String.IsNullOrEmpty(host.GetRequestIfMatchHeader())) { checkForEquality = true; etagValues = ParseETagValue(etagProperties, host.GetRequestIfMatchHeader()); } else { etagValues = WebUtil.EmptyKeyValuePairStringObject; } concurrencyProvider.SetConcurrencyValues(resourceCookie, checkForEquality, etagValues); } else if (String.IsNullOrEmpty(host.GetRequestIfMatchHeader())) { throw DataServiceException.CreateBadRequestError(Strings.DataService_CannotPerformOperationWithoutETag(resourceType.FullName)); } else if (host.GetRequestIfMatchHeader() != XmlConstants.HttpAnyETag) { // Compare If-Match header value with the current etag value, if the If-Match header value is not equal to '*' string etagValue = WebUtil.GetETagValue(resourceCookie, resourceType, etagProperties, this.service, false /*getMethod*/); Debug.Assert(!String.IsNullOrEmpty(etagValue), "etag value can never be null"); if (etagValue != host.GetRequestIfMatchHeader()) { throw DataServiceException.CreatePreConditionFailedError(Strings.Serializer_ETagValueDoesNotMatch); } } }
/// <summary> /// update the request version header, if it is not specified. /// </summary> /// <param name="maxProtocolVersion">protocol version as specified in the config.</param> internal void InitializeRequestVersionHeaders(Version maxProtocolVersion) { if (this.requestVersionHeadersInitialized) { return; } this.requestVersionHeadersInitialized = true; Debug.Assert(this.requestVersion == null, "this.requestVersion == null"); Debug.Assert(this.RequestMaxVersion == null, "this.RequestMaxVersion == null"); Version maxRequestVersionAllowed = GetMaxRequestVersionAllowed(maxProtocolVersion); // read the request version headers from the underlying host this.requestVersionString = this.host.RequestVersion; this.RequestVersion = ValidateVersionHeader(XmlConstants.HttpODataVersion, this.requestVersionString); this.RequestMaxVersion = ValidateVersionHeader(XmlConstants.HttpODataMaxVersion, this.host.RequestMaxVersion); // If the request version is not specified. if (this.requestVersion == null) { // In request headers, if the OData-Version header is not specified and OData-MaxVersion is specified, // we should ideally set the OData-Version header to whatever the value was specified in OData-MaxVersion // header has. if (this.RequestMaxVersion != null) { // set the OData-Version to minimum of OData-MaxVersion and ProtocolVersion. this.RequestVersion = (this.RequestMaxVersion < maxProtocolVersion) ? this.RequestMaxVersion : maxProtocolVersion; } else { // If both request DSV and request MaxDSV is not specified, then set the request DSV to the MPV this.RequestVersion = maxProtocolVersion; } this.requestVersionString = this.RequestVersion.ToString(2); } else { if (this.RequestVersion > maxRequestVersionAllowed) { throw DataServiceException.CreateBadRequestError(Strings.DataService_RequestVersionMustBeLessThanMPV(this.RequestVersion, maxProtocolVersion)); } // Verify that the request DSV is a known version number. if (!VersionUtil.IsKnownRequestVersion(this.RequestVersion)) { string message = Strings.DataService_InvalidRequestVersion( this.RequestVersion.ToString(2), KnownODataVersionsToString(maxRequestVersionAllowed)); throw DataServiceException.CreateBadRequestError(message); } } // Initialize the request OData-MaxVersion if not specified. if (this.RequestMaxVersion == null) { this.RequestMaxVersion = maxProtocolVersion; } else if (this.RequestMaxVersion < VersionUtil.DataServiceDefaultResponseVersion) { // We need to make sure the MaxDSV is at least 1.0. This was the V1 behavior. // Verified that this was checked both in batch and non-batch cases. string message = Strings.DataService_MaxDSVTooLow( this.RequestMaxVersion.ToString(2), VersionUtil.DataServiceDefaultResponseVersion.Major, VersionUtil.DataServiceDefaultResponseVersion.Minor); throw DataServiceException.CreateBadRequestError(message); } }
/// <summary>Creates a new exception to indicate a syntax error.</summary> /// <param name="message">Plain text error message for this exception.</param> /// <returns>A new exception to indicate a syntax error.</returns> internal static DataServiceException CreateSyntaxError(string message) { return(DataServiceException.CreateBadRequestError(message)); }
/// <summary>Creates a new "Bad Request" exception for recursion limit exceeded.</summary> /// <returns>A new exception to indicate that the request is rejected.</returns> internal static DataServiceException CreateDeepRecursion_General() { return(DataServiceException.CreateBadRequestError(Strings.BadRequest_DeepRecursion_General)); }
/// <summary>Creates a new "Bad Request" exception for recursion limit exceeded.</summary> /// <param name="recursionLimit">Recursion limit that was reaced.</param> /// <returns>A new exception to indicate that the request is rejected.</returns> internal static DataServiceException CreateDeepRecursion(int recursionLimit) { return(DataServiceException.CreateBadRequestError(Strings.BadRequest_DeepRecursion(recursionLimit))); }
/// <summary>Creates an <see cref="SegmentInfo"/> list for the given <paramref name="path"/>.</summary> /// <param name="path">Segments to process.</param> /// <returns>Segment information describing the given <paramref name="path"/>.</returns> internal IList <SegmentInfo> ConvertPath(ODataPath path) { Debug.Assert(path != null, "path != null"); SegmentInfo previous = null; List <SegmentInfo> segmentInfos = new List <SegmentInfo>(); bool crossReferencingUri = false; foreach (ODataPathSegment segment in path) { crossReferencingUri |= segment is BatchReferenceSegment; SegmentInfo segmentInfo; if (previous == null) { segmentInfo = this.CreateFirstSegment(segment); } else { ThrowIfMustBeLeafSegment(previous); var keySegment = segment as KeySegment; if (keySegment != null) { // All service operations other than those returning IQueryable MUST NOT have a key expression. if (!IsSegmentComposable(previous)) { throw DataServiceException.CreateBadRequestError(Strings.RequestUriProcessor_SegmentDoesNotSupportKeyPredicates(previous.Identifier)); } CheckSegmentIsComposable(previous); if (crossReferencingUri) { throw DataServiceException.CreateBadRequestError(Strings.BadRequest_ResourceCanBeCrossReferencedOnlyForBindOperation); } previous.SingleResult = true; previous.Key = keySegment; continue; } if (segment is NavigationPropertyLinkSegment) { segmentInfo = CreateEntityReferenceLinkSegment(previous); #if DEBUG segmentInfo.AssertValid(); #endif segmentInfos.Add(segmentInfo); previous = segmentInfo; } segmentInfo = this.CreateNextSegment(previous, segment); } Debug.Assert(segmentInfo != null, "segment != null"); #if DEBUG segmentInfo.AssertValid(); #endif segmentInfos.Add(segmentInfo); // we need to copy the segment over (even if it was an escape marker) as decisions will be made about it the next time through the loop. previous = segmentInfo; } return(segmentInfos); }
/// <summary> /// Binds the paths from the request's $select query option to the sets/types/properties from the metadata provider of the service. /// </summary> /// <param name="requestDescription">The request description.</param> /// <param name="dataService">The data service.</param> /// <param name="selectQueryOption">The raw value of the $select query option.</param> /// <returns>The bound select segments.</returns> internal static IList <IList <SelectItem> > BindSelectSegments(RequestDescription requestDescription, IDataService dataService, string selectQueryOption) { Debug.Assert(requestDescription != null, "requestDescription != null"); Debug.Assert(dataService != null, "dataService != null"); if (string.IsNullOrEmpty(selectQueryOption)) { return(new List <IList <SelectItem> >()); } // Throw if $select requests have been disabled by the user Debug.Assert(dataService.Configuration != null, "dataService.Configuration != null"); if (!dataService.Configuration.DataServiceBehavior.AcceptProjectionRequests) { throw DataServiceException.CreateBadRequestError(Strings.DataServiceConfiguration_ProjectionsNotAccepted); } IList <IList <string> > selectPathsAsText = SplitSelect(selectQueryOption, dataService.Provider); Debug.Assert(selectPathsAsText != null, "selectPathsAsText != null"); List <IList <SelectItem> > boundSegments = new List <IList <SelectItem> >(selectPathsAsText.Count); if (selectPathsAsText.Count == 0) { return(boundSegments); } ValidateSelectIsAllowedForRequest(requestDescription); MetadataProviderEdmModel metadataProviderEdmModel = dataService.Provider.GetMetadataProviderEdmModel(); for (int i = selectPathsAsText.Count - 1; i >= 0; i--) { IList <string> path = selectPathsAsText[i]; List <SelectItem> boundSegmentPath = new List <SelectItem>(path.Count); boundSegments.Add(boundSegmentPath); ResourceType targetResourceType = requestDescription.TargetResourceType; ResourceSetWrapper targetResourceSet = requestDescription.TargetResourceSet; // if we get to here, we're building a partial selection List <TypeSegment> typeSegments = new List <TypeSegment>(); bool previousSegmentIsTypeSegment = false; for (int j = 0; j < path.Count; j++) { string pathSegment = path[j]; bool lastPathSegment = (j == path.Count - 1); // '*' is special, it means "Project all immediate properties on this level." if (pathSegment == "*") { Debug.Assert(lastPathSegment, "A wildcard select segment must be the last one. This should have been checked already when splitting appart the paths."); boundSegmentPath.Add(CreateWildcardSelection()); continue; } bool nameIsContainerQualified; string nameFromContainerQualifiedName = dataService.Provider.GetNameFromContainerQualifiedName(pathSegment, out nameIsContainerQualified); if (nameFromContainerQualifiedName == "*") { Debug.Assert(lastPathSegment, "A wildcard select segment must be the last one. This should have been checked already when splitting appart the paths."); Debug.Assert(nameIsContainerQualified, "nameIsContainerQualified == true"); boundSegmentPath.Add(CreateContainerQualifiedWildcardSelection(metadataProviderEdmModel)); continue; } ResourceProperty property = targetResourceType.TryResolvePropertyName(pathSegment); if (property == null) { ResourceType resolvedResourceType = WebUtil.ResolveTypeIdentifier(dataService.Provider, pathSegment, targetResourceType, previousSegmentIsTypeSegment); if (resolvedResourceType != null) { previousSegmentIsTypeSegment = true; if (lastPathSegment) { throw DataServiceException.CreateBadRequestError(Strings.RequestQueryProcessor_QueryParametersPathCannotEndInTypeIdentifier(XmlConstants.HttpQueryStringSelect, resolvedResourceType.FullName)); } targetResourceType = resolvedResourceType; // Whenever we encounter the type segment, we need to only verify that the MPV is set to 3.0 or higher. // There is no need to check for request DSV, request MinDSV or request MaxDSV since there are no protocol changes in // the payload for uri's with type identifier. requestDescription.VerifyProtocolVersion(VersionUtil.Version3Dot0, dataService); IEdmSchemaType edmType = metadataProviderEdmModel.EnsureSchemaType(targetResourceType); TypeSegment typeSegment = new TypeSegment(edmType); typeSegments.Add(typeSegment); continue; } previousSegmentIsTypeSegment = false; // If the currentResourceType is an open type, we require the service action name to be fully qualified or else we treat it as an open property name. if (!targetResourceType.IsOpenType || nameIsContainerQualified) { // Note that if the service does not implement IDataServiceActionProvider and the MaxProtocolVersion < V3, // GetActionsBoundToAnyTypeInResourceSet() would simply return an empty ServiceOperationWrapper collection. Debug.Assert(dataService.ActionProvider != null, "dataService.ActionProvider != null"); IEnumerable <OperationWrapper> allOperationsInSet = dataService.ActionProvider.GetActionsBoundToAnyTypeInResourceSet(targetResourceSet); List <OperationWrapper> selectedServiceActions = allOperationsInSet.Where(o => o.Name == nameFromContainerQualifiedName).ToList(); if (selectedServiceActions.Count > 0) { if (!lastPathSegment) { throw DataServiceException.CreateBadRequestError(Strings.RequestQueryProcessor_ServiceActionMustBeLastSegmentInSelect(pathSegment)); } boundSegmentPath.Add(CreateOperationSelection(metadataProviderEdmModel, selectedServiceActions, typeSegments)); continue; } } if (!targetResourceType.IsOpenType) { throw DataServiceException.CreateSyntaxError(Strings.RequestUriProcessor_PropertyNotFound(targetResourceType.FullName, pathSegment)); } if (!lastPathSegment) { // Open navigation properties are not supported on OpenTypes. throw DataServiceException.CreateBadRequestError(Strings.OpenNavigationPropertiesNotSupportedOnOpenTypes(pathSegment)); } boundSegmentPath.Add(CreateOpenPropertySelection(pathSegment, typeSegments)); } else { previousSegmentIsTypeSegment = false; ValidateSelectedProperty(targetResourceType, property, lastPathSegment); boundSegmentPath.Add(CreatePropertySelection(metadataProviderEdmModel, targetResourceType, property, typeSegments)); if (property.TypeKind == ResourceTypeKind.EntityType) { targetResourceSet = dataService.Provider.GetResourceSet(targetResourceSet, targetResourceType, property); targetResourceType = property.ResourceType; } } } // Note that this check is also covering cases where a type segment is followed by a wildcard. if (previousSegmentIsTypeSegment) { throw DataServiceException.CreateBadRequestError(Strings.RequestQueryProcessor_QueryParametersPathCannotEndInTypeIdentifier(XmlConstants.HttpQueryStringSelect, targetResourceType.FullName)); } } return(boundSegments); }