Exemple #1
0
        public static Stream CreateSourceStream(IBaseMessage pInMsg)
        {
            SeekableReadOnlyStream stream = new SeekableReadOnlyStream(pInMsg.BodyPart.GetOriginalDataStream());
            Stream sourceStream = pInMsg.BodyPart.GetOriginalDataStream();

            return sourceStream;
        }
Exemple #2
0
        public static IBaseMessagePart CreateIBaseMsgPart(IBaseMessage pInMsg, ref long position, ref Stream sourceStream)
        {
            if (!sourceStream.CanSeek)
            {
                SeekableReadOnlyStream seekableStream = new SeekableReadOnlyStream(sourceStream);

                pInMsg.BodyPart.Data = seekableStream;

                sourceStream = pInMsg.BodyPart.Data;
            }

            position = sourceStream.Position;

            sourceStream.Position = position;

            IBaseMessagePart bodyPart = pInMsg.BodyPart;

            return bodyPart;
        }
        private static void GetDocumentFormatAndType(IBaseMessage pInMsg, ref string documentType, ref SeekableReadOnlyStream stream, 
                                                     ref Stream sourceStream, XmlDocument inDocument, ref string sourceString, 
                                                     XmlDocument receivedDocument)
        {
            if (receivedDocument != null)
            {
                documentType = GetDocumentTypeFromXml(documentType, inDocument, receivedDocument);
            }
            else
            {
                string fileLabel = string.Empty;

                GetDocumentTypeFromBinary(pInMsg, ref documentType, ref fileLabel);
            }
        }
        private static Stream ExtractSourceStreamFromReceivedMessage(IBaseMessage pInMsg)
        {
            SeekableReadOnlyStream stream = new SeekableReadOnlyStream(pInMsg.BodyPart.GetOriginalDataStream());
            Stream sourceStream = pInMsg.BodyPart.GetOriginalDataStream();
            sourceStream.Position = 0;

            return sourceStream;
        }
        private static void ApplyWindowsEncodingToXml(IBaseMessage pInMsg, ref SeekableReadOnlyStream stream, ref Stream sourceStream,
                                                      XmlDocument inDocument, ref string sourceString, Encoding oWindowsEnc)
        {
            // Get the text and byte array of the source file.
            StreamReader oStreamReader = null;

            string sSourceContent = inDocument.OuterXml;

            // Pass the source file through the XML DOM and get the parsed XML back.
            XmlDocument oParserDoc = new XmlDocument();
            oParserDoc.LoadXml(sSourceContent);
            string sParsedSourceXML = oParserDoc.OuterXml;
            byte[] bDestSourceFile = oWindowsEnc.GetBytes(sParsedSourceXML);

            // Get the XML String, write it back to a virtual stream, read it from the virtual
            // stream again.
            string sDestXML = oWindowsEnc.GetString(bDestSourceFile);
            VirtualStream oDestStream = new VirtualStream();
            StreamWriter oStreamWriter = new StreamWriter(oDestStream);
            oStreamWriter.BaseStream.Position = 0;
            oStreamWriter.BaseStream.Write(bDestSourceFile, 0, bDestSourceFile.Length);
            oStreamReader = new StreamReader(oDestStream);
            oDestStream.Position = 0;
            sourceString = oStreamReader.ReadToEnd();

            sourceStream.Position = 0;
            VirtualStream oStream = new VirtualStream();

            XmlDocument d = new XmlDocument();
            d.LoadXml(sourceString);
            d.Save(oStream);

            oStream.Position = 0;
            pInMsg.BodyPart.Data = oStream;
            stream = new SeekableReadOnlyStream(oStream);
            sourceStream = oStream;
            // END_OBERHOS_C1: Encoding change

            //remove xml declaration
            sourceString = sourceString.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
            sourceString = sourceString.Replace("<?xml version=\"1.0\" encoding=\"utf-16\" ?>", "");
            sourceString = sourceString.Replace("<?xml version=\"1.0\"?>", "");

            inDocument.LoadXml(sourceString);
        }
        private void PromoteProperties(IBaseMessage pInMsg, ref string documentType, string nodeName, ref SeekableReadOnlyStream stream, 
                                       ref Stream sourceStream, XmlDocument inDocument, ref string sourceString, bool isFlatFile, 
                                       string originalFileName)
        {
            XmlDocument receivedDocument = ValidateXml(sourceStream);

            GetDocumentFormatAndType(pInMsg, ref documentType, ref stream, ref sourceStream, inDocument,
                                     ref sourceString, receivedDocument);

            string txNum = GetDocumentTransactionNumber(documentType, nodeName, receivedDocument);

            PromoteContextProperties(pInMsg, documentType, txNum, IsFlatFile,originalFileName);

            stream.Position = 0;
            pInMsg.BodyPart.Data = stream;
        }
        public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            string documentType = string.Empty;
            string nodeName = string.Empty;
            string defaultContextNamespace = string.Empty;
            string pipelineActivityId = string.Empty;
            string sendActivityId = string.Empty;
            string activityId = string.Empty;
            string txNo = string.Empty;
            string TrackingId = string.Empty;
            string originalFileExtenstion = string.Empty;
            string interChangeId = string.Empty;
            string originalFileName = string.Empty;

            long position = 0;

            try
            {
                lock (outputMessages)
                {
                    interChangeId = (string)pInMsg.Context.Read("InterchangeID", "http://schemas.microsoft.com/BizTalk/2003/system-properties");
                    interChangeId = interChangeId.Replace("{", "").Replace("}", "").ToLower();

                    #region / * Internal Pipeline Gunk (Not refactorable)*/
                    if (null == pContext)
                        throw new ArgumentNullException("pContext");

                    if (null == pInMsg)
                        throw new ArgumentNullException("pInMsg");

                    if (null == pInMsg.BodyPart || null == pInMsg.BodyPart.GetOriginalDataStream())
                        throw new ArgumentNullException("pInMsg");

                    SeekableReadOnlyStream stream = new SeekableReadOnlyStream(pInMsg.BodyPart.GetOriginalDataStream());
                    Stream sourceStream = pInMsg.BodyPart.GetOriginalDataStream();

                    // Check if source stream can seek
                    if (!sourceStream.CanSeek)
                    {
                        // Create a virtual (seekable) stream
                        SeekableReadOnlyStream seekableStream = new SeekableReadOnlyStream(sourceStream);

                        // Set new stream for the body part data of the input message. This new stream will then used for further processing.
                        // We need to do this because input stream may not support seeking, so we wrap it with a seekable stream.
                        pInMsg.BodyPart.Data = seekableStream;

                        // Replace sourceStream with a new seekable stream wrapper
                        sourceStream = pInMsg.BodyPart.Data;
                    }

                    // Preserve the stream position
                    position = sourceStream.Position;
                    #endregion

                    pInMsg.BodyPart.Data.Position = 0;

                    XmlDocument inDocument = new XmlDocument();
                    string sourceString = string.Empty;

                    string fileName = string.Empty;

                    fileName = GetReceivedFileName(pInMsg, fileName);

                    originalFileExtenstion = fileName.Split(".".ToCharArray())[1].ToString();

                    PromoteProperties(pInMsg, ref documentType, nodeName, ref stream, ref sourceStream, inDocument, ref sourceString, IsFlatFile, fileName);

                    WriteMessageToLogDocs(pContext, pInMsg, ref documentType, ref txNo, ref TrackingId, ref position, ref sourceStream, originalFileExtenstion);

                    if (!IsFlatFile)
                    {
                        originalFileName = fileName;

                        pInMsg.BodyPart.Data.Position = 0;
                        inDocument.Load(pInMsg.BodyPart.Data);

                        PromoteBasePropertiesForXml(pInMsg, documentType, txNo, inDocument);

                        RouteMessageViaItinerary(pContext, pInMsg, documentType, originalFileName, interChangeId);
                    }
                    else
                    {
                        originalFileName = string.Empty;

                        originalFileName = FilterContextPropertiesForFileLabel(pInMsg, originalFileName);

                        originalFileName = GetTransactionNumberForFlatFile(originalFileName);

                        string configurationState = GetConfigurationCultureForFlatFile(originalFileName);

                        PromoteBasePropertiesForFlatFile(pInMsg, documentType, originalFileName);

                        RouteFlatMessageViaItinerary(pContext, pInMsg, documentType, string.Empty, originalFileName, interChangeId,
                                                     configurationState);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #8
0
        public bool Probe(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            // Check arguments
            if (null == pContext)
            {
                throw new ArgumentNullException("pContext");
            }

            if (null == pInMsg)
            {
                throw new ArgumentNullException("pInMsg");
            }

            // We need to determine a document schema to use based on message content. For the sake of simplicity of this
            // sample, we will check the first two characters in input stream and map them to some schema message types we
            // have predefined. The more sofisticated component could use UI configuration options to map identification
            // text located at specified offsets in message stream and having specified length, which could map to specified
            // message type or document spec type name.

            // Check whether input message doesn't have a body part or it is set to null, fail probe in those cases
            if (null == pInMsg.BodyPart || null == pInMsg.BodyPart.GetOriginalDataStream())
            {
                return(false);
            }

            SeekableReadOnlyStream stream = new SeekableReadOnlyStream(pInMsg.BodyPart.GetOriginalDataStream());
            Stream sourceStream           = pInMsg.BodyPart.GetOriginalDataStream();

            // Check if source stream can seek
            if (!sourceStream.CanSeek)
            {
                // Create a virtual (seekable) stream
                SeekableReadOnlyStream seekableStream = new SeekableReadOnlyStream(sourceStream);

                // Set new stream for the body part data of the input message. This new stream will then used for further processing.
                // We need to do this because input stream may not support seeking, so we wrap it with a seekable stream.
                pInMsg.BodyPart.Data = seekableStream;

                // Replace sourceStream with a new seekable stream wrapper
                sourceStream = pInMsg.BodyPart.Data;
            }

            // Preserve the stream position
            long position = sourceStream.Position;

            char [] signature = new char[2];
            try
            {
                // Read signature from a stream
                StreamReader reader = new StreamReader(sourceStream);
                if (reader.Read(signature, 0, signature.Length) < signature.Length)
                {
                    return(false);
                }

                // Don't close stream reader to avoid closing of underlying stream
            }
            finally
            {
                // Restore the stream position
                sourceStream.Position = position;
            }

            // Get message type from signature
            string messageType = GetMessageType(new string(signature));

            // Fail if message type is unknown
            if (null == messageType)
            {
                return(false);
            }

            // Get document spec from the message type
            IDocumentSpec documentSpec = pContext.GetDocumentSpecByType(messageType);

            // Instead of loading schema to get a document spec type name we could change implementation to return defined
            // during a design time document spec type name and directly specify it in the call below:

            // Write document spec type name to the message context so Flat File disassembler could access this property and
            // do message processing for a schema which has document spec type name we've discovered
            pInMsg.Context.Write(DocumentSpecNamePropertyName, XmlNormNamespaceURI, documentSpec.DocSpecStrongName);

            // Delegate call to Flat File disassembler
            return(disassembler.Probe(pContext, pInMsg));
        }