예제 #1
0
        /// <summary>
        /// Processes the route.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Exception.</returns>
        protected override RuntimeContext ProcessRoute(HttpRequest request)
        {
            var result = new RuntimeContext();
            var rawUrl = request.RawUrl;
            var rawFullUrl = request.ToFullRawUrl();

            if (!request.FillRouteInfo(result))
            {
                throw new InvalidObjectException("URL");
            }

            if (result.Version.Equals(BuildInFeatureVersionKeyword, StringComparison.OrdinalIgnoreCase))
            {
                return result;
            }

            if (string.IsNullOrWhiteSpace(result.ResourceName))
            {
                throw new ResourceNotFoundException(rawFullUrl, "resourceName");
            }

            RuntimeRoute runtimeRoute;

            if (!routes.TryGetValue(GetRouteKey(result.Version, result.ResourceName, request.HttpMethod, result.Parameter1), out runtimeRoute))
            {
                routes.TryGetValue(GetRouteKey(result.Version, result.ResourceName, request.HttpMethod, null), out runtimeRoute);
            }
            else
            {
                if (runtimeRoute != null && (!string.IsNullOrWhiteSpace(result.Parameter1) && !runtimeRoute.IsActionUsed))
                {
                    throw new ResourceNotFoundException(rawFullUrl);
                }
            }

            if (runtimeRoute == null)
            {
                throw new ResourceNotFoundException(rawFullUrl);
            }

            // Override out parameters
            result.ApiMethod = runtimeRoute.MethodInfo;
            result.ApiInstance = runtimeRoute.Instance;
            result.ApiServiceName = runtimeRoute.ServiceName;
            result.IsActionUsed = runtimeRoute.IsActionUsed;
            result.ModuleName = runtimeRoute.ModuleName;
            result.ApiTransportAttribute = runtimeRoute.Transport;
            result.IsVoid = runtimeRoute.MethodInfo.ReturnType.IsVoid();
            result.Settings = runtimeRoute.Setting;
            result.CustomizedHeaderKeys = runtimeRoute.HeaderKeys;

            var tokenHeaderKey = (result.Settings ?? DefaultSettings)?.TokenHeaderKey;
            var token = (request != null && !string.IsNullOrWhiteSpace(tokenHeaderKey)) ? request.TryGetHeader(tokenHeaderKey) : string.Empty;

            string userIdentifier = ContextHelper.ApiContext.Token = token;

            if (runtimeRoute.Transport == null)
            {
                var authenticationException = Authenticate(runtimeRoute, token, out userIdentifier);

                if (authenticationException != null)
                {
                    throw authenticationException.Handle("ProcessRoute", new { result.ApiMethod.Name, token });
                }
            }

            result.UserIdentifier = userIdentifier;
            return result;
        }