public async Task Invoke(HttpContext context) { var messageHeader = new MessageHeader { }; var messageFooter = new MessageFooter { Sender = context.Request.Path, //TODO: get request finger print FingerPrint = context.Request.Path, Route = JsonConvert.SerializeObject(context.Request.Query.ToDictionary(key => key.Key, vallue => vallue.Value.FirstOrDefault()), Defaults.JsonSerializerSettings), Hint = Enum.GetName(typeof(ResponseHint), ResponseHint.Custom) }; context.Response.ContentType = new MediaTypeHeaderValue(Identifiers.ApplicationJson)?.MediaType; using (var writer = new StreamWriter(context.Response.Body)) { var content = JsonConvert.SerializeObject(closureGenerateResponseMessage(), Defaults.JsonSerializerSettings); await writer.WriteAsync(content); } await _next(context); object closureGenerateResponseMessage() { return(new { header = messageHeader, body = string.Empty, footer = messageFooter }); } }
public Task OnExceptionAsync(ExceptionContext context) { _logger.LogCritical(context.Exception, context.Exception.Message); var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId]; //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance. var correlationId = _operationalUnit.InstanceId; if (!string.IsNullOrEmpty(correlationHeader)) { Guid.TryParse(correlationHeader, out correlationId); } var exception = context.Exception; (int code, string message, ResponseHint responseHint) = (exception is CustomException ex) ? ex.CustomMessage : (exception.HResult, exception.Message, ResponseHint.SystemError); var messageHeader = new MessageHeader(isSucceed: false) { CorrelateId = correlationId }; var messageFooter = new MessageFooter { Sender = context.ActionDescriptor.DisplayName, Environemnt = _operationalUnit.Environment, Assembly = _operationalUnit.Assembly, FingerPrint = context.ActionDescriptor.Id, Route = context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Hint = responseHint }; context.ExceptionHandled = true; context.Result = new ContentResult() { StatusCode = StatusCodes.Status500InternalServerError, ContentType = Identifiers.ApplicationJson, Content = JsonConvert.SerializeObject(closureGenerateErrorMessage(), Utilities.DefaultJsonSerializerSettings) }; return(Task.CompletedTask); object closureGenerateErrorMessage() { return(new { header = messageHeader, //TODO: system message only should share user friendly messages for system messages, as well to not break system security. body = new { code, message, systemMessage = exception.Message }, footer = messageFooter }); } }
public Task OnAuthorizationAsync(AuthorizationFilterContext context) { _logger.LogInformation("Authorize request header"); var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId]; //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance. var correlationId = _operationalUnit.InstanceId; if (!string.IsNullOrEmpty(correlationHeader)) { Guid.TryParse(correlationHeader, out correlationId); } //TODO: remove bypassing authorization if (false && string.IsNullOrEmpty(context.HttpContext.Request.Headers["authorization"])) { var messageHeader = new MessageHeader { CorrelateId = correlationId }; var messageFooter = new MessageFooter { Sender = context.ActionDescriptor.DisplayName, Environemnt = _operationalUnit.Environment, Assembly = _operationalUnit.Assembly, FingerPrint = context.ActionDescriptor.Id, Route = context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Hint = ResponseHint.UnAuthorized }; //reject the request context.Result = new ContentResult() { StatusCode = StatusCodes.Status401Unauthorized, ContentType = Identifiers.ApplicationJson, Content = JsonConvert.SerializeObject(closureGenerateResponseMessage(), Utilities.DefaultJsonSerializerSettings) }; return(Task.CompletedTask); object closureGenerateResponseMessage() { return(new { header = messageHeader, body = string.Empty, footer = messageFooter }); } } //continue the pipeline return(Task.CompletedTask); }
public Task OnAuthorizationAsync(AuthorizationFilterContext context) { _logger.LogInformation("Authorize request header"); var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId]; //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance. var correlationId = _operationalUnit.InstanceId; if (!string.IsNullOrEmpty(correlationHeader) && Guid.TryParse(correlationHeader, out Guid paresedCorId)) { correlationId = paresedCorId; } //TODO: remove bypassing authorization if (false && string.IsNullOrEmpty(context.HttpContext.Request.Headers["authorization"])) { var messageHeader = new MessageHeader { CorrelationId = correlationId }; var messageFooter = new MessageFooter { Sender = context.ActionDescriptor.DisplayName, Environment = _operationalUnit.Environment, Assembly = _operationalUnit.Assembly, FingerPrint = context.ActionDescriptor.Id, Route = JsonConvert.SerializeObject(context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Defaults.JsonSerializerSettings), Hint = Enum.GetName(typeof(ResponseHint), ResponseHint.UnAuthorized) }; messageHeader.GetType().GetProperties() .ToList().ForEach(prop => { context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageHeader)?.ToString())); }); messageFooter.GetType().GetProperties() .ToList().ForEach(prop => { context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageFooter)?.ToString())); }); context.Result = new ContentResult() { StatusCode = context.HttpContext.Response.StatusCode, ContentType = Identifiers.ApplicationJson, Content = JsonConvert.SerializeObject(Enum.GetName(typeof(ResponseHint), ResponseHint.UnAuthorized), Defaults.JsonSerializerSettings) }; _logger.LogInformation("Override response!"); return(Task.CompletedTask); } return(Task.CompletedTask); }
public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId]; //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance. var correlationId = _operationalUnit.InstanceId; if (!string.IsNullOrEmpty(correlationHeader)) { Guid.TryParse(correlationHeader, out correlationId); } var messageHeader = new MessageHeader(isSucceed: context.HttpContext.Response.StatusCode == 200) { CorrelateId = correlationId }; var messageFooter = new MessageFooter { Sender = context.ActionDescriptor.DisplayName, Environemnt = _operationalUnit.Environment, Assembly = _operationalUnit.Assembly, FingerPrint = context.ActionDescriptor.Id, Route = context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), //TODO: infer the hint from HTTP status code Hint = ResponseHint.OK }; var rawContent = (context.Result as ContentResult)?.Content; context.Result = new ContentResult() { StatusCode = context.HttpContext.Response.StatusCode, ContentType = Identifiers.ApplicationJson, Content = JsonConvert.SerializeObject(closureGenerateResponseMessage(), Utilities.DefaultJsonSerializerSettings) }; _logger.LogInformation("Overriding response!"); return(next.Invoke()); object closureGenerateResponseMessage() { return(new { header = messageHeader, body = rawContent, footer = messageFooter }); } }
public static DomainModel <T> Convert <T>(DbModel model) { var header = new MessageHeader(executionId: model.ExecutionId, correlationId: model.CorrelationId, timestamp: model.Timestamp, isSucceed: true); var body = model.Data.ToObject <T>(); var footer = new MessageFooter { Assembly = model.Assembly, Environment = model.Environment, FingerPrint = model.FingerPrint, Hint = model.Hint, Route = JsonConvert.SerializeObject(model.Route, Defaults.JsonSerializerSettings), Sender = model.Sender }; return(new DomainModel <T> { Header = header, Body = body, Footer = footer }); }
public static DbModel Create <T>(MessageHeader header, T body, MessageFooter footer) { return(new DbModel { ExecutionId = header.ExecutionId, CorrelationId = header.CorrelationId, Timestamp = header.Timestamp, Data = JObject.FromObject(body), Sender = footer.Sender, Environment = footer.Environment, Assembly = footer.Assembly, FingerPrint = footer.FingerPrint, Hint = footer.Hint, Route = JsonConvert.DeserializeObject <IDictionary <string, string> >(footer.Route, Defaults.JsonSerializerSettings) }); }
/// <summary> /// Create an EmailCampaign object from user inputs /// </summary> /// <returns></returns> private EmailCampaign CreateCampaignFromInputs() { EmailCampaign campaign = new EmailCampaign(); #region General settings if (!string.IsNullOrWhiteSpace(txtCampaignName.Text)) { campaign.Name = txtCampaignName.Text.Trim(); } if (cbCampaignType.SelectedItem != null) { //campaign.TemplateType = GetCampaignType(cbCampaignType.SelectedItem as ItemInfo); } if (!string.IsNullOrWhiteSpace(txtSubject.Text)) { campaign.Subject = txtSubject.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtFromName.Text)) { campaign.FromName = txtFromName.Text.Trim(); } if (cbFromEmail.SelectedIndex != null) { campaign.FromEmail = GetSelectedValue(cbFromEmail.SelectedItem as ItemInfo); } if (cbReplyToEmail.SelectedIndex != null) { campaign.ReplyToEmail = GetSelectedValue(cbReplyToEmail.SelectedItem as ItemInfo); } if (rbnHtml.Checked) { campaign.EmailContentFormat = CampaignEmailFormat.HTML; } else { campaign.EmailContentFormat = CampaignEmailFormat.XHTML; } if (!string.IsNullOrWhiteSpace(txtGreetingString.Text)) { campaign.GreetingString = txtGreetingString.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtEmailContent.Text)) { campaign.EmailContent = txtEmailContent.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtContent.Text)) { campaign.TextContent = txtContent.Text.Trim(); } #endregion General settings #region Message footer settings //organization name is required for message footer section if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text)) { MessageFooter msgFooter = new MessageFooter(); if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text)) { msgFooter.OrganizationName = txtOrganizationName.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtAddressLine1.Text)) { msgFooter.AddressLine1 = txtAddressLine1.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtAddressLine2.Text)) { msgFooter.AddressLine2 = txtAddressLine2.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtAddressLine3.Text)) { msgFooter.AddressLine3 = txtAddressLine3.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtCity.Text)) { msgFooter.City = txtCity.Text.Trim(); } if (!string.IsNullOrWhiteSpace(txtZip.Text)) { msgFooter.PostalCode = txtZip.Text.Trim(); } if (cbCountry.SelectedItem != null) { msgFooter.Country = GetSelectedValue(cbCountry.SelectedItem as ItemInfo); } if (cbState.SelectedItem != null) { var state = cbState.SelectedItem as ItemInfo; if (state != null) { msgFooter.State = state.Name; } } if (chkIncludeForwardEmail.Checked) { msgFooter.ForwardEmailLinkText = txtIncludeForwardEmail.Text.Trim(); msgFooter.IncludeForwardEmail = true; } if (chkIncludeSubscribeLink.Checked) { msgFooter.SubscribeLinkText = txtIncludeSubscribeLink.Text.Trim(); msgFooter.IncludeSubscribeLink = true; } campaign.MessageFooter = msgFooter; } #endregion Message footer settings #region Contact lists settings List <SentContactList> lists = new List <SentContactList>(); if (lstContactList.SelectedItems.Count > 0) { foreach (var item in lstContactList.SelectedItems) { SentContactList contactList = new SentContactList() { Id = GetSelectedValue(item as ItemInfo) }; lists.Add(contactList); } } campaign.Lists = lists; #endregion Contact lists settings #region Schedule campaign settings if (rbnDraft.Checked) { campaign.Status = CampaignStatus.DRAFT; } else if (rbnSendNow.Checked) { campaign.Status = CampaignStatus.SENT; } else if (rbnScheduled.Checked) { campaign.Status = CampaignStatus.SCHEDULED; } #endregion Schedule campaign settings return(campaign); }
public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId]; //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance. var correlationId = _operationalUnit.InstanceId; if (!string.IsNullOrEmpty(correlationHeader) && Guid.TryParse(correlationHeader, out Guid paresedCorId)) { correlationId = paresedCorId; } var messageHeader = new MessageHeader(isSucceed: context.HttpContext.Response.StatusCode == 200) { CorrelationId = correlationId }; var messageFooter = new MessageFooter { Sender = context.ActionDescriptor.DisplayName, Environment = _operationalUnit.Environment, Assembly = _operationalUnit.Assembly, FingerPrint = context.ActionDescriptor.Id, Route = JsonConvert.SerializeObject(context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Defaults.JsonSerializerSettings), Hint = Enum.GetName(typeof(ResponseHint), ResponseHint.OK) //TODO: infer the hint from HTTP status code }; messageHeader.GetType().GetProperties() .ToList().ForEach(prop => { context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageHeader)?.ToString())); }); messageFooter.GetType().GetProperties() .ToList().ForEach(prop => { context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageFooter)?.ToString())); }); context.Result = new ContentResult() { StatusCode = context.HttpContext.Response.StatusCode, ContentType = Identifiers.ApplicationJson, Content = JsonConvert.SerializeObject(getContentResult(context.Result), Defaults.JsonSerializerSettings) }; _logger.LogInformation("Override response!"); return(next.Invoke()); object getContentResult(IActionResult result) { switch (result) { case ContentResult CntR: { return(CntR?.Content); } case ObjectResult ObjR: { return(ObjR?.Value); } case JsonResult JsonR: { return(JsonR?.Value); } default: { return(result); } } }; }
private EmailCampaign CreateCampaignFromInputs() { EmailCampaign campaign = new EmailCampaign(); #region General settings // if (!string.IsNullOrWhiteSpace(txtCampaignName.Text)) { campaign.Name = "Campaign Name here"; } // if (cbCampaignType.SelectedItem != null) { //campaign.TemplateType = GetCampaignType(cbCampaignType.SelectedItem as ItemInfo); } // if (!string.IsNullOrWhiteSpace(txtSubject.Text)) { campaign.Subject = "subject here"; } // if (!string.IsNullOrWhiteSpace(txtFromName.Text)) { campaign.FromName = "FromName"; } // if (cbFromEmail.SelectedIndex != null) { campaign.FromEmail = "FromEmail"; } // if (cbReplyToEmail.SelectedIndex != null) { campaign.ReplyToEmail = "ReplyEmail"; } // if (rbnHtml.Checked) { campaign.EmailContentFormat = CampaignEmailFormat.HTML; } // else // { // campaign.EmailContentFormat = CampaignEmailFormat.XHTML; // } // if (!string.IsNullOrWhiteSpace(txtGreetingString.Text)) { campaign.GreetingString = "greetings"; } // if (!string.IsNullOrWhiteSpace(txtEmailContent.Text)) { campaign.EmailContent = "email body"; } // if (!string.IsNullOrWhiteSpace(txtContent.Text)) { campaign.TextContent = ".text content"; } #endregion General settings //#region Message footer settings ////organization name is required for message footer section //if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text)) //{ MessageFooter msgFooter = new MessageFooter(); // if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text)) // { msgFooter.OrganizationName = "Org name"; // } // if (!string.IsNullOrWhiteSpace(txtAddressLine1.Text)) // { msgFooter.AddressLine1 = "address"; // } // if (!string.IsNullOrWhiteSpace(txtAddressLine2.Text)) // { // msgFooter.AddressLine2 = txtAddressLine2.Text.Trim(); // } // if (!string.IsNullOrWhiteSpace(txtAddressLine3.Text)) // { // msgFooter.AddressLine3 = txtAddressLine3.Text.Trim(); // } // if (!string.IsNullOrWhiteSpace(txtCity.Text)) // { msgFooter.City = "NJ"; // } // if (!string.IsNullOrWhiteSpace(txtZip.Text)) // { msgFooter.PostalCode = "07095"; // } // if (cbCountry.SelectedItem != null) // { msgFooter.Country = "US";// GetSelectedValue(cbCountry.SelectedItem as ItemInfo); // } // if (cbState.SelectedItem != null) // { // var state = cbState.SelectedItem as ItemInfo; // if (state != null) // { msgFooter.State = "NJ"; // } // } // if (chkIncludeForwardEmail.Checked) // { // msgFooter.ForwardEmailLinkText = txtIncludeForwardEmail.Text.Trim(); // msgFooter.IncludeForwardEmail = true; // } // if (chkIncludeSubscribeLink.Checked) // { // msgFooter.SubscribeLinkText = txtIncludeSubscribeLink.Text.Trim(); // msgFooter.IncludeSubscribeLink = true; // } campaign.MessageFooter = msgFooter; //} //#endregion Message footer settings #region Contact lists settings List <SentContactList> lists = new List <SentContactList>(); if (contactListFromListService.Count > 0) { foreach (var item in contactListFromListService.ToList <ContactList>()) { SentContactList contactList = new SentContactList() { Id = item.Id }; lists.Add(contactList); } } campaign.Lists = lists; #endregion Contact lists settings #region Schedule campaign settings //if (rbnDraft.Checked) //{ // campaign.Status = CampaignStatus.DRAFT; //} //else if (rbnSendNow.Checked) //{ // campaign.Status = CampaignStatus.SENT; //} //else if (rbnScheduled.Checked) //{ campaign.Status = CampaignStatus.SCHEDULED; //} #endregion Schedule campaign settings return(campaign); }
private IEnumerable <EmailCampaign> CreateCampaigns(Dictionary <string, string> parameters) { XDocument emailTemplate = new XDocument(); using (System.Xml.XmlWriter writer = emailTemplate.CreateWriter()) { XslCompiledTransform emailTemplateTransform = new XslCompiledTransform(); XsltArgumentList emailTemplateArgList = new XsltArgumentList(); foreach (KeyValuePair <string, string> parameter in parameters) { emailTemplateArgList.AddParam(parameter.Key, "", parameter.Value); } emailTemplateTransform.Load(_emailTemplateTransformUrl); emailTemplateTransform.Transform(createEmailConnectionXML().CreateReader(), emailTemplateArgList, writer); } List <EmailCampaign> campaigns = new List <EmailCampaign>(); foreach (XElement campaignElement in emailTemplate.Descendants("campaign")) { EmailCampaign campaign = new EmailCampaign(); campaign.EmailContentFormat = CampaignEmailFormat.XHTML; campaign.Status = CampaignStatus.SENT; foreach (XElement property in campaignElement.Elements("property")) { string name = property.Attribute("name").Value; string value = String.Concat(property.Nodes()); try { campaign.GetType().GetProperty(name).SetValue(campaign, value, null); } catch (Exception) { throw new Exception($"Error with campaign property setting, {name}, with value, {value}"); } } MessageFooter msgFooter = new MessageFooter(); campaign.MessageFooter = msgFooter; foreach (XElement method in emailTemplate.Descendants("footer").Elements("property")) { string name = method.Attribute("name").Value; string value = method.Value; try { msgFooter.GetType().GetProperty(name).SetValue(msgFooter, value, null); } catch (Exception) { throw new Exception($"Error with footer property setting, {name}, with value, {value}"); } } List <SentContactList> lists = new List <SentContactList>(); foreach (XElement method in emailTemplate.Descendants("contacts").Elements("contact")) { string id = method.Attribute("id").Value; SentContactList contactList = new SentContactList() { Id = id }; lists.Add(contactList); } campaign.Lists = lists; campaigns.Add(campaign); } return(campaigns); }