/// <summary>
 /// Create a builder for a new <see cref="IMessage"/> instance pre-populated with
 /// all of the _headers copied from the provided message. The _payload will
 /// also be taken from the provided message.
 /// </summary>
 /// <param name="message">the Message from which the payload and all headers will be copied</param>
 /// <returns>a MessageBuilder containing the payload and the headers from <paramref name="message"/></returns>
 public static MessageBuilder FromMessage(IMessage message)
 {
     AssertUtils.ArgumentNotNull(message, "message must not be null");
     MessageBuilder builder = new MessageBuilder(message.Payload, message);
     return builder;
 }
 /// <summary>
 /// Create a builder for a new <see cref="IMessage"/> instance with the provided payload.
 /// </summary>
 /// <param name="payload">the payload for the new message</param>
 /// <returns>a MessageBuilder containing the payload from <paramref name="payload"/></returns>
 public static MessageBuilder WithPayload(object payload)
 {
     MessageBuilder builder = new MessageBuilder(payload, null);
     return builder;
 }
Ejemplo n.º 3
0
        public IMessage ToMessage(object obj)
        {
            object[] parameters = obj as object[];
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters must not be null");
            }
            if (parameters.Length == 0)
            {
                throw new ArgumentException("Paramter ArrayUtils is required");
            }

            if (parameters.Length != _method.GetParameters().Length)
            {
                throw new ArgumentException("wrong number of parameters: expected " + _method.GetParameters().Length + ", received " + parameters.Length);
            }

            IMessage message = null;
            object   payload = null;
            IDictionary <string, object> headers = new Dictionary <string, object>();

            for (int i = 0; i < parameters.Length; ++i)
            {
                object value = parameters[i];

                MethodParameterMetadata metadata = _parameterMetadata[i];

                if (metadata.HeaderAttr != null)
                {
                    if (value != null)
                    {
                        headers.Add(metadata.HeaderName, value);
                    }
                    else
                    {
                        if (metadata.HeaderAttr.Required)
                        {
                            throw new ArgumentException("header '" + _parameterMetadata[i].HeaderName + "' is required");
                        }
                    }
                }
                else if (metadata.HasHeadersAnnotation)
                {
                    if (value != null)
                    {
                        AddHeadersAnnotatedParameterToMap(value, headers);
                    }
                }
                else if (typeof(IMessage).IsAssignableFrom(metadata.ParameterType))
                {
                    message = (IMessage)value;
                }
                else
                {
                    if (value == null)
                    {
                        throw new ArgumentException("payload object must not be null");
                    }
                    payload = value;
                }
            }
            if (message != null)
            {
                if (headers.Count == 0)
                {
                    return(message);
                }
                return(MessageBuilder.FromMessage(message).CopyHeadersIfAbsent(headers).Build());
            }
            if (payload == null)
            {
                throw new ArgumentException("no parameter available for Message or payload");
            }

            return(MessageBuilder.WithPayload(payload).CopyHeaders(headers).Build());
        }