Esempio n. 1
0
        public static IBizMessage CreateBizMessageFromStream(Stream stream)
        {
            IList <KeyValuePair <string, string> > headerList;
            MemoryStream bodyStream = null;

            try
            {
                bodyStream = StreamHelper.DeserializeStreamToHeadersAndBody(stream, out headerList);
            }
            catch (ArgumentOutOfRangeException)
            {
                return(null);
            }

            IBizMessage message = CreateBizMessage();

            message.Payload = Encoding.UTF8.GetString(bodyStream.GetBuffer(), (int)bodyStream.Position, (int)bodyStream.Length - (int)bodyStream.Position);
            int prevIndex = -1;
            int currentIndex;
            BizMessageAction currentAction = new BizMessageAction();

            foreach (KeyValuePair <string, string> keyValuePair in headerList)
            {
                string key = keyValuePair.Key;

                if (string.Compare(key, BizMessageType, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    message.Type = (BizMessageType)Enum.Parse(typeof(BizMessageType), keyValuePair.Value, true);
                }
                else if (string.Compare(key, BizUniqueId, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    message.UniqueId = keyValuePair.Value;
                }
                else if (string.Compare(key, BizCreatedTime, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    DateTime createdTime;
                    DateTime.TryParse(keyValuePair.Value, out createdTime);
                    message.CreatedTime = createdTime;
                }

                //now you are reading a list field
                else if ((currentIndex = key.IndexOf(IndexSeperator)) > 0)
                {
                    //read the list till you have the same index
                    string fieldName = key.Substring(0, currentIndex);
                    currentIndex = int.Parse(key.Substring(currentIndex + 1));

                    if (prevIndex != currentIndex)
                    {
                        //create a new action
                        currentAction = new BizMessageAction();
                        message.Actions.Add(currentAction);
                    }

                    if (string.Compare(fieldName, PayloadSection, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        currentAction.PayloadSection = keyValuePair.Value;
                    }

                    else if (string.Compare(fieldName, Description, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        currentAction.Description = keyValuePair.Value;
                    }

                    else if (string.Compare(fieldName, Status, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        currentAction.Status = (MessageActionStatus)Enum.Parse(typeof(MessageActionStatus), keyValuePair.Value, true);
                    }

                    else if (string.Compare(fieldName, CreatedTime, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        currentAction.CreatedTime = DateTime.Parse(keyValuePair.Value);
                    }

                    else if (string.Compare(fieldName, LastUpdateTime, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        currentAction.LastUpdateTime = DateTime.Parse(keyValuePair.Value);
                    }


                    prevIndex = currentIndex;
                }
            }

            // old message may not have unique id and created time, so need to assign them with something
            if (string.IsNullOrEmpty(message.UniqueId))
            {
                message.UniqueId = Guid.NewGuid().ToString();
            }
            if (message.CreatedTime == DateTime.MinValue)
            {
                message.CreatedTime = DateTime.Now;
            }

            return(message);
        }