private async Task HandleRequestValidationException(string correlation, RequestValidationException e)
    {
        _httpContextService.SetStatusCode(HttpStatusCode.NotImplemented);
        _httpContextService.TryAddHeader(CorrelationHeaderKey, correlation);
        if (_settings?.Gui?.EnableUserInterface == true)
        {
            var pageContents =
                StaticResources.stub_not_configured_html_page.Replace("[ROOT_URL]", _httpContextService.RootUrl);
            _httpContextService.AddHeader("Content-Type", Constants.HtmlMime);
            await _httpContextService.WriteAsync(pageContents);
        }

        _logger.LogInformation($"Request validation exception thrown: {e.Message}");
    }
예제 #2
0
 /// <summary>
 /// Handles the middleware.
 /// </summary>
 public async Task Invoke(HttpContext context)
 {
     if (_httpContextService.Path?.Contains("ph-api/") == true)
     {
         try
         {
             await _next(context);
         }
         catch (ConflictException)
         {
             _httpContextService.SetStatusCode(HttpStatusCode.Conflict);
         }
         catch (NotFoundException)
         {
             _httpContextService.SetStatusCode(HttpStatusCode.NotFound);
         }
         catch (ForbiddenException)
         {
             _httpContextService.SetStatusCode(HttpStatusCode.Forbidden);
         }
         catch (ArgumentException ex)
         {
             _httpContextService.SetStatusCode(HttpStatusCode.BadRequest);
             _httpContextService.AddHeader("Content-Type", Constants.JsonMime);
             await _httpContextService.WriteAsync(JsonConvert.SerializeObject(new[] { ex.Message }));
         }
         catch (ValidationException ex)
         {
             _httpContextService.SetStatusCode(HttpStatusCode.BadRequest);
             _httpContextService.AddHeader("Content-Type", Constants.JsonMime);
             await _httpContextService.WriteAsync(JsonConvert.SerializeObject(ex.ValidationErrors));
         }
     }
     else
     {
         await _next(context);
     }
 }
 /// <summary>
 /// Handles the middleware.
 /// </summary>
 public async Task Invoke(HttpContext context)
 {
     if (_httpContextService.Path.Contains("ph-api/"))
     {
         _httpContextService.AddHeader("Access-Control-Allow-Origin", "*");
         _httpContextService.AddHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
         _httpContextService.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
         _httpContextService.AddHeader("Cache-Control", "no-store, no-cache");
         _httpContextService.AddHeader("Expires", "-1");
         if (_httpContextService.GetHeaders().ContainsKey("Origin") && _httpContextService.Method.Equals("OPTIONS"))
         {
             _httpContextService.SetStatusCode(HttpStatusCode.OK);
         }
         else
         {
             await _next(context);
         }
     }
     else
     {
         await _next(context);
     }
 }
예제 #4
0
        // ReSharper disable once UnusedMember.Global
        public async Task Invoke(HttpContext context)
        {
            var path = _httpContextService.Path;

            if (_segmentsToIgnore.Any(s => path.Contains(s, StringComparison.OrdinalIgnoreCase)))
            {
                await _next(context);

                return;
            }

            const string correlationHeaderKey = "X-HttPlaceholder-Correlation";
            var          correlation          = Guid.NewGuid().ToString();
            var          requestLogger        = _requestLoggerFactory.GetRequestLogger();

            requestLogger.SetCorrelationId(correlation);
            try
            {
                // Enable rewind here to be able to read the posted body multiple times.
                _httpContextService.EnableRewind();

                // Log the request here
                requestLogger.LogRequestParameters(
                    _httpContextService.Method,
                    _httpContextService.DisplayUrl,
                    _httpContextService.GetBody(),
                    _clientDataResolver.GetClientIp(),
                    _httpContextService.GetHeaders());

                _httpContextService.ClearResponse();
                _httpContextService.TryAddHeader(correlationHeaderKey, correlation);
                var response = await _stubRequestExecutor.ExecuteRequestAsync();

                _httpContextService.SetStatusCode(response.StatusCode);
                foreach (var(key, value) in response.Headers)
                {
                    _httpContextService.AddHeader(key, value);
                }

                if (response.Body != null)
                {
                    await _httpContextService.WriteAsync(response.Body);
                }
            }
            catch (RequestValidationException e)
            {
                _httpContextService.SetStatusCode((int)HttpStatusCode.InternalServerError);
                _httpContextService.TryAddHeader(correlationHeaderKey, correlation);
                _logger.LogInformation($"Request validation exception thrown: {e.Message}");
            }
            catch (Exception e)
            {
                _httpContextService.SetStatusCode((int)HttpStatusCode.InternalServerError);
                _httpContextService.TryAddHeader(correlationHeaderKey, correlation);
                _logger.LogWarning($"Unexpected exception thrown: {e}");
            }

            var loggingResult        = requestLogger.GetResult();
            var jsonLoggingResult    = JObject.FromObject(loggingResult);
            var enableRequestLogging = _settings.Storage?.EnableRequestLogging ?? false;

            if (enableRequestLogging)
            {
                _logger.LogInformation(jsonLoggingResult.ToString());
            }

            await _stubContext.AddRequestResultAsync(loggingResult);

            // We need to map the model to a DTO here, because the frontend expected that.
            await _requestNotify.NewRequestReceivedAsync(_mapper.Map <RequestResultDto>(loggingResult));
        }