Exemplo n.º 1
0
        public T TransformMessage <T>(IMessage message)
        {
            if (message is T)
            {
                return((T)message);
            }
            else
            {
                IMessage result = null;

                if (message is IBytesMessage)
                {
                    IBytesMessage bytesMsg = message as IBytesMessage;
                    bytesMsg.Reset();
                    IBytesMessage msg = DoCreateBytesMessage();

                    try
                    {
                        for (;;)
                        {
                            // Reads a byte from the message stream until the stream is empty
                            msg.WriteByte(bytesMsg.ReadByte());
                        }
                    }
                    catch
                    {
                    }

                    result = msg;
                }
                else if (message is IMapMessage)
                {
                    IMapMessage mapMsg = message as IMapMessage;
                    IMapMessage msg    = DoCreateMapMessage();

                    foreach (string key in mapMsg.Body.Keys)
                    {
                        msg.Body[key] = mapMsg.Body[key];
                    }

                    result = msg;
                }
                else if (message is IObjectMessage)
                {
                    IObjectMessage objMsg = message as IObjectMessage;
                    IObjectMessage msg    = DoCreateObjectMessage();
                    msg.Body = objMsg.Body;

                    result = msg;
                }
                else if (message is IStreamMessage)
                {
                    IStreamMessage streamMessage = message as IStreamMessage;
                    streamMessage.Reset();
                    IStreamMessage msg = DoCreateStreamMessage();

                    object obj = null;

                    try
                    {
                        while ((obj = streamMessage.ReadObject()) != null)
                        {
                            msg.WriteObject(obj);
                        }
                    }
                    catch
                    {
                    }

                    result = msg;
                }
                else if (message is ITextMessage)
                {
                    ITextMessage textMsg = message as ITextMessage;
                    ITextMessage msg     = DoCreateTextMessage();
                    msg.Text = textMsg.Text;
                    result   = msg;
                }
                else
                {
                    result = DoCreateMessage();
                }

                CopyProperties(message, result);

                // Let the subclass have a chance to do any last minute configurations
                // on the newly converted message.
                DoPostProcessMessage(result);

                return((T)result);
            }
        }