Example #1
0
        private IHttpRequestFeature CreateHttpRequest(IMqRequestFeature mqRequest, IHeaderConverter headerConverter)
        {
            var props   = mqRequest.Properties;
            var headers = props?.Headers;

            var httpRequest = new HttpRequestFeature
            {
                Scheme   = "amqp",
                Protocol = _mqConnection.Connection.Protocol.ApiName,
            };

            httpRequest.PathBase = _pathBaseBase
                                   + "/" + (mqRequest.Exchange ?? String.Empty)
                                   + "/" + (mqRequest.RoutingKey ?? String.Empty);

            if (mqRequest.Body != null)
            {
                httpRequest.Body = new MemoryStream(mqRequest.Body, writable: false);
                httpRequest.Headers[HeaderNames.ContentLength] = mqRequest.Body.LongLength.ToString();
            }

            if (props != null)
            {
                // TODO: fill headers from props.
            }

            if (headers != null)
            {
                if (headers.TryGetValue(HeaderNames.Method, out var rawMethod))
                {
                    httpRequest.Method = headerConverter.ReadString(rawMethod);
                }
                if (headers.TryGetValue(HeaderNames.Path, out var rawPath))
                {
                    httpRequest.Path = headerConverter.ReadString(rawPath);
                }
                if (headers.TryGetValue(HeaderNames.QueryString, out var rawQueryString))
                {
                    httpRequest.QueryString = headerConverter.ReadString(rawQueryString);
                }

                foreach (var item in headers)
                {
                    httpRequest.Headers[item.Key] = headerConverter.Read(item.Value);
                }
            }

            return(httpRequest);
        }
Example #2
0
        public void InitializeRequest(IMqRequestFeature mqRequest)
        {
            var headerConverter = _serverFeatures.Get <IHeaderConverter>();

            var mqResponse   = CreateMqResponse(mqRequest);
            var httpRequest  = CreateHttpRequest(mqRequest, headerConverter);
            var httpResponse = CreateHttpResponse(mqResponse);

            Features = new FeatureCollection(_serverFeatures);
            Features.Set <IMqConnection>(_mqConnection);
            Features.Set <IMqRequestFeature>(mqRequest);
            Features.Set <IMqResponseFeature>(mqResponse);
            Features.Set <IHttpRequestFeature>(httpRequest);
            Features.Set <IHttpResponseFeature>(httpResponse);
        }
Example #3
0
        private IMqResponseFeature CreateMqResponse(IMqRequestFeature mqRequest)
        {
            var mqResponse = new MqResponseFeature
            {
                BodyStream     = new MemoryStream(),
                Mandatory      = false,
                Properties     = _mqConnection.Model.CreateBasicProperties(),
                AckDeliveryTag = mqRequest.DeliveryTag,
            };

            mqResponse.Properties.Headers = new Dictionary <String, Object>();

            var requestProps = mqRequest.Properties;

            if (requestProps.IsReplyToPresent() && requestProps.ReplyToAddress != null)
            {
                var addr = requestProps.ReplyToAddress;
                mqResponse.Exchange   = addr.ExchangeName;
                mqResponse.RoutingKey = addr.RoutingKey;
            }

            return(mqResponse);
        }
        public async Task ProcessRequestAsync <TContext>(IMqConnection connection, IHttpApplication <TContext> application, IMqRequestFeature requestFeature)
        {
            try
            {
                var serverContext = _serverContextFactory.CreateContext(connection);
                serverContext.InitializeRequest(requestFeature);
                var features = serverContext.Features;

                var       context          = application.CreateContext(features);
                Exception contextException = null;
                try
                {
                    Exception requestException = null;
                    try
                    {
                        await application.ProcessRequestAsync(context);
                    }
                    catch (Exception ex)
                    {
                        requestException = ex;
                        _logger.LogError(ex, "Request processing error");
                    }

                    if (requestException != null)
                    {
                        serverContext.SetError(StatusCodes.Status500InternalServerError, requestException);
                    }

                    await serverContext.SendResponseAsync();
                }
                catch (Exception ex)
                {
                    contextException = ex;
                    throw;
                }
                finally
                {
                    application.DisposeContext(context, contextException);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Processing error");
            }
        }
 public abstract Task ProcessRequestAsync(IMqConnection connection, IMqRequestFeature requestFeature);