/// <summary>
        /// Gets the resource details for a given url
        /// </summary>
        /// <param name="projectId">ProjectId</param>
        /// <param name="path">Only path of the url is being sent not the entire url </param>
        /// <param name="kitsuneRequestUrlType">To Identify DEMO | PREVIEW | PRODUCTION</param>
        /// <returns>
        /// Return null if file not found
        /// Throws error if Failed fetching route
        /// </returns>
        public static void CreateRouteTree(string projectId, string path, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            if (String.IsNullOrEmpty(projectId))
            {
                throw new ArgumentNullException(nameof(projectId));
            }

            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            try
            {
                var requestType = kitsuneRequestUrlType.Equals(KitsuneRequestUrlType.PRODUCTION) ? 1 : 0;
                var temp        = "{\"ProjectId\":\"" + projectId + "\"}";

                var routingRequestParams = new String[] { requestType.ToString(), projectId, "new_KitsuneResourcesProduction", temp };

                var response       = AWSLambdaHelpers.InvokeAWSLambda(Constants.RoutingCreateFunctionName, string.Join("\n", routingRequestParams), RegionEndpoint.GetBySystemName(IdentifierEnvironmentConstants.IdentifierConfigurations.RoutingLambdaCredentials.Region)).Result;
                var responseObject = JsonConvert.DeserializeObject <dynamic>(response);
                if (responseObject["body"] != null)
                {
                    var responseData = ((string)responseObject["body"]).Split('\n');
                    if (responseData.Length <= 0 || responseData[0] != "200")
                    {
                        throw new Exception(message: $"ProjectId : {projectId} and SourcePath : {path}, Error : Unable to create routing tree with message {(responseData.Length > 1 ? responseData[1] : "")}");
                    }
                }
                else
                {
                    throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Unable to create routing tree");
                }
            }
            catch (WebException webException)
            {
                HttpWebResponse response = (HttpWebResponse)webException.Response;
                switch (response.StatusCode)
                {
                case HttpStatusCode.NotFound:
                    break;
                }
                throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Error from server with ResponseCode : {response.StatusCode}");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        internal static async Task <KitsuneDomainDetails> GetDomainDetailsAsync(string domain, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            if (String.IsNullOrEmpty(domain) || domain.Trim().Length == 0)
            {
                return(null);
            }

            try
            {
                if (kitsuneRequestUrlType == KitsuneRequestUrlType.PRODUCTION)
                {
                    domain = domain.Trim().ToUpper();
                }

                var domainDetails = await MongoHelper.GetCustomerDetailsFromDomainAsync(domain, kitsuneRequestUrlType);

                if (domainDetails == null)
                {
                    throw new Exception($"'{domain}' Domain not found");
                }

                return(domainDetails);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        internal static async Task <ResourceDetails> GetResourceDetailsAsync(string projectid, string encodedUrlPath, string originalUrlAbsolutePath, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION, bool isDefaultView = false)
        {
            var kitsuneResourceDetails = new KitsuneResource();

            try
            {
                if (!Utils.IsStaticFile(encodedUrlPath))
                {
                    //CALL THE ROUTING API TO FETCH THE DETAILS
                    var result = APIHelper.GetRoutingObject(projectid, encodedUrlPath, kitsuneRequestUrlType, createIfNotExists: true);

                    if (result != null)
                    {
                        if (!string.IsNullOrEmpty(result.RedirectPath))
                        {
                            //Redirect
                            return(new ResourceDetails {
                                IsRedirect = true, RedirectPath = result.RedirectPath
                            });
                        }

                        if (!String.IsNullOrEmpty(result.ResourceId))
                        {
                            //Get Resource Details
                            var resourceDetails = await MongoHelper.GetProjectResourceAsync(projectid, result.ResourceId, kitsuneRequestUrlType);

                            return(new ResourceDetails {
                                OptimizedPath = resourceDetails.OptimizedPath, UrlPattern = resourceDetails.UrlPattern, isStatic = resourceDetails.IsStatic, SourcePath = resourceDetails.SourcePath, PageType = resourceDetails.PageType, UrlPatternRegex = resourceDetails.UrlPatternRegex
                            });
                        }
                        else if (result.StatusCode > 400 && result.StatusCode < 500)
                        {
                            return(new ResourceDetails {
                                StatusCode = result.StatusCode
                            });
                        }
                    }
                }

                kitsuneResourceDetails = await MongoHelper.GetProjectResourceDetailsAsync(projectid, originalUrlAbsolutePath, isDefaultView, kitsuneRequestUrlType);

                if (kitsuneResourceDetails != null)
                {
                    return(new ResourceDetails
                    {
                        OptimizedPath = kitsuneResourceDetails.OptimizedPath,
                        UrlPattern = kitsuneResourceDetails.UrlPattern,
                        isStatic = kitsuneResourceDetails.IsStatic,
                        SourcePath = kitsuneResourceDetails.SourcePath,
                        PageType = kitsuneResourceDetails.PageType,
                        UrlPatternRegex = kitsuneResourceDetails.UrlPatternRegex
                    });
                }
            }
            catch (Exception ex)
            {
                try
                {
                    kitsuneResourceDetails = await MongoHelper.GetProjectResourceDetailsAsync(projectid, originalUrlAbsolutePath, isDefaultView, kitsuneRequestUrlType);

                    if (kitsuneResourceDetails != null)
                    {
                        return(new ResourceDetails
                        {
                            OptimizedPath = kitsuneResourceDetails.OptimizedPath,
                            UrlPattern = kitsuneResourceDetails.UrlPattern,
                            isStatic = kitsuneResourceDetails.IsStatic,
                            SourcePath = kitsuneResourceDetails.SourcePath,
                            PageType = kitsuneResourceDetails.PageType,
                            UrlPatternRegex = kitsuneResourceDetails.UrlPatternRegex
                        });
                    }
                }
                catch { }
            }
            return(null);
        }
예제 #4
0
        public static async Task <string> GetDeveloperIdFromProjectIdAsync(string projectId, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            try
            {
                if (_server == null)
                {
                    InitiateConnection();
                }

                var collectionName = KLM_Constants.KitsuneProjectProductionCollection;
                if (kitsuneRequestUrlType == KitsuneRequestUrlType.DEMO || kitsuneRequestUrlType == KitsuneRequestUrlType.PREVIEW)
                {
                    collectionName = KLM_Constants.KitsuneProjectCollection;
                }

                var collection = _kitsuneDB.GetCollection <ProductionKitsuneProject>(collectionName);
                var project    = Builders <ProductionKitsuneProject> .Projection;
                var cursor     = await collection.Find(x => x.ProjectId == projectId,
                                                       new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) })
                                 .Project <ProductionKitsuneProject>(project.Include(x => x.UserEmail)).Limit(1).FirstAsync();

                if (!String.IsNullOrEmpty(cursor.UserEmail))
                {
                    var usersCollection = _kitsuneDB.GetCollection <UserModel>("users");

                    var usersProject = Builders <UserModel> .Projection;
                    var usersCursor  = await usersCollection.Find(x => x.Email == cursor.UserEmail,
                                                                  new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) })
                                       .Project <UserModel>(usersProject.Include(x => x._id)).Limit(1).FirstAsync();

                    return(usersCursor._id);
                }
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
예제 #5
0
        public static async Task <KitsuneResource> GetProjectDefaultResourceAsync(string projectid, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            try
            {
                if (_server == null)
                {
                    InitiateConnection();
                }

                string collectionName = KLM_Constants.KitsuneResourcesProductionCollection;
                IMongoCollection <KitsuneResource> resourceCollection;

                if (kitsuneRequestUrlType == KitsuneRequestUrlType.DEMO || kitsuneRequestUrlType == KitsuneRequestUrlType.PREVIEW)
                {
                    collectionName = KLM_Constants.KitsuneResourcesCollection;
                }

                resourceCollection = _kitsuneDB.GetCollection <KitsuneResource>(collectionName);
                //optimized resource fatching
                var projection = new ProjectionDefinitionBuilder <KitsuneResource>()
                                 .Include(x => x.OptimizedPath)
                                 .Include(x => x.SourcePath)
                                 .Include(x => x.UrlPattern)
                                 .Include(x => x.UrlPatternRegex)
                                 .Include(x => x.PageType)
                                 .Include(x => x.IsStatic);
                KitsuneResource resource = await resourceCollection.Find(x => x.IsDefault && x.ProjectId.Equals(projectid), new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) })
                                           .Project <KitsuneResource>(projection).FirstOrDefaultAsync();

                if (resource != null)
                {
                    return(resource);
                }
                else
                {
                    throw new Exception($"Default resource for ProjectId : {projectid} and not found in DB");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #6
0
        public static async Task <KitsuneResource> GetProjectResourceDetailsAsync(string projectid, string sourcePath, bool isDefaultView = false, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION, bool isStaticFile = false)
        {
            try
            {
                if (_server == null)
                {
                    InitiateConnection();
                }

                string collectionName = KLM_Constants.KitsuneResourcesProductionCollection;
                IMongoCollection <KitsuneResource> projectsCollection;
                KitsuneResource response = null;

                if (kitsuneRequestUrlType == KitsuneRequestUrlType.DEMO || kitsuneRequestUrlType == KitsuneRequestUrlType.PREVIEW)
                {
                    collectionName = KLM_Constants.KitsuneResourcesCollection;
                }

                projectsCollection = _kitsuneDB.GetCollection <KitsuneResource>(collectionName);

                //optimized resource fatching
                var projection = new ProjectionDefinitionBuilder <KitsuneResource>()
                                 .Include(x => x.OptimizedPath)
                                 .Include(x => x.SourcePath)
                                 .Include(x => x.UrlPattern)
                                 .Include(x => x.UrlPatternRegex)
                                 .Include(x => x.PageType)
                                 .Include(x => x.IsStatic);
                //CHECK AND RETURN IF THE REQUEST TYPE IS DEFAULTVIEW
                if (isDefaultView)
                {
                    response = await projectsCollection.Find(x => x.ProjectId == projectid &&
                                                             x.IsDefault == true && x.IsStatic == false &&
                                                             x.ResourceType == ResourceType.LINK,
                                                             new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) })
                               .Project <KitsuneResource>(projection).FirstOrDefaultAsync();
                }

                if (response == null)
                {
                    var requestUrl = sourcePath.ToLower();

                    #region Mimetype detection

                    var tempRequestUrl = requestUrl;
                    if (((String.Compare(tempRequestUrl, "/") == 0) && isDefaultView) || (tempRequestUrl.EndsWith("/")))
                    {
                        tempRequestUrl += "index.html";
                    }

                    //check if it is static file
                    var filter = (Builders <KitsuneResource> .Filter.Eq(document => document.ProjectId, projectid) &
                                  Builders <KitsuneResource> .Filter.Eq(document => document.IsStatic, true)) &
                                 (Builders <KitsuneResource> .Filter.Where(document => document.OptimizedPath.ToLower() == tempRequestUrl) |
                                  Builders <KitsuneResource> .Filter.Where(document => document.SourcePath.ToLower() == tempRequestUrl));

                    var tempCheck = projectsCollection.Find(filter, new FindOptions {
                        MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut)
                    })
                                    .Project <KitsuneResource>(projection).Limit(1).ToList();

                    if (tempCheck != null && tempCheck.Count() > 0 && !String.IsNullOrEmpty(tempCheck.FirstOrDefault()._id))
                    {
                        return(tempCheck.FirstOrDefault());
                    }

                    if ((Helper.IsStaticFile(tempRequestUrl)) || (tempCheck != null && tempCheck.Count > 0 && tempCheck.FirstOrDefault().IsStatic))
                    {
                        return(null);
                    }

                    #endregion

                    var allResources = await projectsCollection.Find(x => x.ProjectId == projectid && x.ResourceType == ResourceType.LINK,
                                                                     new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) })
                                       .Project <KitsuneResource>(projection).ToListAsync();

                    var tempResourceDictionary = new Dictionary <string, int>();
                    foreach (var resource in allResources)
                    {
                        //To-Do re-optimize this case
                        if (resource.UrlPatternRegex == null)
                        {
                            resource.UrlPatternRegex = resource.OptimizedPath;
                        }

                        if (resource.UrlPatternRegex != null)
                        {
                            var pathMatchWeight     = 0;
                            var pathMatchPercentage = 0;
                            var regexMatch          = Helper.GetRegexValue(requestUrl, resource.UrlPatternRegex);
                            if (regexMatch.Success)
                            {
                                pathMatchWeight += regexMatch.Length;
                            }

                            var urlPatternRegexPathSplit      = resource.UrlPatternRegex.TrimStart('/').Split('/');
                            var urlPatternRegexPathSplitCount = urlPatternRegexPathSplit.Count();

                            var urlPathSplit      = requestUrl.TrimStart('/').Split('/');
                            var urlPathSplitCount = urlPathSplit.Count();

                            for (var index = 0; index < urlPatternRegexPathSplitCount; index++)
                            {
                                try
                                {
                                    if (index < urlPathSplitCount)
                                    {
                                        var regexPathValue = urlPatternRegexPathSplit[index];
                                        var urlPathValue   = urlPathSplit[index];

                                        if ((!String.IsNullOrEmpty(regexPathValue) && !String.IsNullOrEmpty(urlPathValue)) && String.Compare(regexPathValue, urlPathValue, StringComparison.InvariantCultureIgnoreCase) == 0)
                                        {
                                            pathMatchWeight += (urlPatternRegexPathSplitCount - index);
                                        }
                                    }
                                }
                                catch { }
                            }

                            pathMatchPercentage = (pathMatchWeight / resource.UrlPatternRegex.Length) * 100;
                            tempResourceDictionary.Add(resource._id, pathMatchWeight);
                        }
                    }

                    try
                    {
                        var bestMatchValue = tempResourceDictionary.OrderByDescending(val => val.Value).FirstOrDefault();
                        if (bestMatchValue.Value > 0)
                        {
                            return(allResources.Find(x => (x._id == bestMatchValue.Key)));
                        }
                    }
                    catch
                    {
                        var bestMatchValue = tempResourceDictionary.OrderByDescending(val => val.Value).FirstOrDefault();
                        if (bestMatchValue.Value > 0)
                        {
                            return(allResources.Find(x => (x._id == bestMatchValue.Key)));
                        }
                    }
                }
                else
                {
                    return(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(null);
        }
예제 #7
0
        public static async Task <KitsuneDomainDetails> GetCustomerDetailsFromDomainAsync(string domainUrl, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            try
            {
                if (_server == null)
                {
                    InitiateConnection();
                }
                if (String.IsNullOrEmpty(domainUrl))
                {
                    return(null);
                }

                var websiteCollection    = _kitsuneDB.GetCollection <KitsuneWebsiteCollection>(KLM_Constants.KitsuneWebsiteCollection);
                var websiteDNSCollection = _kitsuneDB.GetCollection <WebsiteDNSInfo>(KLM_Constants.KitsuneDNSCollection);

                var result        = new KitsuneDomainDetails();
                var tempDomainUrl = domainUrl.ToUpper();

                var filter = (Builders <KitsuneWebsiteCollection> .Filter.Eq(document => document.WebsiteUrl, tempDomainUrl)) &
                             Builders <KitsuneWebsiteCollection> .Filter.Eq(document => document.IsActive, true);

                if (kitsuneRequestUrlType == KitsuneRequestUrlType.DEMO || kitsuneRequestUrlType == KitsuneRequestUrlType.PREVIEW)
                {
                    filter = Builders <KitsuneWebsiteCollection> .Filter.Eq(document => document._id, domainUrl);
                }


                var customer = websiteCollection.Find(filter, new FindOptions {
                    MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut)
                }).FirstOrDefault();
                if (customer != null)
                {
                    result.Domain      = customer.WebsiteUrl;
                    result.ProjectId   = customer.ProjectId;
                    result.Version     = customer.KitsuneProjectVersion;
                    result.CustomerId  = customer._id;
                    result.WebsiteTag  = customer.WebsiteTag;
                    result.DeveloperId = customer.DeveloperId;
                    result.ClientId    = customer.ClientId;

                    try
                    {
                        var filterDNS = Builders <WebsiteDNSInfo> .Filter.Eq(document => document.DomainName, customer.WebsiteUrl)
                                        & Builders <WebsiteDNSInfo> .Filter.Eq(document => document.DNSStatus, DNSStatus.Active);

                        var dnsProjectionBuilder = new ProjectionDefinitionBuilder <WebsiteDNSInfo>();
                        var dnsProjection        = dnsProjectionBuilder.Include(x => x.WebsiteId).Include(x => x.IsSSLEnabled)
                                                   .Include(x => x._id);

                        var dnsInfo = websiteDNSCollection.Find(filterDNS,
                                                                new FindOptions {
                            MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut)
                        }).Project <WebsiteDNSInfo>(dnsProjection)
                                      .Sort(Builders <WebsiteDNSInfo> .Sort.Descending(x => x.CreatedOn))
                                      ?.FirstOrDefault();

                        if (dnsInfo == null)
                        {
                            Console.WriteLine($"No active DNS record found for {tempDomainUrl}");
                            throw new Exception($"No active DNS record found for {tempDomainUrl}");
                        }

                        if (dnsInfo.IsSSLEnabled)
                        {
                            result.isSSLEnabled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    #region Handle .getkitsune.com subdomain redirection

                    var filterDNS = Builders <WebsiteDNSInfo> .Filter.Eq(document => document.DomainName, tempDomainUrl)
                                    & Builders <WebsiteDNSInfo> .Filter.Eq(document => document.DNSStatus, DNSStatus.Active);

                    var dnsProjectionBuilder = new ProjectionDefinitionBuilder <WebsiteDNSInfo>();

                    var dnsInfo = websiteDNSCollection.Find(filterDNS, new FindOptions {
                        MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut)
                    }).FirstOrDefault();
                    if (dnsInfo != null)
                    {
                        filter = (Builders <KitsuneWebsiteCollection> .Filter.Eq(document => document._id, dnsInfo.WebsiteId));

                        //customerDetails = CommonKitsuneDBGetQueryDetailsForAnyCollection<KitsuneWebsiteCollection>(Identifier_Constants.KitsuneWebsiteCollection, filter, projec, null, 1).FirstOrDefault();
                        customer = await websiteCollection.Find(filter, new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) }).FirstOrDefaultAsync();

                        try
                        {
                            filterDNS = Builders <WebsiteDNSInfo> .Filter.Eq(document => document.DomainName, customer.WebsiteUrl)
                                        & Builders <WebsiteDNSInfo> .Filter.Eq(document => document.DNSStatus, DNSStatus.Active);

                            var newDNSInfo = websiteDNSCollection.Find(filterDNS, new FindOptions {
                                MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut)
                            })
                                             .Sort(Builders <WebsiteDNSInfo> .Sort.Descending(x => x.CreatedOn))
                                             ?.FirstOrDefault();

                            if (customer != null)
                            {
                                result.Domain       = tempDomainUrl;
                                result.ProjectId    = customer.ProjectId;
                                result.Version      = customer.KitsuneProjectVersion;
                                result.CustomerId   = customer._id;
                                result.RedirectUrl  = customer.WebsiteUrl;
                                result.IsRedirect   = true;
                                result.WebsiteTag   = customer.WebsiteTag;
                                result.DeveloperId  = customer.DeveloperId;
                                result.ClientId     = customer.ClientId;
                                result.isSSLEnabled = newDNSInfo.IsSSLEnabled;
                            }
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        return(null);
                    }
                    #endregion
                }

                //TO-Do update to BasePlugin
                if ((kitsuneRequestUrlType == KitsuneRequestUrlType.DEMO || kitsuneRequestUrlType == KitsuneRequestUrlType.PREVIEW) || (customer != null && !customer.IsActive && customer.WebsiteUrl.ToLower().EndsWith("getkitsune.com")))
                {
                    result.IsRedirect = false;
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #8
0
        public static async Task <ProjectDetails> GetProjectDetailsAsync(string projectId, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            try
            {
                if (_server == null)
                {
                    InitiateConnection();
                }

                var projectionBuilder = new ProjectionDefinitionBuilder <ProductionKitsuneProject>();
                var projection        = projectionBuilder.Include(x => x.BucketNames)
                                        .Include(x => x.SchemaId)
                                        .Include(x => x.Version)
                                        .Include(x => x.Components)
                                        .Include(x => x.RuntimeOptimization)
                                        .Include(x => x._id)
                                        .Include(x => x.ProjectId)
                                        .Include(x => x.UserEmail)
                                        .Include(x => x.CompilerVersion);

                var filter = Builders <ProductionKitsuneProject> .Filter.Eq(document => document.ProjectId, projectId);

                var collectionName = KLM_Constants.KitsuneProjectProductionCollection;
                if (kitsuneRequestUrlType == KitsuneRequestUrlType.DEMO || kitsuneRequestUrlType == KitsuneRequestUrlType.PREVIEW)
                {
                    collectionName = KLM_Constants.KitsuneProjectCollection;
                }

                var productionKitsuneProjectsCOllection = _kitsuneDB.GetCollection <ProductionKitsuneProject>(collectionName);
                var result = await productionKitsuneProjectsCOllection.Find(filter,
                                                                            new FindOptions { MaxTime = TimeSpan.FromMilliseconds(KLM_Constants.MongoQueryMaxtimeOut) }).Project <ProductionKitsuneProject>(projection).FirstOrDefaultAsync();

                return(new ProjectDetails()
                {
                    BucketNames = result.BucketNames,
                    SchemaId = result.SchemaId,
                    Version = result.Version,
                    Components = result.Components,
                    DeveloperEmail = result.UserEmail,
                    RuntimeOptimization = result.RuntimeOptimization,
                    ProjectId = result.ProjectId,
                    CompilerVersion = result.CompilerVersion
                });
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
        /// <summary>
        /// Gets the resource details for a given url
        /// </summary>
        /// <param name="projectId">ProjectId</param>
        /// <param name="path">Only path of the url is being sent not the entire url </param>
        /// <param name="kitsuneRequestUrlType">To Identify DEMO | PREVIEW | PRODUCTION</param>
        /// <returns>
        /// Return null if file not found
        /// Throws error if Failed fetching route
        /// </returns>
        public static RoutingObjectModel GetRoutingObject(string projectId, string path, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION, bool createIfNotExists = false)
        {
            if (String.IsNullOrEmpty(projectId))
            {
                throw new ArgumentNullException(nameof(projectId));
            }

            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            try
            {
                var requestType          = kitsuneRequestUrlType.Equals(KitsuneRequestUrlType.PRODUCTION) ? 1 : 0;
                var routingRequestParams = new String[] { requestType.ToString(), projectId, path };

                var response       = AWSLambdaHelpers.InvokeAWSLambda(Constants.RoutingMatcherFunctionName, string.Join("\n", routingRequestParams), RegionEndpoint.GetBySystemName(IdentifierEnvironmentConstants.IdentifierConfigurations.RoutingLambdaCredentials.Region)).Result;
                var responseObject = JsonConvert.DeserializeObject <dynamic>(response);
                if (responseObject["body"] != null)
                {
                    var responseData = ((string)responseObject["body"]).Split('\n');
                    var resp         = new RoutingObjectModel()
                    {
                        File         = responseData[2],
                        ResourceId   = responseData[1],
                        RedirectPath = responseData[3],
                        StatusCode   = int.Parse(responseData[0])
                    };
                    if (resp.StatusCode == 500 && createIfNotExists == true)                     //500 means routing tree not found.
                    {
                        CreateRouteTree(projectId, path, kitsuneRequestUrlType);
                        return(GetRoutingObject(projectId, path, kitsuneRequestUrlType, false));
                    }
                    return(resp);
                }

                throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Unable to Retrieve the Message");
            }
            catch (Exception ex)
            {
                throw new Exception($"ProjectId : {projectId} and SourcePath : {path}, Error : Error from server with message : {ex.Message}");
            }
        }
예제 #10
0
        public static async Task <KLMResponseModel> GetHtmlFromKlmAsync(RequestDetails request, string domainName, string url, string projectId = null,
                                                                        string schemeId   = null, string s3UrlForResource = null, string noCacheQueryParam = null, string developerId = null,
                                                                        string urlPattern = null, string websiteId        = null, Kitsune.Models.Project.KitsunePageType pageType = Kitsune.Models.Project.KitsunePageType.DEFAULT,
                                                                        KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION, string httpProtocol = "http://",
                                                                        List <Kitsune.Models.Project.ProjectComponent> components = null, string s3FolderUrl = null, string optimizedFilePath = null,
                                                                        string urlPatternRegex = null, int compilerVersion = 0, string fptag = null)
        {
            try
            {
                var tempVariableForCache = (!string.IsNullOrEmpty(noCacheQueryParam)) ? !(string.Compare(noCacheQueryParam, "true", true) == 0) : isKLMApiCacheEnabled;
                var functionLog = new Dictionary <string, long>(); var functionStopWatch = new Stopwatch();

                #region HTTP HEADER INFO

                var    ipAddress = request.IPAddress;
                string perfLog   = request.Perflog;

                #endregion

                functionStopWatch.Start();
                var websiteName = domainName.Split(',')[0];

                //if (isNFSite)
                //{
                //    var themeId = MongoHelper.GetThemeIdForUser(ipAddress, domainName);
                //    if (themeId != null)
                //    {
                //        projectId = themeId;
                //    }
                //}

                #region GET ENTITY INFO

                var entity = new KEntity();

                var EntityId = schemeId;
                //if (string.IsNullOrEmpty(EntityId))
                //{
                //    EntityId = "58d717e667962d6f40f5c198";
                //}
                if (tempVariableForCache && !string.IsNullOrEmpty(EntityId))
                {
                    entity = await CacheHelper.GetEntityInfoAsync(EntityId, tempVariableForCache);
                }

                if ((entity == null || entity.Classes == null) && !string.IsNullOrEmpty(EntityId))
                {
                    entity = await MongoHelper.GetLanguageEntityAsync(EntityId);

                    if (tempVariableForCache)
                    {
                        CacheHelper.SaveEntityInfo(EntityId, entity);
                    }
                }

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

                var businessClass = Kitsune.Language.Helper.Helper.GetClassFromJson(entity);
                var auditLog      = new Kitsune.Models.KLMAuditLogModel();

                #endregion

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

                //if (!string.IsNullOrEmpty(websiteName))
                //{
                #region WEBSITE DETAILS FROM CACHE

                functionStopWatch.Start();

                var requestUrl        = new Uri(url);
                var httpRequestObject = JsonConvert.DeserializeObject <Newtonsoft.Json.Linq.JObject>(JsonConvert.SerializeObject(new { url = requestUrl.AbsoluteUri, urlpath = requestUrl.AbsolutePath, urlsegments = requestUrl.Segments }));

                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
                var rootaliasurl = String.Format("{0}{1}", httpProtocol, websiteName);
                if (!string.IsNullOrEmpty(s3UrlForResource) && !string.IsNullOrEmpty(projectId))
                {
                    isDetailsView = (pageType == Kitsune.Models.Project.KitsunePageType.DETAILS) ? true : false;
                    isSearchView  = (pageType == Kitsune.Models.Project.KitsunePageType.SEARCH) ? true : false;
                    bool isDefaultView = (pageType == Kitsune.Models.Project.KitsunePageType.DEFAULT) ? true : false;

                    var tempUrl = new Uri(url);
                    viewDetails = PaginationHelper.GetViewDetails(url.Trim('/'), urlPattern, rootaliasurl, isDetailsView, isSearchView);
                    if (isDetailsView)
                    {
                        queryString = Helper.Helper.GetQueryStringForDL(urlPattern);
                    }
                }
                if (string.IsNullOrEmpty(s3UrlForResource))
                {
                    return(null);
                }

                dynamic websiteData = null;
                //TODO: Need to stop backward compatibility.
                //if (compilerVersion != 1)
                //{
                //    var websiteDetails = CacheHelper.GetBusinessDetailsFromCache(websiteName, tempVariableForCache, domainName, isNFSite, businessClass, entity.EntityName, developerId, websiteId, EntityId == "5aa8ffd8942c3406a81d0d7c", null);
                //    websiteData = websiteDetails;
                //}

                // if (websiteData == null)
                // {
                websiteData = (Newtonsoft.Json.Linq.JToken)JsonConvert.DeserializeObject("{ _system:{}, rootaliasurl:{} }");
                //}

                websiteData["_system"]            = (Newtonsoft.Json.Linq.JToken)JsonConvert.DeserializeObject("{viewbag:{}}");
                websiteData["_system"]["request"] = httpRequestObject;
                if (!String.IsNullOrEmpty(websiteData?.rootaliasurl?.url?.Value))
                {
                    rootaliasurl = websiteData.rootaliasurl.url.Value;
                }
                else
                {
                    websiteData["rootaliasurl"] = (Newtonsoft.Json.Linq.JToken)JsonConvert.DeserializeObject($"{{ url :  '{rootaliasurl}'  }}");
                }

                websiteData["rootaliasurl"]["url"] = (rootaliasurl).ToLower();

                //Get Component
                //To be reviewed
                #region Component Data
                if (components != null && components.Any())
                {
                    try
                    {
                        websiteData["_system"]["components"] = ApiHelper.GetComponentsData(components, projectId, websiteId, url, s3FolderUrl, rootaliasurl, kitsuneRequestUrlType);
                    }
                    catch { }
                }
                #endregion

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

                #endregion

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

                string htmlString;

                //TODO: Need to stop backward compatibility.
                //if (compilerVersion == 1)
                //{
                htmlString = Helper.Helper.GetHtmlStringFromUrl(s3UrlForResource + ".kc");
                //}
                //else
                //{
                //    htmlString = Helper.Helper.GetHtmlStringFromUrl(s3UrlForResource);
                //}
                KLMResponseModel klmResponse;
                if (string.IsNullOrEmpty(htmlString))
                {
                    klmResponse          = new KLMResponseModel();
                    klmResponse.HtmlCode = string.Empty;
                    return(klmResponse);
                }

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



                //if (compilerVersion == 1)
                //{
                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);
                BlockLevelKLMExecutor blklmexecutor = new BlockLevelKLMExecutor(entity, functionLog, page, viewDetails, rootaliasurl, websiteId, schemeId, url, urlPattern, urlPatternRegex, websiteData, fptag, kitsuneRequestUrlType == KitsuneRequestUrlType.PRODUCTION, developerId);
                functionStopWatch.Start();

                klmResponse         = blklmexecutor.Execute();
                klmResponse.PerfLog = functionLog;
                Helper.Helper.UpdateFunctionLog(functionLog, "New KLM Flow", functionStopWatch.ElapsedMilliseconds);
                functionStopWatch.Reset();

                //To be reviewed
                #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 (kitsuneRequestUrlType == KitsuneRequestUrlType.PRODUCTION && 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
                //}
                //else
                //{
                //    var document = new HtmlDocument();
                //    htmlString = WebUtility.HtmlDecode(htmlString);
                //    document.LoadHtml(htmlString);

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

                //                                    var tempBodyDocumentReference = document.DocumentNode.SelectSingleNode("//body");
                //                                    tempBodyDocumentReference.AppendChild(tempKappNode);
                //                                }
                //                            }
                //                            catch { }
                //                            break;
                //                        }
                //                }
                //            }
                //        }
                //        htmlString = document.DocumentNode.OuterHtml;
                //    }
                //    catch { }
                //    #endregion

                //    klmExecutor = new KLMExecutor(kitsuneRequestUrlType);
                //    htmlString = klmExecutor.Execute(websiteId, entity, websiteData, viewDetails, queryString, document, functionLog, isDetailsView, isNFSite);

                //    #region MINIFY HTML
                //    functionStopWatch.Start();
                //    try
                //    {
                //        //var minify = Uglify.Html(htmlString, new NUglify.Html.HtmlSettings() { DecodeEntityCharacters = false, KeepOneSpaceWhenCollapsing = true });
                //        NUglify.Html.HtmlSettings settings = new NUglify.Html.HtmlSettings() { DecodeEntityCharacters = false, RemoveOptionalTags = false, ShortBooleanAttribute = false };
                //        //settings.TagsWithNonCollapsableWhitespaces.Add("p", false);
                //        var minify = Uglify.Html(htmlString, settings);
                //        htmlString = minify.Code;
                //    }
                //    catch (Exception ex)
                //    {
                //        //TODO: Log Error while minifing html
                //    }
                //    finally
                //    {
                //        functionStopWatch.Stop();
                //        Helper.Helper.UpdateFunctionLog(functionLog, Helper.Constant.MINIFICATION, functionStopWatch.ElapsedMilliseconds);
                //        functionStopWatch.Reset();
                //    }
                //    klmResponse = new KLMResponseModel();
                //    klmResponse.HtmlCode = htmlString;
                //    klmResponse.CacheableResult = true;
                //    klmResponse.PerfLog = functionLog;
                //    #endregion
                //}

                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 = websiteName, ipAddress = ipAddress, themeId = projectId, loadTime = functionStopWatch.Elapsed.Seconds
                };
                KinesisHelper.LogKLMRequestDetailsIntoKinesis(auditLog, url);

                #endregion

                klmResponse.HtmlCode = klmResponse.HtmlCode.Replace("[LOG_ID]", auditLog._id);
                klmResponse.HtmlCode = klmResponse.HtmlCode.Replace("[KITSUNE_WEBSITE_ID]", websiteId);

                if (perfLog != null && perfLog?.ToLower() == "true")
                {
                    klmResponse.HtmlCode += "\nPerf extract:\n";
                    foreach (string key in functionLog.Keys)
                    {
                        klmResponse.HtmlCode += key + " : " + functionLog[key] + "\n";
                    }
                }
                return(klmResponse);
                //}
            }
            catch (Exception ex)
            {
                throw;
                //return ex.Message + ex.StackTrace;
            }

            return(null);
        }
예제 #11
0
        internal static dynamic GetComponentsData(List <Kitsune.Models.Project.ProjectComponent> components, string projectId, string websiteId, string requestUrl, string s3SettingsRootPath = null, string rootaliasurl = null, KitsuneRequestUrlType kitsuneRequestUrlType = KitsuneRequestUrlType.PRODUCTION)
        {
            dynamic kresultDynamic = JObject.Parse("{}");

            //var demoProject = MongoConnector.MongoHelper.GetProjectDetailsAsync(projectId, kitsuneRequestUrlType).Result;

            var riaArgsModel = ParseRiaSettingsJSON(requestUrl, s3SettingsRootPath);

            foreach (var component in components)
            {
                switch (component.ProjectId)
                {
                //RIA App ID
                case "5ab5190ba35c3b04e9817cb5":
                {
                    try
                    {
                        var project = MongoConnector.MongoHelper.GetKitsuneProductionProjectDetails(projectId);
                        kresultDynamic["_" + component.SchemaId] = (JToken)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(RiaHelper.GetRIAAppData(project, websiteId, riaArgsModel, rootaliasurl)));
                    }
                    catch (Exception ex)
                    {
                    }
                }
                break;

                //super_app : dummy data
                case "5ab5190ba35c3b04e9817cb7":
                {
                    try
                    {
                        kresultDynamic["_" + component.SchemaId] = (JToken)JsonConvert.DeserializeObject("{'name' : 'jio_user_name', 'email' : 'jio_user_email', 'mobile_number' : '8XXXXXXX789', 'profile_pic' : 'https://xyz.com/user/zyx/profile.jpg', 'is_wallet_active' : true, 'jio_id' : '1234567890' }");
                    }
                    catch (Exception ex)
                    {
                    }
                }
                break;
                }
            }
            return(kresultDynamic);
        }
예제 #12
0
 public KLMExecutor(KitsuneRequestUrlType requestType)
 {
     dynamicTagDescriptors = GetDynamicTagDescriptors(typeof(LanguageAttributes));
     processorFactory      = ProcessorFactory.GetProcessorFactory();
     this.requestType      = requestType;
 }