Ejemplo n.º 1
0
        public ResponseInteropCommunication Execute(string req, string contentType, string url)
        {
            var btResponse = new ResponseInteropCommunication();

            HttpWebRequest request         = CreateWebRequest(contentType, url);
            XmlDocument    soapEnvelopeXml = new XmlDocument();

            soapEnvelopeXml.LoadXml(req);

            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                btResponse.MimeType   = response.ContentType;
                btResponse.StatusCode = response.StatusCode;
                using (StreamReader rd = new StreamReader(stream: response.GetResponseStream()))
                {
                    btResponse.Response = rd.ReadToEnd();
                    return(btResponse);
                }
            }
        }
Ejemplo n.º 2
0
        // Опис: Метод кој овозможува процесирање на НТТР Web повици
        // Влезни параметри: HttpContext context
        // Излезни параметри: /
        public void ProcessRequest(HttpContext context)
        {
            var transactionId = Guid.NewGuid();
            var validTId      = true;

            try
            {
                var urlSegments = _requestHelper.GetUrlSegments(context.Request.RawUrl);

                _logger.Info("context.Request.RawUrl: " + context.Request.RawUrl);
                const string ourMim     = "MIM1$$";
                const string secoundMim = "MIM2$$";
                bool         routingTokenContainOurMim = urlSegments.RoutingToken.Contains(ourMim);
                //_logger.Info("Go sodrzi nasiot Mim: " + routingTokenContainOurMim);
                bool routingTokenContainsSecoundMim = urlSegments.RoutingToken.Contains(secoundMim);
                //_logger.Info("Go sodrzi vtoriot Mim: " + routingTokenContainsSecoundMim);
                if (!routingTokenContainOurMim && !routingTokenContainsSecoundMim)
                {
                    //Dodavame MIM1$$ na RountingToken ako istiot nema
                    urlSegments.RoutingToken = "MIM1$$" + urlSegments.RoutingToken;
                    //_logger.Info("Nas mim e, treba da dodade i sea Routing token e: " + urlSegments.RoutingToken);
                }
                var logUrlSegments = JsonConvert.SerializeObject(urlSegments);

                _logger.Info("urlSegments e: " + JsonConvert.SerializeObject(logUrlSegments) + "urlsegment: " +
                             context.Request.RawUrl);
                var contentType = context.Request.ContentType;
                var action      = contentType.Split(';').Last();
                //_logger.Info("action e:" + action);
                var actionName          = string.Empty;
                var executionMethodName = string.Empty;
                if (!string.IsNullOrEmpty(action))
                {
                    actionName          = action.Substring(action.IndexOf('"') + 1);
                    executionMethodName = actionName.Substring(0, actionName.Length - 1);
                }

                //_logger.Info("actionName e:" + actionName);
                //_logger.Info("executionMethodName e:" + executionMethodName);
                var soapBody = string.Empty;
                try
                {
                    var logGetSoapBody = AppSettings.Get <bool>("LogGetSoapBody");
                    if (logGetSoapBody)
                    {
                        _logger.Info(
                            "Pred da go zeme samo soap body od porakata " +
                            DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "BeforeGetOnlySoapBody");
                    }
                    soapBody = _requestHelper.GetOnlySoapBody(context.Request.InputStream);
                    if (logGetSoapBody)
                    {
                        _logger.Info(
                            "Otkako kje se zeme samo soap body od porakata " +
                            DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "AfterGetOnlySoapBody");
                    }
                    _logger.Info("soapBody e: " + soapBody);
                }
                catch (Exception ex)
                {
                    _logger.Error("Nastanata e greska kaj GetOnlySoapBody", ex);
                }

                var logCreateMimRequestMsg = AppSettings.Get <bool>("LogCreateMimRequestMsg");
                if (logCreateMimRequestMsg)
                {
                    _logger.Info("Pred CreateMimRequestMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeCreateMimRequestMsg");
                }
                var mimMsg = _mimMsgHelper.CreateMimRequestMsg(urlSegments, transactionId.ToString());
                if (logCreateMimRequestMsg)
                {
                    _logger.Info("Posle CreateMimRequestMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterCreateMimRequestMsg");
                }

                #region Log SOAP Request Message

                var logSoap = AppSettings.Get <bool>("LogSoap");
                if (logSoap)
                {
                    _logger.Info(soapBody + Environment.NewLine + "ContentType: " + contentType + Environment.NewLine +
                                 "Soap action: " + actionName);
                }

                #endregion

                if (string.IsNullOrEmpty(soapBody) || !urlSegments.IsUrlCorrrect)
                {
                    validTId = false;
                    context.Response.StatusCode = 400;
                    context.Response.End();
                }

                #region Log MIM Message

                var logInsertRequestLogInDb = AppSettings.Get <bool>("LogInsertRequestLogInDb");
                if (logInsertRequestLogInDb)
                {
                    _logger.Info("Pred LogInitialMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeLogInitialMimMessage");
                }
                _mimMsgHelper.LogInitialMimMessage(mimMsg, _logger);
                if (logInsertRequestLogInDb)
                {
                    _logger.Info("Posle LogInitialMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterLogInitialMimMessage");
                }

                #endregion

                if (urlSegments.Async)
                {
                    //TODO Async req
                }
                var logLoadOwnCertificate = AppSettings.Get <bool>("LogLoadOwnCertificate");
                if (logLoadOwnCertificate)
                {
                    _logger.Info("Pred LoadOwnCertificate " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeLoadOwnCertificate");
                }
                var ownCert = _mimMsgHelper.LoadOwnCertificate(_logger);
                if (logLoadOwnCertificate)
                {
                    _logger.Info("Posle LoadOwnCertificate " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterLoadOwnCertificate");
                }

                var logGetPrivateKey = AppSettings.Get <bool>("LogGetPrivateKey");
                if (logGetPrivateKey)
                {
                    _logger.Info("Pred GetPrivateKey " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeGetPrivateKey");
                }
                var privateKey = _mimMsgHelper.GetPrivateKey(ownCert.PrivateKey);
                if (logGetPrivateKey)
                {
                    _logger.Info("Posle GetPrivateKey " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterGetPrivateKey");
                }

                string original = soapBody;

                var logGetPublicKeyForProvider = AppSettings.Get <bool>("LogGetPublicKeyForProvider");
                if (logGetPublicKeyForProvider)
                {
                    _logger.Info("Pred GetPublicKeyForProvider " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeGetPublicKeyForProvider");
                }
                var publicKey = _mimMsgHelper.GetPublicKeyForProvider(urlSegments.RoutingToken);
                if (logGetPublicKeyForProvider)
                {
                    _logger.Info(
                        "Posle GetPublicKeyForProvider " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                        "AfterGetPublicKeyForProvider");
                }

                var logEncryption = AppSettings.Get <bool>("LogEncryption");
                if (logEncryption)
                {
                    _logger.Info(
                        "Pred da go enkriptira body koe kje se isprati vo requestot " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "BeforeEncryptRequest");
                }
                var encryptedBlock = _mimMsgHelper.EncryptSoapBody(original, publicKey.PublicKeyRsa);
                if (logEncryption)
                {
                    _logger.Info(
                        "Otkako kje go enkriptira body koe kje se isprati vo requestot " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "AfterEncryptRequest");
                }
                //var soapMethodName = _requestHelper.GetSoapMethodName(soapBody);

                #region Create MIM Message

                //set values to missing properties
                mimMsg.Body.MimBody.Message    = Convert.ToBase64String(encryptedBlock.EncryptedData);
                mimMsg.Header.CryptoHeader.Key = Convert.ToBase64String(encryptedBlock.EncryptedSessionKey);
                mimMsg.Header.CryptoHeader.InitializationVector = Convert.ToBase64String(encryptedBlock.Iv);
                mimMsg.Header.MimHeader.ServiceMethod           = executionMethodName;
                mimMsg.Header.MimHeader.PublicKey = ownCert.CertString;
                _logger.Info("mimMsg.Header.MimHeader.PublicKey is: " + ownCert.CertString);

                var logCreateMimXmlMsg = AppSettings.Get <bool>("LogCreateMimXmlMsg");
                if (logCreateMimXmlMsg)
                {
                    _logger.Info("Pred CreateMimXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeCreateMimXmlMsg");
                }
                var doc = _mimMsgHelper.CreateMimXmlMsg(mimMsg);
                if (logCreateMimXmlMsg)
                {
                    _logger.Info("Posle CreateMimXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterACreateMimXmlMsg");
                }
                //_logger.Info("MIM message before KIBS" + doc);

                try
                {
                    KIBSResponse resultkibs = null;
                    if (mimMsg.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                    {
                        resultkibs = new KIBSResponse
                        {
                            Hash = "InteropTestCommunicationCallHash"
                        };
                    }
                    else
                    {
                        if (AppSettings.Get <string>("KIBSEnviroment") != null)
                        {
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Test")
                            {
                                var logKibsTimestamp = AppSettings.Get <bool>("LogKIBS");
                                if (logKibsTimestamp)
                                {
                                    _logger.Info("Se koristi testen KIBS.");
                                }
                                var requestBeforeKibs = DateTime.Now;
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred request ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPredRequest");
                                }
                                resultkibs = KIBS.KIBS.GenerateTimeStamp(doc.ToString());
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred request ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPosleRequest");
                                    var totalMinRequestKibs     = (int)(DateTime.Now - requestBeforeKibs).TotalMinutes;
                                    var totalSecRequestKibs     = (int)(DateTime.Now - requestBeforeKibs).TotalSeconds;
                                    var totalMilisecRequestKibs =
                                        (int)(DateTime.Now - requestBeforeKibs).TotalMilliseconds;
                                    _logger.Info(
                                        "Kibs ni vratil odgovor za " + totalMinRequestKibs + " min " +
                                        totalSecRequestKibs + " sec " + totalMilisecRequestKibs + " miliseconds",
                                        "RequestKIBS");
                                }
                            }
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Production")
                            {
                                _logger.Info("Se koristi produkciski KIBS.");
                                resultkibs = KIBS.KIBS.GenerateTimeStampProduction(doc.ToString());
                            }
                        }
                    }

                    try
                    {
                        //resultkibs = new KIBSResponse
                        //{
                        //    Hash = "InteropTestCommunicationCallHash",
                        //    TimeStamp = DateTime.Now
                        //};
                        if (resultkibs != null)
                        {
                            //_logger.Info("Pred update message log: " + mimMsg.Header.MimHeader.Dir + "/" + mimMsg.Header.MimHeader.ServiceMethod + "/" + mimMsg.Header.MimHeader.PublicKey + "/" + mimMsg.Header.MimHeader.TransactionId);

                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp
                            // _messageLogsRepository.UpdateMessageLog(mimMsg.Header.MimHeader.Dir, resultkibs.TimeStamp,
                            //resultkibs.Hash, mimMsg.Header.MimHeader.ServiceMethod, mimMsg.Header.MimHeader.PublicKey,
                            //mimMsg.Header.MimHeader.TransactionId);
                            var logUpdateMessageLog = AppSettings.Get <bool>("LogUpdateMessageLog");
                            if (logUpdateMessageLog)
                            {
                                _logger.Info(
                                    "Pred UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                    "BeforeUpdateMessageLog");
                            }
                            _messageLogsRepository.UpdateMessageLog(mimMsg.Header.MimHeader.Dir, resultkibs.Hash,
                                                                    mimMsg.Header.MimHeader.ServiceMethod, mimMsg.Header.MimHeader.PublicKey,
                                                                    mimMsg.Header.MimHeader.TransactionId);
                            if (logUpdateMessageLog)
                            {
                                _logger.Info(
                                    "Posle UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                    "AfterUpdateMessageLog");
                            }

                            //mimMsg.Header.MimHeader.TimeStamp = resultkibs.TimeStamp; //.ToString();

                            mimMsg.Header.MimHeader.TimeStamp = DateTime.Now;
                            mimMsg.Header.MimAdditionalHeader.TimeStampToken = resultkibs.Hash;

                            //_logger.Info("posle kibs so result: " + resultkibs.TimeStamp);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger.Error("Error while updating mim message: ", exception);
                        throw new Exception("Неуспешна комуникација со KIBS!");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("KIBS error posle CreateMimRequestMsg: " + ex.Message);
                    throw new Exception("KIBS врати грешка!");
                }

                var logCreateMimSignedXmlMsg = AppSettings.Get <bool>("LogCreateMimSignedXmlMsg");
                if (logCreateMimSignedXmlMsg)
                {
                    _logger.Info("Pred CreateMimSignedXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeCreateMimSignedXmlMsg");
                }
                var mimMsgXml = _mimMsgHelper.CreateMimSignedXmlMsg(mimMsg, ownCert, _logger);
                if (logCreateMimSignedXmlMsg)
                {
                    _logger.Info("Posle CreateMimSignedXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterCreateMimSignedXmlMsg");
                }

                //_logger.Info("Created MIM signed xml message: " + mimMsgXml);

                #endregion



                var urlToBizTalk = AppSettings.Get <string>("URLWebRequest");
                ResponseInteropCommunication responseBizTalk = null;
                try
                {
                    //_logger.Info("pred responseBizTalk urlToBizTalk" + urlToBizTalk);
                    var timeBeforeCallBizTalk = DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt");
                    responseBizTalk = _soapRequestHelper.Execute(mimMsgXml, contentType, urlToBizTalk, _logger);

                    _logger.Info(
                        "Calling BizTalk in " + timeBeforeCallBizTalk + " , receving response in " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "BizTalkResponseTime");
                    //Real MIM message
                    var logUnwrapSoapFaultMessage = AppSettings.Get <bool>("LogUnwrapSoapFaultMessage");
                    if (logUnwrapSoapFaultMessage)
                    {
                        _logger.Info(
                            "Pred UnwrapSoapFaultMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                            "BeforeUnwrapSoapFaultMessage");
                    }
                    var soapFaultUnwrapped = _soapRequestHelper.UnwrapSoapFaultMessage(responseBizTalk.Response);
                    if (logUnwrapSoapFaultMessage)
                    {
                        _logger.Info(
                            "Posle UnwrapSoapFaultMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                            "AfterUnwrapSoapFaultMessage");
                    }
                    //_logger.Info("Pominal kaj soapFaultUnwrapped!");
                    _logger.Info(responseBizTalk.Response, "BTResponseReceived");


                    if (soapFaultUnwrapped.Body.Fault != null)
                    {
                        // BizzTalk error
                        if (soapFaultUnwrapped.Body.Fault.Code.Subcode.value == "10000")
                        {
                            if (soapFaultUnwrapped.Body.Fault.Code.value == "10001")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    AppSettings.Get <string>("SoapFaultAccessMapping"));
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10002")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    AppSettings.Get <string>("SoapFaultParticipantInactive"));
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10003")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    AppSettings.Get <string>("SoapFaultServiceUnReachable"));
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10004")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    AppSettings.Get <string>("SoapFaultKIBSUnReachable"));
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10005")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    soapFaultUnwrapped.Body.Fault.Reason.Text.value);
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10010")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    soapFaultUnwrapped.Body.Fault.Reason.Text.value);
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10007")
                            {
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    AppSettings.Get <string>("SoapFaultUnsuccessfulRequest"));
                            }
                            else if (soapFaultUnwrapped.Body.Fault.Code.value == "10000")
                            {
                                //ova e vo slucaj koga CC-to e nedostapno
                                throw new Exception("BTMstatus10000BTMend;" +
                                                    AppSettings.Get <string>("SoapFaultServiceUnReachable"));
                            }
                            throw new Exception("BTMstatus10000BTMend;" +
                                                soapFaultUnwrapped.Body.Fault.Reason.Text.value);
                        }
                        // Our error
                        else
                        {
                            throw new Exception(soapFaultUnwrapped.Body.Fault.Reason.Text.value);
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.Error("Greska vo povikot do BizTalk", e);
                    throw new Exception(e.Message);
                }

                var logUnwrapMimMessage = AppSettings.Get <bool>("LogUnwrapMimMessage");
                if (logUnwrapMimMessage)
                {
                    _logger.Info("Pred UnwrapMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeUnwrapMimMessage");
                }
                var mimMsgResponse = _soapRequestHelper.UnwrapMimMessage(responseBizTalk.Response);
                if (logUnwrapMimMessage)
                {
                    _logger.Info("Posle UnwrapMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterUnwrapMimMessage");
                }

                #region Log MIM Message

                var logMimMessage = AppSettings.Get <bool>("LogMimMessage");
                if (logMimMessage)
                {
                    _logger.Info("Pred LogMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeLogMimMessage");
                }
                _mimMsgHelper.LogMimMessage(mimMsgResponse, _logger);
                if (logMimMessage)
                {
                    _logger.Info("posle LogMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterLogMimMessage");
                }

                #endregion

                #region ValidateMsg

                try
                {
                    var logValidateSignature = AppSettings.Get <bool>("LogValidateSignature");
                    if (logValidateSignature)
                    {
                        _logger.Info("Pred ValidateSignature " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                     "BeforeValidateSignature");
                        _logger.Info("Own cert string " + ownCert.CertString, "OwnCertString");
                    }
                    if (_soapRequestHelper.ValidateSignature(responseBizTalk.Response, ownCert.CertString, _logger))
                    {
                        if (logValidateSignature)
                        {
                            _logger.Info(
                                "Posle ValidateSignature " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                "AfterValidateSignature");
                        }
                        var logValidateXml = AppSettings.Get <bool>("LogValidateXml");
                        if (logValidateXml)
                        {
                            _logger.Info("Pred ValidateXml " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                         "BeforeValidateXml");
                        }
                        _validXmlMsgHelper.ValidateXml(responseBizTalk.Response);
                        if (logValidateXml)
                        {
                            _logger.Info("Posle ValidateXml " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                         "AfterValidateXml");
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Nastanala greska kaj ValidateSignature", ex);
                }

                #endregion

                var symmetricKey = mimMsgResponse.Header.CryptoHeader.Key;
                var iVector      = mimMsgResponse.Header.CryptoHeader.InitializationVector;
                var decryptBody  = string.Empty;

                try
                {
                    //_logger.Info("symmetricKey" + symmetricKey);
                    //_logger.Info("iVector" + iVector);
                    if (logEncryption)
                    {
                        _logger.Info(
                            "Pred da go dekriptira body koe kje se vrati kako response " +
                            DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "BeforeDecryptResponse");
                    }
                    decryptBody =
                        _mimMsgHelper.DecryptSoapBody(
                            Convert.FromBase64String(mimMsgResponse.Body.MimBody.Message),
                            Convert.FromBase64String(symmetricKey), Convert.FromBase64String(iVector), privateKey);
                    if (logEncryption)
                    {
                        _logger.Info(
                            "Otkako kje go dekriptira body koe kje se vrati kako response " +
                            DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "AfterDecryptResponse");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Nastanala greska kaj DecryptSoapBody", ex);
                }


                #region Log DecryptBody

                if (AppSettings.Get <bool>("LogDecryptBody"))
                {
                    _logger.Info("Decripting MIM message: " + decryptBody);
                }

                #endregion

                #region KIBS after BT

                try
                {
                    KIBSResponse resultkibsAfterBt = null;
                    if (mimMsgResponse.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                    {
                        resultkibsAfterBt = new KIBSResponse
                        {
                            Hash = "InteropTestCommunicationCallHash"
                        };
                    }
                    else
                    {
                        if (AppSettings.Get <string>("KIBSEnviroment") != null)
                        {
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Test")
                            {
                                var logKibsTimestamp = AppSettings.Get <bool>("LogKIBS");
                                if (logKibsTimestamp)
                                {
                                    _logger.Info("Se koristi testen KIBS.");
                                }
                                var responseBeforeKibs = DateTime.Now;
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred response ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPredResponse");
                                }
                                resultkibsAfterBt = KIBS.KIBS.GenerateTimeStamp(responseBizTalk.Response);
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Posle response ." + DateTime.Now.ToString("dd.MM.yyy hh:mm:ss.fff tt"),
                                        "KIBSPosleResponse");
                                    var totalMinRequestKibs     = (int)(DateTime.Now - responseBeforeKibs).TotalMinutes;
                                    var totalSecRequestKibs     = (int)(DateTime.Now - responseBeforeKibs).TotalSeconds;
                                    var totalMilisecRequestKibs =
                                        (int)(DateTime.Now - responseBeforeKibs).TotalMilliseconds;
                                    //TimeSpan a = DateTime.Now - responseBeforeKibs;
                                    //int b = a.Seconds;
                                    //int c = a.Milliseconds;
                                    //int d = a.Minutes;
                                    //_logger.Info("minuti ima " + d, "minuti");
                                    //_logger.Info("sekindi ima " + b, "sekundi");
                                    //_logger.Info("milisekunds ima " + c, "milisekunds");
                                    _logger.Info(
                                        "Kibs ni vratil odgovor za " + totalMinRequestKibs + " min " +
                                        totalSecRequestKibs + " sec " + totalMilisecRequestKibs + " miliseconds",
                                        "ResponseKIBS");
                                }
                            }
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Production")
                            {
                                _logger.Info("Se koristi produkciski KIBS.");
                                resultkibsAfterBt = KIBS.KIBS.GenerateTimeStampProduction(responseBizTalk.Response);
                            }
                        }
                    }
                    try
                    {
                        //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                        //Sega se zapishuva DateTime.Now vo poleto Timestamp

                        //_messageLogsRepository.UpdateMessageLog(mimMsgResponse.Header.MimHeader.Dir,
                        //    resultkibsAfterBt.TimeStamp, resultkibsAfterBt.Hash, string.Empty, string.Empty,
                        //    mimMsg.Header.MimHeader.TransactionId);

                        var logUpdateMessageLogResponse = AppSettings.Get <bool>("LogUpdateMessageLogResponse");
                        if (logUpdateMessageLogResponse)
                        {
                            _logger.Info(
                                "Pred UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                "BeforeUpdateMessageLogResponse");
                        }
                        _messageLogsRepository.UpdateMessageLog(mimMsgResponse.Header.MimHeader.Dir,
                                                                resultkibsAfterBt.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);
                        if (logUpdateMessageLogResponse)
                        {
                            _logger.Info(
                                "Posle UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                "AfterUpdateMessageLogResponse");
                        }
                        //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                        //Sega se zapishuva DateTime.Now vo poleto Timestamp

                        //mimMsgResponse.Header.MimHeader.TimeStamp = resultkibsAfterBt.TimeStamp; //.ToString();

                        mimMsgResponse.Header.MimHeader.TimeStamp = DateTime.Now;
                        mimMsgResponse.Header.MimAdditionalHeader.TimeStampToken = resultkibsAfterBt.Hash;
                    }
                    catch (Exception exception)
                    {
                        _logger.Error("Error while updating mim message: ", exception);
                        throw new Exception("Неуспешна комуникација со KIBS!");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("KIBS error after BT: " + ex.Message);
                    throw new Exception("KIBS врати грешка!");
                }

                #endregion

                #region Log SOAP Response Message

                if (logSoap)
                {
                    _logger.Info(responseBizTalk.Response, "BizTalk response");
                }

                #endregion

                string decryptedResponseBodyWrappedInSoapEnvelope =
                    "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Body>" + decryptBody +
                    "</s:Body></s:Envelope>";
                //_logger.Info(decryptedResponseBodyWrappedInSoapEnvelope, "decryptedResponseBodyWrappedInSoapEnvelope");
                _logger.Info("Zavsilo se na External handler vo " + DateTime.Now + " casot.", "EndedOnExternalHandler");
                HttpContext.Current.Response.ContentType = "application/soap+xml";
                context.Response.Write(decryptedResponseBodyWrappedInSoapEnvelope);
            }
            catch (Exception ex)
            {
                //AccessMap=0;Status=;StatusDescription="";IsActive=1;IsExternal=1;Uri="http://internalHandler-fpiom/";KIBS=True;
                if (validTId)
                {
                    var soapFault = new SoapFaultMessage();

                    if (ex.Message.StartsWith("BTMstatus10000BTMend"))
                    {
                        // BizzTalk error handled - Custom
                        //var soapFaultError = _soapRequestHelper.BTsoapFault(ex.Message);
                        // BizzTalk error handled - Kornel
                        var soapFaultError = ex.Message.Split(';')[1];

                        soapFault = _mimMsgHelper.CreateSoapFault("Code value", "Code - SubCode value",
                                                                  "Details - MaxTime value", soapFaultError);
                    }
                    else
                    {
                        soapFault = _mimMsgHelper.CreateSoapFault("Code value", "Code - SubCode value",
                                                                  "Details - MaxTime value", ex.Message);
                    }

                    _logger.Error("Generalen exception", ex);
                    _logger.Error(soapFault.Body.Fault.Reason.Text.value, ex, "Error on response");

                    var soapFaultDB = _soapRequestHelper.CreateSoapFaultDB(transactionId,
                                                                           soapFault.Body.Fault.Code.value, soapFault.Body.Fault.Code.Subcode.value,
                                                                           soapFault.Body.Fault.Detail.maxTime, soapFault.Body.Fault.Reason.Text.value);

                    _soapFaultRepo.InsertSoapFault(soapFaultDB);

                    var soapFaultXml = _mimMsgHelper.CreateFaultMessage(soapFault);

                    HttpContext.Current.Response.ContentType = "application/soap+xml";
                    HttpContext.Current.Response.Write(soapFaultXml.InnerXml);
                }
            }
        }
Ejemplo n.º 3
0
        // Опис: Метод кој овозможува процесирање на НТТР Web повици
        // Влезни параметри: HttpContext context
        // Излезни параметри: /
        public void ProcessRequest(HttpContext context)
        {
            var validTId      = false;
            var transactionId = new Guid();

            try
            {
                var logGetSoapBody = AppSettings.Get <bool>("LogGetSoapBody");
                if (logGetSoapBody)
                {
                    _logger.Info(
                        "Pred da go zeme samo soap body od porakata " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "BeforeGetSoapBody");
                }
                var soapBody = _requestHelper.GetSoapBody(context.Request.InputStream);
                if (logGetSoapBody)
                {
                    _logger.Info(
                        "Otkako kje go zeme soap body od porakata " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "AfterGetSoapBody");
                }
                var contentType = context.Request.ContentType;



                #region Log SOAP Request Message

                var logSoap = AppSettings.Get <bool>("LogSoap");
                if (logSoap)
                {
                    _logger.Info(soapBody + Environment.NewLine + "ContentType: " + contentType,
                                 "Request to Internal handler");
                }
                //soapBody = File.ReadAllText(@"C:\NSRequest.txt"); ;

                #endregion

                if (string.IsNullOrEmpty(soapBody))
                {
                    context.Response.StatusCode = 400;
                    context.Response.End();
                }

                var logUnwrapMimMessage = AppSettings.Get <bool>("LogUnwrapMimMessage");
                if (logUnwrapMimMessage)
                {
                    _logger.Info("Pred UnwrapMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeUnwrapMimMessage");
                }
                var mimMsg = _soapRequestHelper.UnwrapMimMessage(soapBody);
                if (logUnwrapMimMessage)
                {
                    _logger.Info("Posle UnwrapMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterUnwrapMimMessage");
                }

                validTId      = true;
                transactionId = new Guid(mimMsg.Header.MimHeader.TransactionId);

                #region Log MIM Message

                var logMimMessageRequest = AppSettings.Get <bool>("LogMimMessageRequest");
                if (logMimMessageRequest)
                {
                    _logger.Info("Pred LogMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeLogMimMessage");
                }
                _mimMsgHelper.LogMimMessage(mimMsg, _logger);
                if (logMimMessageRequest)
                {
                    _logger.Info("Posle LogMimMessage " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterLogMimMessage");
                }

                #endregion

                var logLoadOwnCertificate = AppSettings.Get <bool>("LogLoadOwnCertificate");
                if (logLoadOwnCertificate)
                {
                    _logger.Info("Pred LoadOwnCertificate " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeLoadOwnCertificate");
                }
                var ownCert = _mimMsgHelper.LoadOwnCertificate(_logger);
                if (logLoadOwnCertificate)
                {
                    _logger.Info("Posle LoadOwnCertificate " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterLoadOwnCertificate");
                }

                var logGetPrivateKey = AppSettings.Get <bool>("LogGetPrivateKey");
                if (logGetPrivateKey)
                {
                    _logger.Info("Pred GetPrivateKey " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeGetPrivateKey");
                }
                var privateKey = _mimMsgHelper.GetPrivateKey(ownCert.PrivateKey);
                if (logGetPrivateKey)
                {
                    _logger.Info("Posle GetPrivateKey " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterGetPrivateKey");
                }
                var publicKey = mimMsg.Header.MimHeader.PublicKey;

                var logValidateSignature = AppSettings.Get <bool>("LogValidateSignature");
                if (logValidateSignature)
                {
                    _logger.Info("Pred ValidateSignature " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeValidateSignature");
                }
                if (_soapRequestHelper.ValidateSignature(soapBody, publicKey, _logger))
                {
                    if (logValidateSignature)
                    {
                        _logger.Info("Posle ValidateSignature " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                     "AfterValidateSignature");
                    }
                    var logValidateXml = AppSettings.Get <bool>("LogValidateXml");
                    if (logValidateXml)
                    {
                        _logger.Info("Pred ValidateXml " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                     "BeforeValidateXml");
                    }
                    _validXmlMsgHelper.ValidateXml(soapBody);
                    if (logValidateXml)
                    {
                        _logger.Info("Posle ValidateXml " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                     "AfterValidateXml");
                    }
                }

                var symmetricKey = mimMsg.Header.CryptoHeader.Key;
                var iVector      = mimMsg.Header.CryptoHeader.InitializationVector;

                var logEncryption = AppSettings.Get <bool>("LogEncryption");
                if (logEncryption)
                {
                    _logger.Info(
                        "Pred da go dekriptira body koe kje se prati vo requestot " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "BeforeDecryptRequest");
                }
                var decryptBody =
                    _mimMsgHelper.DecryptSoapBody(Convert.FromBase64String(mimMsg.Body.MimBody.Message.ToString()),
                                                  Convert.FromBase64String(symmetricKey), Convert.FromBase64String(iVector), privateKey);
                if (logEncryption)
                {
                    _logger.Info(
                        "Otkako kje go dekriptira body koe kje se prati vo requestot " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "AfterDecryptRequest");
                }

                #region Log DecryptBody

                if (AppSettings.Get <bool>("LogDecryptBody"))
                {
                    _logger.Info("Decripting MIM message: " + decryptBody);
                }

                #endregion

                #region KIBS after DecryptSoapBody

                try
                {
                    KIBSResponse resultkibs = null;
                    if (mimMsg.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                    {
                        resultkibs = new KIBSResponse
                        {
                            Hash = "InteropTestCommunicationCallHash"
                        };
                    }
                    else
                    {
                        if (AppSettings.Get <string>("KIBSEnviroment") != null)
                        {
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Test")
                            {
                                var logKibsTimestamp = AppSettings.Get <bool>("LogKIBS");
                                if (logKibsTimestamp)
                                {
                                    _logger.Info("Se koristi testen KIBS.");
                                }
                                var requestBeforeKibs = DateTime.Now;
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred request ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPredRequest");
                                }
                                resultkibs = KIBS.KIBS.GenerateTimeStamp(soapBody);
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred request ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPosleRequest");
                                    var totalMinRequestKibs     = (int)(DateTime.Now - requestBeforeKibs).TotalMinutes;
                                    var totalSecRequestKibs     = (int)(DateTime.Now - requestBeforeKibs).TotalSeconds;
                                    var totalMilisecRequestKibs =
                                        (int)(DateTime.Now - requestBeforeKibs).TotalMilliseconds;
                                    _logger.Info(
                                        "Kibs ni vratil odgovor za " + totalMinRequestKibs + " min " +
                                        totalSecRequestKibs + " sec " + totalMilisecRequestKibs + " miliseconds",
                                        "RequestKIBS");
                                }
                            }
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Production")
                            {
                                _logger.Info("Se koristi produkciski KIBS.");
                                resultkibs = KIBS.KIBS.GenerateTimeStampProduction(soapBody);
                            }
                        }

                        //resultkibs = new KIBSResponse
                        //{
                        //    Hash = "InteropTestCommunicationCallHash",
                        //    TimeStamp = DateTime.Now
                        //};
                    }

                    try
                    {
                        if (resultkibs != null)
                        {
                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp

                            //_messageLogsRepository.UpdateMessageLog(mimMsg.Header.MimHeader.Dir, resultkibs.TimeStamp,
                            //    resultkibs.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);
                            var logUpdateMessageLog = AppSettings.Get <bool>("LogUpdateMessageLog");
                            if (logUpdateMessageLog)
                            {
                                _logger.Info(
                                    "Pred UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                    "BeforeUpdateMessageLogRequest");
                            }
                            _messageLogsRepository.UpdateMessageLog(mimMsg.Header.MimHeader.Dir, resultkibs.Hash,
                                                                    string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);
                            if (logUpdateMessageLog)
                            {
                                _logger.Info(
                                    "Posle UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                    "AfterUpdateMessageLogRequest");
                            }
                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp

                            //mimMsg.Header.MimHeader.TimeStamp = resultkibs.TimeStamp; //.ToString();

                            mimMsg.Header.MimHeader.TimeStamp = DateTime.Now;
                            mimMsg.Header.MimAdditionalHeader.TimeStampToken = resultkibs.Hash;
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger.Error("Error while updating mim message: ", exception.Message);
                        throw new Exception("Грешка при UpdateMessageLog!");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("KIBS error posle DecryptSoapBody: " + ex.Message);
                    throw new Exception("KIBS врати грешка!");
                }

                #endregion

                //_logger.Info("posle LogMimMessage");

                var logGetServiceUrl = AppSettings.Get <bool>("LogGetServiceUrl");
                if (logGetServiceUrl)
                {
                    _logger.Info("Pred GetServiceUrl " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeGetServiceUrl");
                }
                var urlToHostedApp = _requestHelper.GetServiceUrl(mimMsg.Header.MimHeader);
                if (logGetServiceUrl)
                {
                    _logger.Info("Posle GetServiceUrl " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterGetServiceUrl");
                }

                //_logger.Info("urlToHostedApp mimheader e: " + mimMsg.Header.MimHeader);
                _logger.Info("urlToHostedApp e: " + urlToHostedApp);

                #region ExecuteRequestToInstitution

                ResponseInteropCommunication responseEndPointInstitution;

                string decryptedRequestBodyWrappedInSoapEnvelope =
                    "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Body>" + decryptBody +
                    "</s:Body></s:Envelope>";

                if (mimMsg.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                {
                    responseEndPointInstitution = new ResponseInteropCommunication
                    {
                        Response =
                            @"<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Body><InteropTestCommunicationService xmlns=""http://tempuri.org/"">InteropTestCommunicationCallResponse</InteropTestCommunicationService></s:Body></s:Envelope>",
                        MimeType = "InteropTestCommunicationCallMimeType"
                    };
                }
                else
                {
                    try
                    {
                        _logger.Info("Povikot koj se prakja e: " + decryptedRequestBodyWrappedInSoapEnvelope);
                        System.Net.ServicePointManager.ServerCertificateValidationCallback =
                            ((sender, certificate, chain, sslPolicyErrors) => true);
                        //sertifikatot ne im e u red za toa go stavam ova za da go ignorira
                        var timeBeforeCallInstitution = DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt");
                        //ovaa linija da se proveri za custom interop fault greski, od tuka pa natamu da se proveruva povikot
                        responseEndPointInstitution =
                            _soapRequestHelper.Execute(decryptedRequestBodyWrappedInSoapEnvelope, contentType,
                                                       urlToHostedApp, _logger);
                        //_logger.Info(
                        //    "decryptedRequestBodyWrappedInSoapEnvelope" + decryptedRequestBodyWrappedInSoapEnvelope +
                        //    "contentType" + contentType + "urltToApp" + urlToHostedApp, "Test");
                        _logger.Info(
                            "Calling institution  in " + timeBeforeCallInstitution + " , receiving response in " +
                            DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"), "InsitutionTotalResponse");

                        //ovie dva loga ne gi pravi bidejki direktno odi na exception!
                        //_logger.Info("Response institution is: " + responseEndPointInstitution.Response, "InstitutionReturnedLength");
                        // _logger.Info("Response institution bez envelope is: " + _requestHelper.GetOnlySoapBodyFromString(responseEndPointInstitution.Response));
                    }
                    //tuka probaj stavi catch (FaultException e) da vidish shto kje vrati
                    catch (FaultException ex)
                    {
                        //_logger.Info("dosol vo fault exception.");
                        //_logger.Error("Error koj se vrakja od fault exception e: ", ex);
                        _logger.Error("Error koj se vrakja od fault exception Message e: ", ex.Message);
                        //_logger.Error("Error koj se vrakja od fault exception inner exception e: ", ex.InnerException.Message);
                        throw new FaultException(ex.Message);
                    }


                    //MARIJA
                    //DODADENO za da se fati Web exception od povikot do adapterot vo slucaj koga toj e nedostapen
                    //INICIJALNO ova BESE ZAKOMENTIRANO
                    //catch (Exception e)
                    //{
                    //    _logger.Info("catch exception e: " + e.Message, "WebErrorVoPovikotDoAdapterot");
                    //    //throw new Exception("Сервисот кој го повикавте врати грешка!", e);
                    //    throw new FaultException(e.Message);
                    //}
                }

                #endregion

                #region Create MIM Message

                var logCreateMimResponseMsg = AppSettings.Get <bool>("LogCreateMimResponseMsg");
                if (logCreateMimResponseMsg)
                {
                    _logger.Info("Pred CreateMimResponseMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeCreateMimResponseMsg");
                }
                var mimMsgResponse = _mimMsgHelper.CreateMimResponseMsg(mimMsg, responseEndPointInstitution.MimeType);
                if (logCreateMimResponseMsg)
                {
                    _logger.Info("Posle CreateMimResponseMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterCreateMimResponseMsg");
                }

                #region Log MIM Message

                //_logger.Info("Pred LogMimMessage.");
                var logMimMessageResponse = AppSettings.Get <bool>("LogMimMessageResponse");
                if (logMimMessageResponse)
                {
                    _logger.Info("Pred LogMimMessageResponse " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeLogMimMessageResponse");
                }
                _mimMsgHelper.LogMimMessage(mimMsgResponse, _logger);
                if (logMimMessageResponse)
                {
                    _logger.Info("Posle LogMimMessageResponse " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterLogMimMessageResponse");
                }
                //_logger.Info("Posle LogMimMessage.");

                #endregion

                //_logger.Info("Pred responseWithoutSoapEnvelope.");
                var logGetOnlySoapBodyFromString = AppSettings.Get <bool>("LogGetOnlySoapBodyFromString");
                if (logGetOnlySoapBodyFromString)
                {
                    _logger.Info(
                        "Pred GetOnlySoapBodyFromString " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                        "BeforeGetOnlySoapBodyFromString");
                }
                string responseWithoutSoapEnvelope =
                    _requestHelper.GetOnlySoapBodyFromString(responseEndPointInstitution.Response);
                if (logGetOnlySoapBodyFromString)
                {
                    _logger.Info(
                        "Posle GetOnlySoapBodyFromString " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                        "AfterGetOnlySoapBodyFromString");
                }
                //_logger.Info("Posle responseWithoutSoapEnvelope.");

                var logGetPublicKeyFromString = AppSettings.Get <bool>("LogGetPublicKeyFromString");
                if (logGetPublicKeyFromString)
                {
                    _logger.Info("Pred GetPublicKeyFromString " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeGetPublicKeyFromString");
                }
                var publicKeyRsa = _mimMsgHelper.GetPublicKeyFromString(publicKey);
                if (logGetPublicKeyFromString)
                {
                    _logger.Info("Posle GetPublicKeyFromString " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterGetPublicKeyFromString");
                }
                if (logEncryption)
                {
                    _logger.Info(
                        "Pred da go enkriptira body koe kje se prati vo responsot " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                        "BeforeEncryptResponse");
                }
                var encryptedBlock = _mimMsgHelper.EncryptSoapBody(responseWithoutSoapEnvelope, publicKeyRsa);
                if (logEncryption)
                {
                    _logger.Info(
                        "Otkako kje go enkriptira body koe kje se prati vo responsot " +
                        DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                        "AfterEncryptResponse");
                }

                var encryptedSoapBody   = Convert.ToBase64String(encryptedBlock.EncryptedData);
                var encryptedSessionKey = Convert.ToBase64String(encryptedBlock.EncryptedSessionKey);
                var encriptedIVector    = Convert.ToBase64String(encryptedBlock.Iv);

                mimMsgResponse.Body.MimBody.Message = encryptedSoapBody;
                mimMsgResponse.Header.CryptoHeader.InitializationVector = encriptedIVector;
                mimMsgResponse.Header.CryptoHeader.Key = encryptedSessionKey;

                var logCreateMimXmlMsg = AppSettings.Get <bool>("LogCreateMimXmlMsg");
                if (logCreateMimXmlMsg)
                {
                    _logger.Info("Pred CreateMimXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeCreateMimXmlMsg");
                }
                var doc = _mimMsgHelper.CreateMimXmlMsg(mimMsgResponse);
                if (logCreateMimXmlMsg)
                {
                    _logger.Info("Posle CreateMimXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterCreateMimXmlMsg");
                }

                #region KIBS after CreateMimXmlMsg

                try
                {
                    KIBSResponse resultkibsResp = null;
                    if (mimMsgResponse.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                    {
                        resultkibsResp = new KIBSResponse
                        {
                            Hash = "InteropTestCommunicationCallHash"
                        };
                    }
                    else
                    {
                        if (AppSettings.Get <string>("KIBSEnviroment") != null)
                        {
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Test")
                            {
                                var logKibsTimestamp = AppSettings.Get <bool>("LogKIBS");
                                if (logKibsTimestamp)
                                {
                                    _logger.Info("Se koristi testen KIBS.");
                                }
                                var responseBeforeKibs = DateTime.Now;
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred request ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPredResponse");
                                }
                                resultkibsResp = KIBS.KIBS.GenerateTimeStamp(doc.ToString());
                                if (logKibsTimestamp)
                                {
                                    _logger.Info(
                                        "Pred request ." + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                        "KIBSPosleResponse");
                                    var totalMinRequestKibs     = (int)(DateTime.Now - responseBeforeKibs).TotalMinutes;
                                    var totalSecRequestKibs     = (int)(DateTime.Now - responseBeforeKibs).TotalSeconds;
                                    var totalMilisecRequestKibs =
                                        (int)(DateTime.Now - responseBeforeKibs).TotalMilliseconds;
                                    _logger.Info(
                                        "Kibs ni vratil odgovor za " + totalMinRequestKibs + " min " +
                                        totalSecRequestKibs + " sec " + totalMilisecRequestKibs + " miliseconds",
                                        "ResponseKIBS");
                                }
                            }
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Production")
                            {
                                _logger.Info("Se koristi produkciski KIBS.");
                                resultkibsResp = KIBS.KIBS.GenerateTimeStampProduction(doc.ToString());
                            }
                        }

                        //resultkibsResp = new KIBSResponse
                        //{
                        //    Hash = "InteropTestCommunicationCallHash",
                        //    TimeStamp = DateTime.Now
                        //};
                    }

                    try
                    {
                        if (resultkibsResp != null)
                        {
                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp

                            //_messageLogsRepository.UpdateMessageLog(mimMsgResponse.Header.MimHeader.Dir, resultkibsResp.TimeStamp, resultkibsResp.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);
                            var logUpdateMessageLogResponse = AppSettings.Get <bool>("LogUpdateMessageLogResponse");
                            if (logUpdateMessageLogResponse)
                            {
                                _logger.Info(
                                    "Pred UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                    "BeforeUpdateMessageLogResponse");
                            }
                            _messageLogsRepository.UpdateMessageLog(mimMsgResponse.Header.MimHeader.Dir,
                                                                    resultkibsResp.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);
                            if (logUpdateMessageLogResponse)
                            {
                                _logger.Info(
                                    "Posle UpdateMessageLog " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                    "AfterUpdateMessageLogResponse");
                            }

                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp
                            //mimMsgResponse.Header.MimHeader.TimeStamp = resultkibsResp.TimeStamp;//.ToString();

                            mimMsgResponse.Header.MimHeader.TimeStamp = DateTime.Now;
                            mimMsgResponse.Header.MimAdditionalHeader.TimeStampToken = resultkibsResp.Hash;
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger.Info("KIBS error: " + exception.Message);
                        throw new Exception("KIBS врати грешка: " + exception.Message);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("KIBS error after CreateMimXmlMsg: " + ex.Message);
                    throw new Exception("KIBS врати грешка: " + ex.Message);
                }

                #endregion

                var logCreateMimSignedXmlMsg = AppSettings.Get <bool>("LogCreateMimSignedXmlMsg");
                if (logCreateMimSignedXmlMsg)
                {
                    _logger.Info("Pred CreateMimSignedXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "BeforeCreateMimSignedXmlMsg");
                }
                var mimMsgResponseXml = _mimMsgHelper.CreateMimSignedXmlMsg(mimMsgResponse, ownCert, _logger);
                //Check for need of encryption
                if (logCreateMimSignedXmlMsg)
                {
                    _logger.Info("Posle CreateMimSignedXmlMsg " + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff tt"),
                                 "AfterCreateMimSignedXmlMsg");
                }

                #endregion

                #region Log SOAP Response Message

                if (logSoap)
                {
                    //LogHelper.WriteInNLoc("", "", mimMsgResponseXml, "Response_" + DateTime.Now, "Info");
                    _logger.Info(mimMsgResponseXml, "ResponseSignedXml");
                }

                #endregion

                HttpContext.Current.Response.ContentType = "application/soap+xml";
                context.Response.Write(mimMsgResponseXml);
                _logger.Info("Zavsilo se na Internal handler vo " + DateTime.Now + " casot.", "EndedOnInternalHandler");
            }
            catch (FaultException ex)
            {
                //_logger.Info("dosol vo fault exception.");
                _logger.Error("Error koj se vrakja od fault exception e: ", ex, "CatchFaultException");
                //_logger.Info("validTID vo glavniot catch e: " + validTId);
                if (validTId)
                {
                    var soapFault   = _mimMsgHelper.CreateSoapFault("Code value", "Code - SubCode value", "Details - MaxTime value", ex.Message);
                    var soapFaultDb = _soapRequestHelper.CreateSoapFaultDB(transactionId,
                                                                           soapFault.Body.Fault.Code.value, soapFault.Body.Fault.Code.Subcode.value,
                                                                           soapFault.Body.Fault.Detail.maxTime, soapFault.Body.Fault.Reason.Text.value);

                    try
                    {
                        _soapFaultRepo.InsertSoapFault(soapFaultDb);
                    }
                    catch (Exception exception)
                    {
                        _logger.Error(exception, "Nastanata greska pri kreiranje na soap fault vo baza.");
                    }

                    var soapFaultXml = _mimMsgHelper.CreateFaultMessage(soapFault);
                    _logger.Info(soapFaultXml.InnerXml, "SoapFaultXml");
                    _logger.Error(soapFault.Body.Fault.Reason.Text.value, ex, "ErrorOnResponseInternalHandler");

                    HttpContext.Current.Response.ContentType = "application/soap+xml";
                    HttpContext.Current.Response.Write(soapFaultXml.InnerXml);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("odma u catch", ex);
                _logger.Info("validTID vo glavniot catch e: " + validTId);
                if (validTId)
                {
                    var soapFault = _mimMsgHelper.CreateSoapFault("Code value", "Code - SubCode value",
                                                                  "Details - MaxTime value", ex.Message);
                    var soapFaultDb = _soapRequestHelper.CreateSoapFaultDB(transactionId,
                                                                           soapFault.Body.Fault.Code.value, soapFault.Body.Fault.Code.Subcode.value,
                                                                           soapFault.Body.Fault.Detail.maxTime, soapFault.Body.Fault.Reason.Text.value);

                    try
                    {
                        _soapFaultRepo.InsertSoapFault(soapFaultDb);
                    }
                    catch (Exception exception)
                    {
                        _logger.Error(exception, "Nastanata greska pri kreiranje na soap fault vo baza.");
                    }

                    var soapFaultXml = _mimMsgHelper.CreateFaultMessage(soapFault);
                    _logger.Error(soapFault.Body.Fault.Reason.Text.value, ex, "Error on response");

                    HttpContext.Current.Response.ContentType = "application/soap+xml";
                    HttpContext.Current.Response.Write(soapFaultXml.InnerXml);
                }
            }
        }
Ejemplo n.º 4
0
        // Опис: Метод кој овозможува процесирање и проследување на Web повици помеѓу системите во рамки на MIM архитектурата
        // Влезни параметри: Податочни вредности req, contentType, url
        // Излезни параметри: ResponseInteropCommunication модел како одговор на соодветен Web повик
        public ResponseInteropCommunication Execute(string req, string contentType, string url, ILogger logger)
        {
            logger.Info("Execute kon BizTalk");
            var btResponse = new ResponseInteropCommunication();

            HttpWebRequest request = CreateWebRequest(contentType, url);

            //request.Timeout = Timeout.Infinite;
            //request.KeepAlive = true;

            //IMPORTANT!!! DO NOT MOVE!!! IT'S SET TO AVOID TIMEOUT THAT APPEARS WHILE SENDING BIG MESSAGES
            request.Timeout = 120000;//600000(10 min)
            ////////////////////

            var soapEnvelopeXml = new XmlDocument();

            soapEnvelopeXml.LoadXml(req);

            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    btResponse.MimeType   = response.ContentType;
                    btResponse.StatusCode = response.StatusCode;
                    var responseFromBizTalk = response.GetResponseStream();
                    if (responseFromBizTalk != null)
                    {
                        using (var rd = new StreamReader(responseFromBizTalk))
                        {
                            btResponse.Response = rd.ReadToEnd();
                            rd.Close();
                            rd.Dispose();
                        }
                        response.Close();
                    }
                }
            }
            catch (WebException exception)
            {
                //Овој catch дел е за да ги прикажеме custom InteropFault грешки, што ги праќаме од адаптерите како и грешки ако адаптерот е недостапен

                logger.Info("errResponse status e: " + exception.Status);
                logger.Error("WebException vo povikot kon BizTalk", exception);
                WebResponse errResponse = exception.Response;
                if (errResponse.ContentLength == 0)
                {
                    if (exception.Status == WebExceptionStatus.ProtocolError ||
                        exception.Status == WebExceptionStatus.Timeout)
                    {
                        logger.Error("Adapterot na institucijata e nedostapen i vrati greska: " + exception.Message,
                                     exception, "ProtocolOrTimeoutErrorOnAdapterRequest");
                        throw new FaultException("Адаптерот на сервисот е недостапен!");
                    }
                }
                else
                {
                    string fullInteropFaultFromAdapter = string.Empty;
                    if (errResponse.ContentLength > 0)
                    {
                        //Se vrakja realen Fault exception od adapterot
                        using (Stream faultFromAnotherSide = errResponse.GetResponseStream())
                        {
                            if (faultFromAnotherSide != null)
                            {
                                var reader = new StreamReader(faultFromAnotherSide);
                                fullInteropFaultFromAdapter = reader.ReadToEnd();
                            }
                        }
                        if (!string.IsNullOrEmpty(fullInteropFaultFromAdapter))
                        {
                            var fullInteropFaultFromAdapterReason = UnwrapSoapFaultMessage(fullInteropFaultFromAdapter);
                            throw new FaultException(fullInteropFaultFromAdapterReason.Body.Fault.Reason.Text.value);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                logger.Error("Exception vo samiot BizTalk/Servisot na institucijata", exception, "ExceptionBizTalkORInstitution");
                throw new Exception(exception.Message, exception.InnerException);
            }

            return(btResponse);
        }
Ejemplo n.º 5
0
        // Опис: Метод кој овозможува процесирање на НТТР Web повици
        // Влезни параметри: HttpContext context
        // Излезни параметри: /
        public void ProcessRequest(HttpContext context)
        {
            var validTId      = false;
            var transactionId = new Guid();

            try
            {
                var soapBody    = _requestHelper.GetSoapBody(context.Request.InputStream);
                var contentType = context.Request.ContentType;
                var soapHeader  = _requestHelper.GetSoapHeader(context.Request.Headers);

                #region Log SOAP Request Message

                var logSoap = AppSettings.Get <bool>("LogSoap");
                if (logSoap)
                {
                    _logger.Info(soapBody + Environment.NewLine + "ContentType: " + contentType + Environment.NewLine + "SoapHeader: " + soapHeader, "Request to Internal handler");
                }
                //soapBody = File.ReadAllText(@"C:\NSRequest.txt"); ;
                #endregion

                if (string.IsNullOrEmpty(soapBody))
                {
                    context.Response.StatusCode = 400;
                    context.Response.End();
                }

                var mimMsg = _soapRequestHelper.UnwrapMimMessage(soapBody);
                validTId      = true;
                transactionId = new Guid(mimMsg.Header.MimHeader.TransactionId);

                #region Log MIM Message
                _mimMsgHelper.LogMimMessage(mimMsg, _logger);
                #endregion

                var ownCert    = _mimMsgHelper.LoadOwnCertificate(_logger);
                var privateKey = _mimMsgHelper.GetPrivateKey(ownCert.PrivateKey);
                var publicKey  = mimMsg.Header.MimHeader.PublicKey;
                if (_soapRequestHelper.ValidateSignature(soapBody, publicKey, _logger))
                {
                    _validXmlMsgHelper.ValidateXml(soapBody);
                }
                var symmetricKey = mimMsg.Header.CryptoHeader.Key;
                var iVector      = mimMsg.Header.CryptoHeader.InitializationVector;

                var decryptBody = _mimMsgHelper.DecryptSoapBody(Convert.FromBase64String(mimMsg.Body.MimBody.Message.ToString()), Convert.FromBase64String(symmetricKey), Convert.FromBase64String(iVector), privateKey);

                #region Log DecryptBody
                if (AppSettings.Get <bool>("LogDecryptBody"))
                {
                    _logger.Info("Decripting MIM message: " + decryptBody);
                }
                #endregion

                #region KIBS after DecryptSoapBody

                try
                {
                    KIBSResponse resultkibs = null;
                    if (mimMsg.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                    {
                        resultkibs = new KIBSResponse
                        {
                            Hash = "InteropTestCommunicationCallHash"
                        };
                    }
                    else
                    {
                        if (AppSettings.Get <string>("KIBSEnviroment") != null)
                        {
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Test")
                            {
                                _logger.Info("Se koristi testen KIBS.");
                                resultkibs = KIBS.KIBS.GenerateTimeStamp(soapBody);
                            }
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Production")
                            {
                                _logger.Info("Se koristi produkciski KIBS.");
                                resultkibs = KIBS.KIBS.GenerateTimeStampProduction(soapBody);
                            }
                        }

                        //resultkibs = new KIBSResponse
                        //{
                        //    Hash = "InteropTestCommunicationCallHash",
                        //    TimeStamp = DateTime.Now
                        //};
                    }

                    try
                    {
                        if (resultkibs != null)
                        {
                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp

                            //_messageLogsRepository.UpdateMessageLog(mimMsg.Header.MimHeader.Dir, resultkibs.TimeStamp,
                            //    resultkibs.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);

                            _messageLogsRepository.UpdateMessageLog(mimMsg.Header.MimHeader.Dir, resultkibs.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);

                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp

                            //mimMsg.Header.MimHeader.TimeStamp = resultkibs.TimeStamp; //.ToString();

                            mimMsg.Header.MimHeader.TimeStamp = DateTime.Now;
                            mimMsg.Header.MimAdditionalHeader.TimeStampToken = resultkibs.Hash;
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger.Error("Error while updating mim message: ", exception);
                        throw new Exception("Неуспешна комуникација со KIBS!");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("KIBS error posle DecryptSoapBody: " + ex.Message);
                    throw new Exception("KIBS врати грешка!");
                }
                #endregion

                _logger.Info("posle LogMimMessage");

                var urlToHostedApp = _requestHelper.GetServiceUrl(mimMsg.Header.MimHeader);

                _logger.Info("urlToHostedApp mimheader e: " + mimMsg.Header.MimHeader);
                _logger.Info("urlToHostedApp e: " + urlToHostedApp);

                #region ExecuteRequestToInstitution

                ResponseInteropCommunication responseEndPointInstitution;

                string decryptedRequestBodyWrappedInSoapEnvelope = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Body>" + decryptBody + "</s:Body></s:Envelope>";

                if (mimMsg.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                {
                    responseEndPointInstitution = new ResponseInteropCommunication
                    {
                        Response = @"<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Body><InteropTestCommunicationService xmlns=""http://tempuri.org/"">InteropTestCommunicationCallResponse</InteropTestCommunicationService></s:Body></s:Envelope>",
                        MimeType = "InteropTestCommunicationCallMimeType"
                    };
                }
                else
                {
                    try
                    {
                        _logger.Info("Povikot koj se prakja e: " + decryptedRequestBodyWrappedInSoapEnvelope);
                        System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);//sertifikatot ne im e u red za toa go stavam ova za da go ignorira
                        //ovaa linija da se proveri za custom interop fault greski, od tuka pa natamu da se proveruva povikot
                        responseEndPointInstitution = _soapRequestHelper.Execute(decryptedRequestBodyWrappedInSoapEnvelope, contentType, urlToHostedApp, _logger);
                        //ovie dva loga ne gi pravi bidejki direktno odi na exception!
                        _logger.Info("Response institution is: " + responseEndPointInstitution.Response);
                        _logger.Info("Response institution bez envelope is: " + _requestHelper.GetOnlySoapBodyFromString(responseEndPointInstitution.Response));
                    }
                    //tuka probaj stavi catch (FaultException e) da vidish shto kje vrati
                    catch (FaultException ex)
                    {
                        _logger.Info("dosol vo fault exception.");
                        _logger.Error("Error koj se vrakja od fault exception e: ", ex);
                        _logger.Error("Error koj se vrakja od fault exception Message e: ", ex.Message);
                        _logger.Error("Error koj se vrakja od fault exception inner exception e: ", ex.InnerException.Message);
                        throw;
                    }
                    catch (Exception e)
                    {
                        _logger.Info("catch exception e: " + e.Message);
                        throw new Exception("Сервисот кој го повикавте врати грешка!");
                    }
                }
                #endregion

                #region Create MIM Message

                var mimMsgResponse = _mimMsgHelper.CreateMimResponseMsg(mimMsg, responseEndPointInstitution.MimeType);

                #region Log MIM Message
                _mimMsgHelper.LogMimMessage(mimMsgResponse, _logger);
                #endregion

                string responseWithoutSoapEnvelope = _requestHelper.GetOnlySoapBodyFromString(responseEndPointInstitution.Response);

                var publicKeyRsa   = _mimMsgHelper.GetPublicKeyFromString(publicKey);
                var encryptedBlock = _mimMsgHelper.EncryptSoapBody(responseWithoutSoapEnvelope, publicKeyRsa);

                var encryptedSoapBody   = Convert.ToBase64String(encryptedBlock.EncryptedData);
                var encryptedSessionKey = Convert.ToBase64String(encryptedBlock.EncryptedSessionKey);
                var encriptedIVector    = Convert.ToBase64String(encryptedBlock.Iv);

                mimMsgResponse.Body.MimBody.Message = encryptedSoapBody;
                mimMsgResponse.Header.CryptoHeader.InitializationVector = encriptedIVector;
                mimMsgResponse.Header.CryptoHeader.Key = encryptedSessionKey;

                var doc = _mimMsgHelper.CreateMimXmlMsg(mimMsgResponse);

                #region KIBS after CreateMimXmlMsg

                try
                {
                    KIBSResponse resultkibsResp = null;
                    if (mimMsgResponse.Header.MimAdditionalHeader.IsInteropTestCommunicationCall)
                    {
                        resultkibsResp = new KIBSResponse
                        {
                            Hash = "InteropTestCommunicationCallHash"
                        };
                    }
                    else
                    {
                        if (AppSettings.Get <string>("KIBSEnviroment") != null)
                        {
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Test")
                            {
                                _logger.Info("Se koristi testen KIBS.");
                                resultkibsResp = KIBS.KIBS.GenerateTimeStamp(doc.ToString());
                            }
                            if (AppSettings.Get <string>("KIBSEnviroment") == "Production")
                            {
                                _logger.Info("Se koristi produkciski KIBS.");
                                resultkibsResp = KIBS.KIBS.GenerateTimeStampProduction(doc.ToString());
                            }
                        }

                        //resultkibsResp = new KIBSResponse
                        //{
                        //    Hash = "InteropTestCommunicationCallHash",
                        //    TimeStamp = DateTime.Now
                        //};
                    }

                    try
                    {
                        if (resultkibsResp != null)
                        {
                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp

                            //_messageLogsRepository.UpdateMessageLog(mimMsgResponse.Header.MimHeader.Dir, resultkibsResp.TimeStamp, resultkibsResp.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);

                            _messageLogsRepository.UpdateMessageLog(mimMsgResponse.Header.MimHeader.Dir, resultkibsResp.Hash, string.Empty, string.Empty, mimMsg.Header.MimHeader.TransactionId);

                            //Prethodno se zemashe vrednosta Timestamp od servisot na KIBS, i istata se zapisuvase vo baza
                            //Sega se zapishuva DateTime.Now vo poleto Timestamp
                            //mimMsgResponse.Header.MimHeader.TimeStamp = resultkibsResp.TimeStamp;//.ToString();

                            mimMsgResponse.Header.MimHeader.TimeStamp = DateTime.Now;
                            mimMsgResponse.Header.MimAdditionalHeader.TimeStampToken = resultkibsResp.Hash;
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger.Info("KIBS error: " + exception.Message);
                        throw new Exception("KIBS врати грешка: " + exception.Message);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("KIBS error after CreateMimXmlMsg: " + ex.Message);
                    throw new Exception("KIBS врати грешка: " + ex.Message);
                }
                #endregion

                var mimMsgResponseXml = _mimMsgHelper.CreateMimSignedXmlMsg(mimMsgResponse, ownCert, _logger); //Check for need of encryption

                #endregion

                #region Log SOAP Response Message

                if (logSoap)
                {
                    //LogHelper.WriteInNLoc("", "", mimMsgResponseXml, "Response_" + DateTime.Now, "Info");
                    _logger.Info(mimMsgResponseXml, "Response");
                }

                #endregion

                HttpContext.Current.Response.ContentType = "application/soap+xml";
                context.Response.Write(mimMsgResponseXml);
            }
            catch (FaultException ex)
            {
                _logger.Info("dosol vo fault exception.");
                _logger.Error("Error koj se vrakja od fault exception e: ", ex);
                _logger.Info("validTID vo glavniot catch e: " + validTId);
                if (validTId)
                {
                    var soapFault = _mimMsgHelper.CreateSoapFault("Code value", "Code - SubCode value",
                                                                  "Details - MaxTime value", ex.Message);
                    var soapFaultDB = _soapRequestHelper.CreateSoapFaultDB(transactionId,
                                                                           soapFault.Body.Fault.Code.value, soapFault.Body.Fault.Code.Subcode.value,
                                                                           soapFault.Body.Fault.Detail.maxTime, soapFault.Body.Fault.Reason.Text.value);

                    try
                    {
                        _soapFaultRepo.InsertSoapFault(soapFaultDB);
                    }
                    catch (Exception exception)
                    {
                        _logger.Error(exception, "Nastanata greska pri kreiranje na soap fault vo baza.");
                    }

                    var soapFaultXml = _mimMsgHelper.CreateFaultMessage(soapFault);
                    _logger.Error(soapFault.Body.Fault.Reason.Text.value, ex, "Error on response");

                    HttpContext.Current.Response.ContentType = "application/soap+xml";
                    HttpContext.Current.Response.Write(soapFaultXml.InnerXml);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("odma u catch", ex);
                _logger.Info("validTID vo glavniot catch e: " + validTId);
                if (validTId)
                {
                    var soapFault = _mimMsgHelper.CreateSoapFault("Code value", "Code - SubCode value",
                                                                  "Details - MaxTime value", ex.Message);
                    var soapFaultDb = _soapRequestHelper.CreateSoapFaultDB(transactionId,
                                                                           soapFault.Body.Fault.Code.value, soapFault.Body.Fault.Code.Subcode.value,
                                                                           soapFault.Body.Fault.Detail.maxTime, soapFault.Body.Fault.Reason.Text.value);

                    try
                    {
                        _soapFaultRepo.InsertSoapFault(soapFaultDb);
                    }
                    catch (Exception exception)
                    {
                        _logger.Error(exception, "Nastanata greska pri kreiranje na soap fault vo baza.");
                    }

                    var soapFaultXml = _mimMsgHelper.CreateFaultMessage(soapFault);
                    _logger.Error(soapFault.Body.Fault.Reason.Text.value, ex, "Error on response");

                    HttpContext.Current.Response.ContentType = "application/soap+xml";
                    HttpContext.Current.Response.Write(soapFaultXml.InnerXml);
                }
            }
        }