/// <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>
        /// <returns>A dictionary with the claim names in format aspectName.propertyName as keys.</returns>
        public IDictionary <string, object> GetContextClaims(string aspectName)
        {
            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.");
                }

                // TODO: Not really nice to use HttpContext at this level.
                HttpContext httpContext = HttpContext.Current;
                if (httpContext == null)
                {
                    throw new DxaException("Cannot obtain HttpContext.");
                }
                HttpRequest httpRequest   = httpContext.Request;
                HttpCookie  contextCookie = httpRequest.Cookies["context"];

                IContextMap contextMap;
                try
                {
                    EvidenceBuilder evidenceBuilder = new EvidenceBuilder().With("user-agent", httpRequest.UserAgent);
                    if (contextCookie != null && !string.IsNullOrEmpty(contextCookie.Value))
                    {
                        evidenceBuilder.With("cookie", string.Format("{0}={1}", contextCookie.Name, contextCookie.Value));
                    }
                    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);
            }
        }
Exemplo n.º 2
0
        /// <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);
            }
        }