/// <summary> /// Gets the context claims. Either all context claims or for a given aspect name. /// </summary> /// <param name="aspectName">The aspect name. If <c>null</c> all context claims are returned.</param> /// <param name="localization">The context Localization.</param> /// <returns>A dictionary with the claim names in format aspectName.propertyName as keys.</returns> public IDictionary <string, object> GetContextClaims(string aspectName, Localization localization) { using (new Tracer(aspectName)) { if (_contextEngineClient == null) { // Apparently an exception occurred in the class constructor; it should have logged the exception already. throw new DxaException("Context Engine Client was not initialized. Check the log file for errors."); } string userAgent = null; string contextCookieValue = null; HttpContext httpContext = HttpContext.Current; // TODO: Not really nice to use HttpContext at this level. if (httpContext != null) { userAgent = httpContext.Request.UserAgent; HttpCookie contextCookie = httpContext.Request.Cookies["context"]; if (contextCookie != null) { contextCookieValue = contextCookie.Value; } } if (string.IsNullOrEmpty(userAgent)) { userAgent = DefaultUserAgent; } IContextMap contextMap; try { EvidenceBuilder evidenceBuilder = new EvidenceBuilder().With("user-agent", userAgent); if (_usePublicationEvidence) { evidenceBuilder.WithPublicationId(Convert.ToInt32(localization.Id)); // TODO: What about URI scheme? } if (!string.IsNullOrEmpty(contextCookieValue)) { evidenceBuilder.With("cookie", string.Format("context={0}", contextCookieValue)); } IEvidence evidence = evidenceBuilder.Build(); contextMap = _contextEngineClient.Resolve(evidence); } catch (Exception ex) { throw new DxaException("An error occurred while resolving evidence using the Context Service.", ex); } IDictionary <string, object> result = new Dictionary <string, object>(); if (string.IsNullOrEmpty(aspectName)) { // Add claims for all aspects. foreach (string aspectKey in contextMap.KeySet) { AddAspectClaims(aspectKey, contextMap, result); } } else { // Add claims for the given aspect. AddAspectClaims(aspectName, contextMap, result); } return(result); } }
/// <summary> /// Gets the context claims. Either all context claims or for a given aspect name. /// </summary> /// <param name="aspectName">The aspect name. If <c>null</c> all context claims are returned.</param> /// <param name="localization">The context Localization.</param> /// <returns>A dictionary with the claim names in format aspectName.propertyName as keys.</returns> public IDictionary <string, object> GetContextClaims(string aspectName, Localization localization) { using (new Tracer(aspectName)) { var contextEngineClient = ODataContextEngineInstance.Instance; if (contextEngineClient == null) { return(new Dictionary <string, object>()); } string userAgent = null; string contextCookieValue = null; HttpContext httpContext = HttpContext.Current; // TODO: Not really nice to use HttpContext at this level. if (httpContext != null) { userAgent = httpContext.Request.UserAgent; HttpCookie contextCookie = httpContext.Request.Cookies["context"]; if (contextCookie != null) { contextCookieValue = contextCookie.Value; } } if (string.IsNullOrEmpty(userAgent)) { userAgent = DefaultUserAgent; } IContextMap contextMap; try { EvidenceBuilder evidenceBuilder = new EvidenceBuilder().With("user-agent", userAgent); if (_usePublicationEvidence) { evidenceBuilder.WithPublicationId(Convert.ToInt32(localization.Id)); // TODO: What about URI scheme? } if (!string.IsNullOrEmpty(contextCookieValue)) { evidenceBuilder.With("cookie", $"context={contextCookieValue}"); } IEvidence evidence = evidenceBuilder.Build(); lock (_lock) { contextMap = contextEngineClient.Resolve(evidence); } } catch (Exception ex) { throw new DxaException("An error occurred while resolving evidence using the Context Service.", ex); } IDictionary <string, object> result = new Dictionary <string, object>(); if (string.IsNullOrEmpty(aspectName)) { // Add claims for all aspects. foreach (string aspectKey in contextMap.KeySet) { AddAspectClaims(aspectKey, contextMap, result); } } else { // Add claims for the given aspect. AddAspectClaims(aspectName, contextMap, result); } return(result); } }
/// <summary> /// Gets the context claims. Either all context claims or for a given aspect name. /// </summary> /// <param name="aspectName">The aspect name. If <c>null</c> all context claims are returned.</param> /// <param name="localization">The context Localization.</param> /// <returns>A dictionary with the claim names in format aspectName.propertyName as keys.</returns> public IDictionary<string, object> GetContextClaims(string aspectName, Localization localization) { using (new Tracer(aspectName)) { if (_contextEngineClient == null) { // Apparently an exception occurred in the class constructor; it should have logged the exception already. throw new DxaException("Context Engine Client was not initialized. Check the log file for errors."); } string userAgent = null; string contextCookieValue = null; HttpContext httpContext = HttpContext.Current; // TODO: Not really nice to use HttpContext at this level. if (httpContext != null) { userAgent = httpContext.Request.UserAgent; HttpCookie contextCookie = httpContext.Request.Cookies["context"]; if (contextCookie != null) { contextCookieValue = contextCookie.Value; } } if (string.IsNullOrEmpty(userAgent)) { userAgent = DefaultUserAgent; } IContextMap contextMap; try { EvidenceBuilder evidenceBuilder = new EvidenceBuilder().With("user-agent", userAgent); if (_usePublicationEvidence) { evidenceBuilder.WithPublicationId(Convert.ToInt32(localization.LocalizationId)); } if (!string.IsNullOrEmpty(contextCookieValue)) { evidenceBuilder.With("cookie", string.Format("context={0}", contextCookieValue)); } IEvidence evidence = evidenceBuilder.Build(); contextMap = _contextEngineClient.Resolve(evidence); } catch (Exception ex) { throw new DxaException("An error occurred while resolving evidence using the Context Service.", ex); } IDictionary<string, object> result = new Dictionary<string, object>(); if (string.IsNullOrEmpty(aspectName)) { // Add claims for all aspects. foreach (string aspectKey in contextMap.KeySet) { AddAspectClaims(aspectKey, contextMap, result); } } else { // Add claims for the given aspect. AddAspectClaims(aspectName, contextMap, result); } return result; } }