Esempio n. 1
0
        /// <summary>
        /// Decode the given stream into a collection of parts.
        /// </summary>
        /// <returns></returns>
        public ITransmissionPartCollection Decode(string name, Stream stream, string mimeType, string id = null)
        {
            ParameterCheck.ParameterRequired(stream, "stream");
            ParameterCheck.StringRequiredAndNotWhitespace(mimeType, "mimeType");

            var transmissionPartCollection = new TransmissionPartCollection();

            try {
                int nextContentId = 1;

                var mime = new Mime(stream);
                Console.WriteLine("Parts " + mime.NumParts);
                logger.DebugFormat("Parts {0}", mime.NumParts);

                for (int partIndex = 0; partIndex < mime.NumParts; partIndex++)
                {
                    Mime   mimePart  = mime.GetPart(partIndex);
                    string contentId = mimePart.GetHeaderField("Content-id");
                    if (contentId.Trim().Length == 0)
                    {
                        var sb = new StringBuilder();
                        sb.Append("OAIPART_"); //TODO determine better name for mime part than OAIPART_
                        sb.Append(DateTime.Now.Ticks.ToString());
                        sb.Append("_");
                        sb.Append(nextContentId.ToString());
                        contentId = sb.ToString();
                        nextContentId++;
                    }

                    string contentType = mimePart.ContentType;

                    //content type may contain ;char-encoding.  Strip it off if found.
                    if (contentType != null)
                    {
                        contentType = contentType.NormalizeContentType();
                    }

                    //TODO We need better way to get a BodyStream from a MimePart.
                    transmissionPartCollection.Add(transmissionPartFactory.CreateTransmissionPart(name,
                                                                                                  new MemoryStream(mimePart.GetBodyBinary()),
                                                                                                  contentType, contentId));
                }
            }
            catch (Exception err) {
                logger.Error(Messages.MimeEncoding_Decode_GeneralDecodingError, err);
                throw new JdfException(Messages.MimeEncoding_Decode_GeneralDecodingError, err);
            }

            if (transmissionPartCollection.Count == 0)
            {
                logger.Error(Messages.MimeEncoding_Decode_NoMessagePartsToDecode);
                throw new JdfException(Messages.MimeEncoding_Decode_NoMessagePartsToDecode);
            }

            return(transmissionPartCollection);
        }