示例#1
0
        public override List <AdapterMessage> ProcessMessage(AdapterMessage message)
        {
            try
            {
                Tracer.WriteBegin();

                NodeParams            p            = NodeParams.Extract(message);
                List <AdapterMessage> responseList = new List <AdapterMessage>();

                String s = message.GetDataAsString();
                if (p.IgnoreCase)
                {
                    if (s.Contains(p.TextToReplace, StringComparison.InvariantCultureIgnoreCase))
                    {
                        s = s.Replace(p.TextToReplace, p.ReplaceWithText, StringComparison.InvariantCultureIgnoreCase);
                        Tracer.WriteLine($"Replaced text '{p.TextToReplace}' with '{p.ReplaceWithText}'.");
                        AdapterMessage response = message.CloneWithNewData(s);
                        return(new List <AdapterMessage>()
                        {
                            response
                        });
                    }
                    else
                    {
                        Tracer.WriteLine("No text to replace found...");
                        return(new List <AdapterMessage>()
                        {
                            message
                        });
                    }
                }
                else
                {
                    if (s.Contains(p.TextToReplace))
                    {
                        s = s.Replace(p.TextToReplace, p.ReplaceWithText);
                        Tracer.WriteLine($"Replaced text '{p.TextToReplace}' with '{p.ReplaceWithText}'.");
                        AdapterMessage response = message.CloneWithNewData(s);
                        return(new List <AdapterMessage>()
                        {
                            response
                        });
                    }
                    else
                    {
                        Tracer.WriteLine("No text to replace found...");
                        return(new List <AdapterMessage>()
                        {
                            message
                        });
                    }
                }
            }
            finally
            {
                Tracer.WriteEnd();
            }
        }
示例#2
0
        private void WriteFile(AdapterMessage message, NodeParams p)
        {
            lock (this)
            {
                string filepath = Path.Combine(p.Path, p.Filename);

                FileMode fileMode = FileMode.Create;
                if (p.AppendToExisting)
                {
                    fileMode = FileMode.Append;
                }

                // Create directory if it does not exist. This enables paths like 'c:\temp\%date%'
                if (Directory.Exists(p.Path) == false)
                {
                    Directory.CreateDirectory(p.Path);
                }

                using (FileStream fs = new FileStream(filepath, fileMode, FileAccess.Write, FileShare.None))
                {
                    // Seek to the end of the file in case we're appending to existing data
                    fs.Seek(0, SeekOrigin.End);
                    if (message.IsString)
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.Write(message.GetDataAsString());
                        }
                    }
                    else
                    {
                        byte[] buffer = message.GetDataAsArray();
                        fs.Write(buffer, 0, buffer.Length);
                    }
                }


                // Preserve the creation time of source if it is configured to do so and we're not appending
                if (p.PreserveCreationTime && fileMode != FileMode.Append)
                {
                    string creationTime = message.Parameters.GetValueAsString("CreationTime");
                    if (creationTime != null)
                    {
                        try
                        {
                            DateTime time = DateTime.Parse(creationTime);
                            System.IO.File.SetCreationTime(filepath, time);
                        }
                        catch (Exception ex)
                        {
                            // Just skip if not possible
                            Tracer.WriteException(ex);
                        }
                    }
                }
            }
        }