示例#1
0
        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);
        }
示例#2
0
        public Relationship CreateResourceRelationship(IHypermediaContext hypermediaContext, IResourcePathContext resourcePathContext, Type clrResourceType, object clrResource, IRelationshipContext relationshipContext)
        {
            Contract.Requires(hypermediaContext != null);
            Contract.Requires(resourcePathContext != null);
            Contract.Requires(clrResourceType != null);
            Contract.Requires(clrResource != null);
            Contract.Requires(relationshipContext != null);

            var apiRel = relationshipContext.Rel;

            var serviceModel = hypermediaContext.GetServiceModel();
            var resourceType = serviceModel.GetResourceType(clrResourceType);
            var relationship = resourceType.GetRelationshipInfo(apiRel);

            // Create the Links object of the relationship.
            var apiRelationshipLinks = CreateResourceRelationshipLinks(hypermediaContext, resourcePathContext, resourceType, clrResource, relationship, relationshipContext);

            // Create the relationship:
            var apiRelationship = CreateResourceRelationship(apiRelationshipLinks, resourceType, relationship, relationshipContext);

            return(apiRelationship);
        }
示例#3
0
        // 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);
        }