Beispiel #1
0
        public IActionResult Execute([FromBody] KitsuneV2KLMRequestModel request)
        {
            KLMResponseModel klmResponse = KitsuneLayoutManager.RequestHandler.GetHtmlFromKlmV2Async(request).GetAwaiter().GetResult();

            if (klmResponse == null)
            {
                return(Ok(""));
            }
            return(Ok(klmResponse));
        }
Beispiel #2
0
        public static async Task <KLMResponseModel> GetHtmlFromKlmV2Async(KitsuneV2KLMRequestModel request)
        {
            try
            {
                var functionLog       = new Dictionary <string, long>();
                var functionStopWatch = new Stopwatch();

                functionStopWatch.Start();

                #region GET ENTITY INFO

                var EntityId = request.SchemaId;
                if (string.IsNullOrEmpty(EntityId))
                {
                    EntityId = "58d717e667962d6f40f5c198";
                }
                KEntity entity = await MongoHelper.GetLanguageEntityAsync(EntityId);

                if (entity == null)
                {
                    return(null);
                }

                Helper.Helper.UpdateFunctionLog(functionLog, KitsuneLayoutManager.Helper.Constant.GETTING_ENTITY, functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();

                var auditLog = new KLMAuditLogModel();
                #endregion

                Helper.Helper.UpdateFunctionLog(functionLog, KitsuneLayoutManager.Helper.Constant.GETTING_HTTP_HEADER_INFO, functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();

                #region GET HTML FROM URL
                functionStopWatch.Start();

                if (string.IsNullOrEmpty(request.HostedFilePath))
                {
                    return(null);
                }
                string htmlString = Helper.Helper.GetHtmlStringFromUrl(request.HostedFilePath);
                if (string.IsNullOrEmpty(htmlString))
                {
                    return(null);
                }

                Helper.Helper.UpdateFunctionLog(functionLog, KitsuneLayoutManager.Helper.Constant.GET_HTML_FROM_URL, functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();
                #endregion

                #region get KitsunePage
                byte[] bytes        = Convert.FromBase64String(htmlString);
                string stringValue  = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                var    jsonsettings = new JsonSerializerSettings();
                jsonsettings.TypeNameHandling = TypeNameHandling.Auto;
                KitsunePage page = JsonConvert.DeserializeObject <KitsunePage>(stringValue, jsonsettings);
                #endregion

                #region FP DETAILS FROM CACHE
                var components = MongoHelper.GetProjectComponents(request.ProjectId, request.ProjectVersion);

                string sourcePath             = page.SourcePath;
                var    projectResourceDetails = MongoHelper.GetUrlPatternDetails(request.ProjectId, request.ProjectVersion, sourcePath);
                string urlPattern             = projectResourceDetails?.UrlPattern;
                string urlPatternRegex        = projectResourceDetails?.UrlPatternRegex;

                functionStopWatch.Start();

                var  view = string.Empty; var viewDetails = new Models.Pagination();
                bool isDetailsView = false; var urlParamList = new Dictionary <string, string>(); bool isSearchView = false;
                var  queryString = string.Empty;

                // GET URL FROM API
                if (!string.IsNullOrEmpty(request.HostedFilePath) && !string.IsNullOrEmpty(request.ProjectId))
                {
                    isDetailsView = (request.PageType == Kitsune.Models.Project.KitsunePageType.DETAILS) ? true : false;
                    isSearchView  = (request.PageType == Kitsune.Models.Project.KitsunePageType.SEARCH) ? true : false;
                    bool isDefaultView = (request.PageType == Kitsune.Models.Project.KitsunePageType.DEFAULT) ? true : false;

                    viewDetails = PaginationHelper.GetViewDetails(request.IncomingUrl.Trim('/'), urlPattern, request.RootPath, isDetailsView, isSearchView);
                    if (isDetailsView)
                    {
                        queryString = Helper.Helper.GetQueryStringForDL(urlPattern);
                    }
                }

                dynamic websiteData = (Newtonsoft.Json.Linq.JToken)JsonConvert.DeserializeObject("{ _system:{}, rootaliasurl:{} }");
                websiteData["_system"]      = (Newtonsoft.Json.Linq.JToken)JsonConvert.DeserializeObject("{viewbag:{}}");
                websiteData["rootaliasurl"] = (Newtonsoft.Json.Linq.JToken)JsonConvert.DeserializeObject($"{{ url :  '{request.RootPath}'  }}");

                //Get Component
                #region Component Data
                if (components != null && components.Any())
                {
                    try
                    {
                        websiteData["_system"]["components"] = ApiHelper.GetComponentsData(components, request.ProjectId, request.WebsiteId, request.IncomingUrl, request.HostedFilePath, request.RootPath);
                    }
                    catch { }
                }
                #endregion

                Helper.Helper.UpdateFunctionLog(functionLog, KitsuneLayoutManager.Helper.Constant.GETTING_FP_DETAILS, functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();
                #endregion

                if (!request.RootPath.StartsWith("http"))
                {
                    Uri requesturl = new Uri(request.IncomingUrl);
                    request.RootPath = requesturl.Scheme + "://" + request.RootPath;
                }

                BlockLevelKLMExecutor blklmexecutor = new BlockLevelKLMExecutor(entity, functionLog, page, viewDetails, request.RootPath, request.WebsiteId, request.SchemaId, request.IncomingUrl, urlPattern, urlPatternRegex, websiteData?._system, request.WebsiteTag, false, request.DeveloperId);
                functionStopWatch.Start();
                KLMResponseModel klmResponse = blklmexecutor.Execute();
                functionStopWatch.Stop();
                Helper.Helper.UpdateFunctionLog(functionLog, "New KLM Flow", functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();

                #region  CUSTOM SUPPORT FOR KAPP MODULES
                try
                {
                    string componentString = "";
                    if (components != null && components.Count > 0 && websiteData?._system?.components != null)
                    {
                        foreach (var component in components)
                        {
                            switch (component.ProjectId)
                            {
                            //RIA App ID
                            case "5ab5190ba35c3b04e9817cb5":
                            {
                                if (websiteData["components"]?["_" + component.SchemaId] != null)
                                {
                                    componentString += "<img src='http://www.google-analytics.com/collect?v=1&tid=UA-35051129-38&t=event&ec=" + websiteData["components"]["_" + component.SchemaId]["notif_type"] ?? "" + "&ea=open&el=" + websiteData["components"]["_" + component.SchemaId]["website_domain"] ?? "" + "&cs=newsletter&cm=email&cn=" + websiteData["components"]["_" + component.SchemaId]["project_id"] ?? "" + "&cm1=1&cd1=" + websiteData["components"]["_" + component.SchemaId]["recipient_email"] ?? "" + "&cid=" + websiteData["components"]["_" + component.SchemaId]["website_user_id"] ?? "" + "' style='z-index:-1; display: none; visibility: hidden; width:0px; height:0px;' />";
                                }
                                break;
                            }
                            }
                        }
                    }
                    componentString     += "</body>";
                    klmResponse.HtmlCode = klmResponse.HtmlCode.Replace("</body>", componentString);
                }
                catch { }
                #endregion

                functionStopWatch.Stop();
                Helper.Helper.UpdateFunctionLog(functionLog, "update custom component modules", functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();

                #region UPDATE LOG
                auditLog = new KLMAuditLogModel()
                {
                    _id = ObjectId.GenerateNewId().ToString(), city = null, country = null, createdOn = DateTime.UtcNow, functionalLog = functionLog, fpTag = request.WebsiteTag, ipAddress = request.ipAddress, themeId = request.ProjectId, loadTime = functionStopWatch.Elapsed.Seconds
                };
                KinesisHelper.LogKLMRequestDetailsIntoKinesis(auditLog, request.IncomingUrl);
                #endregion

                klmResponse.HtmlCode = klmResponse.HtmlCode.Replace("[LOG_ID]", auditLog._id);
                klmResponse.HtmlCode = klmResponse.HtmlCode.Replace("[KITSUNE_WEBSITE_ID]", request.WebsiteId);
                klmResponse.PerfLog  = functionLog;
                return(klmResponse);
            }
            catch (Exception ex)
            {
                throw;
            }

            return(null);
        }