public async Task Invoke(HttpContext httpContext,
                                 IControllerResourceMapping controllerResourceMapping,
                                 IJsonApiOptions options,
                                 ICurrentRequest currentRequest,
                                 IResourceGraph resourceGraph)
        {
            _httpContext               = httpContext;
            _currentRequest            = currentRequest;
            _controllerResourceMapping = controllerResourceMapping;
            _resourceGraph             = resourceGraph;
            _options     = options;
            _routeValues = httpContext.GetRouteData().Values;
            var requestResource = GetCurrentEntity();

            if (requestResource != null)
            {
                _currentRequest.SetRequestResource(requestResource);
                _currentRequest.IsRelationshipPath = PathIsRelationship();
                _currentRequest.BasePath           = GetBasePath(requestResource.ResourceName);
                _currentRequest.BaseId             = GetBaseId();
                _currentRequest.RelationshipId     = GetRelationshipId();
            }

            if (IsValid())
            {
                await _next(httpContext);
            }
        }
        public async Task Invoke(HttpContext httpContext,
                                 IControllerResourceMapping controllerResourceMapping,
                                 IJsonApiOptions options,
                                 ICurrentRequest currentRequest,
                                 IResourceGraph resourceGraph)
        {
            var routeValues = httpContext.GetRouteData().Values;

            var resourceContext = CreateResourceContext(routeValues, controllerResourceMapping, resourceGraph);

            if (resourceContext != null)
            {
                if (!await ValidateContentTypeHeaderAsync(httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupCurrentRequest(currentRequest, resourceContext, routeValues, options, httpContext.Request);

                httpContext.SetJsonApiRequest();
            }

            await _next(httpContext);
        }
        public async Task Invoke(HttpContext httpContext,
                                 IControllerResourceMapping controllerResourceMapping,
                                 IJsonApiOptions options,
                                 IJsonApiRequest request,
                                 IResourceContextProvider resourceContextProvider)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }
            if (controllerResourceMapping == null)
            {
                throw new ArgumentNullException(nameof(controllerResourceMapping));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (resourceContextProvider == null)
            {
                throw new ArgumentNullException(nameof(resourceContextProvider));
            }

            var routeValues = httpContext.GetRouteData().Values;

            var primaryResourceContext = CreatePrimaryResourceContext(routeValues, controllerResourceMapping, resourceContextProvider);

            if (primaryResourceContext != null)
            {
                if (!await ValidateContentTypeHeaderAsync(HeaderConstants.MediaType, httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(_mediaType, httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupResourceRequest((JsonApiRequest)request, primaryResourceContext, routeValues, options, resourceContextProvider, httpContext.Request);

                httpContext.RegisterJsonApiRequest();
            }
            else if (IsOperationsRequest(routeValues))
            {
                if (!await ValidateContentTypeHeaderAsync(HeaderConstants.AtomicOperationsMediaType, httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(_atomicOperationsMediaType, httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupOperationsRequest((JsonApiRequest)request, options, httpContext.Request);

                httpContext.RegisterJsonApiRequest();
            }

            await _next(httpContext);
        }
Example #4
0
        private Task RunMiddlewareTask(InvokeConfiguration holder)
        {
            IControllerResourceMapping controllerResourceMapping = holder.ControllerResourceMapping.Object;
            HttpContext     context       = holder.HttpContext;
            IJsonApiOptions options       = holder.Options.Object;
            JsonApiRequest  request       = holder.Request;
            IResourceGraph  resourceGraph = holder.ResourceGraph.Object;

            return(holder.MiddleWare.InvokeAsync(context, controllerResourceMapping, options, request, resourceGraph));
        }
        private static ResourceContext CreateResourceContext(RouteValueDictionary routeValues,
                                                             IControllerResourceMapping controllerResourceMapping, IResourceContextProvider resourceGraph)
        {
            var controllerName = (string)routeValues["controller"];

            if (controllerName == null)
            {
                return(null);
            }

            var resourceType = controllerResourceMapping.GetAssociatedResource(controllerName);

            return(resourceGraph.GetResourceContext(resourceType));
        }
        private static ResourceContext CreatePrimaryResourceContext(RouteValueDictionary routeValues,
                                                                    IControllerResourceMapping controllerResourceMapping, IResourceContextProvider resourceContextProvider)
        {
            var controllerName = (string)routeValues["controller"];

            if (controllerName != null)
            {
                var resourceType = controllerResourceMapping.GetResourceTypeForController(controllerName);
                if (resourceType != null)
                {
                    return(resourceContextProvider.GetResourceContext(resourceType));
                }
            }

            return(null);
        }
Example #7
0
        public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMapping controllerResourceMapping, IJsonApiOptions options,
                                      IJsonApiRequest request, IResourceContextProvider resourceContextProvider)
        {
            ArgumentGuard.NotNull(httpContext, nameof(httpContext));
            ArgumentGuard.NotNull(controllerResourceMapping, nameof(controllerResourceMapping));
            ArgumentGuard.NotNull(options, nameof(options));
            ArgumentGuard.NotNull(request, nameof(request));
            ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));

            if (!await ValidateIfMatchHeaderAsync(httpContext, options.SerializerSettings))
            {
                return;
            }

            RouteValueDictionary routeValues            = httpContext.GetRouteData().Values;
            ResourceContext      primaryResourceContext = CreatePrimaryResourceContext(httpContext, controllerResourceMapping, resourceContextProvider);

            if (primaryResourceContext != null)
            {
                if (!await ValidateContentTypeHeaderAsync(HeaderConstants.MediaType, httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(MediaType, httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupResourceRequest((JsonApiRequest)request, primaryResourceContext, routeValues, options, resourceContextProvider, httpContext.Request);

                httpContext.RegisterJsonApiRequest();
            }
            else if (IsOperationsRequest(routeValues))
            {
                if (!await ValidateContentTypeHeaderAsync(HeaderConstants.AtomicOperationsMediaType, httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(AtomicOperationsMediaType, httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupOperationsRequest((JsonApiRequest)request, options, httpContext.Request);

                httpContext.RegisterJsonApiRequest();
            }

            // Workaround for bug https://github.com/dotnet/aspnetcore/issues/33394
            httpContext.Features.Set <IQueryFeature>(new FixedQueryFeature(httpContext.Features));

            await _next(httpContext);
        }
Example #8
0
        public LinkBuilder(IJsonApiOptions options, IJsonApiRequest request, IPaginationContext paginationContext,
                           IResourceContextProvider resourceContextProvider, IHttpContextAccessor httpContextAccessor, LinkGenerator linkGenerator,
                           IControllerResourceMapping controllerResourceMapping)
        {
            ArgumentGuard.NotNull(options, nameof(options));
            ArgumentGuard.NotNull(request, nameof(request));
            ArgumentGuard.NotNull(paginationContext, nameof(paginationContext));
            ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));
            ArgumentGuard.NotNull(linkGenerator, nameof(linkGenerator));
            ArgumentGuard.NotNull(controllerResourceMapping, nameof(controllerResourceMapping));

            _options                   = options;
            _request                   = request;
            _paginationContext         = paginationContext;
            _resourceContextProvider   = resourceContextProvider;
            _httpContextAccessor       = httpContextAccessor;
            _linkGenerator             = linkGenerator;
            _controllerResourceMapping = controllerResourceMapping;
        }
Example #9
0
        public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMapping controllerResourceMapping, IJsonApiOptions options,
                                      IJsonApiRequest request, IResourceContextProvider resourceContextProvider)
        {
            ArgumentGuard.NotNull(httpContext, nameof(httpContext));
            ArgumentGuard.NotNull(controllerResourceMapping, nameof(controllerResourceMapping));
            ArgumentGuard.NotNull(options, nameof(options));
            ArgumentGuard.NotNull(request, nameof(request));
            ArgumentGuard.NotNull(resourceContextProvider, nameof(resourceContextProvider));

            RouteValueDictionary routeValues = httpContext.GetRouteData().Values;

            ResourceContext primaryResourceContext = CreatePrimaryResourceContext(httpContext, controllerResourceMapping, resourceContextProvider);

            if (primaryResourceContext != null)
            {
                if (!await ValidateContentTypeHeaderAsync(HeaderConstants.MediaType, httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(MediaType, httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupResourceRequest((JsonApiRequest)request, primaryResourceContext, routeValues, options, resourceContextProvider, httpContext.Request);

                httpContext.RegisterJsonApiRequest();
            }
            else if (IsOperationsRequest(routeValues))
            {
                if (!await ValidateContentTypeHeaderAsync(HeaderConstants.AtomicOperationsMediaType, httpContext, options.SerializerSettings) ||
                    !await ValidateAcceptHeaderAsync(AtomicOperationsMediaType, httpContext, options.SerializerSettings))
                {
                    return;
                }

                SetupOperationsRequest((JsonApiRequest)request, options, httpContext.Request);

                httpContext.RegisterJsonApiRequest();
            }

            await _next(httpContext);
        }
        private static ResourceContext CreatePrimaryResourceContext(HttpContext httpContext, IControllerResourceMapping controllerResourceMapping,
                                                                    IResourceContextProvider resourceContextProvider)
        {
            Endpoint endpoint = httpContext.GetEndpoint();
            var      controllerActionDescriptor = endpoint?.Metadata.GetMetadata <ControllerActionDescriptor>();

            if (controllerActionDescriptor != null)
            {
                Type controllerType = controllerActionDescriptor.ControllerTypeInfo;
                Type resourceType   = controllerResourceMapping.GetResourceTypeForController(controllerType);

                if (resourceType != null)
                {
                    return(resourceContextProvider.GetResourceContext(resourceType));
                }
            }

            return(null);
        }