Beispiel #1
0
        private static async Task <IHydraResource> GatherLinks(
            ITypedEntity resource,
            Statement statement,
            ProcessingState processingState,
            IHydraResource hydraResource,
            CancellationToken cancellationToken)
        {
            var relationResource = await resource.Context.Load <IDereferencableLink>(statement.Predicate, cancellationToken);

            var @object = await resource.Context.Load <IResource>(statement.Object, cancellationToken);

            var linkType = relationResource.GetLinkType() ?? @object.GetLinkType(processingState.LinksPolicy, processingState.Root);

            if (linkType != null)
            {
                hydraResource = hydraResource ?? resource.ActLike <IHydraResource>();
                var owner = hydraResource;
                processingState.MarkAsOwned(relationResource.Iri);
                processingState.MarkAsOwned(@object.Iri);
                processingState.ProcessingCompleted +=
                    (sender, e) => CreateRelationHandler((ProcessingState)sender, owner, relationResource, @object, linkType);
            }

            return(hydraResource);
        }
 private static Task <IResource> PointingResourceInitializer(
     ITypedEntity resource,
     IHydraClient client,
     ProcessingState processingState,
     CancellationToken cancellationToken)
 {
     resource.Unwrap().SetProperty(ResourceExtensions.BaseUrlPropertyInfo, (Uri)processingState.BaseUrl);
     return(ResourceInitializer(resource.ActLike <IHydraResource>(), client, processingState, cancellationToken));
 }
 private static Task <IResource> ClientInitializer(
     ITypedEntity resource,
     IHydraClient client,
     ProcessingState processingState,
     CancellationToken cancellationToken)
 {
     resource.Unwrap().SetProperty(ClientPropertyInfo, client);
     return(ResourceInitializer(
                resource.Is(hydra.ApiDocumentation) ? (ITypedEntity)resource.ActLike <IApiDocumentation>() : resource.ActLike <ICollection>(),
                client,
                processingState,
                cancellationToken));
 }
Beispiel #4
0
        private static void GatherOperationTargets(IHydraResource resource, ProcessingState processingState)
        {
            IEnumerable <IOperation> operations = resource.Operations;
            var supportedOperationsContainer    = OperationContainers.Where(_ => resource.Type.Contains(_.Key)).Select(_ => _.Value).FirstOrDefault();

            if (supportedOperationsContainer != null)
            {
                operations = operations.Concat(supportedOperationsContainer(resource));
            }

            foreach (var operation in operations)
            {
                var proxy = operation.Unwrap();
                proxy.SetProperty(ResourceExtensions.TargetPropertyInfo, resource);
                proxy.SetProperty(ResourceExtensions.OriginatingMediaTypeProperty, processingState.OriginatingMediaType);
            }
        }
Beispiel #5
0
        private static void CreateRelationHandler(
            ProcessingState processingState,
            IHydraResource owner,
            IDereferencableLink relationResource,
            IResource @object,
            Iri type)
        {
            var handlers = relationResource.SupportedOperations.Any() && @object != null
                ? CreateTemplatedOperation(owner, relationResource, @object, hydra.Operation)
                : (IEnumerable)CreateLink(owner, relationResource, @object, type);

            foreach (IResource handler in handlers)
            {
                var proxy = handler.Unwrap();
                proxy.SetProperty(ResourceExtensions.BaseUrlPropertyInfo, (Uri)processingState.BaseUrl);
                processingState.ForbiddenHypermeda.Add(handler.Iri);
            }
        }
        /// <inheritdoc />
        public async Task <IHypermediaContainer> Process(
            IResponse response,
            IHydraClient hydraClient,
            IHypermediaProcessingOptions options,
            CancellationToken cancellationToken)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            if (hydraClient == null)
            {
                throw new ArgumentNullException(nameof(hydraClient));
            }

            var       responseIri = options?.OriginalUrl ?? response.Url;
            var       context     = EntityContextFactory.Value.Create();
            IResource resource;
            var       hypermedia = new List <IResource>();

            using (var processingState = new ProcessingState(
                       context,
                       _ontologyProvider,
                       responseIri,
                       options?.LinksPolicy ?? LinksPolicy.Strict,
                       response.Headers[HydraClient.ContentType].FirstOrDefault()))
            {
                var rdfReader = await CreateRdfReader(response, cancellationToken);

                var serializableSource = (ISerializableEntitySource)context.EntitySource;
                using (processingState.StartGatheringStatementsFor(serializableSource, _ => _.Object != null && !IsStandaloneControl(_.Predicate)))
                    using (var reader = new StreamReader(await response.GetBody(cancellationToken)))
                    {
                        await serializableSource.Read(reader, rdfReader, response.Url, cancellationToken);
                    }

                resource = await ProcessResources(processingState, hydraClient, hypermedia, cancellationToken);
            }

            return(new HypermediaContainer(response, resource, hypermedia));
        }
        private async Task <IResource> ProcessResources(
            ProcessingState processingState,
            IHydraClient hydraClient,
            List <IResource> hypermedia,
            CancellationToken cancellationToken)
        {
            var resource = await processingState.Context.Load <IResource>(processingState.BaseUrl, cancellationToken);

            foreach (var entity in processingState.Context.AsQueryable <ITypedEntity>())
            {
                Func <ITypedEntity, IHydraClient, ProcessingState, CancellationToken, Task <IResource> > initializer = Initializers[Untyped];
                foreach (var type in entity.Type.Where(item => item.ToString().StartsWith(hydra.Namespace)))
                {
                    if (!Initializers.TryGetValue(type, out initializer))
                    {
                        initializer = Initializers[Untyped];
                        continue;
                    }

                    break;
                }

                var currentResource = await initializer(entity, hydraClient, processingState, cancellationToken);

                if (currentResource != null)
                {
                    if (processingState.AllHypermedia.Contains(currentResource.Iri))
                    {
                        currentResource = EnsureTypeCastedFor(currentResource);
                        hypermedia.Add(currentResource);
                    }

                    if (currentResource.Iri == resource.Iri)
                    {
                        resource = currentResource;
                    }
                }
            }

            return(resource);
        }
Beispiel #8
0
        private static async Task <IResource> ResourceInitializer(
            ITypedEntity resource,
            IHydraClient client,
            ProcessingState processingState,
            CancellationToken cancellationToken)
        {
            IHydraResource hydraResource = resource as IHydraResource
                                           ?? (resource.Is(hydra.Resource) ? resource.ActLike <IHydraResource>() : null);

            bool hasView = false;

            foreach (var statement in processingState.StatementsOf(resource.Iri))
            {
                hydraResource = await GatherLinks(resource, statement, processingState, hydraResource, cancellationToken);

                if (statement.Predicate == hydra.view)
                {
                    hasView = true;
                }
            }

            if (hydraResource != null && processingState.NumberOfStatementsOf(resource.Iri) > 0)
            {
                GatherOperationTargets(hydraResource, processingState);
                if (hasView)
                {
                    hydraResource = hydraResource.ActLike <IResourceView>();
                }

                var addToHypermedia = !processingState.ForbiddenHypermeda.Contains(hydraResource.Iri) &&
                                      (!hydraResource.Iri.IsBlank || IsHydraDependent(hydraResource));
                if (addToHypermedia)
                {
                    processingState.AllHypermedia.Add(hydraResource.Iri);
                }
            }

            return(hydraResource);
        }