Ejemplo n.º 1
0
        public static string ParseWsPath(string template, string serviceName)
        {
            var wsPath = string.Empty;

            template = TrimPrefix(template);
            var segementLines = template.Split(separator);

            foreach (var segementLine in segementLines)
            {
                var segmentType = TemplateSegmentHelper.GetSegmentType(segementLine, serviceName);
                switch (segmentType)
                {
                case SegmentType.Literal:
                    wsPath += "/" + segementLine;
                    break;

                case SegmentType.AppService:
                {
                    var appServiceName = ParseAppServiceName(TemplateSegmentHelper.GetSegmentVal(segementLine),
                                                             serviceName);
                    wsPath += "/" + appServiceName;
                    break;
                }

                default:
                    throw new LmsException("解析websocketPath失败");
                }
            }

            return(wsPath.TrimEnd('/'));
        }
Ejemplo n.º 2
0
        private string GenerateRoutePath()
        {
            var routePath = string.Empty;

            foreach (var segment in RouteTemplate.Segments)
            {
                if (!segment.IsParameter)
                {
                    routePath += segment.Value + "/";
                }
                else
                {
                    routePath += "{" + TemplateSegmentHelper.GetSegmentVal(segment.Value) + "}" + "/";
                }
            }

            return(routePath.ToLower().TrimEnd('/'));
        }
Ejemplo n.º 3
0
        private void ParseRouteTemplate(string template, string serviceName, MethodInfo methodInfo)
        {
            template = TrimPrefix(template);
            var segementLines = template.Split(separator);

            foreach (var segementLine in segementLines)
            {
                var segmentType = TemplateSegmentHelper.GetSegmentType(segementLine, serviceName);
                switch (segmentType)
                {
                case SegmentType.Literal:
                    RouteTemplate.Segments.Add(new TemplateSegment(SegmentType.Literal, segementLine));
                    break;

                case SegmentType.AppService:
                {
                    var appServiceName = ParseAppServiceName(TemplateSegmentHelper.GetSegmentVal(segementLine),
                                                             serviceName);
                    RouteTemplate.Segments.Add(new TemplateSegment(SegmentType.AppService, appServiceName));
                    break;
                }

                case SegmentType.Path:
                {
                    var pathParameterSegment =
                        ParsePathParameterSegment(TemplateSegmentHelper.GetSegmentVal(segementLine), methodInfo);
                    RouteTemplate.Segments.Add(pathParameterSegment.Item1);
                    RouteTemplate.Parameters.Add(pathParameterSegment.Item2);
                    break;
                }
                }
            }

            var appServiceSegmentCount = RouteTemplate.Segments?.Count(p => p.SegmentType == SegmentType.AppService);

            if (appServiceSegmentCount != 1)
            {
                throw new LmsException("路由模板未指定服务应用", StatusCode.RouteParseError);
            }
        }
Ejemplo n.º 4
0
        public static string GetWsPath([NotNull] this IRouteTemplateProvider routeTemplateProvider, string serviceName)
        {
            Check.NotNull(routeTemplateProvider, nameof(routeTemplateProvider));
            var template      = TrimPrefix(routeTemplateProvider.Template);
            var segementLines = template.Split(separator);
            var wsPath        = "/";

            foreach (var segementLine in segementLines)
            {
                var segmentType = TemplateSegmentHelper.GetSegmentType(segementLine, serviceName);
                if (segmentType == SegmentType.AppService)
                {
                    var appServiceName = ParseAppServiceName(TemplateSegmentHelper.GetSegmentVal(segementLine),
                                                             serviceName);
                    wsPath += appServiceName;
                }
                else
                {
                    wsPath += TemplateSegmentHelper.GetSegmentVal(segementLine);
                }
            }

            return(wsPath);
        }
Ejemplo n.º 5
0
        private static ParameterDescriptor CreateParameterDescriptor(MethodInfo methodInfo, ParameterInfo parameter,
                                                                     HttpMethod httpMethod)
        {
            var bindingSourceMetadata =
                parameter.GetCustomAttributes().OfType <IBindingSourceMetadata>().FirstOrDefault();
            ParameterDescriptor parameterDescriptor = null;

            if (bindingSourceMetadata != null)
            {
                var parameterFrom = bindingSourceMetadata.BindingSource.Id.To <ParameterFrom>();
                if (httpMethod == HttpMethod.Get && parameterFrom == ParameterFrom.Body)
                {
                    throw new LmsException("Get请求不允许通过RequestBody获取参数值");
                }

                if (parameterFrom == ParameterFrom.Path && !parameter.ParameterType.IsSample())
                {
                    throw new LmsException($"路由类型参数不允许为复杂数据类型");
                }

                parameterDescriptor = new ParameterDescriptor(parameterFrom, parameter);
            }
            else
            {
                if (parameter.IsSampleType())
                {
                    var httpMethodAttribute =
                        methodInfo.GetCustomAttributes().OfType <HttpMethodAttribute>().FirstOrDefault(p =>
                                                                                                       p.HttpMethods.Contains(httpMethod.ToString().ToUpper()));
                    if (httpMethodAttribute == null)
                    {
                        parameterDescriptor =
                            new ParameterDescriptor(ParameterFrom.Query, parameter);
                    }
                    else
                    {
                        var routeTemplate         = httpMethodAttribute.Template;
                        var routeTemplateSegments = routeTemplate.Split("/");
                        var parameterFromPath     = false;
                        foreach (var routeTemplateSegment in routeTemplateSegments)
                        {
                            if (TemplateSegmentHelper.IsVariable(routeTemplateSegment) &&
                                TemplateSegmentHelper.GetVariableName(routeTemplateSegment) == parameter.Name)
                            {
                                parameterDescriptor =
                                    new ParameterDescriptor(ParameterFrom.Path, parameter, TemplateSegmentHelper.GetSegmentVal(routeTemplateSegment));
                                parameterFromPath = true;
                                break;
                            }
                        }

                        if (!parameterFromPath)
                        {
                            parameterDescriptor =
                                new ParameterDescriptor(ParameterFrom.Query, parameter);
                        }
                    }
                }
                else
                {
                    parameterDescriptor =
                        new ParameterDescriptor(ParameterFrom.Body, parameter);
                }
            }

            return(parameterDescriptor);
        }