Example #1
0
        public void WriteToInbox(IBizMessage message)
        {
            string identifier = Guid.NewGuid().ToString("N").Substring(0, 15);

            string    name = DateTime.UtcNow.Ticks + "-" + identifier + "inboxMessage";
            CloudBlob blob = inboxContainer.GetBlobReference(name);

            using (Stream stream = BizMessageFactory.WriteBizMessageToStream(message))
            {
                blob.UploadFromStream(stream);
            }
        }
Example #2
0
 public void SaveBizMessage(IBizMessage message)
 {
     if (!string.IsNullOrEmpty(message.StoreId))
     {
         CloudBlob blob = inboxContainer.GetBlobReference(message.StoreId);
         if (blob != null)
         {
             Stream stream = BizMessageFactory.WriteBizMessageToStream(message);
             blob.UploadFromStream(stream);
         }
     }
 }
Example #3
0
        public static Stream WriteBizMessageToStream(IBizMessage message)
        {
            IList <KeyValuePair <string, string> > headerList = new List <KeyValuePair <string, string> >();

            headerList.Add(new KeyValuePair <string, string>(BizMessageType, message.Type.ToString()));
            headerList.Add(new KeyValuePair <string, string>(BizUniqueId, message.UniqueId));
            headerList.Add(new KeyValuePair <string, string>(BizCreatedTime, message.CreatedTime.ToString()));

            int    index = 0;
            string key;
            string value;

            foreach (BizMessageAction action in message.Actions)
            {
                key   = PayloadSection + IndexSeperator + index;
                value = action.PayloadSection;
                headerList.Add(new KeyValuePair <string, string>(key, value));

                key   = Description + IndexSeperator + index;
                value = action.Description;
                headerList.Add(new KeyValuePair <string, string>(key, value));

                key   = Status + IndexSeperator + index;
                value = action.Status.ToString();
                headerList.Add(new KeyValuePair <string, string>(key, value));

                key   = CreatedTime + IndexSeperator + index;
                value = action.CreatedTime.ToString();
                headerList.Add(new KeyValuePair <string, string>(key, value));

                key   = LastUpdateTime + IndexSeperator + index;
                value = action.LastUpdateTime.ToString();
                headerList.Add(new KeyValuePair <string, string>(key, value));

                index++;
            }

            Stream headerStream = StreamHelper.SerializeHeadersToStream(headerList, true);
            Stream bodyStream   = new MemoryStream(Encoding.UTF8.GetBytes(message.Payload));

            return(new ConcatenatingStream(headerStream, bodyStream, null));
        }
Example #4
0
        public IList <IBizMessage> GetBizMessageList()
        {
            IEnumerable <IListBlobItem> items   = inboxContainer.ListBlobs();
            IList <IBizMessage>         msgList = new List <IBizMessage>();

            foreach (IListBlobItem item in items)
            {
                string       blobId       = item.Uri.ToString();
                CloudBlob    blob         = inboxContainer.GetBlobReference(blobId);
                MemoryStream memoryStream = new MemoryStream();
                blob.DownloadToStream(memoryStream);
                memoryStream.Position = 0;
                IBizMessage message = BizMessageFactory.CreateBizMessageFromStream(memoryStream);
                if (message != null)
                {
                    message.StoreId = blobId;   // TBD: this is temporary solution
                    msgList.Add(message);
                }
            }

            return(msgList);
        }
Example #5
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);
        }