private static Link CreateResourceRelationshipLink(IHypermediaContext hypermediaContext, IResourcePathContext resourcePathContext, IResourceType resourceType, object clrResource, bool addRelationshipsPathSegment, IRelationshipInfo relationship, Meta apiRelationshipLinkMeta) { Contract.Requires(hypermediaContext != null); Contract.Requires(resourcePathContext != null); Contract.Requires(resourceType != null); Contract.Requires(clrResource != null); Contract.Requires(relationship != null); var clrResourceType = resourceType.ClrType; var urlBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(clrResourceType); var apiId = resourceType.GetApiId(clrResource); var apiResourcePath = resourcePathContext.GetResourceSelfPath(apiId); var apiRelationshipRelPathSegment = relationship.ApiRelPathSegment; var apiRelationshipLinkHRef = UrlBuilder.Create(urlBuilderConfiguration) .Path(apiResourcePath) .Path(Keywords.Relationships, addRelationshipsPathSegment) .Path(apiRelationshipRelPathSegment) .Build(); var apiRelationshipLink = new Link { HRef = apiRelationshipLinkHRef, Meta = apiRelationshipLinkMeta }; return(apiRelationshipLink); }
public Link CreateDocumentLink(IHypermediaContext hypermediaContext, IDocumentPathContext documentPathContext, DocumentType documentType, Type clrResourceType, object clrResource, ILinkContext linkContext) { Contract.Requires(hypermediaContext != null); Contract.Requires(documentPathContext != null); Contract.Requires(clrResourceType != null); Contract.Requires(clrResource != null); Contract.Requires(linkContext != null); var apiDocumentLink = CreateStandardDocumentLink(hypermediaContext, documentPathContext, linkContext); if (apiDocumentLink != null) { return(apiDocumentLink); } var urlBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(clrResourceType); var throwExceptionOnLinkBuildError = urlBuilderConfiguration.ThrowExceptionOnLinkBuildError; if (throwExceptionOnLinkBuildError == false) { return(null); } var apiRel = linkContext.Rel; var detail = ServerErrorStrings.DocumentBuildExceptionDetailBuildNonStandardDocumentLink .FormatWith(apiRel); throw new DocumentBuildException(detail); }
// PUBLIC METHODS /////////////////////////////////////////////////// #region Extensions Methods public static UrlBuilder CreateUrlBuilder(this IHypermediaContext hypermediaContext, Type resourceType) { Contract.Requires(hypermediaContext != null); Contract.Requires(resourceType != null); var urlBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(resourceType); var urlBuilder = UrlBuilder.Create(urlBuilderConfiguration); return(urlBuilder); }
private static Link CreateStandardResourceLink(IHypermediaContext hypermediaContext, IResourcePathContext resourcePathContext, Type clrResourceType, object clrResource, ILinkContext linkContext) { Contract.Requires(hypermediaContext != null); Contract.Requires(resourcePathContext != null); Contract.Requires(clrResourceType != null); Contract.Requires(clrResource != null); Contract.Requires(linkContext != null); var apiRel = linkContext.Rel; var apiLinkMeta = linkContext.Meta; switch (apiRel) { case Keywords.Canonical: case Keywords.Self: { var urlBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(clrResourceType); var serviceModel = hypermediaContext.GetServiceModel(); var resourceType = serviceModel.GetResourceType(clrResourceType); var isSelfLink = String.Compare(Keywords.Self, apiRel, StringComparison.Ordinal) == 0; var apiId = resourceType.GetApiId(clrResource); var apiResourcePath = isSelfLink ? resourcePathContext.GetResourceSelfPath(apiId) : resourcePathContext.GetResourceCanonicalPath(apiId); var apiHRef = UrlBuilder.Create(urlBuilderConfiguration) .Path(apiResourcePath) .Build(); var apiResourceLink = new Link { HRef = apiHRef, Meta = apiLinkMeta }; return(apiResourceLink); } } return(null); }
// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private static Link CreateStandardDocumentLink(IHypermediaContext hypermediaContext, IDocumentPathContext documentPathContext, ILinkContext linkContext) { Contract.Requires(hypermediaContext != null); Contract.Requires(documentPathContext != null); Contract.Requires(linkContext != null); var apiRel = linkContext.Rel; var apiLinkMeta = linkContext.Meta; switch (apiRel) { case Keywords.Up: { var apiDocumentSelfPath = documentPathContext.DocumentSelfPath .SafeToList(); var apiDocumentSelfPathCount = apiDocumentSelfPath.Count; var apiDocumentUpPath = new List <IHypermediaPath>(apiDocumentSelfPathCount); for (var i = 0; i < apiDocumentSelfPathCount; ++i) { var hypermediaPath = apiDocumentSelfPath[i]; var lastHypermediaPath = i == apiDocumentSelfPathCount - 1; if (lastHypermediaPath) { var hypermediaPathType = hypermediaPath.HypermediaPathType; switch (hypermediaPathType) { // Don't add the last hypermedia path. case HypermediaPathType.ResourceCollectionPath: case HypermediaPathType.ToOneResourcePath: case HypermediaPathType.ToManyResourceCollectionPath: case HypermediaPathType.NonResourcePath: case HypermediaPathType.SingletonPath: continue; case HypermediaPathType.ResourcePath: { // Turn last resource path into a resource collection path. var resourceHypermediaPath = (ResourceHypermediaPath)hypermediaPath; var clrResourceType = resourceHypermediaPath.ClrResourceType; var apiCollectionPathSegment = resourceHypermediaPath.ApiCollectionPathSegment; var resourceCollectionHypermediaPath = new ResourceCollectionHypermediaPath(clrResourceType, apiCollectionPathSegment); hypermediaPath = resourceCollectionHypermediaPath; } break; case HypermediaPathType.ToManyResourcePath: { // Turn last resource path into a to-many resource collection path. var toManyResourceHypermediaPath = (ToManyResourceHypermediaPath)hypermediaPath; var clrResourceType = toManyResourceHypermediaPath.ClrResourceType; var apiRelationshipPathSegment = toManyResourceHypermediaPath.ApiRelationshipPathSegment; var toManyResourceCollectionHypermediaPath = new ToManyResourceCollectionHypermediaPath(clrResourceType, apiRelationshipPathSegment); hypermediaPath = toManyResourceCollectionHypermediaPath; } break; default: throw new ArgumentOutOfRangeException(); } } apiDocumentUpPath.Add(hypermediaPath); } if (apiDocumentUpPath.Any() == false) { return(null); } var apiDocumentUpPathContext = new DocumentPathContext(apiDocumentUpPath); var clrPrimaryResourceType = apiDocumentUpPathContext.ClrResourceTypes.Any() ? apiDocumentUpPathContext.GetPrimaryClrResourceType() : documentPathContext.GetPrimaryClrResourceType(); var urlBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(clrPrimaryResourceType); var apiHRef = UrlBuilder.Create(urlBuilderConfiguration) .Path(apiDocumentUpPath) .Build(); var apiDocumentLink = new Link { HRef = apiHRef, Meta = apiLinkMeta }; return(apiDocumentLink); } case Keywords.Self: { // Get the resource type that generated this document. var clrSelfResourceType = documentPathContext.GetPrimaryClrResourceType(); var apiDocumentSelfPath = documentPathContext.DocumentSelfPath.SafeToReadOnlyCollection(); var apiDocumentSelfPathClrTypesOnly = apiDocumentSelfPath.Where(x => x.HasClrResourceType()) .ToList(); var apiDocumentSelfPathClrTypesOnlyCount = apiDocumentSelfPathClrTypesOnly.Count; if (apiDocumentSelfPathClrTypesOnlyCount > 1) { var lastHypermediaPath = apiDocumentSelfPathClrTypesOnly[apiDocumentSelfPathClrTypesOnlyCount - 1]; var lastHypermediaPathType = lastHypermediaPath.HypermediaPathType; switch (lastHypermediaPathType) { case HypermediaPathType.ToOneResourcePath: case HypermediaPathType.ToManyResourceCollectionPath: { var nextToLastHypermediaPath = apiDocumentSelfPathClrTypesOnly[apiDocumentSelfPathClrTypesOnlyCount - 2]; clrSelfResourceType = nextToLastHypermediaPath.GetClrResourceType(); break; } } } var urlBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(clrSelfResourceType); var query = documentPathContext.DocumentSelfQuery; var apiHRef = UrlBuilder.Create(urlBuilderConfiguration) .Path(apiDocumentSelfPath) .Query(query) .Build(); var apiDocumentLink = new Link { HRef = apiHRef, Meta = apiLinkMeta }; return(apiDocumentLink); } } return(null); }
// PUBLIC METHODS /////////////////////////////////////////////////// #region Extensions Methods public static IEnumerable <IHypermediaPath> ParseDocumentSelfPath(this Uri url, IHypermediaContext hypermediaContext) { Contract.Requires(url != null); Contract.Requires(hypermediaContext != null); // Create a URL path segment enumerator to enumerate over the URL path segments. var urlPathSegmentsFromUri = new List <string>(GetUrlPathSegments(url)); // Remove any root path segments var uriBuilderConfiguration = hypermediaContext.GetUrlBuilderConfiguration(url); if (uriBuilderConfiguration.RootPathSegments != null) { var rootPathSegments = uriBuilderConfiguration.RootPathSegments.SafeToReadOnlyList(); var rootPathSegmentsCount = rootPathSegments.Count; urlPathSegmentsFromUri = urlPathSegmentsFromUri.SkipWhile((pathSegment, index) => { if (index >= rootPathSegmentsCount) { return(false); } var rootPathSegment = rootPathSegments[index]; var rootPathSegmentEqualToPathSegment = String.CompareOrdinal(rootPathSegment, pathSegment) == 0; return(rootPathSegmentEqualToPathSegment); }) .ToList(); } var urlPathSegments = new List <string>(); var serviceModel = hypermediaContext.GetServiceModel(); var homeDocumentExists = serviceModel.TryGetHomeResourceType(out var homeResourceType); if (homeDocumentExists) { var homeResourceTypeApiCollectionPathSegment = homeResourceType.HypermediaInfo.ApiCollectionPathSegment; if (String.IsNullOrEmpty(homeResourceTypeApiCollectionPathSegment)) { urlPathSegments.Add(String.Empty); } } urlPathSegments.AddRange(urlPathSegmentsFromUri); var urlPathSegmentsEnumerator = (IEnumerator <string>)urlPathSegments.GetEnumerator(); // Parse the raw URL path for document self link path objects by // looking for the following: // 1. resource collection hypermedia path objects // 2. resource hypermedia path objects // 3. non-resource hypermedia path objects // 4. to-many resource collection hypermedia path objects // 5. to-many resource hypermedia path objects var documentSelfPath = new List <IHypermediaPath>(); var continueIterating = InitialIteration(serviceModel, urlPathSegmentsEnumerator, documentSelfPath); if (continueIterating && homeDocumentExists) { continueIterating = InitialIteration(serviceModel, urlPathSegmentsEnumerator, documentSelfPath); } while (continueIterating) { continueIterating = NextIteration(serviceModel, urlPathSegmentsEnumerator, documentSelfPath); } return(documentSelfPath); }