コード例 #1
0
 /// <summary>
 /// Initializes the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="traceId">The trace identifier.</param>
 /// <param name="entryStamp">The entry stamp.</param>
 internal static void Initialize(RuntimeContext context, string traceId, DateTime? entryStamp = null)
 {
     if (!string.IsNullOrWhiteSpace(traceId))
     {
         _root = _current = context.ToTraceLog(null, entryStamp);
         TraceId = traceId;
     }
 }
コード例 #2
0
        /// <summary>
        /// Processes the build in feature.
        /// </summary>
        /// <param name="runtimeContext">The runtime context.</param>
        /// <param name="isLocalhost">The is localhost.</param>
        /// <returns>System.Object.</returns>
        protected virtual object ProcessBuildInFeature(RuntimeContext runtimeContext, bool isLocalhost)
        {
            object result = null;

            switch (runtimeContext?.ResourceName.SafeToLower())
            {
                case "server":
                    result = Framework.AboutService();
                    break;
                case "machine":
                    result = SystemManagementExtension.GetMachineHealth();
                    break;
                default: break;
            }

            return result;
        }
コード例 #3
0
 /// <summary>
 /// Enters the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="entryStamp">The entry stamp.</param>
 internal static void Enter(RuntimeContext context, DateTime? entryStamp = null)
 {
     Enter(context.ToTraceLog(_current, entryStamp ?? DateTime.UtcNow));
 }
コード例 #4
0
        /// <summary>
        /// Processes the built in feature.
        /// </summary>
        /// <param name="httpRequest">The HTTP request.</param>
        /// <param name="runtimeContext">The runtime context.</param>
        /// <param name="isLocalhost">The is localhost.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns>
        /// System.Object.
        /// </returns>
        protected virtual object ProcessBuiltInFeature(HttpRequest httpRequest, RuntimeContext runtimeContext, bool isLocalhost, out string contentType)
        {
            object result = null;
            contentType = HttpConstants.ContentType.Json;

            switch (runtimeContext?.ResourceName.SafeToLower())
            {
                case "server":
                    result = Framework.AboutService();
                    break;
                case "machine":
                    result = SystemManagementExtension.GetMachineHealth();
                    break;
                case "cache":
                    result = CacheRealm.GetSummary();
                    break;
                case "clearcache":
                    result = isLocalhost ? CacheRealm.ClearAll() : "This API is available at localhost machine." as object;
                    break;
                case "i18n":
                    result = GlobalCultureResourceCollection.Instance?.AvailableCultureInfo ?? new Collection<CultureInfo>();
                    break;
                case "mirror":
                    var apiContext = ContextHelper.ApiContext;
                    var headers = new Dictionary<string, string>();

                    foreach (var key in httpRequest.Headers.AllKeys)
                    {
                        headers.Add(key, httpRequest.Headers[key]);
                    }

                    result = new
                    {
                        RawUrl = httpRequest.RawUrl,
                        HttpMethod = httpRequest.HttpMethod,
                        Headers = headers,
                        UserAgent = apiContext.UserAgent,
                        IpAddress = apiContext.IpAddress,
                        CultureCode = apiContext.CultureCode
                    };
                    break;
                case "assemblyhash":
                    result = EnvironmentCore.GetAssemblyHash();
                    break;
                case "dll":
                    var dllName = httpRequest.QueryString.Get("name");
                    if (!string.IsNullOrWhiteSpace(dllName) && httpRequest.HttpMethod.MeaningfulEquals(HttpConstants.HttpMethod.Post, StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            var dllPath = Path.Combine(EnvironmentCore.ApplicationBaseDirectory, dllName + ".dll");
                            if (File.Exists(dllPath))
                            {
                                result = File.ReadAllBytes(dllPath);
                                contentType = HttpConstants.ContentType.BinaryDefault;
                            }
                        }
                        catch { }
                    }
                    break;
                default:
                    break;
            }

            return result;
        }
コード例 #5
0
 /// <summary>
 /// Initializes the context.
 /// <remarks>
 /// This method would be called after <c>ProcessRoute</c> and before <c>Invoke</c>. It can be used to help you to do some context initialization, such as get something from database for later actions.
 /// ou can save them in Thread data so that you can get them later in <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>
 /// <param name="runtimeContext">The runtime context.</param>
 protected virtual void InitializeContext(HttpRequest request, RuntimeContext runtimeContext)
 {
     //Do nothing here.
 }
コード例 #6
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;
        }
コード例 #7
0
        /// <summary>
        /// Processes the build in feature.
        /// </summary>
        /// <param name="runtimeContext">The runtime context.</param>
        /// <param name="isLocalhost">The is localhost.</param>
        /// <returns>System.Object.</returns>
        protected override object ProcessBuildInFeature(RuntimeContext runtimeContext, bool isLocalhost)
        {
            object result = null;

            switch (runtimeContext?.ResourceName.SafeToLower())
            {
                case "apilist":
                    result = routes.Keys.Select(x => x.TrimEnd('/') + "/").ToList();
                    break;
                case "configuration":
                    if (isLocalhost)
                    {
                        result = Framework.configurationReader.GetValues();
                    }
                    else
                    {
                        result = "This API is available at localhost machine.";
                    }
                    break;
                default: break;
            }

            return result ?? base.ProcessBuildInFeature(runtimeContext, isLocalhost);
        }
コード例 #8
0
        /// <summary>
        /// Processes the build in feature.
        /// </summary>
        /// <param name="httpRequest">The HTTP request.</param>
        /// <param name="runtimeContext">The runtime context.</param>
        /// <param name="isLocalhost">The is localhost.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns>
        /// System.Object.
        /// </returns>
        protected override object ProcessBuiltInFeature(HttpRequest httpRequest, RuntimeContext runtimeContext, bool isLocalhost, out string contentType)
        {
            object result = null;
            contentType = HttpConstants.ContentType.Json;

            switch (runtimeContext?.ResourceName.SafeToLower())
            {
                case "apilist":
                    result = routes.Select(x => new { Url = x.Key.TrimEnd('/') + "/", Method = x.Value.MethodInfo?.Name }).ToList();
                    break;
                case "configuration":
                    result = isLocalhost ? Framework.ConfigurationReader.GetValues() : "This API is available at localhost machine." as object;
                    break;
                case "license":
                    if (isLocalhost)
                    {
                        var licenseDictionary = new Dictionary<string, object>();
                        foreach (var one in BeyovaLicenseContainer.Licenses)
                        {
                            licenseDictionary.Add(one.Key, new
                            {
                                one.Value.LicensePath,
                                one.Value.Entry
                            });
                        }
                    }
                    else
                    {
                        result = "This API is available at localhost machine.";
                    }
                    break;
                case "doc":
                    DocumentGenerator generator = new DocumentGenerator(DefaultSettings.TokenHeaderKey.SafeToString(HttpConstants.HttpHeader.TOKEN));
                    result = generator.WriteHtmlDocumentToZip((from item in routes select item.Value.InstanceType).Distinct().ToArray());
                    contentType = HttpConstants.ContentType.ZipFile;
                    break;
                default: break;
            }

            return result ?? base.ProcessBuiltInFeature(httpRequest, runtimeContext, isLocalhost, out contentType);
        }
コード例 #9
0
 /// <summary>
 /// Enters the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="entryStamp">The entry stamp.</param>
 /// <param name="setNameAsMajor">The set name as major.</param>
 internal static void Enter(RuntimeContext context, DateTime? entryStamp = null, bool setNameAsMajor = false)
 {
     Enter(context.ToTraceLog(_current, entryStamp ?? DateTime.UtcNow), setNameAsMajor);
 }
コード例 #10
0
        /// <summary>
        /// Fills the route information.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if succeed to match url format and fill route information, <c>false</c> otherwise.</returns>
        internal static bool FillRouteInfo(this HttpRequest request, RuntimeContext context)
        {
            try
            {
                request.CheckNullObject("request");
                context.CheckNullObject("context");

                var match = methodMatch.Match(request.Url.PathAndQuery);

                context.ResourceName = match.Success ? match.Result("${resource}") : string.Empty;
                context.Version = match.Success ? match.Result("${version}") : string.Empty;
                context.Parameter1 = match.Success ? match.Result("${parameter1}") : string.Empty;
                context.Parameter2 = match.Success ? match.Result("${parameter2}") : string.Empty;

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