public HttpResponseMessage HandleResponse(IOnPremiseConnectorRequest request, HttpRequestMessage message, IPrincipal clientUser, IOnPremiseConnectorResponse response, bool forwardOnPremiseTargetErrorResponse)
        {
            if (_responseInterceptor == null)
            {
                return(_httpResponseMessageBuilder.BuildFromConnectorResponse(response, forwardOnPremiseTargetErrorResponse, request.RequestId));
            }

            _logger.Verbose("Handling response. request-id={RequestId}", request.RequestId);

            try
            {
                var interceptedRequest = CreateInterceptedRequest(request, message, clientUser);

                HttpResponseMessage immediateResponse = null;
                if (response == null)
                {
                    immediateResponse = _responseInterceptor.OnResponseFailed(interceptedRequest);
                }
                else
                {
                    var interceptedResponse = new InterceptedResponse(_logger.ForContext <IInterceptedResponse>(), response);
                    immediateResponse = _responseInterceptor.OnResponseReceived(interceptedRequest, interceptedResponse);

                    if (immediateResponse == null)
                    {
                        return(_httpResponseMessageBuilder.BuildFromConnectorResponse(interceptedResponse, forwardOnPremiseTargetErrorResponse, request.RequestId));
                    }
                }

                if (immediateResponse != null)
                {
                    immediateResponse.RequestMessage = message;
                }

                return(immediateResponse ?? _httpResponseMessageBuilder.BuildFromConnectorResponse(null, forwardOnPremiseTargetErrorResponse, request.RequestId));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error while executing the response interceptor. type-name={InterceptorType}, request-id={RequestId}", _requestInterceptor?.GetType().Name, request.RequestId);
                return(null);
            }
        }
        public async Task <HttpResponseMessage> Relay(string fullPathToOnPremiseEndpoint)
        {
            _logger?.Debug("Relaying request. method={RequestMethod}, path={RequestPath}", ControllerContext.Request.Method, fullPathToOnPremiseEndpoint);

            if (fullPathToOnPremiseEndpoint == null)
            {
                _logger?.Information("Path to on premise endpoint is not set");
                return(NotFound());
            }

            var pathInformation = _pathSplitter.Split(fullPathToOnPremiseEndpoint);
            var link            = _linkRepository.GetLink(pathInformation.UserName);

            if (!CanRequestBeHandled(fullPathToOnPremiseEndpoint, pathInformation, link))
            {
                _logger?.Information("Request cannot be handled");
                return(NotFound());
            }

            var request = await _onPremiseRequestBuilder.BuildFromHttpRequest(Request, _backendCommunication.OriginId, pathInformation.PathWithoutUserName).ConfigureAwait(false);

            var statusCode = HttpStatusCode.GatewayTimeout;
            IOnPremiseConnectorResponse response = null;

            try
            {
                request = _interceptorManager.HandleRequest(request, Request, User, out var message);
                if (message != null)
                {
                    _logger?.Verbose("Interceptor caused direct answering of request. request-id={RequestId}, status-code={ResponseStatusCode}", request.RequestId, message.StatusCode);

                    statusCode = message.StatusCode;

                    if (request.AlwaysSendToOnPremiseConnector)
                    {
                        _logger?.Verbose("Interceptor caused always sending of request. request-id={RequestId}", request.RequestId);
                        SendOnPremiseConnectorRequest(link.Id, request);
                    }

                    return(message);
                }

                var task = _backendCommunication.GetResponseAsync(request.RequestId);
                SendOnPremiseConnectorRequest(link.Id, request);

                _logger?.Verbose("Waiting for response. request-id={RequestId}, link-id={LinkId}", request.RequestId, link.Id);
                response = await task.ConfigureAwait(false);

                if (response != null)
                {
                    _logger?.Verbose("Response received. request-id={RequestId}, link-id={LinkId}", request.RequestId, link.Id);
                    statusCode = response.StatusCode;
                }
                else
                {
                    _logger?.Verbose("No response received because of on-premise timeout. request-id={RequestId}, link-id={LinkId}", request.RequestId, link.Id);
                }

                return(_interceptorManager.HandleResponse(request, Request, User, response) ?? _httpResponseMessageBuilder.BuildFromConnectorResponse(response, link, request.RequestId));
            }
            finally
            {
                FinishRequest(request as OnPremiseConnectorRequest, response, link.Id, fullPathToOnPremiseEndpoint, statusCode);
            }
        }