Пример #1
0
        public override IProcessingContextCollection GenerateProcessingContexts(string kraftRequestFlagsKey, ISecurityModel securityModel = null)
        {
            Dictionary <string, object> data        = ReconstructFromMultipart();
            List <InputModel>           inputModels = new List <InputModel>();

            if (securityModel == null)
            {
                if (_KraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                {
                    securityModel = new SecurityModel(_HttpContext);
                }
                else
                {
                    securityModel = new SecurityModelMock(_KraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                }
            }
            InputModelParameters inputModelParameters = CreateBaseInputModelParameters(_KraftGlobalConfigurationSettings, securityModel);

            inputModelParameters             = ExtendInputModelParameters(inputModelParameters);
            inputModelParameters.LoaderType |= ELoaderType.DataLoader; //TODO Not override passed in flags

            inputModelParameters.Data = data;


            _ProcessingContextCollection = new ProcessingContextCollection(CreateProcessingContexts(new List <InputModel>()
            {
                new InputModel(inputModelParameters)
            }));
            return(_ProcessingContextCollection);
        }
Пример #2
0
        public async Task ExecuteAsync()
        {
            //REQUESTRECORDER
            if (_KraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.IsEnabled)
            {
                ISecurityModel securityModel;
                if (_KraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                {
                    securityModel = new SecurityModel(_HttpContext);
                }
                else
                {
                    securityModel = new SecurityModelMock(_KraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                }
                if (securityModel.IsAuthenticated)
                {
                    RecordersStoreImp recordersStoreImp = _HttpContext.RequestServices.GetRequiredService <RecordersStoreImp>();
                    IRequestRecorder  requestRecorder   = recordersStoreImp.Get(securityModel.UserName);
                    if (requestRecorder != null && requestRecorder.IsRunning)
                    {
                        await requestRecorder.HandleRequest(_HttpContext.Request);
                    }
                }
            }

            AbstractProcessorFactory     processorFactory   = new KraftProcessorFactory();
            IProcessorHandler            processor          = processorFactory.CreateProcessor(_HttpContext, _KraftModuleCollection, _NodesSetService, _ServiceProvider.GetService <KraftGlobalConfigurationSettings>());
            IProcessingContextCollection processingContexts = processor.GenerateProcessingContexts(_KraftGlobalConfigurationSettings.GeneralSettings.KraftRequestFlagsKey);

            if (processingContexts == null)
            {
                Utilities.ExtensionMethods.KraftResult(_HttpContext, HttpStatusCode.InternalServerError, $"ExecuteAsync.CreateProcessingContexts returned null.");
                return;
            }


            Task[] tasks = new Task[processingContexts.Length];
            int    i     = 0;

            try
            {
                _IsSystemInMaintenanceMode = processingContexts.IsMaintenance;

                foreach (IProcessingContext processingContext in processingContexts.ProcessingContexts)
                {
                    tasks[i++] = Task.Run(() =>
                    {
                        ExecuteReEntrance(processingContext, processingContexts.IsMaintenance);
                    });
                }
                await Task.WhenAll(tasks);
            }
            finally
            {
                _IsSystemInMaintenanceMode = false;
                processor.GenerateResponse();
            }
        }
        private InputModel CreateInputModel(BatchRequest request, KraftGlobalConfigurationSettings kraftGlobalConfigurationSettings, string kraftRequestFlagsKey, ISecurityModel securityModel = null)
        {
            if (securityModel == null)
            {
                if (kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                {
                    securityModel = new SecurityModel(_HttpContext);
                }
                else
                {
                    securityModel = new SecurityModelMock(kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                }
            }
            InputModelParameters inputModelParameters = CreateBaseInputModelParameters(kraftGlobalConfigurationSettings, securityModel);
            //we expect the routing to be like this:
            //domain.com/startnode/<read|write>/module/nodeset/<nodepath>?lang=de
            string decodedUrl = WebUtility.UrlDecode(request.Url);
            string pattern    = @"(?:http:\/\/.+?\/|https:\/\/.+?\/|www.+?\/)(?:.+?)\/(read|write)*(?:\/)*(.+?)\/(.+?)($|\/.+?)($|\?.+)";
            Regex  regex      = new Regex(pattern);
            var    match      = regex.Match(decodedUrl);

            if (match.Groups[1] != null)
            {
                inputModelParameters.IsWriteOperation = match.Groups[1].Value == "write";
            }
            inputModelParameters.Module   = match.Groups[2].Value;
            inputModelParameters.Nodeset  = match.Groups[3].Value;
            inputModelParameters.Nodepath = string.IsNullOrEmpty(match.Groups[4].Value) ? string.Empty : match.Groups[4].Value.Substring(1);
            string paramsStr = string.IsNullOrEmpty(match.Groups[5].Value) ? string.Empty : match.Groups[5].Value.Substring(1);

            if (!string.IsNullOrEmpty(paramsStr))
            {
                string[] parameters = paramsStr.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var parameter in parameters)
                {
                    string[] kvp   = parameter.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    string   key   = kvp[0];
                    string   value = kvp[1];

                    if (key.Equals(kraftRequestFlagsKey, StringComparison.CurrentCultureIgnoreCase))
                    {
                        inputModelParameters.LoaderType = GetLoaderContent(value);
                    }
                    else
                    {
                        inputModelParameters.QueryCollection.Add(key, value);
                    }
                }
            }
            return(new InputModel(inputModelParameters));
        }
Пример #4
0
        public override IProcessingContextCollection GenerateProcessingContexts(KraftGlobalConfigurationSettings kraftGlobalConfigurationSettings, string kraftRequestFlagsKey, ISecurityModel securityModel = null)
        {
            if (securityModel == null)
            {
                if (kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                {
                    securityModel = new SecurityModel(_HttpContext);
                }
                else
                {
                    securityModel = new SecurityModelMock(kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                }
            }
            InputModelParameters inputModelParameters = CreateBaseInputModelParameters(kraftGlobalConfigurationSettings, securityModel);

            inputModelParameters = ExtendInputModelParameters(inputModelParameters);
            if (_RequestContentType == ESupportedContentTypes.JSON)
            {
                inputModelParameters.Data = GetBodyJson <Dictionary <string, object> >(_HttpContext.Request);
            }
            else
            {
                inputModelParameters.Data = _FormCollection;
            }
            inputModelParameters.FormCollection = _FormCollection;
            inputModelParameters.LoaderType     = GetLoaderType(kraftRequestFlagsKey);

            RouteData routeData = _HttpContext.GetRouteData();

            if (routeData != null)
            {
                string routeDataKey = routeData.DataTokens["key"]?.ToString()?.ToLower();
                if (!string.IsNullOrEmpty(routeDataKey))
                {
                    string signal           = routeData.Values[Constants.RouteSegmentConstants.RouteModuleSignalParameter]?.ToString();
                    bool   isWriteOperation = routeDataKey.Equals(Constants.RouteSegmentConstants.RouteDataTokenSignalWrite);
                    return(PrepareSignals(inputModelParameters.Module, signal, isWriteOperation, inputModelParameters));
                }
            }
            //Return only empty collection
            return(new ProcessingContextCollection(new List <IProcessingContext>()));
        }
Пример #5
0
        public override IProcessingContextCollection GenerateProcessingContexts(KraftGlobalConfigurationSettings kraftGlobalConfigurationSettings, string kraftRequestFlagsKey, ISecurityModel securityModel = null)
        {
            Dictionary <string, object> files       = ResolveMultipartAsJson();
            List <InputModel>           inputModels = new List <InputModel>();

            if (files != null)
            {
                foreach (string key in files.Keys)
                {
                    if (files[key] is IPostedFile postedFile)
                    {
                        if (securityModel == null)
                        {
                            if (kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                            {
                                securityModel = new SecurityModel(_HttpContext);
                            }
                            else
                            {
                                securityModel = new SecurityModelMock(kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                            }
                        }
                        InputModelParameters inputModelParameters = CreateBaseInputModelParameters(kraftGlobalConfigurationSettings, securityModel);
                        inputModelParameters             = ExtendInputModelParameters(inputModelParameters);
                        inputModelParameters.LoaderType |= ELoaderType.DataLoader; //TODO Not override passed in flags

                        foreach (string metaInfoKey in postedFile.MetaInfo.Keys)
                        {
                            inputModelParameters.Data.Add(metaInfoKey, postedFile.MetaInfo[metaInfoKey]);
                        }
                        inputModelParameters.Data.Add(key, postedFile);

                        inputModels.Add(new InputModel(inputModelParameters));
                    }
                }
            }
            _ProcessingContextCollection = new ProcessingContextCollection(CreateProcessingContexts(inputModels));
            return(_ProcessingContextCollection);
        }
Пример #6
0
        public override IProcessingContextCollection GenerateProcessingContexts(KraftGlobalConfigurationSettings kraftGlobalConfigurationSettings, string kraftRequestFlagsKey, ISecurityModel securityModel = null)
        {
            if (securityModel == null)
            {
                if (kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                {
                    securityModel = new SecurityModel(_HttpContext);
                }
                else
                {
                    securityModel = new SecurityModelMock(kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                }
            }
            InputModelParameters inputModelParameters = CreateBaseInputModelParameters(kraftGlobalConfigurationSettings, securityModel);

            inputModelParameters                = ExtendInputModelParameters(inputModelParameters);
            inputModelParameters.Data           = GetBodyJson <Dictionary <string, object> >(_HttpContext.Request);
            inputModelParameters.FormCollection = _FormCollection;
            inputModelParameters.LoaderType     = GetLoaderType(kraftRequestFlagsKey);
            if (inputModelParameters.LoaderType == ELoaderType.None)
            {
                inputModelParameters.LoaderType = ELoaderType.ViewLoader;
            }
            RouteData routeData = _HttpContext.GetRouteData();

            if (routeData != null)
            {
                inputModelParameters.BindingKey = routeData.Values[Constants.RouteSegmentConstants.RouteBindingkey] as string;
            }
            IProcessingContext processingContext = new ProcessingContext(this);

            processingContext.InputModel = new InputModel(inputModelParameters);
            List <IProcessingContext> processingContexts = new List <IProcessingContext>(1);

            processingContexts.Add(processingContext);
            _ProcessingContextCollection = new ProcessingContextCollection(processingContexts);
            return(_ProcessingContextCollection);
        }
Пример #7
0
        public override IProcessingContextCollection GenerateProcessingContexts(KraftGlobalConfigurationSettings kraftGlobalConfigurationSettings, string kraftRequestFlagsKey, ISecurityModel securityModel = null)
        {
            if (securityModel == null)
            {
                if (kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                {
                    securityModel = new SecurityModel(_HttpContext);
                }
                else
                {
                    securityModel = new SecurityModelMock(kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                }
            }
            InputModelParameters inputModelParameters = CreateBaseInputModelParameters(kraftGlobalConfigurationSettings, securityModel);

            inputModelParameters                = ExtendInputModelParameters(inputModelParameters);
            inputModelParameters.Data           = GetBodyJson <Dictionary <string, object> >(_HttpContext.Request);
            inputModelParameters.FormCollection = _FormCollection;
            inputModelParameters.LoaderType     = GetLoaderType(kraftRequestFlagsKey);
            _ProcessingContextCollection        = new ProcessingContextCollection(CreateProcessingContexts(new List <InputModel> {
                new InputModel(inputModelParameters)
            }));
            return(_ProcessingContextCollection);
        }
Пример #8
0
        internal static RequestDelegate ExecutionDelegate(IApplicationBuilder app, KraftGlobalConfigurationSettings kraftGlobalConfigurationSettings)
        {
            RequestDelegate requestDelegate = async httpContext =>
            {
                httpContext.Request.RouteValues.TryGetValue("p", out object val);
                ISecurityModel securityModel = null;
                const string   contentType   = "text/html; charset=UTF-8";
                int            statusCode    = 200;
                string         message       = string.Empty;
                if (kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.IsEnabled)
                {
                    if (kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection.RequireAuthorization)
                    {
                        securityModel = new SecurityModel(httpContext);
                    }
                    else
                    {
                        securityModel = new SecurityModelMock(kraftGlobalConfigurationSettings.GeneralSettings.AuthorizationSection);
                    }
                }
                switch (val)
                {
                case "0":
                {
                    if (kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.IsEnabled)
                    {
                        if (securityModel.IsAuthenticated)
                        {
                            RecordersStoreImp recordersStoreImp = app.ApplicationServices.GetRequiredService <RecordersStoreImp>();
                            IRequestRecorder  requestRecorder   = recordersStoreImp.Get(securityModel.UserName);
                            if (requestRecorder != null)
                            {
                                requestRecorder.IsRunning = false;
                                message = "Recorder is paused.";
                            }
                            else
                            {
                                message = "The Recorder is not running.";
                            }
                        }
                        else
                        {
                            message = "Please login because the recorder can't be run for anonymous users.";
                        }
                    }
                    else
                    {
                        statusCode = (int)HttpStatusCode.NotFound;
                        message    = "Recorder is not configured and can't be started.";
                    }
                    break;
                }

                case "1":
                {
                    if (kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.IsEnabled)
                    {
                        if (securityModel.IsAuthenticated)
                        {
                            Type              typeRecorder      = Type.GetType(kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.ImplementationAsString, true);
                            IRequestRecorder  requestRecorder   = Activator.CreateInstance(typeRecorder) as IRequestRecorder;
                            RecordersStoreImp recordersStoreImp = app.ApplicationServices.GetRequiredService <RecordersStoreImp>();
                            recordersStoreImp.Set(requestRecorder, securityModel.UserName);
                            requestRecorder.IsRunning = true;
                            message = "Recorder is enabled";
                        }
                        else
                        {
                            statusCode = (int)HttpStatusCode.Unauthorized;
                            message    = "Please login because the recorder can't be run for anonymous users.";
                        }
                    }
                    else
                    {
                        statusCode = (int)HttpStatusCode.NotFound;
                        message    = "Recorder is not configured and can't be started.";
                    }
                    break;
                }

                case "2":
                {
                    if (kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.IsEnabled)
                    {
                        if (securityModel.IsAuthenticated)
                        {
                            RecordersStoreImp recordersStoreImp = app.ApplicationServices.GetRequiredService <RecordersStoreImp>();
                            IRequestRecorder  requestRecorder   = recordersStoreImp.Get(securityModel.UserName);
                            if (requestRecorder != null)
                            {
                                message = requestRecorder.GetFinalResult()?.Result ?? string.Empty;
                                Type typeRecorder = Type.GetType(kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.ImplementationAsString, true);
                                requestRecorder = Activator.CreateInstance(typeRecorder) as IRequestRecorder;
                                recordersStoreImp.Set(requestRecorder, securityModel.UserName);
                                httpContext.Response.Clear();
                                httpContext.Response.Headers.Add("Content-Length", message.Length.ToString());
                                httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=RecordedSession.json");
                            }
                            else
                            {
                                message = "The Recorder is not enabled and no data is available.";
                            }
                        }
                        else
                        {
                            statusCode = (int)HttpStatusCode.Unauthorized;
                            message    = "Please login because the recorder can't be run for anonymous users.";
                        }
                    }
                    else
                    {
                        statusCode = (int)HttpStatusCode.NotFound;
                        message    = "Recorder is not configured and can't be started.";
                    }
                    break;
                }

                case "3":
                {
                    if (kraftGlobalConfigurationSettings.GeneralSettings.ToolsSettings.RequestRecorder.IsEnabled)
                    {
                        if (securityModel.IsAuthenticated)
                        {
                            RecordersStoreImp recordersStoreImp = app.ApplicationServices.GetRequiredService <RecordersStoreImp>();
                            IRequestRecorder  requestRecorder   = recordersStoreImp.Get(securityModel.UserName);
                            if (requestRecorder != null)
                            {
                                recordersStoreImp.Remove(securityModel.UserName);
                                message = "Recorder is destroyed.";
                            }
                            else
                            {
                                message = "The Recorder is not running.";
                            }
                        }
                        else
                        {
                            statusCode = (int)HttpStatusCode.Unauthorized;
                            message    = "Please login because the recorder can't be used for anonymous users.";
                        }
                    }
                    else
                    {
                        statusCode = (int)HttpStatusCode.NotFound;
                        message    = "Recorder is not configured and can't be started.";
                    }
                    break;
                }

                default:
                    break;
                }
                httpContext.Response.StatusCode  = statusCode;
                httpContext.Response.ContentType = contentType;
                await httpContext.Response.WriteAsync(message);
            };

            return(requestDelegate);
        }