public static string GetCsvNationRecordField(NationRecordFields recordField, Nation nation) { string fieldString = string.Empty; switch (recordField) { case NationRecordFields.Name: fieldString += ExtendedStringWriter.GetSafeString(nation.Name); break; case NationRecordFields.Capital: fieldString += ExtendedStringWriter.GetSafeString(nation.Capital); break; case NationRecordFields.Latitude: fieldString += nation.Longitude; break; case NationRecordFields.Longitude: fieldString += nation.Longitude; break; case NationRecordFields.ImageUri: fieldString += ExtendedStringWriter.GetSafeString(nation.ImageUri); break; case NationRecordFields.GeographyXml: fieldString += ExtendedStringWriter.GetSafeString(nation.GeographyXml); break; } return(fieldString); }
private static void SendErrorMessage(Exception ex) { CustomError error = new CustomError(); error.application_name = "frontend"; error.timestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fff"); error.message = ex.ToString(); //Make an XML from the error object XmlSerializer xmlSerializer = new XmlSerializer(typeof(CustomError)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); string xml; var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }; var stringBuilder = new StringBuilder(); using (var sww = new ExtendedStringWriter(stringBuilder, Encoding.UTF8)) { using (XmlWriter writer = XmlWriter.Create(sww, settings)) { xmlSerializer.Serialize(writer, error, ns); xml = sww.ToString(); } } //XML validation with XSD var xmlValidationResponse = XsdValidation.XmlStringValidation(xml); if (xmlValidationResponse != null) { using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { var addUserBody = Encoding.UTF8.GetBytes(xml); channel.BasicPublish(exchange: "logs.exchange", routingKey: "", body: addUserBody ); } } }
private static void SendLogToLogExchange(string action) { //make log file entity Log log = new Log("Frontend " + action); //Make an XML from the object XmlSerializer xmlSerializer = new XmlSerializer(log.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); string xml; var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }; var stringBuilder = new StringBuilder(); using (var sww = new ExtendedStringWriter(stringBuilder, Encoding.UTF8)) { using (XmlWriter writer = XmlWriter.Create(sww, settings)) { xmlSerializer.Serialize(writer, log, ns); xml = sww.ToString(); } } //Validate XML var xmlResponse = XsdValidation.XmlStringValidation(xml); //when no errors send the message to rabbitmq if (xmlResponse != null) { using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { var addUserBody = Encoding.UTF8.GetBytes(xml); channel.BasicPublish(exchange: "logs.exchange", routingKey: "", body: addUserBody ); } } }
private static void SendMessageToErrorExchange(Exception error) { CustomError customError = new CustomError(); customError.application_name = "frontend"; customError.timestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fff"); customError.message = error.ToString(); //Make an XML from the error object XmlSerializer xmlSerializer = new XmlSerializer(typeof(CustomError)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); string xml; var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }; var stringBuilder = new StringBuilder(); using (var sww = new ExtendedStringWriter(stringBuilder, Encoding.UTF8)) { using (XmlWriter writer = XmlWriter.Create(sww, settings)) { xmlSerializer.Serialize(writer, customError, ns); xml = sww.ToString(); } } //XML validation with XSD string xsdData = @"<?xml version='1.0'?> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> <xs:element name='error'> <xs:complexType> <xs:sequence> <xs:element name='application_name' type='xs:string'/> <xs:element name='timestamp' type='xs:string'/> <xs:element name='message' type='xs:string'/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>"; XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add("", XmlReader.Create(new StringReader(xsdData))); var xDoc = XDocument.Parse(xml); bool errors = false; xDoc.Validate(schemas, (o, e) => { errors = true; }); if (!errors) { using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { var addUserBody = Encoding.UTF8.GetBytes(xml); channel.BasicPublish(exchange: "errors.exchange", routingKey: "", body: addUserBody ); } } }
public static string GetCsvBookRecordField(BookRecordFields recordField, Book book) { string fieldString = string.Empty; switch (recordField) { case BookRecordFields.DateString: fieldString += book.DateString; break; case BookRecordFields.Date: fieldString += book.Date.Day + "/" + book.Date.Month + "/" + book.Date.Year; break; case BookRecordFields.Author: fieldString += ExtendedStringWriter.GetSafeString(book.Author); break; case BookRecordFields.Title: fieldString += ExtendedStringWriter.GetSafeString(book.Title); break; case BookRecordFields.Pages: if (!(book.Pages == 0 && (book.Format == "Audio" || book.Format == "Comic"))) { fieldString += book.Pages; } break; case BookRecordFields.Note: fieldString += ExtendedStringWriter.GetSafeString(book.Note); break; case BookRecordFields.Nationality: fieldString += ExtendedStringWriter.GetSafeString(book.Nationality); break; case BookRecordFields.OriginalLanguage: fieldString += ExtendedStringWriter.GetSafeString(book.OriginalLanguage); break; case BookRecordFields.Book: if (book.Format == "Book") { fieldString += "x"; } break; case BookRecordFields.Comic: if (book.Format == "Comic") { fieldString += "x"; } break; case BookRecordFields.Audio: if (book.Format == "Audio") { fieldString += "x"; } break; case BookRecordFields.Image: fieldString += ExtendedStringWriter.GetSafeString(book.ImageUrl); break; case BookRecordFields.Tags: { string tags = string.Empty; for (int i = 0; i < book.Tags.Length; i++) { if (i != 0) { tags += ", "; } tags += book.Tags[i]; } fieldString += ExtendedStringWriter.GetSafeString(tags); } break; } return(fieldString); }
public static string XmlObjectValidation(IXsdValidation objectThatNeedsValidation) { //Make an XML from the object XmlSerializer xmlSerializer = new XmlSerializer(objectThatNeedsValidation.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); string xml; var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }; var stringBuilder = new StringBuilder(); using (var sww = new ExtendedStringWriter(stringBuilder, Encoding.UTF8)) { using (XmlWriter writer = XmlWriter.Create(sww, settings)) { xmlSerializer.Serialize(writer, objectThatNeedsValidation, ns); xml = sww.ToString(); } } //XML validation with XSD //Select the xsd file string xsdData = ""; if (typeof(PatchUserFromFrontend).IsInstanceOfType(objectThatNeedsValidation)) { xsdData = xsdPatchUser; } else if (typeof(AddUserFromFrontend).IsInstanceOfType(objectThatNeedsValidation)) { xsdData = xsdAddUser; } else if (typeof(RequestInvoiceFromFrontend).IsInstanceOfType(objectThatNeedsValidation)) { xsdData = xsdRequestInvoice; } else if (typeof(Log).IsInstanceOfType(objectThatNeedsValidation)) { xsdData = xsdLog; } else if (typeof(CustomError).IsInstanceOfType(objectThatNeedsValidation)) { xsdData = xsdError; } else if (typeof(EmailEvent).IsInstanceOfType(objectThatNeedsValidation)) { xsdData = xsdEmailEvent; } else { return(null); } XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add("", XmlReader.Create(new StringReader(xsdData))); //Validation of XML var xDoc = XDocument.Parse(xml); bool errors = false; xDoc.Validate(schemas, (o, e) => { errors = true; }); //Return null when validation has errors if (errors) { return(null); } else { return(xml); } }