Пример #1
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (context.Request.Method != HttpMethods.Get)
            {
                await next(context);

                return;
            }

            var routeMatcher = new RouteMatcher();

            if (!routeMatcher.TryMatch(CreateRouteTemplate(), context.Request.Path, out RouteValueDictionary routeValues))
            {
                await next(context);

                return;
            }

            var schema = routeValues["schema"].ToString();

            var xsdFileInformationByUriSegment = _xsdFileInformationProvider.XsdFileInformationByUriSegment(schema);

            if (xsdFileInformationByUriSegment == default)
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                return;
            }

            string assemblyName          = xsdFileInformationByUriSegment.AssemblyName;
            string fullQualifiedFileName = $"Artifacts/Schemas/{routeValues["file"]}.xsd";

            var embeddedFileProvider = _embeddedFileProviderByAssemblyName.GetOrAdd(
                assemblyName, key => new EmbeddedFileProvider(_assembliesProvider.Get(assemblyName)));

            context.Response.ContentType = "application/xml";
            context.Response.StatusCode  = StatusCodes.Status200OK;

            await context.Response.SendFileAsync(embeddedFileProvider.GetFileInfo(fullQualifiedFileName));

            string CreateRouteTemplate()
            {
                string template = $"metadata/";

                if (_apiSettings.GetApiMode() == ApiMode.YearSpecific)
                {
                    template += RouteConstants.SchoolYearFromRoute;
                }

                if (_apiSettings.GetApiMode() == ApiMode.InstanceYearSpecific)
                {
                    template += RouteConstants.InstanceIdFromRoute;
                    template += RouteConstants.SchoolYearFromRoute;
                }

                return(template + "xsd/{schema}/{file}.xsd");
            }
        }
Пример #2
0
        private OpenApiMetadataRequest CreateOpenApiMetadataRequest(string path)
        {
            // need to build the request model manually as binding does not exist in the middleware pipeline.
            // this is less effort that rewriting the open api metadata cache.
            var openApiMetadataRequest = new OpenApiMetadataRequest();

            var matcher = new RouteMatcher();

            foreach (var routeInformation in _routeInformations)
            {
                string routeTemplate = routeInformation.GetRouteInformation()
                                       .Template;

                if (matcher.TryMatch(routeTemplate, path, out RouteValueDictionary values))
                {
                    if (values.ContainsKey("document"))
                    {
                        // the route for resources/descriptors is the same format as the schema endpoint.
                        // we need to validate that it is a schema instead.
                        string documentName = values["document"]
                                              .ToString();

                        if (_schemaNameMaps.Value.Any(x => x.UriSegment.EqualsIgnoreCase(documentName)))
                        {
                            openApiMetadataRequest.SchemaName = documentName;
                        }

                        if (documentName.EqualsIgnoreCase("resources") || documentName.EqualsIgnoreCase("descriptors"))
                        {
                            openApiMetadataRequest.ResourceType = documentName;
                        }
                    }

                    if (values.ContainsKey("schoolYearFromRoute"))
                    {
                        string schoolYear = values["schoolYearFromRoute"]
                                            .ToString();

                        if (int.TryParse(schoolYear, out int schoolYearFromRoute))
                        {
                            openApiMetadataRequest.SchoolYearFromRoute = schoolYearFromRoute;
                        }
                    }

                    if (values.ContainsKey("instanceIdFromRoute"))
                    {
                        var instance   = values["instanceIdFromRoute"];
                        var instanceId = instance as string;

                        if (!string.IsNullOrEmpty(instanceId))
                        {
                            openApiMetadataRequest.InstanceIdFromRoute = instanceId;
                        }
                    }

                    if (values.ContainsKey("organizationCode"))
                    {
                        openApiMetadataRequest.SchemaName = values["organizationCode"]
                                                            .ToString();
                    }

                    if (values.ContainsKey("compositeCategoryName"))
                    {
                        openApiMetadataRequest.CompositeCategoryName = values["compositeCategoryName"]
                                                                       .ToString();
                    }

                    if (values.ContainsKey("profileName"))
                    {
                        openApiMetadataRequest.ProfileName = values["profileName"]
                                                             .ToString();
                    }

                    if (values.ContainsKey("other"))
                    {
                        openApiMetadataRequest.OtherName = values["other"]
                                                           .ToString();
                    }
                }
            }

            return(openApiMetadataRequest);
        }