Esempio n. 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;
        }
        /// <summary>
        /// Prepares the specified request.
        /// <remarks>
        /// This method would be called before <c>ProcessRoute</c>. It can be used to help you to do some preparation, such as get something from headers or cookie for later actions.
        /// ou can save them in Thread data so that you can get them later in <c>ProcessRoute</c>, <c>Invoke</c>, <c>PackageOutput</c> ,etc.
        /// If any exception is throw from this method, the process flow would be interrupted.
        /// </remarks>
        /// </summary>
        /// <param name="request">The request.</param>
        protected override void Prepare(HttpRequest request)
        {
            base.Prepare(request);

            var rsaPublicKey = request.TryGetHeader(HttpConstants.HttpHeader.SECUREKEY);
            if (string.IsNullOrWhiteSpace(rsaPublicKey))
            {
                throw ExceptionFactory.CreateUnauthorizedTokenException(rsaPublicKey);
            }

            var privateKey = GetPrivateKeyByPublicKey(rsaPublicKey);
            if (string.IsNullOrWhiteSpace(privateKey))
            {
                throw ExceptionFactory.CreateUnauthorizedTokenException(rsaPublicKey);
            }

            var currentGravityContext = GravityContext.Current;
            currentGravityContext.PublicKey = rsaPublicKey;
            currentGravityContext.PrivateKey = privateKey;
        }