public static bool TryParse( Uri identifier, IReadOnlyCollection <IExtension> extensions, out IUniformResourceIdentifier parsedIdentifier) { parsedIdentifier = null; if (null == identifier) { throw new ArgumentNullException(UniformResourceIdentifier.ArgumentNameIdentifier); } IReadOnlyCollection <IExtension> effectiveExtensions = extensions ?? Array.Empty <IExtension>(); IResourceQuery query = new ResourceQuery(identifier); IReadOnlyCollection <string> alternatePathCollection = effectiveExtensions .Select( (IExtension item) => string.Format(CultureInfo.InvariantCulture, UniformResourceIdentifier.AlternatePathTemplate, item.Path)) .ToArray(); string alternatePaths = string.Join(string.Empty, alternatePathCollection); string retrievalPattern = string.Format(CultureInfo.InvariantCulture, UniformResourceIdentifier.RetrievalPatternTemplate, alternatePaths); Regex retrievalExpression = new Regex(retrievalPattern, RegexOptions.IgnoreCase); Match expressionMatch = retrievalExpression.Match(identifier.AbsoluteUri); if (!expressionMatch.Success) { return(false); } string type = expressionMatch.Groups[UniformResourceIdentifier.ExpressionGroupNameType].Value; if (string.IsNullOrWhiteSpace(type)) { return(false); } string schemaIdentifier; switch (type) { case ProtocolConstants.PathGroups: schemaIdentifier = SchemaIdentifiers.Core2Group; break; case ProtocolConstants.PathUsers: schemaIdentifier = SchemaIdentifiers.Core2EnterpriseUser; break; default: if (null == extensions) { return(false); } schemaIdentifier = effectiveExtensions .Where( (IExtension item) => string.Equals(item.Path, type, StringComparison.OrdinalIgnoreCase)) .Select( (IExtension item) => item.SchemaIdentifier) .SingleOrDefault(); if (string.IsNullOrWhiteSpace(schemaIdentifier)) { return(false); } break; } IResourceIdentifier resourceIdentifier = new ResourceIdentifier(); resourceIdentifier.SchemaIdentifier = schemaIdentifier; string resourceIdentifierValue = expressionMatch.Groups[UniformResourceIdentifier.ExpressionGroupNameIdentifier].Value; if (!string.IsNullOrWhiteSpace(resourceIdentifierValue)) { string unescapedIdentifier = Uri.UnescapeDataString(resourceIdentifierValue); resourceIdentifier.Identifier = unescapedIdentifier; } parsedIdentifier = new UniformResourceIdentifier(resourceIdentifier, query); return(true); }
public ResourceQuery(Uri resource) { if (null == resource) { throw new ArgumentNullException(nameof(resource)); } string query = resource.Query; if (!string.IsNullOrWhiteSpace(query)) { NameValueCollection keyedValues = HttpUtility.ParseQueryString(query); IEnumerable <string> keys = keyedValues.AllKeys; foreach (string key in keys) { if (string.Equals(key, QueryKeys.Attributes, StringComparison.OrdinalIgnoreCase)) { string attributeExpression = keyedValues[key]; if (!string.IsNullOrWhiteSpace(attributeExpression)) { this.Attributes = ResourceQuery.ParseAttributes(attributeExpression); } } if (string.Equals(key, QueryKeys.Count, StringComparison.OrdinalIgnoreCase)) { Action <IPaginationParameters, int> action = new Action <IPaginationParameters, int>( (IPaginationParameters pagination, int paginationValue) => pagination.Count = paginationValue); this.ApplyPaginationParameter(keyedValues[key], action); } if (string.Equals(key, QueryKeys.ExcludedAttributes, StringComparison.OrdinalIgnoreCase)) { string attributeExpression = keyedValues[key]; if (!string.IsNullOrWhiteSpace(attributeExpression)) { this.ExcludedAttributes = ResourceQuery.ParseAttributes(attributeExpression); } } if (string.Equals(key, QueryKeys.Filter, StringComparison.OrdinalIgnoreCase)) { string filterExpression = keyedValues[key]; if (!string.IsNullOrWhiteSpace(filterExpression)) { this.Filters = ResourceQuery.ParseFilters(filterExpression); } } if (string.Equals(key, QueryKeys.StartIndex, StringComparison.OrdinalIgnoreCase)) { Action <IPaginationParameters, int> action = new Action <IPaginationParameters, int>( (IPaginationParameters pagination, int paginationValue) => pagination.StartIndex = paginationValue); this.ApplyPaginationParameter(keyedValues[key], action); } } } if (null == this.Filters) { this.Filters = Array.Empty <Filter>(); } if (null == this.Attributes) { this.Attributes = Array.Empty <string>(); } if (null == this.ExcludedAttributes) { this.ExcludedAttributes = Array.Empty <string>(); } }