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 apiRel = linkContext.Rel; var detail = ServerErrorStrings.DocumentBuildExceptionDetailBuildNonStandardDocumentLink .FormatWith(apiRel); throw new DocumentBuildException(detail); }
public Link CreateResourceLink(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 apiResourceLink = CreateStandardResourceLink(hypermediaContext, resourcePathContext, clrResourceType, clrResource, linkContext); if (apiResourceLink != null) { return(apiResourceLink); } var apiRel = linkContext.Rel; var clrResourceTypeName = clrResourceType.Name; var detail = ServerErrorStrings.DocumentBuildExceptionDetailBuildNonStandardResourceLink .FormatWith(apiRel, clrResourceTypeName); throw new DocumentBuildException(detail); }
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); }
/// <summary> /// Called at the beginning of the linking. /// </summary> public virtual void PreLink(ILinkContext context) { this.Context = context; this.Messaging = context.ServiceProvider.GetService <IMessaging>(); }
public Link CreateDocumentLink(IHypermediaContext hypermediaContext, IDocumentPathContext documentPathContext, DocumentType documentType, Type clrResourceType, IEnumerable <object> clrResourceCollection, ILinkContext linkContext) { Contract.Requires(hypermediaContext != null); Contract.Requires(documentPathContext != null); Contract.Requires(clrResourceType != null); Contract.Requires(clrResourceCollection != 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); }
// 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: case Keywords.Self: { var urlBuilderConfiguration = hypermediaContext.UrlBuilderConfiguration; var apiDocumentSelfPath = documentPathContext.DocumentSelfPath; var apiHRef = UrlBuilder.Create(urlBuilderConfiguration) .Path(apiDocumentSelfPath) .RemoveLastPathSegment(apiRel == Keywords.Up) .Build(); var apiDocumentLink = new Link { HRef = apiHRef, Meta = apiLinkMeta }; return(apiDocumentLink); } } return(null); }