Exemplo n.º 1
0
        private string ProcessRequest(Uri requestUri, out bool success)
        {
            using (new Tracer(requestUri))
            {
                Log.Debug($"Sending {ModelServiceName} Request: {requestUri}");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
                request.Timeout     = _serviceTimeout;
                request.ContentType = "application/json; charset=utf-8";
                // handle OAuth if available/required
                if (_tokenProvider != null)
                {
                    request.Headers.Add(_tokenProvider.AuthRequestHeaderName, _tokenProvider.AuthRequestHeaderValue);
                }

                // forward on session preview cookie/header if available in ADF claimstore
                IClaimStore claimStore = AmbientDataContext.CurrentClaimStore;
                if (claimStore != null)
                {
                    Dictionary <string, string[]> headers = claimStore.Get <Dictionary <string, string[]> >(new Uri(WebClaims.REQUEST_HEADERS));
                    if ((headers != null) && headers.ContainsKey(PreviewSessionTokenHeader))
                    {
                        // See CRQ-3935
                        SetCookie(request, PreviewSessionTokenCookie, headers[PreviewSessionTokenHeader][0]);
                    }

                    Dictionary <string, string> cookies = claimStore.Get <Dictionary <string, string> >(new Uri(WebClaims.REQUEST_COOKIES));
                    if ((cookies != null) && cookies.ContainsKey(PreviewSessionTokenCookie))
                    {
                        SetCookie(request, PreviewSessionTokenCookie, cookies[PreviewSessionTokenCookie]);
                    }
                }

                int attempt = 0;
                do
                {
                    try
                    {
                        attempt++;
                        using (WebResponse response = request.GetResponse())
                        {
                            string responseBody = GetResponseBody(response);
                            success = true;
                            return(responseBody);
                        }
                    }
                    catch (WebException ex)
                    {
                        if (ex.Status == WebExceptionStatus.Timeout && attempt < _serviceRetryCount)
                        {
                            Log.Debug("{0} timed out, attempting a retry request for URL '{1}'.", ModelServiceName,
                                      requestUri);
                            Task.Delay(TimeSpan.FromMilliseconds(2000)).Wait();
                        }
                        else
                        {
                            if (ex.Status != WebExceptionStatus.ProtocolError)
                            {
                                throw new DxaException(
                                          $"{ModelServiceName} request for URL '{requestUri}' failed: {ex.Status}", ex);
                            }
                            success = false;
                            return(GetResponseBody(ex.Response));
                        }
                    }
                } while (true);
            }
        }
Exemplo n.º 2
0
        private string PerformRequest(Uri requestUri)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            request.Method      = WebRequestMethods.Http.Get;
            request.Timeout     = Timeout;
            request.ContentType = "application/json; charset=utf-8";
            request.UserAgent   = "Microsoft ADO.NET Data Services";

            // handle OAuth if available/required
            if (_tokenProvider != null)
            {
                request.Headers.Add(_tokenProvider.AuthRequestHeaderName,
                                    _tokenProvider.AuthRequestHeaderValue);
            }

            // forward on session preview cookie/header if available in ADF claimstore
            IClaimStore claimStore = AmbientDataContext.CurrentClaimStore;

            if (claimStore != null)
            {
                Dictionary <string, string[]> headers =
                    claimStore.Get <Dictionary <string, string[]> >(new Uri(WebClaims.REQUEST_HEADERS));
                if ((headers != null) && headers.ContainsKey(PreviewSessionTokenHeader))
                {
                    // See CRQ-3935
                    SetCookie(request, PreviewSessionTokenCookie, headers[PreviewSessionTokenHeader][0]);
                }

                Dictionary <string, string> cookies =
                    claimStore.Get <Dictionary <string, string> >(new Uri(WebClaims.REQUEST_COOKIES));
                if ((cookies != null) && cookies.ContainsKey(PreviewSessionTokenCookie))
                {
                    SetCookie(request, PreviewSessionTokenCookie, cookies[PreviewSessionTokenCookie]);
                }

                // Forward all claims on to model-service
                var forwardedClaimValues = AmbientDataContext.ForwardedClaims;
                if (forwardedClaimValues != null && forwardedClaimValues.Count > 0)
                {
                    Dictionary <Uri, object> forwardedClaims =
                        forwardedClaimValues.Select(claim => new Uri(claim, UriKind.RelativeOrAbsolute))
                        .Distinct()
                        .Where(uri => claimStore.Contains(uri) && claimStore.Get <object>(uri) != null)
                        .ToDictionary(uri => uri, uri => claimStore.Get <object>(uri));

                    if (forwardedClaims.Count > 0)
                    {
                        ClaimCookieSerializer serializer    = new ClaimCookieSerializer(AmbientDataContext.ForwardedClaimsCookieName);
                        List <ClaimsCookie>   claimsCookies = serializer.SerializeClaims(forwardedClaims);
                        foreach (ClaimsCookie claimsCookie in claimsCookies)
                        {
                            byte[] cookieValue = claimsCookie.Value;
                            Cookie cookie      = new Cookie(claimsCookie.Name, new System.Text.ASCIIEncoding().GetString(cookieValue));
                            SetCookie(request, cookie.Name, cookie.Value);
                        }
                    }
                }

                // To support the TridionDocs module (should not require this when model-service is PCA plugin)
                var conditions = claimStore.Get <string>(new Uri("taf:ish:userconditions"));
                if (conditions != null)
                {
                    SetCookie(request, "taf.ish.userconditions", Base64.Encode(conditions));
                }
            }

            int attempt = 0;

            do
            {
                try
                {
                    attempt++;
                    using (WebResponse response = request.GetResponse())
                    {
                        return(GetResponseBody(response));
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.Timeout && attempt < RetryCount)
                    {
                        Task.Delay(TimeSpan.FromMilliseconds(2000)).Wait();
                    }
                    else
                    {
                        if (ex.Status != WebExceptionStatus.ProtocolError)
                        {
                            throw new ModelServiceException(
                                      $"{ModelServiceName} request for URL '{requestUri}' failed: {ex.Status}", ex);
                        }
                        throw new ModelServiceRequestException(GetResponseBody(ex.Response));
                    }
                }
            } while (true);
        }
        private string PerformRequest(Uri requestUri)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            request.Method      = WebRequestMethods.Http.Get;
            request.Timeout     = Timeout;
            request.ContentType = "application/json; charset=utf-8";
            request.UserAgent   = "Microsoft ADO.NET Data Services";

            // handle OAuth if available/required
            if (_tokenProvider != null)
            {
                request.Headers.Add(_tokenProvider.AuthRequestHeaderName,
                                    _tokenProvider.AuthRequestHeaderValue);
            }

            // forward on session preview cookie/header if available in ADF claimstore
            IClaimStore claimStore = AmbientDataContext.CurrentClaimStore;

            if (claimStore != null)
            {
                Dictionary <string, string[]> headers =
                    claimStore.Get <Dictionary <string, string[]> >(new Uri(WebClaims.REQUEST_HEADERS));
                if ((headers != null) && headers.ContainsKey(PreviewSessionTokenHeader))
                {
                    // See CRQ-3935
                    SetCookie(request, PreviewSessionTokenCookie, headers[PreviewSessionTokenHeader][0]);
                }

                Dictionary <string, string> cookies =
                    claimStore.Get <Dictionary <string, string> >(new Uri(WebClaims.REQUEST_COOKIES));
                if ((cookies != null) && cookies.ContainsKey(PreviewSessionTokenCookie))
                {
                    SetCookie(request, PreviewSessionTokenCookie, cookies[PreviewSessionTokenCookie]);
                }
            }

            int attempt = 0;

            do
            {
                try
                {
                    attempt++;
                    using (WebResponse response = request.GetResponse())
                    {
                        return(GetResponseBody(response));
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.Timeout && attempt < RetryCount)
                    {
                        Task.Delay(TimeSpan.FromMilliseconds(2000)).Wait();
                    }
                    else
                    {
                        if (ex.Status != WebExceptionStatus.ProtocolError)
                        {
                            throw new ModelServiceException(
                                      $"{ModelServiceName} request for URL '{requestUri}' failed: {ex.Status}", ex);
                        }
                        throw new ModelServiceRequestException(GetResponseBody(ex.Response));
                    }
                }
            } while (true);
        }