static void TestReadListResponse() { IConfigurationRepository configurationRepository = new ConfigFileConfigurationRepository(); loggingService = new Log4NetLoggingService(configurationRepository, new ThreadContextService()); LogProviderContext.Current = loggingService; string filename = @"C:\Temp\LocalDataRequestMessage_2014_10_16_10_33_50.xml"; XmlDocument listResponse = new XmlDocument(); listResponse.Load(filename); IMessageReader reader = new DefaultMessageReader(loggingService, configurationRepository, listResponse.ToXDocument()); reader.ReadMessage(); string[] results = reader.GetMessageResults <string[]>(); string qualifier = reader.GetQualifier(); string function = reader.GetFunction(); DataTable listResults = reader.GetMessageResults <DataTable>(); GovTalkMessage message = reader.Message(); Console.WriteLine("Message from {0}", message.Header.MessageDetails.ResponseEndPoint.Value.ToString()); }
public static void TestDecompressMessage(XmlDocument compressedXmlDocument) { string contents = CommonUtilityHelper.DecompressData(compressedXmlDocument.XmlToBytes()); XmlDocument decompressedDocument = new XmlDocument(); decompressedDocument.LoadXml(contents); DefaultMessageReader reader = new DefaultMessageReader(loggingService, configurationRepository, decompressedDocument.ToXDocument()); string[] results = reader.GetMessageResults <string[]>(); Console.WriteLine(reader.GetQualifier()); }
static void TestReadErrors(IConfigurationRepository configurationRepository, ILoggingService loggingService) { XDocument errorDoc = XDocument.Load(@"C:\Temp\test_reply__error_20150825151729.xml"); IMessageReader reader = new DefaultMessageReader(loggingService, configurationRepository, errorDoc); reader.ReadMessage(); DataTable errorTable = reader.GetMessageResults <DataTable>(); foreach (DataColumn column in errorTable.Columns) { Console.Write("\t{0}", column.ColumnName); } Console.WriteLine(""); foreach (DataRow row in errorTable.Rows) { foreach (DataColumn column in errorTable.Columns) { Console.Write("\t{0}", row[column]); } Console.WriteLine(""); } errorTable.Clear(); errorDoc = XDocument.Load(@"C:\Temp\RequestMessage_1422880486_File14393268203594585061_error_20150202123518_.xml"); reader = new DefaultMessageReader(loggingService, configurationRepository, errorDoc); reader.ReadMessage(); errorTable = reader.GetMessageResults <DataTable>(); Console.WriteLine(""); foreach (DataRow row in errorTable.Rows) { foreach (DataColumn column in errorTable.Columns) { Console.Write("\t{0}", row[column]); } Console.WriteLine(""); } Console.ReadKey(); }
static void TestReadListResponse() { IConfigurationRepository configurationRepository = new ConfigFileConfigurationRepository(); loggingService = new Log4NetLoggingService(configurationRepository, new ThreadContextService()); LogProviderContext.Current = loggingService; string filename = @"C:\Temp\LocalDataRequestMessage_2014_10_16_10_33_50.xml"; XmlDocument listResponse = new XmlDocument(); listResponse.Load(filename); IMessageReader reader = new DefaultMessageReader(loggingService, configurationRepository, listResponse.ToXDocument()); reader.ReadMessage(); string[] results = reader.GetMessageResults<string[]>(); string qualifier = reader.GetQualifier(); string function = reader.GetFunction(); DataTable listResults = reader.GetMessageResults<DataTable>(); GovTalkMessage message = reader.Message(); Console.WriteLine("Message from {0}", message.Header.MessageDetails.ResponseEndPoint.Value.ToString()); }
static void TestReadErrors(IConfigurationRepository configurationRepository, ILoggingService loggingService) { XDocument errorDoc = XDocument.Load(@"C:\Temp\test_reply__error_20150825151729.xml"); IMessageReader reader = new DefaultMessageReader(loggingService, configurationRepository, errorDoc); reader.ReadMessage(); DataTable errorTable = reader.GetMessageResults<DataTable>(); foreach (DataColumn column in errorTable.Columns) Console.Write("\t{0}", column.ColumnName); Console.WriteLine(""); foreach (DataRow row in errorTable.Rows) { foreach (DataColumn column in errorTable.Columns) Console.Write("\t{0}", row[column]); Console.WriteLine(""); } errorTable.Clear(); errorDoc = XDocument.Load(@"C:\Temp\RequestMessage_1422880486_File14393268203594585061_error_20150202123518_.xml"); reader = new DefaultMessageReader(loggingService, configurationRepository, errorDoc); reader.ReadMessage(); errorTable = reader.GetMessageResults<DataTable>(); Console.WriteLine(""); foreach (DataRow row in errorTable.Rows) { foreach (DataColumn column in errorTable.Columns) Console.Write("\t{0}", row[column]); Console.WriteLine(""); } Console.ReadKey(); }
/// <summary> /// Demonstrate using the message reader strategies to get results from a message reply /// </summary> /// <param name="loggingService"></param> /// <param name="messageToRead"></param> static void DemonstrateReadMessage(ILoggingService loggingService, XmlDocument messageToRead) { // Set up a message reading strategy IMessageReader messageReader = new DefaultMessageReader(loggingService, configurationRepository, messageToRead.ToXDocument()); messageReader.ReadMessage(); // We don't know what we've got back from the Gateway, but all replies are GovTalkMessages if(messageReader.HasErrors()) { //There are errors in the results file so we can deal with them // Get a DataTable of the results and have a look at that DataTable errorTable = messageReader.GetMessageResults<DataTable>(); // Or set up an error return strategy and do something with that IErrorReturnCalculator errorCalculator = new DefaultErrorReturnCalculator(); GovTalkMessageGovTalkDetailsError error = messageReader.GetMessageResults<GovTalkMessageGovTalkDetailsError>(); Console.WriteLine(errorCalculator.CalculateErrorReturn(error)); if(error.Number == "3001") { ErrorResponse errResponse = messageReader.GetMessageResults<ErrorResponse>(); } } else { // It's either an acknowledgement so we need to get the poll interval and URL, or a response. string[] results = messageReader.GetMessageResults<string[]>(); foreach(var result in results) { Console.WriteLine(result); } if(messageReader.GetQualifier() == "response") { string body = messageReader.GetBodyType(); if(body != null) { DataTable responseTable = messageReader.GetMessageResults<DataTable>(); LocalHelp.ConsolePrintDataTable(responseTable); } else { Console.WriteLine("No body content"); } } } GovTalkMessageFileName ReplyNamer = new GovTalkMessageFileName.FileNameBuilder() .AddLogger(loggingService) .AddConfigurationRepository(configurationRepository) .AddMessageIntention("ReplyMessage") .AddCorrelationId(messageReader.GetCorrelationId()) .AddFilePath(@"C:\Temp\") .BuildFileName(); string replyFileName = ReplyNamer.ToString(); messageToRead.Save(replyFileName); }
public static void DemonstrateLocalProcess() { // Set up the logging IConfigurationRepository configurationRepository = new ConfigFileConfigurationRepository(); ILoggingService loggingService = new Log4NetLoggingService(configurationRepository, new ThreadContextService()); DataTableRepaymentPopulater.SetLogger(loggingService); // Create a file of donations records DataTableRepaymentPopulater.GiftAidDonations = DataHelpers.GetDataTableFromCsv(@"C:\Temp\Donations.csv", true); // Set up app.config as a source for the reference data ReferenceDataManager.SetSource(ReferenceDataManager.SourceTypes.ConfigFile); // Build a GovTalkMessage GovTalkMessageCreator submitMessageCreator = new GovTalkMessageCreator(new SubmitRequestMessageBuilder(loggingService), loggingService); submitMessageCreator.CreateGovTalkMessage(); // Get the GovTalkMessage that has been built GovTalkMessage submitMessage = submitMessageCreator.GetGovTalkMessage(); // Serialize the GovTalkMessage to an XmlDocument XmlDocument xd = submitMessageCreator.SerializeGovTalkMessage(); // Set the IRmark for the GovTalkMessage XmlDocument GovTalkMessageHelper gtmHelper = new GovTalkMessageHelper(configurationRepository, loggingService); XmlDocument finalXd = gtmHelper.SetIRmark(xd); // Set the URI to send the file to string uri = configurationRepository.GetConfigurationValue<string>("SendURILocal"); // Create a client to send the file to the target gateway CharitiesOnline.MessageService.Client client = new MessageService.Client(loggingService); // Create an XmlDocument of the reply from the endpoint XmlDocument reply = client.SendRequest(xd, uri); // Set up a message reading strategy IMessageReader _messageReader = new DefaultMessageReader(loggingService,configurationRepository,reply.ToXDocument()); _messageReader.ReadMessage(); string[] results = _messageReader.GetMessageResults<string[]>(); //int correlationIdIndex = Array.IndexOf(results, "CorrelationId"); int correlationIdPosition = Array.FindIndex(results, element => element.StartsWith("CorrelationId")); if (correlationIdPosition < 0) throw new ArgumentNullException("CorrelationId"); int qualifierPosition = Array.FindIndex(results, element => element.StartsWith("Qualifier")); if (qualifierPosition < 0) throw new ArgumentNullException("Qualifier"); Console.WriteLine(string.Join("\n", results)); #region old // This bit, bunch of if-thens, should be covered by the reader strategy ... //string bodytype = _messageReader.GetBodyType(reply.ToXDocument()); //if(bodytype == null) //{ // //acknowledgment // Console.WriteLine("CorrelationId is {0}",_messageReader.ReadMessage<string>(reply.ToXDocument())); //} //else if(bodytype == "hmrcclasses.SuccessResponse") //{ // //success // string[] success = _messageReader.ReadMessage<string[]>(reply.ToXDocument()); // Console.WriteLine(string.Join("\n", success)); //} //else if(bodytype == "hmrcclasses.ErrorResponse") //{ // //error // string[] error = _messageReader.ReadMessage<string[]>(reply.ToXDocument()); // Console.WriteLine(string.Join("\n", error)); //} #endregion old // Need to get correlationId GovTalkMessageFileName fileNamer = new GovTalkMessageFileName.FileNameBuilder() .AddLogger(loggingService) .AddFilePath(configurationRepository.GetConfigurationValue<string>("TempFolder")) .AddEnvironment("local") .AddMessageIntention("reply") .AddCorrelationId(results[correlationIdPosition].Substring(results[correlationIdPosition].IndexOf("::") + 2)) .AddMessageQualifier(results[qualifierPosition].Substring(results[qualifierPosition].IndexOf("::") + 2)) //could check for < 0 here and pass empty string .BuildFileName(); string filename = fileNamer.ToString(); reply.Save(filename); // reply.Save(@"C:\Temp\localreply.xml"); }
public static void TestDecompressMessage(XmlDocument compressedXmlDocument) { string contents = CommonUtilityHelper.DecompressData(compressedXmlDocument.XmlToBytes()); XmlDocument decompressedDocument = new XmlDocument(); decompressedDocument.LoadXml(contents); DefaultMessageReader reader = new DefaultMessageReader(loggingService, configurationRepository, decompressedDocument.ToXDocument()); string[] results = reader.GetMessageResults<string[]>(); Console.WriteLine(reader.GetQualifier()); }
public static void DemonstrateLocalProcess() { // Set up the logging IConfigurationRepository configurationRepository = new ConfigFileConfigurationRepository(); ILoggingService loggingService = new Log4NetLoggingService(configurationRepository, new ThreadContextService()); DataTableRepaymentPopulater.SetLogger(loggingService); // Create a file of donations records DataTableRepaymentPopulater.GiftAidDonations = DataHelpers.GetDataTableFromCsv(@"C:\Temp\Donations.csv", true); // Set up app.config as a source for the reference data ReferenceDataManager.SetSource(ReferenceDataManager.SourceTypes.ConfigFile); // Build a GovTalkMessage GovTalkMessageCreator submitMessageCreator = new GovTalkMessageCreator(new SubmitRequestMessageBuilder(loggingService), loggingService); submitMessageCreator.CreateGovTalkMessage(); // Get the GovTalkMessage that has been built GovTalkMessage submitMessage = submitMessageCreator.GetGovTalkMessage(); // Serialize the GovTalkMessage to an XmlDocument XmlDocument xd = submitMessageCreator.SerializeGovTalkMessage(); // Set the IRmark for the GovTalkMessage XmlDocument GovTalkMessageHelper gtmHelper = new GovTalkMessageHelper(configurationRepository, loggingService); XmlDocument finalXd = gtmHelper.SetIRmark(xd); // Set the URI to send the file to string uri = configurationRepository.GetConfigurationValue <string>("SendURILocal"); // Create a client to send the file to the target gateway CharitiesOnline.MessageService.Client client = new MessageService.Client(loggingService); // Create an XmlDocument of the reply from the endpoint XmlDocument reply = client.SendRequest(xd, uri); // Set up a message reading strategy IMessageReader _messageReader = new DefaultMessageReader(loggingService, configurationRepository, reply.ToXDocument()); _messageReader.ReadMessage(); string[] results = _messageReader.GetMessageResults <string[]>(); //int correlationIdIndex = Array.IndexOf(results, "CorrelationId"); int correlationIdPosition = Array.FindIndex(results, element => element.StartsWith("CorrelationId")); if (correlationIdPosition < 0) { throw new ArgumentNullException("CorrelationId"); } int qualifierPosition = Array.FindIndex(results, element => element.StartsWith("Qualifier")); if (qualifierPosition < 0) { throw new ArgumentNullException("Qualifier"); } Console.WriteLine(string.Join("\n", results)); #region old // This bit, bunch of if-thens, should be covered by the reader strategy ... //string bodytype = _messageReader.GetBodyType(reply.ToXDocument()); //if(bodytype == null) //{ // //acknowledgment // Console.WriteLine("CorrelationId is {0}",_messageReader.ReadMessage<string>(reply.ToXDocument())); //} //else if(bodytype == "hmrcclasses.SuccessResponse") //{ // //success // string[] success = _messageReader.ReadMessage<string[]>(reply.ToXDocument()); // Console.WriteLine(string.Join("\n", success)); //} //else if(bodytype == "hmrcclasses.ErrorResponse") //{ // //error // string[] error = _messageReader.ReadMessage<string[]>(reply.ToXDocument()); // Console.WriteLine(string.Join("\n", error)); //} #endregion old // Need to get correlationId GovTalkMessageFileName fileNamer = new GovTalkMessageFileName.FileNameBuilder() .AddLogger(loggingService) .AddFilePath(configurationRepository.GetConfigurationValue <string>("TempFolder")) .AddEnvironment("local") .AddMessageIntention("reply") .AddCorrelationId(results[correlationIdPosition].Substring(results[correlationIdPosition].IndexOf("::") + 2)) .AddMessageQualifier(results[qualifierPosition].Substring(results[qualifierPosition].IndexOf("::") + 2)) //could check for < 0 here and pass empty string .BuildFileName(); string filename = fileNamer.ToString(); reply.Save(filename); // reply.Save(@"C:\Temp\localreply.xml"); }
/// <summary> /// Demonstrate using the message reader strategies to get results from a message reply /// </summary> /// <param name="loggingService"></param> /// <param name="messageToRead"></param> static void DemonstrateReadMessage(ILoggingService loggingService, XmlDocument messageToRead) { // Set up a message reading strategy IMessageReader messageReader = new DefaultMessageReader(loggingService, configurationRepository, messageToRead.ToXDocument()); messageReader.ReadMessage(); // We don't know what we've got back from the Gateway, but all replies are GovTalkMessages if (messageReader.HasErrors()) { //There are errors in the results file so we can deal with them // Get a DataTable of the results and have a look at that DataTable errorTable = messageReader.GetMessageResults <DataTable>(); // Or set up an error return strategy and do something with that IErrorReturnCalculator errorCalculator = new DefaultErrorReturnCalculator(); GovTalkMessageGovTalkDetailsError error = messageReader.GetMessageResults <GovTalkMessageGovTalkDetailsError>(); Console.WriteLine(errorCalculator.CalculateErrorReturn(error)); if (error.Number == "3001") { ErrorResponse errResponse = messageReader.GetMessageResults <ErrorResponse>(); } } else { // It's either an acknowledgement so we need to get the poll interval and URL, or a response. string[] results = messageReader.GetMessageResults <string[]>(); foreach (var result in results) { Console.WriteLine(result); } if (messageReader.GetQualifier() == "response") { string body = messageReader.GetBodyType(); if (body != null) { DataTable responseTable = messageReader.GetMessageResults <DataTable>(); LocalHelp.ConsolePrintDataTable(responseTable); } else { Console.WriteLine("No body content"); } } } GovTalkMessageFileName ReplyNamer = new GovTalkMessageFileName.FileNameBuilder() .AddLogger(loggingService) .AddConfigurationRepository(configurationRepository) .AddMessageIntention("ReplyMessage") .AddCorrelationId(messageReader.GetCorrelationId()) .AddFilePath(@"C:\Temp\") .BuildFileName(); string replyFileName = ReplyNamer.ToString(); messageToRead.Save(replyFileName); }