コード例 #1
0
        public static string ToReplyUrl(this object requestDto, string format = "json")
        {
            var requestType     = requestDto.GetType();
            var predefinedRoute = "/{0}/reply/{1}".Fmt(format, requestType.GetOperationName());
            var queryProperties = RestRoute.GetQueryProperties(requestDto.GetType());

            predefinedRoute += "?" + RestRoute.GetQueryString(requestDto, queryProperties);

            return(predefinedRoute);
        }
コード例 #2
0
 public static Dictionary<string, Type> GetQueryPropertyTypes(this Type requestType)
 {
     var map = RestRoute.GetQueryProperties(requestType);
     var to = new Dictionary<string, Type>();
     foreach (var entry in map)
     {
         to[entry.Key] = entry.Value.GetMemberType();
     }
     return to;
 }
コード例 #3
0
        public static string ToReplyUrl(this object requestDto, string format = "json")
        {
            var requestType = requestDto.GetType();
            var predefinedRoute = $"/{format}/reply/{requestType.GetOperationName()}";
            var queryProperties = RestRoute.GetQueryProperties(requestDto.GetType());
            var queryString = RestRoute.GetQueryString(requestDto, queryProperties);
            if (!IsNullOrEmpty(queryString))
                predefinedRoute += "?" + queryString;

            return predefinedRoute;
        }
コード例 #4
0
        public static string ToOneWayUrl(this object requestDto, string format = "json")
        {
            var requestType     = requestDto.GetType();
            var predefinedRoute = "/{0}/oneway/{1}".Fmt(format, requestType.GetOperationName());
            var queryProperties = RestRoute.GetQueryProperties(requestDto.GetType());
            var queryString     = RestRoute.GetQueryString(requestDto, queryProperties);

            if (!string.IsNullOrEmpty(queryString))
            {
                predefinedRoute += "?" + queryString;
            }

            return(predefinedRoute);
        }
コード例 #5
0
        internal static string GetQueryString(object request, IDictionary <string, RouteMember> propertyMap)
        {
            var result = new StringBuilder();

            foreach (var queryProperty in propertyMap)
            {
                var value = queryProperty.Value.GetValue(request, true);
                if (value == null)
                {
                    continue;
                }

                result.Append(queryProperty.Key)
                .Append('=')
                .Append(RestRoute.FormatQueryParameterValue(value))
                .Append('&');
            }

            if (result.Length > 0)
            {
                result.Length -= 1;
            }
            return(result.ToString());
        }
コード例 #6
0
 public static RouteResolutionResult Success(RestRoute route, string uri)
 {
     return(new RouteResolutionResult {
         Route = route, Uri = uri, Priority = route.Priority
     });
 }
コード例 #7
0
 public static RouteResolutionResult Error(RestRoute route, string errorMsg)
 {
     return(new RouteResolutionResult {
         Route = route, FailReason = errorMsg
     });
 }
コード例 #8
0
        public static string ToUrl(this object requestDto, string httpMethod = "GET", string formatFallbackToPredefinedRoute = null)
        {
            httpMethod = httpMethod.ToUpper();

            var requestType   = requestDto.GetType();
            var requestRoutes = routesCache.GetOrAdd(requestType, GetRoutesForType);

            if (requestRoutes.Count == 0)
            {
                if (formatFallbackToPredefinedRoute == null)
                {
                    throw new InvalidOperationException("There are no rest routes mapped for '{0}' type. ".Fmt(requestType)
                                                        + "(Note: The automatic route selection only works with [Route] attributes on the request DTO and "
                                                        + "not with routes registered in the IAppHost!)");
                }

                var predefinedRoute = "/{0}/reply/{1}".Fmt(formatFallbackToPredefinedRoute, requestType.GetOperationName());
                if (httpMethod == "GET" || httpMethod == "DELETE" || httpMethod == "OPTIONS" || httpMethod == "HEAD")
                {
                    var queryProperties = RestRoute.GetQueryProperties(requestDto.GetType());
                    predefinedRoute += "?" + RestRoute.GetQueryString(requestDto, queryProperties);
                }

                return(predefinedRoute);
            }

            var routesApplied  = requestRoutes.Select(route => route.Apply(requestDto, httpMethod)).ToList();
            var matchingRoutes = routesApplied.Where(x => x.Matches).ToList();

            if (matchingRoutes.Count == 0)
            {
                var errors = string.Join(String.Empty, routesApplied.Select(x => "\r\n\t{0}:\t{1}".Fmt(x.Route.Path, x.FailReason)).ToArray());
                var errMsg = "None of the given rest routes matches '{0}' request:{1}"
                             .Fmt(requestType.GetOperationName(), errors);

                throw new InvalidOperationException(errMsg);
            }

            RouteResolutionResult matchingRoute;

            if (matchingRoutes.Count > 1)
            {
                matchingRoute = FindMostSpecificRoute(matchingRoutes);
                if (matchingRoute == null)
                {
                    var errors = String.Join(String.Empty, matchingRoutes.Select(x => "\r\n\t" + x.Route.Path).ToArray());
                    var errMsg = "Ambiguous matching routes found for '{0}' request:{1}".Fmt(requestType.Name, errors);
                    throw new InvalidOperationException(errMsg);
                }
            }
            else
            {
                matchingRoute = matchingRoutes[0];
            }

            var url = matchingRoute.Uri;

            if (!httpMethod.HasRequestBody())
            {
                var queryParams = matchingRoute.Route.FormatQueryParameters(requestDto);
                if (!String.IsNullOrEmpty(queryParams))
                {
                    url += "?" + queryParams;
                }
            }

            return(url);
        }
コード例 #9
0
 public static RouteResolutionResult Success(RestRoute route, string uri)
 {
     return new RouteResolutionResult { Route = route, Uri = uri, Priority = route.Priority };
 }
コード例 #10
0
 public static RouteResolutionResult Error(RestRoute route, string errorMsg)
 {
     return new RouteResolutionResult { Route = route, FailReason = errorMsg };
 }
コード例 #11
0
        public static string ToUrl(this object requestDto, string httpMethod, Func <Type, string> fallback)
        {
            httpMethod = httpMethod.ToUpper();
            var urlFilter = requestDto as IUrlFilter;

            var requestType   = requestDto.GetType();
            var requestRoutes = routesCache.GetOrAdd(requestType, GetRoutesForType);

            if (requestRoutes.Count == 0)
            {
                if (fallback == null)
                {
                    throw new InvalidOperationException($"There are no rest routes mapped for '{requestType}' type. "
                                                        + "(Note: The automatic route selection only works with [Route] attributes on the request DTO and "
                                                        + "not with routes registered in the IAppHost!)");
                }

                var predefinedRoute = fallback(requestType);
                if (httpMethod is "GET" or "DELETE" or "OPTIONS" or "HEAD")
                {
                    var queryProperties = RestRoute.GetQueryProperties(requestDto.GetType());
                    if (queryProperties.Count > 0)
                    {
                        predefinedRoute += "?" + RestRoute.GetQueryString(requestDto, queryProperties);
                    }
                }

                return(urlFilter == null ? predefinedRoute : urlFilter.ToUrl(predefinedRoute));
            }

            var routesApplied  = requestRoutes.Select(route => route.Apply(requestDto, httpMethod)).ToList();
            var matchingRoutes = routesApplied.Where(x => x.Matches).ToList();

            if (matchingRoutes.Count == 0)
            {
                var errors = Join(Empty, routesApplied.Select(x =>
                                                              $"\r\n\t{x.Route.Path}:\t{x.FailReason}").ToArray());
                var errMsg = $"None of the given rest routes matches '{requestType.GetOperationName()}' request:{errors}";

                throw new InvalidOperationException(errMsg);
            }

            RouteResolutionResult matchingRoute;

            if (matchingRoutes.Count > 1)
            {
                matchingRoute = FindMostSpecificRoute(matchingRoutes);
                if (matchingRoute == null)
                {
                    var errors = Join(Empty, matchingRoutes.Select(x => "\r\n\t" + x.Route.Path).ToArray());
                    var errMsg = $"Ambiguous matching routes found for '{requestType.Name}' request:{errors}";
                    throw new InvalidOperationException(errMsg);
                }
            }
            else
            {
                matchingRoute = matchingRoutes[0];
            }

            var url = matchingRoute.Uri;

            if (!HttpUtils.HasRequestBody(httpMethod))
            {
                var queryParams = matchingRoute.Route.FormatQueryParameters(requestDto);
                if (!IsNullOrEmpty(queryParams))
                {
                    url += "?" + queryParams;
                }
            }

            return(urlFilter == null ? url : urlFilter.ToUrl(url));
        }