protected virtual IMessage <T> BuildMessage(object result)
        {
            if (result == null)
            {
                return(null);
            }

            IMessage message = null;
            var      headers = EvaluateHeaders();

            if (result is AbstractMessageBuilder)
            {
                if (headers != null && headers.Count > 0)
                {
                    ((AbstractMessageBuilder)result).CopyHeaders(headers);
                }

                message = ((AbstractMessageBuilder)result).Build();
            }
            else if (result is IMessage)
            {
                message = (IMessage)result;
                if (headers != null && headers.Count > 0)
                {
                    // create a new Message from this one in order to apply headers
                    message = MessageBuilderFactory.FromMessage(message).CopyHeaders(headers).Build();
                }
            }
            else
            {
                message = MessageBuilderFactory.WithPayload(result).CopyHeaders(headers).Build();
            }

            return((IMessage <T>)message);
        }
        protected override object DoReceive()
        {
            var connection = ConnectionFactory.CreateConnection();
            var channel    = connection.CreateChannel(Transacted);

            try
            {
                var resp = channel.BasicGet(QueueName, false);
                if (resp == null)
                {
                    RabbitUtils.CloseChannel(channel);
                    RabbitUtils.CloseConnection(connection);
                    return(null);
                }

                var callback          = AckCallbackFactory.CreateCallback(new RabbitAckInfo(connection, channel, Transacted, resp));
                var envelope          = new Envelope(resp.DeliveryTag, resp.Redelivered, resp.Exchange, resp.RoutingKey);
                var messageProperties = MessageHeaderConverter.ToMessageHeaders(resp.BasicProperties, envelope, EncodingUtils.Utf8);
                var accessor          = RabbitHeaderAccessor.GetMutableAccessor(messageProperties);
                accessor.ConsumerQueue = this.QueueName;

                // Map<String, Object> headers = this.headerMapper.toHeadersFromRequest(messageProperties);
                var message = Message.Create <byte[]>(resp.Body, accessor.MessageHeaders);

                object payload;
                if (BatchingStrategy.CanDebatch(message.Headers))
                {
                    var payloads = new List <object>();
                    BatchingStrategy.DeBatch(message, fragment => payloads.Add(MessageConverter.FromMessage(fragment, null)));
                    payload = payloads;
                }
                else
                {
                    payload = MessageConverter.FromMessage(message, null);
                }

                var builder = MessageBuilderFactory.WithPayload(payload)
                              .CopyHeaders(accessor.MessageHeaders)
                              .SetHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback);
                if (RawMessageHeader)
                {
                    builder.SetHeader(RabbitMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, message);
                    builder.SetHeader(IntegrationMessageHeaderAccessor.SOURCE_DATA, message);
                }

                return(builder);
            }
            catch (Exception e)
            {
                RabbitUtils.CloseChannel(channel);
                RabbitUtils.CloseConnection(connection);
                throw RabbitExceptionTranslator.ConvertRabbitAccessException(e);
            }
        }
Example #3
0
        protected override object DoTransform(IMessage message)
        {
            var headers       = message.Headers;
            var removeHeaders = false;
            var targetClass   = ObtainResolvableTypeFromHeadersIfAny(headers);

            if (targetClass == null)
            {
                targetClass = TargetType;
            }
            else
            {
                removeHeaders = true;
            }

            var    payload = message.Payload;
            object result  = null;

            if (payload is string)
            {
                result = JsonConvert.DeserializeObject((string)payload, targetClass, Settings);
            }
            else if (payload is byte[])
            {
                var contentAsString = DefaultCharset.GetString((byte[])payload);
                result = JsonConvert.DeserializeObject(contentAsString, targetClass, Settings);
            }
            else
            {
                throw new MessageConversionException("Failed to convert Message content, message missing byte[] or string: " + payload.GetType());
            }

            if (removeHeaders)
            {
                return(MessageBuilderFactory
                       .WithPayload(result)
                       .CopyHeaders(headers)
                       .RemoveHeaders(MessageHeaders.TYPE_ID, MessageHeaders.CONTENT_TYPE_ID, MessageHeaders.KEY_TYPE_ID)
                       .Build());
            }

            return(result);
        }
        protected virtual IMessage <T> BuildMessage(object result)
        {
            if (result == null)
            {
                return(null);
            }

            var headers = EvaluateHeaders();

            IMessage message;

            switch (result)
            {
            case AbstractMessageBuilder amBuilder:
                if (headers != null && headers.Count > 0)
                {
                    amBuilder.CopyHeaders(headers);
                }

                message = amBuilder.Build();
                break;

            case IMessage mResult:
                message = mResult;
                if (headers != null && headers.Count > 0)
                {
                    // create a new Message from this one in order to apply headers
                    message = MessageBuilderFactory.FromMessage(message).CopyHeaders(headers).Build();
                }

                break;

            default:
                message = MessageBuilderFactory.WithPayload(result).CopyHeaders(headers).Build();
                break;
            }

            return((IMessage <T>)message);
        }
Example #5
0
        protected override object DoTransform(IMessage message)
        {
            var payload     = BuildJsonPayload(message.Payload);
            var accessor    = MessageHeaderAccessor.GetMutableAccessor(message);
            var contentType = accessor.ContentType;

            if (string.IsNullOrEmpty(contentType))
            {
                accessor.ContentType = ContentType;
            }

            var headers = accessor.MessageHeaders;

            _defaultTypeMapper.FromType(message.Payload.GetType(), headers);
            if (ResultType == typeof(string))
            {
                return(MessageBuilderFactory.WithPayload <string>((string)payload).CopyHeaders(headers).Build());
            }
            else
            {
                return(MessageBuilderFactory.WithPayload <byte[]>((byte[])payload).CopyHeaders(headers).Build());
            }
        }