/// <summary>
        /// Initializes a new instance of the <see cref="RuntimeRoute" /> class.
        /// </summary>
        /// <param name="routeIdentifier">The route key.</param>
        /// <param name="methodInfo">The method information.</param>
        /// <param name="instanceType">Type of the instance.</param>
        /// <param name="instance">The instance.</param>
        /// <param name="isActionUsed">if set to <c>true</c> [is action used].</param>
        /// <param name="isTokenRequired">if set to <c>true</c> [is token required].</param>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="setting">The setting.</param>
        /// <param name="apiCacheAttribute">The API cache attribute.</param>
        /// <param name="omitApiTracking">The omit API tracking.</param>
        /// <param name="permissions">The permissions.</param>
        /// <param name="headerKeys">The header keys.</param>
        public RuntimeRoute(ApiRouteIdentifier routeIdentifier, MethodInfo methodInfo, Type instanceType, object instance, bool isActionUsed, bool isTokenRequired, string moduleName, string contentType, RestApiSettings setting, ApiCacheAttribute apiCacheAttribute, OmitApiTrackingAttribute omitApiTracking, IDictionary <string, ApiPermissionAttribute> permissions = null, List <string> headerKeys = null) : this()
        {
            ApiMethod       = methodInfo;
            ApiInstance     = instance;
            IsActionUsed    = isActionUsed;
            InstanceType    = instanceType;
            Setting         = setting;
            OmitApiTracking = omitApiTracking;

            OperationParameters = new RuntimeApiOperationParameters
            {
                ContentType          = contentType,
                IsTokenRequired      = isTokenRequired,
                CustomizedHeaderKeys = headerKeys,
                Permissions          = permissions,
                ModuleName           = moduleName
            };

            ApiRouteIdentifier = routeIdentifier;
            IsVoid             = ApiMethod?.ReturnType?.IsVoid();

            if (apiCacheAttribute != null)
            {
                ApiCacheAttribute = apiCacheAttribute;
                ApiCacheContainer = apiCacheAttribute.CacheContainer ?? new ApiCacheContainer(routeIdentifier.ToString(), apiCacheAttribute.CacheParameter);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes the type of the API.
        /// </summary>
        /// <param name="doneInterfaceTypes">The done interface types.</param>
        /// <param name="routes">The routes.</param>
        /// <param name="interfaceType">Type of the interface.</param>
        /// <param name="instance">The instance.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="parentApiContractAttribute">The parent API class attribute.</param>
        /// <param name="parentApiModuleAttribute">The parent API module attribute.</param>
        /// <param name="omitApiTrackingAttribute">The omit API tracking attribute.</param>
        /// <param name="parentTokenRequiredAttribute">The parent token required attribute.</param>
        /// <exception cref="DataConflictException">routeKey</exception>
        private static void InitializeApiType(List <string> doneInterfaceTypes, Dictionary <ApiRouteIdentifier, RuntimeRoute> routes, Type interfaceType, object instance, RestApiSettings settings = null, ApiContractAttribute parentApiContractAttribute = null, ApiModuleAttribute parentApiModuleAttribute = null, OmitApiTrackingAttribute omitApiTrackingAttribute = null, TokenRequiredAttribute parentTokenRequiredAttribute = null)
        {
            if (routes != null && interfaceType != null && doneInterfaceTypes != null)
            {
                if (doneInterfaceTypes.Contains(interfaceType.FullName))
                {
                    return;
                }

                var apiContract            = parentApiContractAttribute ?? interfaceType.GetCustomAttribute <ApiContractAttribute>(true);
                var omitApiTracking        = omitApiTrackingAttribute ?? interfaceType.GetCustomAttribute <OmitApiTrackingAttribute>(true);
                var apiModule              = parentApiModuleAttribute ?? interfaceType.GetCustomAttribute <ApiModuleAttribute>(true);
                var tokenRequiredAttribute = parentTokenRequiredAttribute ?? interfaceType.GetCustomAttribute <TokenRequiredAttribute>(true);
                var moduleName             = apiModule?.ToString();

                if (apiContract != null && !string.IsNullOrWhiteSpace(apiContract.Version))
                {
                    if (apiContract.Version.SafeEquals(ApiConstants.BuiltInFeatureVersionKeyword, StringComparison.OrdinalIgnoreCase))
                    {
                        throw ExceptionFactory.CreateInvalidObjectException(nameof(apiContract.Version), reason: "<builtin> cannot be used as version due to it is used internally.");
                    }

                    foreach (var method in interfaceType.GetMethods())
                    {
                        var apiOperationAttribute = method.GetCustomAttribute <ApiOperationAttribute>(true);

                        #region Initialize based on ApiOperation

                        if (apiOperationAttribute != null)
                        {
                            var permissions          = new Dictionary <string, ApiPermissionAttribute>();
                            var additionalHeaderKeys = new HashSet <string>();

                            var apiPermissionAttributes =
                                method.GetCustomAttributes <ApiPermissionAttribute>(true);

                            var apiCacheAttribute = method.GetCustomAttribute <ApiCacheAttribute>(true);

                            if (apiPermissionAttributes != null)
                            {
                                foreach (var one in apiPermissionAttributes)
                                {
                                    permissions.Merge(one.PermissionIdentifier, one);
                                }
                            }

                            var headerKeyAttributes = method.GetCustomAttributes <ApiHeaderAttribute>(true);
                            if (headerKeyAttributes != null)
                            {
                                foreach (var one in headerKeyAttributes)
                                {
                                    additionalHeaderKeys.Add(one.HeaderKey);
                                }
                            }

                            var routeKey = ApiRouteIdentifier.FromApiObjects(apiContract, apiOperationAttribute);

                            var tokenRequired =
                                method.GetCustomAttribute <TokenRequiredAttribute>(true) ??
                                tokenRequiredAttribute;

                            // If method can not support API cache, consider as no api cache.
                            if (apiCacheAttribute != null && (!apiOperationAttribute.HttpMethod.Equals(HttpConstants.HttpMethod.Get, StringComparison.OrdinalIgnoreCase) || !apiCacheAttribute.InitializeParameterNames(method)))
                            {
                                apiCacheAttribute = null;
                            }

                            var runtimeRoute = new RuntimeRoute(routeKey, method, interfaceType, instance,
                                                                !string.IsNullOrWhiteSpace(apiOperationAttribute.Action),
                                                                tokenRequired != null && tokenRequired.TokenRequired, moduleName, apiOperationAttribute.ContentType, settings, apiCacheAttribute, omitApiTracking ?? method.GetCustomAttribute <OmitApiTrackingAttribute>(true), permissions, additionalHeaderKeys.ToList());

                            if (routes.ContainsKey(routeKey))
                            {
                                throw new DataConflictException(nameof(routeKey), objectIdentity: routeKey?.ToString(), data: new
                                {
                                    existed      = routes[routeKey].SafeToString(),
                                    newMethod    = method.GetFullName(),
                                    newInterface = interfaceType.FullName
                                });
                            }

                            // EntitySynchronizationModeAttribute
                            var entitySynchronizationModeAttribute = method.GetCustomAttribute <EntitySynchronizationModeAttribute>(true);
                            if (entitySynchronizationModeAttribute != null)
                            {
                                if (EntitySynchronizationModeAttribute.IsReturnTypeMatched(method.ReturnType))
                                {
                                    runtimeRoute.OperationParameters.EntitySynchronizationMode = entitySynchronizationModeAttribute;
                                }
                            }

                            routes.Add(routeKey, runtimeRoute);
                        }

                        #endregion Initialize based on ApiOperation
                    }

                    foreach (var one in interfaceType.GetInterfaces())
                    {
                        InitializeApiType(doneInterfaceTypes, routes, one, instance, settings, apiContract, apiModule, omitApiTracking, tokenRequiredAttribute);
                    }

                    //Special NOTE:
                    // Move this add action in scope of if apiContract is valid.
                    // Reason: in complicated cases, when [A:Interface1] without ApiContract, but [Interface2: Interface] with defining ApiContract, and [B: A, Interface2], then correct contract definition might be missed.
                    doneInterfaceTypes.Add(interfaceType.FullName);
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Gets the option.
 /// </summary>
 /// <param name="routeIdentifier">The route identifier.</param>
 /// <param name="omitParameters">if set to <c>true</c> [omit parameters].</param>
 /// <returns></returns>
 public ApiEventTrackingOption GetOption(ApiRouteIdentifier routeIdentifier, bool omitParameters = false)
 {
     return(_trackingOption);
 }
        /// <summary>
        /// Processes the route.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="doAuthentication">if set to <c>true</c> [do authentication].</param>
        /// <returns></returns>
        /// <exception cref="ResourceNotFoundException">
        /// ResourceName
        /// or
        /// or
        /// </exception>
        internal virtual RuntimeContext ProcessRoute(HttpApiContextContainer <TRequest, TResponse> context, bool doAuthentication = true)
        {
            try
            {
                context.CheckNullObject(nameof(context));
                var httpMethod = context.HttpMethod;
                var uri        = context.Url;

                var rawFullUrl = string.Format("{0}: {1}", httpMethod, context.Url);
                var result     = InitializeRuntimeContext(uri);

                result.CheckNullObjectAsInvalid("URL");

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

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

                RuntimeRoute runtimeRoute;
                var          apiRouterIdentifier = new ApiRouteIdentifier(result.Realm, result.Version, result.ResourceName, httpMethod, result.Parameter1);
                if (!RestApiRoutePool.Routes.TryGetValue(apiRouterIdentifier, out runtimeRoute))
                {
                    apiRouterIdentifier = new ApiRouteIdentifier(result.Realm, result.Version, result.ResourceName, httpMethod, null);
                    RestApiRoutePool.Routes.TryGetValue(apiRouterIdentifier, out runtimeRoute);
                }
                else
                {
                    if (runtimeRoute != null && (!string.IsNullOrWhiteSpace(result.Parameter1) && !runtimeRoute.IsActionUsed))
                    {
                        throw new ResourceNotFoundException(rawFullUrl);
                    }
                }

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

                result.ApiRouterIdentifier = apiRouterIdentifier;

                // Override out parameters
                result.OperationParameters = runtimeRoute.OperationParameters ?? new RuntimeApiOperationParameters();

                result.ApiMethod    = runtimeRoute.ApiMethod;
                result.ApiInstance  = runtimeRoute.ApiInstance;
                result.IsActionUsed = runtimeRoute.IsActionUsed;
                result.IsVoid       = runtimeRoute.IsVoid;
                context.Settings    = result.Settings = runtimeRoute.Setting ?? RestApiSettingPool.DefaultRestApiSettings;
                result.OmitApiEvent = runtimeRoute.OmitApiTracking?.Omit(ApiTrackingType.Event) ?? false;

                if (runtimeRoute.ApiCacheAttribute != null)
                {
                    result.ApiCacheIdentity = runtimeRoute.ApiRouteIdentifier.Clone() as ApiRouteIdentifier;
                    if (runtimeRoute.ApiCacheAttribute.CacheParameter.CachedByParameterizedIdentity)
                    {
                        result.ApiCacheIdentity.SetParameterizedIdentifier(uri.ToQueryString());
                    }

                    result.ApiCacheContainer = runtimeRoute.ApiCacheContainer;

                    if (result.ApiCacheContainer != null)
                    {
                        string cachedResponseBody;
                        if (result.ApiCacheContainer.GetCacheResult(result.ApiCacheIdentity, out cachedResponseBody))
                        {
                            result.CachedResponseBody = cachedResponseBody;
                            result.ApiCacheStatus     = ApiCacheStatus.UseCache;
                        }
                        else
                        {
                            result.ApiCacheStatus = ApiCacheStatus.UpdateCache;
                        }
                    }
                    else
                    {
                        result.ApiCacheStatus = ApiCacheStatus.NoCache;
                    }
                }

                // Fill basic context info.
                var userAgentHeaderKey = context.Settings?.OriginalUserAgentHeaderKey;
                var token = context.TryGetRequestHeader((context.Settings?.TokenHeaderKey).SafeToString(HttpConstants.HttpHeader.TOKEN));
                ContextHelper.ApiContext.IndicatedOperator = context.TryGetRequestHeader(HttpConstants.HttpHeader.OPERATOR);

                ContextHelper.ConsistContext(
                    // TOKEN
                    token,
                    // Settings
                    context.Settings,
                    // IP Address
                    context.TryGetRequestHeader(context.Settings?.OriginalIpAddressHeaderKey.SafeToString(HttpConstants.HttpHeader.ORIGINAL)).SafeToString(context.ClientIpAddress),
                    // User Agent
                    string.IsNullOrWhiteSpace(userAgentHeaderKey) ? context.UserAgent : context.TryGetRequestHeader(userAgentHeaderKey).SafeToString(context.UserAgent),
                    // Culture Code
                    context.QueryString.Get(HttpConstants.QueryString.Language).SafeToString(context.UserLanguages.SafeFirstOrDefault()).EnsureCultureCode(),
                    // Current Uri
                    context.Url,
                    HttpExtension.GetBasicAuthentication(context.TryGetRequestHeader(HttpConstants.HttpHeader.Authorization).DecodeBase64()),
                    apiRouterIdentifier.ToApiUniqueIdentifier()
                    );

                string userIdentifier = ContextHelper.ApiContext.CurrentCredential?.Name.SafeToString(token);

                var authenticationException = doAuthentication ? Authorize(runtimeRoute, ContextHelper.ApiContext) : null;

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

                return(result);
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }