You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by GitBox <gi...@apache.org> on 2019/07/10 14:47:50 UTC

[GitHub] [activemq-nms-amqp] HavretGC commented on a change in pull request #4: [WIP] Failover implementation

HavretGC commented on a change in pull request #4: [WIP] Failover implementation
URL: https://github.com/apache/activemq-nms-amqp/pull/4#discussion_r302106620
 
 

 ##########
 File path: src/NMS.AMQP/Provider/Amqp/Message/AmqpCodec.cs
 ##########
 @@ -0,0 +1,141 @@
+using System;
+using Amqp.Framing;
+using Amqp.Types;
+using Apache.NMS.AMQP.Message.Facade;
+using Apache.NMS.AMQP.Util;
+
+namespace Apache.NMS.AMQP.Provider.Amqp.Message
+{
+    public class AmqpCodec
+    {
+        public static INmsMessageFacade DecodeMessage(IAmqpConsumer consumer, global::Amqp.Message amqpMessage)
+        {
+            // First we try the easy way, if the annotation is there we don't have to work hard.
+            AmqpNmsMessageFacade result = CreateFromMsgAnnotation(amqpMessage.MessageAnnotations);
+            if (result == null)
+            {
+                // Next, match specific section structures and content types
+                result = CreateWithoutAnnotation(amqpMessage.BodySection, amqpMessage.Properties);
+            }
+
+            if (result != null)
+            {
+                result.Initialize(consumer, amqpMessage);
+                return result;
+            }
+
+            throw new NMSException("Could not create a NMS message from incoming message");
+        }
+
+        private static AmqpNmsMessageFacade CreateFromMsgAnnotation(MessageAnnotations messageAnnotations)
+        {
+            object annotation = messageAnnotations?[SymbolUtil.JMSX_OPT_MSG_TYPE];
+
+            if (annotation != null)
+            {
+                byte type = Convert.ToByte(annotation);
+                switch (type)
+                {
+                    case MessageSupport.JMS_TYPE_MSG:
+                        return new AmqpNmsMessageFacade();
+                    case MessageSupport.JMS_TYPE_BYTE:
+                        return new AmqpNmsBytesMessageFacade();
+                    case MessageSupport.JMS_TYPE_TXT:
+                        return new AmqpNmsTextMessageFacade();
+                    case MessageSupport.JMS_TYPE_OBJ:
+                        return new AmqpNmsObjectMessageFacade();
+                    case MessageSupport.JMS_TYPE_STRM:
+                        return new AmqpNmsStreamMessageFacade();
+                    case MessageSupport.JMS_TYPE_MAP:
+                        return new AmqpNmsMapMessageFacade();
+                    default:
+                        throw new NMSException("Invalid Message Type annotation value found in message: " + annotation);
+                }
+            }
+
+            return null;
+        }
+
+        private static AmqpNmsMessageFacade CreateWithoutAnnotation(RestrictedDescribed body, Properties properties)
+        {
+            Symbol contentType = GetContentType(properties);
+
+            if (body == null)
+            {
+                if (IsContentType(SymbolUtil.SERIALIZED_DOTNET_OBJECT_CONTENT_TYPE, contentType))
+                    return new AmqpNmsObjectMessageFacade();
+                else if (IsContentType(SymbolUtil.OCTET_STREAM_CONTENT_TYPE, contentType) || IsContentType(null, contentType))
+                    return new AmqpNmsBytesMessageFacade();
+                else
+                {
+                    if (IsTextualContent(contentType))
+                        return new AmqpNmsTextMessageFacade();
+                    else
+                        return new AmqpNmsMessageFacade();
+                }
+            }
+            else if (body is Data)
+            {
+                if (IsContentType(SymbolUtil.OCTET_STREAM_CONTENT_TYPE, contentType) || IsContentType(null, contentType))
+                    return new AmqpNmsBytesMessageFacade();
+                else if (IsContentType(SymbolUtil.SERIALIZED_DOTNET_OBJECT_CONTENT_TYPE, contentType))
+                    return new AmqpNmsObjectMessageFacade();
+                else
+                {
+                    if (IsTextualContent(contentType))
+                        return new AmqpNmsTextMessageFacade();
+                    else
+                        return new AmqpNmsBytesMessageFacade();
+                }
+            }
+            else if (body is AmqpValue amqpValue)
+            {
+                object value = amqpValue.Value;
+                if (value == null || value is string)
+                    return new AmqpNmsTextMessageFacade();
+                else if (value is byte[])
+                    return new AmqpNmsBytesMessageFacade();
+                else
+                    return new AmqpNmsObjectMessageFacade();
+            }
+            else if (body is AmqpSequence)
+                return new AmqpNmsObjectMessageFacade();
+
+            return null;
+        }
+
+        private static bool IsTextualContent(Symbol contentType)
+        {
+            return contentType != null && contentType.ToString().Equals("text/plain", StringComparison.InvariantCultureIgnoreCase);
+        }
+
+        private static bool IsContentType(Symbol contentType, Symbol messageContentType)
+        {
+            if (contentType == null)
+                return messageContentType == null;
+            if (messageContentType == null)
+                return false;
+            return contentType.Equals(messageContentType);
+        }
+
+        private static Symbol GetContentType(Properties properties)
+        {
+            return properties?.ContentType;
+        }
+
+        public static void EncodeMessage(AmqpNmsMessageFacade messageFacade)
+        {
+            if (messageFacade.Message.MessageAnnotations != null)
 
 Review comment:
   Good catch! 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services