示例#1
0
        public static ParamContainer CreateParamContainer(ILogger loggerController, IActionExecuter actionExecuter)
        {
            ILogReader         logReader         = Logger.GetLogReader();
            IAppSettingService appSettingService = AppSettingService.Instance;
            ParamContainer     paramContainer    = new ParamContainer();

            IDBService       dbService       = ApplicationConfig.DependencyResolver.GetInstance <IDBService>();
            IPingService     pingService     = ApplicationConfig.DependencyResolver.GetInstance <IPingService>();
            IViewEngine      viewEngine      = ApplicationConfig.DependencyResolver.GetInstance <IViewEngine>();
            IEncryption      encryption      = ApplicationConfig.DependencyResolver.GetInstance <IEncryption>();
            IKeyValueStorage keyValueStorage = ApplicationConfig.DependencyResolver.GetInstance <IKeyValueStorage>();

            ISMSService   smsService   = new SMSService(loggerController, dbService, actionExecuter, viewEngine, paramContainer);
            IEmailService emailService = new EmailService(loggerController, dbService, actionExecuter, viewEngine, paramContainer);
            IOTPService   otpService   = new OTPService(loggerController, dbService, smsService, emailService, appSettingService);

            paramContainer.AddKey(CommonConst.CommonValue.PARAM_DBPROXY, () => { return(dbService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_LOGGER, () => { return(loggerController); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_ACTIONEXECUTER, () => { return(actionExecuter); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_PING_SERVICE, () => { return(pingService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_PING_SERVICE, () => { return(pingService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_APP_SETTING, () => { return(appSettingService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_VIEW_ENGINE, () => { return(viewEngine); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_OTP_SERVICE, () => { return(otpService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_SMS_SERVICE, () => { return(smsService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_EMAIL_SERVICE, () => { return(emailService); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_ENCRYPTION_SERVICE, () => { return(encryption); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_KEY_VALUE_STORAGE, () => { return(keyValueStorage); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_HTTP_CLIENT, () => { return(new HttpRestClient()); });

            return(paramContainer);
        }
示例#2
0
 public WwwrootContentHandler(IHttpContextProxy httpProxy, IDBService dbProxy, IViewEngine viewEngine, IActionExecuter actionExecuter, ILogger logger)
 {
     _logger         = logger;
     _dbProxy        = dbProxy;
     _httpProxy      = httpProxy;
     _viewEngine     = viewEngine;
     _actionExecuter = actionExecuter;
 }
示例#3
0
        private static void ExecEvent(IActionExecuter actionExecuter, RoutingModel route, ParamContainer paramContainer, Models.ExecutionEventType eventType)
        {
            IDBService dbProxy = paramContainer.GetKey(CommonConst.CommonValue.PARAM_DBPROXY);
            ILogger    logger  = paramContainer.GetKey(CommonConst.CommonValue.PARAM_LOGGER);

            foreach (var eventSubscriber in EventSubscription.GetInstance(dbProxy, logger).GetSubscriptions(route.GetEventName(), eventType))
            {
                actionExecuter.Exec(eventSubscriber.ExecultAssembly, eventSubscriber.ExecuteType, eventSubscriber.ExecuteMethod, paramContainer);
            }
        }
 public StaticContentHandler(IDBService dbService, ILogger logger, IActionExecuter actionExecuter, IHttpContextProxy httpProxy, ISessionProvider sessionProvider, IViewEngine viewEngine, IKeyValueStorage keyValueStorage)
 {
     _dbService       = dbService;
     _logger          = logger;
     _actionExecuter  = actionExecuter;
     _viewEngine      = viewEngine;
     _keyValueStorage = keyValueStorage;
     _httpProxy       = httpProxy;
     _sessionProvider = sessionProvider;
 }
示例#5
0
        private ParamContainer CreateParamContainer(ILogger loggerController, IActionExecuter actionExecuter)
        {
            ParamContainer  paramContainer  = ActionExecuterHelper.CreateParamContainer(_logger, actionExecuter);
            IRoutings       routings        = Routings.Routings.GetRoutings();
            ILogReader      logReader       = Logger.GetLogReader();
            ResponseBuilder responseBuilder = new ResponseBuilder(loggerController, logReader, new CronJobInitData(loggerController.TransactionId));

            paramContainer.AddKey(CommonConst.CommonValue.PARAM_RESPONBUILDER, () => { return(responseBuilder); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_ROUTING_OBJECT, () => { return(routings); });
            return(paramContainer);
        }
示例#6
0
 public EmailService(ILogger logger,
                     IDBService dbService,
                     IActionExecuter actionExecuter,
                     IViewEngine viewEngine,
                     ParamContainer paramContainer)
 {
     _logger         = logger;
     _actionExecuter = actionExecuter;
     _dbService      = dbService;
     _viewEngine     = viewEngine;
     _paramContainer = paramContainer;
 }
示例#7
0
        public virtual void ProcessRequest(HttpContext context)
        {
            var dbProxy    = new MongoDBService();
            var encryption = new EncryptionService();

            _httpProxy      = new HttpContextProxy(context);
            _initData       = _httpProxy;
            _logger         = Logger.GetLogger(this.GetType().Name, _httpProxy.TransactionId, dbProxy);
            _actionExecuter = new ActionExecuter(_logger);
            CreateRoute();
            _httpContext    = context;
            _contentHandler = new WwwrootContentHandler(_httpProxy, dbProxy, _viewEngine, _actionExecuter, _logger);
            HandleAuthTokenKey(dbProxy, encryption);
            HandleSession(context);
        }
示例#8
0
        /// <summary>
        /// 请求的数据处理
        /// </summary>
        /// <param name="userToken">用户连接对象</param>
        /// <param name="data">请求数据及响应数据</param>
        /// <param name="actionExecuter"></param>
        /// <returns>是否有数据响应 true代表有相应数据并由data传出 false代表没有相应</returns>
        public bool DataHandle(AsyncUserToken userToken, ref byte[] data, IActionExecuter actionExecuter)
        {
            Request request = Request.Parser.ParseFrom(data);

            ProtobufExecuteContext executeContext = new ProtobufExecuteContext();

            executeContext.ControllerName = request.Controller;
            executeContext.ActionName     = request.Action;
            executeContext.UserToken      = userToken;

            #region 子参数解析
            if (request.SubRequest != null)
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                var      type     = assembly.GetType(Any.GetTypeName(request.SubRequest.TypeUrl));
                if (type != null /*&& request.SubRequest.Is((type as IMessage).Descriptor)*/)
                {
                    var      uppackMethod = request.SubRequest.GetType().GetMethod(nameof(request.SubRequest.Unpack)).MakeGenericMethod(type);
                    IMessage subMessagae  = uppackMethod.Invoke(request.SubRequest, null) as IMessage;
                    executeContext.SubMessage = subMessagae;
                }
            }
            #endregion

            var has = actionExecuter.TryExecuteAction(executeContext, out object obj);
            if (has)
            {
                if (obj == null)
                {
                    return(false);
                }
            }
            else
            {
                //没有找到action
                obj = "无效的请求";
            }

            var respone = MakeRespone(request, obj);
            data = respone.ToByteArray();
            return(true);
        }
示例#9
0
        private static Dictionary <string, dynamic> SetDefaultModel(IDBService dbProxy, IHttpContextProxy httpProxy, ILogger logger, IViewEngine viewEngine, IActionExecuter actionExecuter, Dictionary <string, dynamic> model, string folderPath = null)
        {
            ISessionProvider sessionProvider = new SessionProvider(httpProxy, dbProxy, logger);

            if (model == null)
            {
                model = new Dictionary <string, dynamic>();
            }
            model[CommonConst.CommonValue.METHODS] = new Dictionary <string, dynamic>();

            Func <string, string, JArray> getData =
                (string collection, string filter) =>
            {
                return(dbProxy.Get(collection, filter));
            };
            Func <string, string> getAppSetting =
                (string key) =>
            {
                var response = AppSettingService.Instance.GetAppSettingData(key);
                if (string.IsNullOrEmpty(response))
                {
                    response = ConfigurationManager.AppSettings[key];
                }
                return(response);
            };
            Func <string, JObject> getSessionValue =
                (string key) =>
            {
                return(sessionProvider.GetValue <JObject>(key));
            };
            Func <string, string> includeTemplete = (string templatePath) =>
            {
                FileInfo fi   = new FileInfo(string.Format("c:\\{0}{1}", folderPath, templatePath));
                string   path = fi.FullName.Replace("c:", "");
                model[CommonConst.CommonValue.PAGE_TEMPLATE_PATH] = path;
                return(string.Empty);
            };
            Func <string, bool> authorized = (string authGroups) =>
            {
                var sessionUser = sessionProvider.GetValue <UserModel>(CommonConst.CommonValue.SESSION_USER_KEY);
                if (sessionUser == null)
                {
                    return(false);
                }

                if (!authGroups.Split(',').Where(i => sessionUser.groups.Contains(i)).Any())
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            };
            Func <string, JObject, JObject> ActionExecute =
                (string actionPath, JObject data) =>
            {
                var param = ActionExecuterHelper.CreateParamContainer(null, httpProxy, logger, actionExecuter);

                if (data != null)
                {
                    foreach (var item in data)
                    {
                        Func <dynamic> funcValue = () => { return(item.Value); };
                        param.AddKey(item.Key, funcValue);
                    }
                }
                return(actionExecuter.Exec <JObject>(actionPath, dbProxy, param));
            };

            Func <string, JObject, Dictionary <string, dynamic> > IncludeModel =
                (string includeModelPath, JObject data) =>
            {
                try
                {
                    var param = ActionExecuterHelper.CreateParamContainer(null, httpProxy, logger, actionExecuter);

                    Dictionary <string, dynamic> modelData = new Dictionary <string, dynamic>();

                    if (data != null)
                    {
                        foreach (var item in data)
                        {
                            Func <dynamic> funcValue = () => { return(item.Value); };
                            param.AddKey(item.Key, funcValue);
                        }
                    }

                    object response = actionExecuter.Exec(includeModelPath, dbProxy, param);
                    if (response is Dictionary <string, dynamic> )
                    {
                        return(response as Dictionary <string, dynamic>);
                    }
                    else
                    {
                        throw new InvalidCastException(string.Format("Invalid respone from {0}", includeModelPath));
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    logger.Error(string.Format("Error While executing Route : {0}, Error : {1}", includeModelPath, ex.Message), ex);
                    throw;
                }
            };

            model[CommonConst.CommonValue.METHODS]["IncludeModel"] = IncludeModel;

            model[CommonConst.CommonValue.METHODS]["ExecuteAction"] = ActionExecute;

            model[CommonConst.CommonValue.METHODS]["InclueTemplate"] = includeTemplete;

            model[CommonConst.CommonValue.METHODS]["GetData"] = getData;

            Func <JObject> requestBody = () => httpProxy.GetRequestBody <JObject>();

            model[CommonConst.CommonValue.METHODS]["RequestBody"] = requestBody;

            Func <string, string> queryString = (string key) => httpProxy.GetQueryString(key);

            model[CommonConst.CommonValue.METHODS]["QueryString"] = queryString;

            model[CommonConst.CommonValue.METHODS]["AppSetting"] = getAppSetting;

            model[CommonConst.CommonValue.METHODS]["GetSessionData"] = getSessionValue;

            model[CommonConst.CommonValue.METHODS]["Authorized"] = authorized;

            Func <string, JObject, string> includeBlock =
                (string blockPath, JObject blockModel) =>
            {
                var inputBlockModel = new Dictionary <string, dynamic>();
                if (blockModel != null)
                {
                    foreach (var item in blockModel)
                    {
                        inputBlockModel[item.Key] = item.Value;
                    }
                }
                if (model != null)
                {
                    foreach (var item in model)
                    {
                        inputBlockModel[item.Key] = item.Value;
                    }
                }
                FileInfo fi   = new FileInfo(string.Format("c:\\{0}{1}", folderPath, blockPath));
                string   path = fi.FullName.Replace("c:", "");
                var      data = StaticContentHandler.GetStringContent(dbProxy, logger, path);
                data = viewEngine.Compile(data, path, ServerPageModelHelper.SetDefaultModel(dbProxy, httpProxy, logger, viewEngine, actionExecuter, inputBlockModel, path.Replace(fi.Name, "")));
                return(data);
            };

            model[CommonConst.CommonValue.METHODS]["Include"] = includeBlock;

            Func <string> randerBody = () =>
            {
                if (model.ContainsKey(CommonConst.CommonValue.RENDERBODY_DATA))
                {
                    return(model[CommonConst.CommonValue.RENDERBODY_DATA]);
                }
                else
                {
                    return(string.Empty);
                }
            };

            model[CommonConst.CommonValue.METHODS]["RenderBody"] = randerBody;

            return(model);
        }
示例#10
0
        public static string ServerSidePageHandler(string requestUriPath, IDBService dbProxy, IHttpContextProxy httpProxy, IViewEngine viewEngine, IActionExecuter actionExecuter, ILogger logger, Dictionary <string, dynamic> pageModel = null)
        {
            var fi   = new FileInfo(requestUriPath);
            var data = StaticContentHandler.GetStringContent(dbProxy, logger, requestUriPath);

            if (data != null)
            {
                if (pageModel == null)
                {
                    pageModel = new Dictionary <string, dynamic>();
                }
                var folderPath = requestUriPath.Replace(fi.Name, "");

                UpdateBaseModel(pageModel, requestUriPath, fi.Name);

                data = viewEngine.Compile(data, requestUriPath, ServerPageModelHelper.SetDefaultModel(dbProxy, httpProxy, logger, viewEngine, actionExecuter, pageModel, folderPath));
                if (pageModel.ContainsKey(CommonConst.CommonValue.PAGE_TEMPLATE_PATH))
                {
                    FileInfo fiTemplete       = new FileInfo(pageModel[CommonConst.CommonValue.PAGE_TEMPLATE_PATH]);
                    var      templateFileData = StaticContentHandler.GetStringContent(dbProxy, logger, pageModel[CommonConst.CommonValue.PAGE_TEMPLATE_PATH]);
                    pageModel[CommonConst.CommonValue.RENDERBODY_DATA] = data;
                    data = viewEngine.Compile(templateFileData, pageModel[CommonConst.CommonValue.PAGE_TEMPLATE_PATH],
                                              ServerPageModelHelper.SetDefaultModel(dbProxy, httpProxy, logger, viewEngine, actionExecuter, pageModel, pageModel[CommonConst.CommonValue.PAGE_TEMPLATE_PATH].Replace(fiTemplete.Name, "")));
                }
                return(data);
            }
            else
            {
                return(string.Empty);
            }
        }
示例#11
0
 public bool ExecBeforeEvent(IActionExecuter actionExecuter, RoutingModel route, ParamContainer paramContainer)
 {
     ExecEvent(actionExecuter, route, paramContainer, Models.ExecutionEventType.Init);
     return(true);
 }
示例#12
0
 public bool ExecAfterEvent(IActionExecuter actionExecuter, RoutingModel route, ParamContainer paramContainer, object objResult)
 {
     ExecEvent(actionExecuter, route, paramContainer, Models.ExecutionEventType.Completed);
     return(true);
 }
示例#13
0
        public static ParamContainer CreateParamContainer(RoutingModel route, IHttpContextProxy httpProxy, ILogger loggerController, IActionExecuter actionExecuter)
        {
            var                    paramContainer  = CreateParamContainer(loggerController, actionExecuter);
            ILogReader             logReader       = Logger.GetLogReader();
            IDBService             dbService       = paramContainer.GetKey(CommonConst.CommonValue.PARAM_DBPROXY);
            IViewEngine            viewEngine      = paramContainer.GetKey(CommonConst.CommonValue.PARAM_VIEW_ENGINE);
            ResponseBuilder        responseBuilder = new ResponseBuilder(loggerController, logReader, httpProxy);
            ISessionProvider       sessionProvider = new SessionProvider(httpProxy, dbService, loggerController);
            IwwwrootContentHandler ContentHandler  = new WwwrootContentHandler(httpProxy, dbService, viewEngine, actionExecuter, loggerController);

            (dbService as MongoDBService).User = () =>
            {
                string result = string.Empty;
                var    user   = sessionProvider.GetValue <UserModel>(CommonConst.CommonValue.SESSION_USER_KEY);
                if (user != null)
                {
                    result = string.Format("{0}::{1}", user.id, user.name);
                }
                return(result);
            };
            (loggerController as Logger).DBProxy = dbService;
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_ROUTE, () => { return(route); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_HTTPREQUESTPROXY, () => { return(httpProxy); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_RESPONBUILDER, () => { return(responseBuilder); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_SESSION_PROVIDER, () => { return(sessionProvider); });
            paramContainer.AddKey(CommonConst.CommonValue.PARAM_CONTENT_HANDLER, () => { return(ContentHandler); });

            return(paramContainer);
        }