public StatusCodeResult EmailEvent([FromBody] Object json)
        {
            try
            {
                //converting json to object
                var emailEvent = new EmailEvent(json.ToString());

                //Validate XML
                var xmlResponse = XsdValidation.XmlObjectValidation(emailEvent);

                if (xmlResponse != null)
                {
                    using (var connection = factory.CreateConnection())
                        using (var channel = connection.CreateModel())
                        {
                            var addUserBody = Encoding.UTF8.GetBytes(xmlResponse);
                            var properties  = channel.CreateBasicProperties();
                            properties.Headers = new Dictionary <string, object>();
                            properties.Headers.Add("eventType", "frontend.email_event");
                            channel.BasicPublish(exchange: "events.exchange",
                                                 routingKey: "",
                                                 basicProperties: properties,
                                                 body: addUserBody
                                                 );
                        }
                }
                return(StatusCode(201));
            }
            catch (Exception ex) {
                Sender.SendErrorMessage(ex);
                return(StatusCode(500));
            }
        }
        public async Task <StatusCodeResult> RequestInvoiceAsync([FromBody] object json)
        {
            try
            {
                //convert json to addUser object
                var requestInvoiceEntity = new RequestInvoiceFromFrontend(json.ToString());

                //Make the call for the UUID
                string responseBody = null;
                var    url          = "http://192.168.1.2/uuid-master/uuids/frontend/" + requestInvoiceEntity.UserId;
                using (var client = new HttpClient())
                    using (var request = new HttpRequestMessage(HttpMethod.Get, url))
                    {
                        using (var response = await client
                                              .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
                                              .ConfigureAwait(false))
                        {
                            response.EnsureSuccessStatusCode();
                            responseBody = await response.Content.ReadAsStringAsync();
                        }
                    }

                //Convert the response from json to dictionary
                var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseBody);

                //add response to addUserEntity uuid
                requestInvoiceEntity.uuid = values["uuid"];

                //Validate XML
                var xml = XsdValidation.XmlObjectValidation(requestInvoiceEntity);

                //when no errors send the message to rabbitmq
                if (xml != null)
                {
                    using (var connection = factory.CreateConnection())
                        using (var channel = connection.CreateModel())
                        {
                            var addUserBody = Encoding.UTF8.GetBytes(xml);
                            var properties  = channel.CreateBasicProperties();
                            properties.Headers = new Dictionary <string, object>();
                            properties.Headers.Add("eventType", "frontend.email_invoice");
                            channel.BasicPublish(exchange: "events.exchange",
                                                 routingKey: "",
                                                 basicProperties: properties,
                                                 body: addUserBody
                                                 );

                            SendLogToLogExchange(" request invoice");
                        }
                }
            }
            catch (Exception ex)
            {
                Sender.SendErrorMessage(ex);
                return(StatusCode(500));
            }

            return(StatusCode(201));
        }
示例#3
0
        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
                                             );
                    }
            }
        }
示例#4
0
        private static void ReceiverRabbitMQ()
        {
            try
            {
                //Make the connection to receive
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += async(model, ea) =>
                        {
                            //Receiving data
                            var body = ea.Body.ToArray();
                            var xml  = Encoding.UTF8.GetString(body);
                            Console.WriteLine(" [x] Received {0}", xml);

                            var xmlValidationResponse = XsdValidation.XmlStringValidation(xml);

                            //When no errors in validation make an object out of the xml data
                            if (xmlValidationResponse != null)
                            {
                                if (xmlValidationResponse == "add_user")
                                {
                                    await ReceivingNewUserAsync(xml);
                                }
                                else if (xmlValidationResponse == "patch_user")
                                {
                                    await ReceivingPatchUserAsync(xml);
                                }
                            }
                        };
                        channel.BasicConsume(queue: "frontend.queue",
                                             autoAck: true,
                                             consumer: consumer);

                        Console.WriteLine(" Press [enter] to exit.");
                        Console.ReadLine();
                    }
            }
            catch (Exception ex) {
                SendErrorMessage(ex);
            }
        }
示例#5
0
        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 void SendLogToLogExchange(string action)
        {
            //make log file entity
            Log log = new Log("Frontend " + action);

            //Validate XML
            var xml = XsdValidation.XmlObjectValidation(log);

            //when no errors send the message to rabbitmq
            if (xml != 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
                                             );
                    }
            }
        }
        public async Task <StatusCodeResult> AddUserAsync([FromBody] Object json)
        {
            try
            {
                //convert json to addUser object
                var addUserEntity = new AddUserFromFrontend(json.ToString());

                //Create body for requesting UUID for the addUser object
                var body = new Dictionary <string, string>();
                body.Add("frontend", addUserEntity.UserId.ToString());

                //Make the call for the UUID
                // https://johnthiriet.com/efficient-post-calls/
                string responseBody = null;
                using (var client = new HttpClient())
                    using (var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.1.2/uuid-master/uuids"))
                        using (var httpContent = CreateHttpContent(body))
                        {
                            request.Content = httpContent;

                            using (var response = await client
                                                  .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
                                                  .ConfigureAwait(false))
                            {
                                response.EnsureSuccessStatusCode();
                                responseBody = await response.Content.ReadAsStringAsync();
                            }
                        }

                //Convert the response from json to dictionary
                var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseBody);

                //add response to addUserEntity uuid
                addUserEntity.uuid             = values["uuid"];
                addUserEntity.application_name = "frontend";

                //Validate XML
                var xml = XsdValidation.XmlObjectValidation(addUserEntity);

                //when no errors send the message to rabbitmq
                if (xml != null)
                {
                    using (var connection = factory.CreateConnection())
                        using (var channel = connection.CreateModel())
                        {
                            var addUserBody = Encoding.UTF8.GetBytes(xml);
                            var properties  = channel.CreateBasicProperties();
                            properties.Headers = new Dictionary <string, object>();
                            properties.Headers.Add("eventType", "frontend.add_user");
                            channel.BasicPublish(exchange: "events.exchange",
                                                 routingKey: "",
                                                 basicProperties: properties,
                                                 body: addUserBody
                                                 );

                            SendLogToLogExchange(" added a user from frontend");
                        }
                }
                return(StatusCode(201));
            }
            catch (Exception ex) {
                Sender.SendErrorMessage(ex);
                return(StatusCode(500));
            }
        }