/// <summary> /// Sets the Alternate Key support for the Uri parser on the configuration. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="alternateKeys"><c>true</c> to enable Alternate Keys, <c>false</c> otherwise.</param> public static void EnableAlternateKeys(this HttpConfiguration configuration, bool alternateKeys) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } ODataUriResolverSetttings settings = configuration.GetResolverSettings(); settings.AlternateKeys = alternateKeys; }
/// <summary> /// Set the UrlConventions in DefaultODataPathHandler. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="conventions">The <see cref="ODataUrlConventions"/></param> public static void SetUrlConventions(this HttpConfiguration configuration, ODataUrlConventions conventions) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } ODataUriResolverSetttings settings = configuration.GetResolverSettings(); settings.UrlConventions = conventions; }
/// <summary> /// Sets the un-qualified function and action name call flag for the Uri parser on the configuration. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="unqualifiedNameCall"><c>true</c> to enable un-qualified name call, <c>false</c> otherwise.</param> public static void EnableUnqualifiedNameCall(this HttpConfiguration configuration, bool unqualifiedNameCall) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } ODataUriResolverSetttings settings = configuration.GetResolverSettings(); settings.UnqualifiedNameCall = unqualifiedNameCall; }
/// <summary> /// Sets the Enum prefix free flag for the Uri parser on the configuration. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="enumPrefixFree"><c>true</c> to enable Enum prefix free, <c>false</c> otherwise.</param> public static void EnableEnumPrefixFree(this HttpConfiguration configuration, bool enumPrefixFree) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } ODataUriResolverSetttings settings = configuration.GetResolverSettings(); settings.EnumPrefixFree = enumPrefixFree; }
/// <summary> /// Sets the case insensitive flag for the Uri parser on the configuration. Both metadata and key words /// are impacted by this flag. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="caseInsensitive"><c>true</c> to enable case insensitive, <c>false</c> otherwise.</param> public static void EnableCaseInsensitive(this HttpConfiguration configuration, bool caseInsensitive) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } ODataUriResolverSetttings settings = configuration.GetResolverSettings(); settings.CaseInsensitive = caseInsensitive; }
/// <summary> /// Initializes a new instance of the <see cref="ODataQueryOptions"/> class based on the incoming request and some metadata information from /// the <see cref="ODataQueryContext"/>. /// </summary> /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information.</param> /// <param name="request">The incoming request message.</param> public ODataQueryOptions(ODataQueryContext context, HttpRequestMessage request) { if (context == null) { throw Error.ArgumentNull("context"); } if (request == null) { throw Error.ArgumentNull("request"); } if (request.GetConfiguration() != null) { _assembliesResolver = request.GetConfiguration().Services.GetAssembliesResolver(); } // fallback to the default assemblies resolver if none available. _assembliesResolver = _assembliesResolver ?? new DefaultAssembliesResolver(); // remember the context and request Context = context; Request = request; // Parse the query from request Uri RawValues = new ODataRawQueryOptions(); IDictionary <string, string> queryParameters = request.GetQueryNameValuePairs().ToDictionary(p => p.Key, p => p.Value); _queryOptionParser = new ODataQueryOptionParser( context.Model, context.ElementType, context.NavigationSource, queryParameters); HttpConfiguration configuration = Request.GetConfiguration(); if (configuration != null) { ODataUriResolverSetttings resolverSettings = configuration.GetResolverSettings(); _queryOptionParser.Resolver = resolverSettings.CreateResolver(context.Model); } BuildQueryOptions(queryParameters); Validator = new ODataQueryValidator(); }
internal static ODataUriResolverSetttings GetResolverSettings(this HttpConfiguration configuration) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } object value; if (configuration.Properties.TryGetValue(ResolverSettingsKey, out value)) { return(value as ODataUriResolverSetttings); } ODataUriResolverSetttings defaultSettings = new ODataUriResolverSetttings(); configuration.Properties[ResolverSettingsKey] = defaultSettings; return(defaultSettings); }
private static ODataPath Parse( IEdmModel model, string serviceRoot, string odataPath, ODataUriResolverSetttings resolverSettings, bool enableUriTemplateParsing) { ODataUriParser uriParser; Uri serviceRootUri = null; Uri fullUri = null; NameValueCollection queryString = null; if (enableUriTemplateParsing) { uriParser = new ODataUriParser(model, new Uri(odataPath, UriKind.Relative)); uriParser.EnableUriTemplateParsing = true; } else { Contract.Assert(serviceRoot != null); serviceRootUri = new Uri( serviceRoot.EndsWith("/", StringComparison.Ordinal) ? serviceRoot : serviceRoot + "/"); fullUri = new Uri(serviceRootUri, odataPath); queryString = fullUri.ParseQueryString(); uriParser = new ODataUriParser(model, serviceRootUri, fullUri); } uriParser.UrlConventions = resolverSettings.UrlConventions; uriParser.Resolver = resolverSettings.CreateResolver(model); Semantic.ODataPath path; UnresolvedPathSegment unresolvedPathSegment = null; Semantic.KeySegment id = null; try { path = uriParser.ParsePath(); } catch (ODataUnrecognizedPathException ex) { if (ex.ParsedSegments != null && ex.ParsedSegments.Count() > 0 && (ex.ParsedSegments.Last().EdmType is IEdmComplexType || ex.ParsedSegments.Last().EdmType is IEdmEntityType) && ex.CurrentSegment != ODataSegmentKinds.Count) { if (ex.UnparsedSegments.Count() == 0) { path = new Semantic.ODataPath(ex.ParsedSegments); unresolvedPathSegment = new UnresolvedPathSegment(ex.CurrentSegment); } else { // Throw ODataException if there is some segment following the unresolved segment. throw new ODataException(Error.Format( SRResources.InvalidPathSegment, ex.UnparsedSegments.First(), ex.CurrentSegment)); } } else { throw; } } if (!enableUriTemplateParsing && path.LastSegment is Semantic.NavigationPropertyLinkSegment) { IEdmCollectionType lastSegmentEdmType = path.LastSegment.EdmType as IEdmCollectionType; if (lastSegmentEdmType != null) { Semantic.EntityIdSegment entityIdSegment = null; bool exceptionThrown = false; try { entityIdSegment = uriParser.ParseEntityId(); if (entityIdSegment != null) { // Create another ODataUriParser to parse $id, which is absolute or relative. ODataUriParser parser = new ODataUriParser(model, serviceRootUri, entityIdSegment.Id); id = parser.ParsePath().LastSegment as Semantic.KeySegment; } } catch (ODataException) { // Exception was thrown while parsing the $id. // We will throw another exception about the invalid $id. exceptionThrown = true; } if (exceptionThrown || (entityIdSegment != null && (id == null || !(id.EdmType.IsOrInheritsFrom(lastSegmentEdmType.ElementType.Definition) || lastSegmentEdmType.ElementType.Definition.IsOrInheritsFrom(id.EdmType))))) { throw new ODataException(Error.Format(SRResources.InvalidDollarId, queryString.Get("$id"))); } } } ODataPath webAPIPath = ODataPathSegmentTranslator.TranslateODataLibPathToWebApiPath( path, model, unresolvedPathSegment, id, enableUriTemplateParsing, uriParser.ParameterAliasNodes); CheckNavigableProperty(webAPIPath, model); return webAPIPath; }
private static ODataPath Parse( IEdmModel model, string serviceRoot, string odataPath, ODataUriResolverSetttings resolverSettings, bool enableUriTemplateParsing) { ODataUriParser uriParser; Uri serviceRootUri = null; Uri fullUri = null; NameValueCollection queryString = null; if (enableUriTemplateParsing) { uriParser = new ODataUriParser(model, new Uri(odataPath, UriKind.Relative)); uriParser.EnableUriTemplateParsing = true; } else { Contract.Assert(serviceRoot != null); serviceRootUri = new Uri( serviceRoot.EndsWith("/", StringComparison.Ordinal) ? serviceRoot : serviceRoot + "/"); fullUri = new Uri(serviceRootUri, odataPath); queryString = fullUri.ParseQueryString(); uriParser = new ODataUriParser(model, serviceRootUri, fullUri); } uriParser.Resolver = resolverSettings.CreateResolver(); Semantic.ODataPath path; UnresolvedPathSegment unresolvedPathSegment = null; Semantic.KeySegment id = null; try { path = uriParser.ParsePath(); } catch (ODataUnrecognizedPathException ex) { if (ex.ParsedSegments != null && ex.ParsedSegments.Count() > 0 && (ex.ParsedSegments.Last().EdmType is IEdmComplexType || ex.ParsedSegments.Last().EdmType is IEdmEntityType) && ex.CurrentSegment != ODataSegmentKinds.Count) { if (ex.UnparsedSegments.Count() == 0) { path = new Semantic.ODataPath(ex.ParsedSegments); unresolvedPathSegment = new UnresolvedPathSegment(ex.CurrentSegment); } else { // Throw ODataException if there is some segment following the unresolved segment. throw new ODataException(Error.Format( SRResources.InvalidPathSegment, ex.UnparsedSegments.First(), ex.CurrentSegment)); } } else { throw; } } if (!enableUriTemplateParsing && path.LastSegment is Semantic.NavigationPropertyLinkSegment) { IEdmCollectionType lastSegmentEdmType = path.LastSegment.EdmType as IEdmCollectionType; if (lastSegmentEdmType != null) { Semantic.EntityIdSegment entityIdSegment = null; bool exceptionThrown = false; try { entityIdSegment = uriParser.ParseEntityId(); if (entityIdSegment != null) { // Create another ODataUriParser to parse $id, which is absolute or relative. ODataUriParser parser = new ODataUriParser(model, serviceRootUri, entityIdSegment.Id); id = parser.ParsePath().LastSegment as Semantic.KeySegment; } } catch (ODataException) { // Exception was thrown while parsing the $id. // We will throw another exception about the invalid $id. exceptionThrown = true; } if (exceptionThrown || (entityIdSegment != null && (id == null || !(id.EdmType.IsOrInheritsFrom(lastSegmentEdmType.ElementType.Definition) || lastSegmentEdmType.ElementType.Definition.IsOrInheritsFrom(id.EdmType))))) { throw new ODataException(Error.Format(SRResources.InvalidDollarId, queryString.Get("$id"))); } } } ODataPath webAPIPath = ODataPathSegmentTranslator.TranslateODLPathToWebAPIPath( path, model, unresolvedPathSegment, id, enableUriTemplateParsing, uriParser.ParameterAliasNodes); CheckNavigableProperty(webAPIPath, model); return(webAPIPath); }
internal static ODataUriResolverSetttings GetResolverSettings(this HttpConfiguration configuration) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } object value; if (configuration.Properties.TryGetValue(ResolverSettingsKey, out value)) { return value as ODataUriResolverSetttings; } ODataUriResolverSetttings defaultSettings = new ODataUriResolverSetttings(); configuration.Properties[ResolverSettingsKey] = defaultSettings; return defaultSettings; }