コード例 #1
0
        public void Deserialize_Normal2()
        {
            // SETUP //
            string segmentAsString =
                HL7Utility.BeginOfMessage +
                "SOA|OK|||<num segments>|" + HL7Utility.EndOfSegment +
                "SOA|OK|OK1|OK2|<num segments>|OK3|" + HL7Utility.EndOfSegment +
                HL7Utility.EndOfMessage;
            // TEST //
            var actualResult = HL7Utility.Deserialize(segmentAsString);

            // ASSES //

            Assert.IsTrue(actualResult != null &&
                          actualResult.Segments != null &&
                          actualResult.Segments.Count == 2 &&
                          actualResult.Segments[0].Elements.Count == 5 &&
                          actualResult.Segments[0].Elements[0] == "SOA" &&
                          actualResult.Segments[0].Elements[1] == "OK" &&
                          actualResult.Segments[0].Elements[2] == "" &&
                          actualResult.Segments[0].Elements[3] == "" &&
                          actualResult.Segments[0].Elements[4] == "<num segments>" &&
                          actualResult.Segments[1].Elements.Count == 6 &&
                          actualResult.Segments[1].Elements[0] == "SOA" &&
                          actualResult.Segments[1].Elements[1] == "OK" &&
                          actualResult.Segments[1].Elements[2] == "OK1" &&
                          actualResult.Segments[1].Elements[3] == "OK2" &&
                          actualResult.Segments[1].Elements[4] == "<num segments>" &&
                          actualResult.Segments[1].Elements[5] == "OK3");
        }
コード例 #2
0
        private static bool ValidateTeam(string teamName, string teamId)
        {
            if (!_settings.ShouldValidateTeam)
            {
                Console.WriteLine("Team authorisation disabled. Change app.config file to enable.");
                return(true);
            }
            var    validateTeamRequest  = CreateValidateTeamRequest(teamName, teamId);
            string validateTeamResponse = null;

            try
            {
                logServerMessage("Calling SOA-Registry with message :", validateTeamRequest);

                validateTeamResponse = SocketClient.SendRequest(validateTeamRequest, _settings.SOARegistryIp, _settings.SOARegistryPort);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                Logger.logException(ex);
            }
            if (validateTeamResponse != null)
            {
                Message message = null;
                try
                {
                    logServerMessage("Response from SOA-Registry :", validateTeamResponse);

                    message = HL7Utility.Deserialize(validateTeamResponse);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);

                    Logger.logException(ex);
                }
                if (message != null &&
                    message.Segments != null &&
                    message.Segments.Count > 0 &&
                    message.Segments[0].Elements != null &&
                    message.Segments[0].Elements.Count >= 2 &&
                    message.Segments[0].Elements[0] == SOADirectiveElement &&
                    message.Segments[0].Elements[1] == SOAOkElement)
                {
                    // The team is authorised to use the service.
                    Console.WriteLine(string.Format("Team {0} with ID {1} is authorised to use the service.", teamName, teamId));
                    return(true);
                }
            }

            Console.WriteLine(string.Format("Team {0} with ID {1} is NOT authorised to use the service.", teamName, teamId));
            return(false);
        }
コード例 #3
0
        private static void logServerMessage(string messageHeader, string HL7Message)
        {
            //Deserialize the message
            Message       tempMsg = HL7Utility.Deserialize(HL7Message);
            StringBuilder sb      = new StringBuilder();

            //Format the segments
            foreach (Segment seg in tempMsg.Segments)
            {
                sb.Append("\t\t>>");
                foreach (string s in seg.Elements)
                {
                    sb.AppendFormat("{0}|", s);
                }
                sb.Append(Environment.NewLine);
            }

            //Write to log file
            Logger.logMessage(messageHeader +
                              Environment.NewLine +
                              sb.ToString());
        }
コード例 #4
0
        private static void logClientMessage(string messageHeader, string HL7Message)
        {
            //Deserialize the message
            Message       tempMsg = HL7Utility.Deserialize(HL7Message.TrimEnd('\n'));
            StringBuilder sb      = new StringBuilder();

            //Format the segments
            foreach (Segment seg in tempMsg.Segments)
            {
                sb.Append("\t\t>>");
                foreach (string s in seg.Elements)
                {
                    sb.AppendFormat("{0}|", s);
                }
                sb.Append(Environment.NewLine);
            }

            //Write to log file
            Logger.logMessage(messageHeader +
                              Environment.NewLine +
                              sb.ToString(),
                              AppDomain.CurrentDomain.BaseDirectory + "\\" + "LogFile.txt");
        }
コード例 #5
0
        static bool RegisterServiceWithSoaRegistry()
        {
            try
            {
                Console.WriteLine(string.Format("Registering service to SOA Registry with team name {0}...", _settings.TeamName));
                // Register team
                string teamRegistractionResponse = null;
                var    registerTeamMessage       = CreateRegisterTeamRequest(_settings.TeamName);
                try
                {
                    logServerMessage("Calling SOA-Registry with message :", registerTeamMessage);
                    teamRegistractionResponse = SocketClient.SendRequest(registerTeamMessage, _settings.SOARegistryIp, _settings.SOARegistryPort);
                }
                catch (Exception ex)
                {
                    Logger.logException(ex);

                    throw new Exception("Could not register the team.", ex);
                }
                if (teamRegistractionResponse != null)
                {
                    logServerMessage("Response from SOA-Registry :", teamRegistractionResponse);

                    var teamRegistractionResponseMessage = HL7Utility.Deserialize(teamRegistractionResponse);
                    if (teamRegistractionResponseMessage.Segments.Count > 0 &&
                        teamRegistractionResponseMessage.Segments[0].Elements.Count > 2 &&
                        teamRegistractionResponseMessage.Segments[0].Elements[0] == SOADirectiveElement &&
                        teamRegistractionResponseMessage.Segments[0].Elements[1] == "OK")
                    {
                        _teamId = teamRegistractionResponseMessage.Segments[0].Elements[2];
                        Console.WriteLine("Team Registered with teamID " + _teamId);
                    }
                    else
                    {
                        throw new Exception("Team registration response is not in valid format.");
                    }

                    // Register service
                    var    registerServiceMessage  = CreateRegisterServiceRequest();
                    string registerServiceResponse = null;
                    try
                    {
                        logServerMessage("Calling SOA-Registry with message :", registerServiceMessage);

                        registerServiceResponse = SocketClient.SendRequest(registerServiceMessage, _settings.SOARegistryIp, _settings.SOARegistryPort);
                        logServerMessage("Response from SOA-Registry :", registerServiceResponse);
                        Console.WriteLine("Response from SOA-Registry :" + registerServiceResponse);

                        var registerServiceResponseMessage = HL7Utility.Deserialize(registerServiceResponse);
                        if (registerServiceResponseMessage.Segments.Count > 0 &&
                            registerServiceResponseMessage.Segments[0].Elements.Count > 2 &&
                            registerServiceResponseMessage.Segments[0].Elements[0] == SOADirectiveElement &&
                            registerServiceResponseMessage.Segments[0].Elements[1] == "OK")
                        {
                            return(true);
                        }
                        else
                        {
                            throw new Exception("Registration failed.");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.logException(ex);
                        throw new Exception("Could not register the service.", ex);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e + "InnerException: " + e.InnerException);

                Logger.logException(e);
            }


            return(false);
        }
コード例 #6
0
        static string HandleRequest(string request)
        {
            Message requestMessage = null;

            try
            {
                requestMessage = HL7Utility.Deserialize(request);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);

                Logger.logException(ex);
            }
            if (requestMessage != null)
            {
                // Check if incoming request is in valid format.
                if (requestMessage.Segments != null &&
                    requestMessage.Segments.Count == 4 &&          // DRC, SRV, ARG, ARG = 4
                    requestMessage.Segments[0].Elements != null && // DRC|EXEC-SERVICE|<team name>|<teamID>|
                    requestMessage.Segments[0].Elements.Count == 4 &&
                    requestMessage.Segments[0].Elements[0] == CommandDirectiveElement &&
                    requestMessage.Segments[0].Elements[1] == ExecuteServiceElement &&
                    requestMessage.Segments[1].Elements != null && // SRV||<service name>||<num args>|||
                    requestMessage.Segments[1].Elements.Count == 7 &&
                    requestMessage.Segments[1].Elements[0] == ServiceDirectiveElement &&
                    requestMessage.Segments[1].Elements[1] == "" &&
                    requestMessage.Segments[1].Elements[3] == "" &&
                    requestMessage.Segments[1].Elements[5] == "" &&
                    requestMessage.Segments[1].Elements[6] == "" &&
                    requestMessage.Segments[2].Elements != null && // ARG|<arg position>|<arg name>|<arg data type>||<arg value>|
                    requestMessage.Segments[2].Elements.Count == 6 &&
                    requestMessage.Segments[2].Elements[0] == ArgumentDirectiveElement &&
                    requestMessage.Segments[2].Elements[2] == "province" &&
                    requestMessage.Segments[2].Elements[3] == "string" &&
                    requestMessage.Segments[3].Elements != null && // ARG|<arg position>|<arg name>|<arg data type>||<arg value>|
                    requestMessage.Segments[3].Elements.Count == 6 &&
                    requestMessage.Segments[3].Elements[0] == ArgumentDirectiveElement &&
                    requestMessage.Segments[3].Elements[2] == "amount" &&
                    requestMessage.Segments[3].Elements[3] == "double")
                {
                    // Now we parse out the team requesting to execute.
                    var teamName = requestMessage.Segments[0].Elements[2];
                    var teamId   = requestMessage.Segments[0].Elements[3];

                    if (ValidateTeam(teamName, teamId))
                    {
                        // Now we parse out the arguments province and amount
                        string provinceArg = requestMessage.Segments[2].Elements[5];
                        double amountArg   = 0;
                        if (double.TryParse(requestMessage.Segments[3].Elements[5], out amountArg))
                        {
                            Models.TaxSummary taxSummary = null;
                            try
                            {
                                taxSummary = _taxCalculator.CalculateTax(provinceArg, amountArg);
                            }
                            catch (ArgumentException ae)
                            {
                                Logger.logException(ae);

                                var errorResponseMessage = new Message();
                                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Arguments not valid.", ae.Message);
                                return(HL7Utility.Serialize(errorResponseMessage));
                            }
                            catch (Exception ex)
                            {
                                Logger.logException(ex);

                                var errorResponseMessage = new Message();
                                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "There was a problem with calculating the tax summary.", "");
                                return(HL7Utility.Serialize(errorResponseMessage));
                            }
                            if (taxSummary != null)
                            {
                                // SUCCESS //
                                var responseMessage = new Message();
                                // PUB|OK|||<num segments>|
                                responseMessage.AddSegment(PublishedServiceDirectiveElement, SOAOkElement, "", "", "5");
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "1", "NetAmount", "double", taxSummary.NetAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "2", "PstAmount", "double", taxSummary.PstAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "3", "HstAmount", "double", taxSummary.HstAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "4", "GstAmount", "double", taxSummary.GstAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "5", "TotalAmount", "double", taxSummary.TotalAmount.ToString());
                                return(HL7Utility.Serialize(responseMessage));
                            }
                            else
                            {
                                var errorResponseMessage = new Message();
                                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "There was a problem with calculating the tax summary.", "");
                                return(HL7Utility.Serialize(errorResponseMessage));
                            }
                        }
                        else
                        {
                            var errorResponseMessage = new Message();
                            errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Could not parse amount argument.", "Make sure that amount argument is of type double.");
                            return(HL7Utility.Serialize(errorResponseMessage));
                        }
                    }
                    else
                    {
                        var errorResponseMessage = new Message();
                        errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Assess denied.", string.Format("Team {0} with ID {1} is not allowed to execute the service.", teamName, teamId));
                        return(HL7Utility.Serialize(errorResponseMessage));
                    }
                }
                else
                {
                    var errorResponseMessage = new Message();
                    errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Request not in valid format.", "Make sure that you comply to SOA Registry message format.");
                    return(HL7Utility.Serialize(errorResponseMessage));
                }
            }
            else
            {
                var errorResponseMessage = new Message();
                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "There was a problem with calculating the tax summary.", "");
                return(HL7Utility.Serialize(errorResponseMessage));
            }
        }
コード例 #7
0
        protected void submitButton_Click(object sender, EventArgs e)
        {
            Message msg            = new Message();
            string  request        = "";
            string  response       = "";
            int     idVerification = 0;

            try
            {
                //  DRC|QUERY-SERVICE|<team name>|<team ID>|
                msg.AddSegment(CommandDirectiveElement, QueryServiceElement, nameTextBox.Text, idTextBox.Text);
                //  SRV|GIORP-TOTAL||||||
                msg.AddSegment(ServiceDirectiveElement, "GIORP-TOTAL", "", "", "", "", "");

                //Serialize the message
                request = HL7Utility.Serialize(msg);
                //Send the request
                response = ServiceClient.SendRequest(request, "10.113.21.139", 3128);
                //Deserialize the response
                msg = HL7Utility.Deserialize(response.TrimEnd('\n'));

                if (msg.Segments[0].Elements[1] == SOAOkElement)
                {
                    errorServiceDiv.Visible = false;
                    noServiceDiv.Visible    = false;

                    // double check if the idTextBox value is valid
                    if (Int32.TryParse(idTextBox.Text, out idVerification) == false)
                    {
                        throw new Exception("Team ID was invalid");
                    }

                    // load up the session variables
                    Session["TeamName"]           = nameTextBox.Text;
                    Session["TeamID"]             = idTextBox.Text;
                    Session["ServiceTeamName"]    = msg.Segments[1].Elements[1];
                    Session["ServiceName"]        = msg.Segments[1].Elements[2];
                    Session["NumArgs"]            = msg.Segments[1].Elements[4];
                    Session["ServiceDescription"] = msg.Segments[1].Elements[6];
                    Session["ArgName1"]           = GetArgument(1, msg)[0];
                    Session["ArgDataType1"]       = GetArgument(1, msg)[1];
                    Session["ArgName2"]           = GetArgument(2, msg)[0];
                    Session["ArgDataType2"]       = GetArgument(2, msg)[1];
                    Session["IPAddress"]          = GetIPAddress(msg);
                    Session["portNumber"]         = GetPortNumber(msg);

                    // transfer to the next page
                    Server.Transfer("default.aspx", true);
                }
                else if (msg.Segments[0].Elements[1] == SOANotOkElement)
                {
                    ErrorMessageID.InnerText = msg.Segments[0].Elements[3];
                    errorServiceDiv.Visible  = true;
                    noServiceDiv.Visible     = false;
                }
                else
                {
                    errorServiceDiv.Visible = false;
                    noServiceDiv.Visible    = true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
コード例 #8
0
        protected void submitButton_Click(object sender, EventArgs e)
        {
            string     province;
            double     amount   = 0;
            string     request  = "";
            string     response = "";
            TaxSummary taxes    = new GIORP_TOTAL.Models.TaxSummary();
            Message    msg      = new Message();

            province = provinceList.SelectedValue;
            double.TryParse(priceBox.Text, out amount);

            try
            {
                Regex priceRegex = new Regex(amountPattern);

                if (!priceRegex.IsMatch(amount.ToString()))
                {
                    throw new Exception("The amount value was invalid");
                }

                //  DRC|EXEC-SERVICE|PhantomPower|0|
                msg.AddSegment(CommandDirectiveElement, ExecuteServiceElement, teamName, teamID);
                //  SRV||PP-GIORP-TOTAL||2|||
                msg.AddSegment(ServiceDirectiveElement, "", serviceName, "", numArgs, "", "");
                //  ARG|1|province|string||PROVINCE CODE|
                if (argDataType1.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    msg.AddSegment(ArgumentDirectiveElement, "1", argName1, argDataType1, "", province);
                }
                else
                {
                    msg.AddSegment(ArgumentDirectiveElement, "1", argName1, argDataType1, "", amount.ToString());
                }
                //  ARG|2|amount|double||AMOUNT|
                if (argDataType2.IndexOf("double", 0, StringComparison.CurrentCultureIgnoreCase) != -1 ||
                    argDataType2.IndexOf("float", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    msg.AddSegment(ArgumentDirectiveElement, "2", argName2, argDataType2, "", amount.ToString());
                }
                else
                {
                    msg.AddSegment(ArgumentDirectiveElement, "2", argName2, argDataType2, "", province);
                }

                //Serialize the message
                request = HL7Utility.Serialize(msg);
                //Send the request
                response = ServiceClient.SendRequest(request, IP, port);
                //Deserialize the response
                msg = HL7Utility.Deserialize(response);

                if (msg.Segments[0].Elements[1] == SOAOkElement)
                {
                    taxes          = null; //MessageToTaxSummary(msg);
                    response1.Text = msg.Segments[1].Elements[2] + ": " + msg.Segments[1].Elements[4];
                    response2.Text = msg.Segments[2].Elements[2] + ": " + msg.Segments[2].Elements[4];
                    response3.Text = msg.Segments[3].Elements[2] + ": " + msg.Segments[3].Elements[4];
                    response4.Text = msg.Segments[4].Elements[2] + ": " + msg.Segments[4].Elements[4];
                    response5.Text = msg.Segments[5].Elements[2] + ": " + msg.Segments[5].Elements[4];

                    // show the results div with the results
                    results.Visible  = true;
                    alertDiv.Visible = false;
                }
                else if (msg.Segments[0].Elements[1] == SOANotOkElement)
                {
                    taxes            = null;
                    alert.Text       = msg.Segments[0].Elements[3];
                    alertDiv.Visible = true;
                    results.Visible  = false;
                }
            }
            catch (Exception ex)
            {
                alert.Text = ex.Message;
                Logger.logException(ex);
            }

            //if (taxes != null)
            //{
            //    // fill in the labels with the responses
            //    try
            //    {
            //        // go through each segment and get the second element (name of field) and forth element (value)
            //        response1.Text = msg.Segments[1].Elements[2] + ": " + msg.Segments[1].Elements[4];
            //        response2.Text = msg.Segments[2].Elements[2] + ": " + msg.Segments[2].Elements[4];
            //        response3.Text = msg.Segments[3].Elements[2] + ": " + msg.Segments[3].Elements[4];
            //        response4.Text = msg.Segments[4].Elements[2] + ": " + msg.Segments[4].Elements[4];
            //        response5.Text = msg.Segments[5].Elements[2] + ": " + msg.Segments[5].Elements[4];
            //    }
            //    catch (Exception ex)
            //    {
            //        alert.Text = ex.Message;
            //        Logger.logException(ex);
            //    }

            //    // format the table
            //    //subtotalAmount.Text = taxes.NetAmount.ToString("C2");
            //    //pstAmount.Text = taxes.PstAmount.ToString("C2");
            //    //hstAmount.Text = taxes.HstAmount.ToString("C2");
            //    //gstAmount.Text = taxes.GstAmount.ToString("C2");
            //    //totalPurchaseAmount.Text = taxes.TotalAmount.ToString("C2");

            //    // show the results div with the results
            //    results.Visible = true;
            //    alertDiv.Visible = false;
            //}
            //else
            //{
            //    alertDiv.Visible = true;
            //    results.Visible = false;
            //}
        }