/// <summary> // Retrieves the HTML content for the specified module instance from the IH services /// </summary> /// <param name="moduleInstanceId"></param> /// <returns></returns> private string GetModuleInstanceContent(string moduleInstanceId, bool isPublishing, User user, Dictionary<string, string> headers, int failCount = 0) { if (!string.IsNullOrEmpty(_authToken)) { var moduleContentUrl = _servicesBaseUrl + "/public/v1/modules/instances/" + (ShowConfigView ? "config/" : string.Empty) + moduleInstanceId + "?authtoken=" + _authToken + (isPublishing ? "&serviceProxyEndpoint=" + Util.UrlEncode(Ih_ModuleHelper.ServiceProxyEndpoint) : string.Empty); var httpParams = new GetHttpParams(); if (headers != null) { foreach (var header in headers) { httpParams.AddHeader(header.Key + ": " + header.Value); } } var result = CrownPeak.CMSAPI.Util.GetHttp(moduleContentUrl, httpParams); if ((result.StatusCode == (int)HttpStatusCode.Unauthorized || result.StatusCode == (int)HttpStatusCode.Redirect) && failCount == 0) { failCount++; _authToken = Ih_AuthenticationHelper.GetAuthToken(true, user, _configSettings); return GetModuleInstanceContent(moduleInstanceId, isPublishing, user, headers, failCount); } if (result.StatusCode == (int)HttpStatusCode.NotFound) return "<div style=\"border:1px solid black;padding:5px;text-align:center;\">Layout for Module Instance not defined</div>"; else return result.ResponseText; } else { return "<div style=\"border:1px solid black;padding:5px;text-align:center;\">Unable to authenticate with services.</div>"; } }
/// <summary> // Retrieves the HTML content for the specified module instances from the IH services as caches them /// </summary> /// <param name="moduleInstanceId"></param> /// <returns></returns> private string GetModuleInstanceContent(string moduleInstanceId, Asset asset, OutputContext context, Dictionary<string, string> headers, PrefetchContentMode prefetchMode, int failCount = 0) { // Check if module content already exists in cache var cacheKey = "GetModuleInstanceContent_" + moduleInstanceId; if (context.UserVariables.HasKey(cacheKey)) return context.UserVariables[cacheKey]; if (!string.IsNullOrEmpty(_authToken)) { var moduleInstanceIds = new List<string>(); // Apply prefetching options if applicable if (prefetchMode == PrefetchContentMode.AllInstances || prefetchMode == PrefetchContentMode.DynamicInstances) moduleInstanceIds.AddRange((!string.IsNullOrEmpty(asset[_dynamicModuleInstanceIdsFieldName])) ? asset[_dynamicModuleInstanceIdsFieldName].Split(",".ToCharArray()) : new string[0]); if (prefetchMode == PrefetchContentMode.AllInstances || prefetchMode == PrefetchContentMode.StaticInstances) moduleInstanceIds.AddRange((!string.IsNullOrEmpty(asset[_staticModuleInstanceIdsFieldName])) ? asset[_staticModuleInstanceIdsFieldName].Split(",".ToCharArray()) : new string[0]); if (moduleInstanceIds.Count == 0) moduleInstanceIds.Add(moduleInstanceId); // Get module content from services var showAdmin = context.IsPublishing ? "false" : "true"; var moduleContentUrl = _servicesBaseUrl + "/public/v1/modules/instances/" + string.Join(",", moduleInstanceIds) + "?showAdmin=" + showAdmin + "&authtoken=" + _authToken + (context.IsPublishing ? "&serviceProxyEndpoint=" + Util.UrlEncode(Ih_ModuleHelper.ServiceProxyEndpoint) : string.Empty) + "&noredirect=true"; var httpParams = new GetHttpParams(); // Add custom headers to request if (headers != null) { foreach (var header in headers) { httpParams.AddHeader(header.Key + ": " + header.Value); } } var result = CrownPeak.CMSAPI.Util.GetHttp(moduleContentUrl, httpParams); // Determine if request succeeded if ((result.StatusCode != (int)HttpStatusCode.OK) && failCount == 0) { failCount++; _authToken = Ih_AuthenticationHelper.GetAuthToken(true, context.UserInfo, _configSettings); return GetModuleInstanceContent(moduleInstanceId, asset, context, headers, prefetchMode, failCount); } if (result.StatusCode == (int)HttpStatusCode.NotFound) { return "<div style=\"border:1px solid black;padding:5px;text-align:center;\">Layout for Module Instance not defined</div>"; } else { // Add module contents to cache and return var markupParts = result.ResponseText.Split(new string[] { _moduleMarkupDelimiter }, StringSplitOptions.None); var markupPartsIndex = 0; foreach (var instanceId in moduleInstanceIds) { if (markupPartsIndex < markupParts.Length) { var tempCacheKey = "GetModuleInstanceContent_" + instanceId; context.UserVariables[tempCacheKey] = markupParts[markupPartsIndex]; markupPartsIndex++; } } return context.UserVariables[cacheKey]; } } else { return "<div style=\"border:1px solid black;padding:5px;text-align:center;\">Unable to authenticate with services.</div>"; } }