From 3a992a95c9665b4e87f29f1783cabf9761e83d8f Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Mon, 3 Apr 2023 19:40:09 +0530 Subject: [PATCH 01/10] Add email to be sent to devs when error arrise --- pom.xml | 6 +- .../consumers/OutboundKafkaController.java | 27 ++++-- .../com/uci/outbound/entity/EmailDetails.java | 18 ++++ .../uci/outbound/service/EmailService.java | 9 ++ .../outbound/service/EmailServiceImpl.java | 92 +++++++++++++++++++ src/main/resources/application.properties | 10 ++ 6 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/uci/outbound/entity/EmailDetails.java create mode 100644 src/main/java/com/uci/outbound/service/EmailService.java create mode 100644 src/main/java/com/uci/outbound/service/EmailServiceImpl.java diff --git a/pom.xml b/pom.xml index db2e440..b06cae4 100644 --- a/pom.xml +++ b/pom.xml @@ -150,7 +150,11 @@ 4.9.3 - + + org.springframework.boot + spring-boot-starter-mail + + diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 28cbc8b..aa412dc 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -4,6 +4,9 @@ import com.uci.adapter.provider.factory.ProviderFactory; import com.uci.dao.models.XMessageDAO; import com.uci.dao.repository.XMessageRepository; +import com.uci.outbound.entity.EmailDetails; +import com.uci.outbound.service.EmailService; +import com.uci.outbound.service.EmailServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import messagerosa.core.model.XMessage; @@ -14,6 +17,7 @@ import io.fusionauth.domain.api.LoginRequest; import messagerosa.xml.XMessageParser; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.event.EventListener; import org.springframework.data.redis.core.HashOperations; @@ -38,27 +42,33 @@ public class OutboundKafkaController { @Autowired private XMessageRepository xMessageRepo; - + @Autowired private RedisCacheService redisCacheService; - + private HashOperations hashOperations; //to access Redis cache + @Autowired + private EmailServiceImpl emailService; + + @Value("${spring.mail.recipient}") + private String recipient; + @EventListener(ApplicationStartedEvent.class) public void onMessage() { - + reactiveKafkaReceiver .doOnNext(new Consumer>() { @Override public void accept(ReceiverRecord msg) { - log.info("kafka message receieved!"); + log.info("kafka message receieved!"); XMessage currentXmsg = null; try { currentXmsg = XMessageParser.parse(new ByteArrayInputStream(msg.value().getBytes())); sendOutboundMessage(currentXmsg); } catch (Exception e) { e.printStackTrace(); - } + } } }) @@ -78,6 +88,7 @@ public void accept(Throwable e) { * @throws Exception */ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { + emailService.sendMailWithAttachment("Error in Outbound", "PFA", recipient, currentXmsg, "XMessage.txt"); String channel = currentXmsg.getChannelURI(); String provider = currentXmsg.getProviderURI(); IProvider iprovider = factoryProvider.getProvider(provider, channel); @@ -85,6 +96,7 @@ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { .doOnError(new Consumer() { @Override public void accept(Throwable e) { + emailService.sendMailWithAttachment("Error in Outbound", "PFA", recipient, currentXmsg, "XMessage.txt"); log.error("Exception in processOutBoundMessageF:"+e.getMessage()); } }).subscribe(new Consumer() { @@ -110,6 +122,7 @@ public void accept(XMessageDAO xMessageDAO) { } }); }catch(Exception e){ + emailService.sendMailWithAttachment("Error in Outbound", "PFA", recipient, xMessage, "XMessage.txt"); log.error("Exception in convertXMessageToDAO:" + e.getMessage()); try{ log.error("The current XMessage was " + xMessage.toXML()); @@ -126,8 +139,8 @@ public void accept(XMessageDAO xMessageDAO) { } }); } - + private String redisKeyWithPrefix(String key) { - return System.getenv("ENV")+"-"+key; + return System.getenv("ENV")+"-"+key; } } diff --git a/src/main/java/com/uci/outbound/entity/EmailDetails.java b/src/main/java/com/uci/outbound/entity/EmailDetails.java new file mode 100644 index 0000000..a00adab --- /dev/null +++ b/src/main/java/com/uci/outbound/entity/EmailDetails.java @@ -0,0 +1,18 @@ +package com.uci.outbound.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +public class EmailDetails { + private String recipient; + private String msgBody; + private String subject; + private String attachment; + private String attachmentFileName; +} \ No newline at end of file diff --git a/src/main/java/com/uci/outbound/service/EmailService.java b/src/main/java/com/uci/outbound/service/EmailService.java new file mode 100644 index 0000000..b228a19 --- /dev/null +++ b/src/main/java/com/uci/outbound/service/EmailService.java @@ -0,0 +1,9 @@ +package com.uci.outbound.service; + +import com.uci.outbound.entity.EmailDetails; + +public interface EmailService { + public void sendSimpleMail(EmailDetails details); + + public void sendMailWithAttachment(EmailDetails details); +} diff --git a/src/main/java/com/uci/outbound/service/EmailServiceImpl.java b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java new file mode 100644 index 0000000..941f287 --- /dev/null +++ b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java @@ -0,0 +1,92 @@ +package com.uci.outbound.service; + + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import javax.mail.internet.MimeMessage; + +import com.uci.outbound.entity.EmailDetails; +import lombok.extern.slf4j.Slf4j; +import messagerosa.core.model.XMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class EmailServiceImpl implements EmailService { + @Autowired + private JavaMailSender javaMailSender; + + @Value("${spring.mail.username}") + private String sender; + + public void sendSimpleMail(EmailDetails details) { + try { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setFrom(sender); + mailMessage.setTo(details.getRecipient()); + mailMessage.setText(details.getMsgBody()); + mailMessage.setSubject(details.getSubject()); + javaMailSender.send(mailMessage); + log.info("Mail Sent Successfully..."); + } catch (Exception e) { + log.error("Error while Sending Mail" + e.getMessage()); + } + } + + public void sendMailWithAttachment(EmailDetails details) { + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper mimeMessageHelper; + String tempPath = "/tmp/" + details.getAttachmentFileName(); + File file = new File(tempPath); + try { + mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); + mimeMessageHelper.setFrom(sender); + if (details.getRecipient().contains(",")) { + String recipients[] = details.getRecipient().split(","); + mimeMessageHelper.setTo(recipients); + } else { + mimeMessageHelper.setTo(details.getRecipient()); + } + mimeMessageHelper.setText(details.getMsgBody()); + mimeMessageHelper.setSubject(details.getSubject()); + createTempFile(details.getAttachment(), tempPath); + FileSystemResource fileSystemResource = new FileSystemResource(file); + mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file); + javaMailSender.send(mimeMessage); + log.info("Mail sent Successfully..."); + } catch (Exception e) { + log.error("Error while sending mail!!! " + e.getMessage()); + } finally { + if (file.exists() && file.delete()) { + log.info("file deleted : " + file.getPath()); + } + } + } + + public void sendMailWithAttachment(String subject, String body, String recipient, XMessage xMessage, String attachmentFileName) { + EmailDetails emailDetails = new EmailDetails(); + emailDetails.setSubject(subject); + emailDetails.setMsgBody(body); + emailDetails.setRecipient(recipient); + emailDetails.setAttachment(xMessage.toString()); + emailDetails.setAttachmentFileName(attachmentFileName); + log.info("EmailDetails :" + emailDetails); + sendMailWithAttachment(emailDetails); + } + + private void createTempFile(String fileData, String filePath) throws IOException { + Path path = Paths.get(filePath); + Files.writeString(path, fileData, StandardCharsets.UTF_8); + log.info("Email attachment temp file is created : " + filePath); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0e79a95..22990da 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -118,3 +118,13 @@ file.cdn.selected=${SELECTED_FILE_CDN:#{"azure"}} #for notification key enable/disable when notification triggered fcm.notificationKeyEnable=${NOTIFICATION_KEY_ENABLE:#{"true"}} + +#Email Config SMTP +spring.mail.host=${EMAIL_HOST:#{""}} +spring.mail.port=${EMAIL_PORT:#{""}} +spring.mail.username=${EMAIL_USERNAME:#{""}} +spring.mail.password=${EMAIL_PASSWORD:#{""}} +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.recipient=${RECIPIENT_EMAILS:#{""}} + From 0832454764383a95e43c40a4603ed4a6761340cf Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Tue, 4 Apr 2023 12:02:19 +0530 Subject: [PATCH 02/10] Add email to be sent to devs when error arrise --- pom.xml | 6 +- .../consumers/OutboundKafkaController.java | 49 +++++++--- .../com/uci/outbound/entity/EmailDetails.java | 16 ++++ .../uci/outbound/service/EmailService.java | 9 ++ .../outbound/service/EmailServiceImpl.java | 92 +++++++++++++++++++ src/main/resources/application.properties | 10 ++ 6 files changed, 167 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/uci/outbound/entity/EmailDetails.java create mode 100644 src/main/java/com/uci/outbound/service/EmailService.java create mode 100644 src/main/java/com/uci/outbound/service/EmailServiceImpl.java diff --git a/pom.xml b/pom.xml index db2e440..b06cae4 100644 --- a/pom.xml +++ b/pom.xml @@ -150,7 +150,11 @@ 4.9.3 - + + org.springframework.boot + spring-boot-starter-mail + + diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 28cbc8b..143718d 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -4,25 +4,24 @@ import com.uci.adapter.provider.factory.ProviderFactory; import com.uci.dao.models.XMessageDAO; import com.uci.dao.repository.XMessageRepository; +import com.uci.dao.utils.XMessageDAOUtils; +import com.uci.outbound.entity.EmailDetails; +import com.uci.outbound.service.EmailServiceImpl; +import com.uci.utils.cache.service.RedisCacheService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import messagerosa.core.model.XMessage; -import com.uci.dao.utils.XMessageDAOUtils; -import com.uci.utils.cache.service.RedisCacheService; - -import io.fusionauth.client.FusionAuthClient; -import io.fusionauth.domain.api.LoginRequest; import messagerosa.xml.XMessageParser; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.event.EventListener; import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.kafka.receiver.ReceiverRecord; -import javax.xml.bind.JAXBException; +import javax.xml.bind.JAXBException; import java.io.ByteArrayInputStream; import java.util.function.Consumer; @@ -38,27 +37,33 @@ public class OutboundKafkaController { @Autowired private XMessageRepository xMessageRepo; - + @Autowired private RedisCacheService redisCacheService; - + private HashOperations hashOperations; //to access Redis cache + @Autowired + private EmailServiceImpl emailService; + + @Value("${spring.mail.recipient}") + private String recipient; + @EventListener(ApplicationStartedEvent.class) public void onMessage() { - + reactiveKafkaReceiver .doOnNext(new Consumer>() { @Override public void accept(ReceiverRecord msg) { - log.info("kafka message receieved!"); + log.info("kafka message receieved!"); XMessage currentXmsg = null; try { currentXmsg = XMessageParser.parse(new ByteArrayInputStream(msg.value().getBytes())); sendOutboundMessage(currentXmsg); } catch (Exception e) { e.printStackTrace(); - } + } } }) @@ -85,6 +90,7 @@ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { .doOnError(new Consumer() { @Override public void accept(Throwable e) { + sentEmail(currentXmsg, "Error in Outbound", "PFA", recipient, "XMessage.txt"); log.error("Exception in processOutBoundMessageF:"+e.getMessage()); } }).subscribe(new Consumer() { @@ -110,7 +116,9 @@ public void accept(XMessageDAO xMessageDAO) { } }); }catch(Exception e){ + sentEmail(xMessage, "Error in Outbound", "PFA", recipient, "XMessage.txt"); log.error("Exception in convertXMessageToDAO:" + e.getMessage()); + e.printStackTrace(); try{ log.error("The current XMessage was " + xMessage.toXML()); }catch(JAXBException j) { @@ -126,8 +134,21 @@ public void accept(XMessageDAO xMessageDAO) { } }); } - + private String redisKeyWithPrefix(String key) { - return System.getenv("ENV")+"-"+key; + return System.getenv("ENV")+"-"+key; + } + + private void sentEmail(XMessage xMessage, String subject, String body, String recipient, String attachmentFileName){ + log.info("Email Sending...."); + EmailDetails emailDetails = new EmailDetails().builder() + .subject(subject) + .msgBody(body) + .recipient(recipient) + .attachment(xMessage.toString()) + .attachmentFileName(attachmentFileName) + .build(); + log.info("EmailDetails :" + emailDetails); + emailService.sendMailWithAttachment(emailDetails); } } diff --git a/src/main/java/com/uci/outbound/entity/EmailDetails.java b/src/main/java/com/uci/outbound/entity/EmailDetails.java new file mode 100644 index 0000000..df3e401 --- /dev/null +++ b/src/main/java/com/uci/outbound/entity/EmailDetails.java @@ -0,0 +1,16 @@ +package com.uci.outbound.entity; + +import lombok.*; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +@Builder +public class EmailDetails { + private String recipient; + private String msgBody; + private String subject; + private String attachment; + private String attachmentFileName; +} \ No newline at end of file diff --git a/src/main/java/com/uci/outbound/service/EmailService.java b/src/main/java/com/uci/outbound/service/EmailService.java new file mode 100644 index 0000000..b228a19 --- /dev/null +++ b/src/main/java/com/uci/outbound/service/EmailService.java @@ -0,0 +1,9 @@ +package com.uci.outbound.service; + +import com.uci.outbound.entity.EmailDetails; + +public interface EmailService { + public void sendSimpleMail(EmailDetails details); + + public void sendMailWithAttachment(EmailDetails details); +} diff --git a/src/main/java/com/uci/outbound/service/EmailServiceImpl.java b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java new file mode 100644 index 0000000..4a8e566 --- /dev/null +++ b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java @@ -0,0 +1,92 @@ +package com.uci.outbound.service; + + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import javax.mail.internet.MimeMessage; + +import com.uci.outbound.entity.EmailDetails; +import lombok.extern.slf4j.Slf4j; +import messagerosa.core.model.XMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class EmailServiceImpl implements EmailService { + @Autowired + private JavaMailSender javaMailSender; + + @Value("${spring.mail.username}") + private String sender; + + public void sendSimpleMail(EmailDetails details) { + try { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setFrom(sender); + mailMessage.setTo(details.getRecipient()); + mailMessage.setText(details.getMsgBody()); + mailMessage.setSubject(details.getSubject()); + javaMailSender.send(mailMessage); + log.info("Mail Sent Successfully..."); + } catch (Exception e) { + log.error("Error while Sending Mail" + e.getMessage()); + } + } + + public void sendMailWithAttachment(EmailDetails details) { + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper mimeMessageHelper; + String tempPath = "/tmp/" + details.getAttachmentFileName(); + File file = new File(tempPath); + try { + mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); + mimeMessageHelper.setFrom(sender); + if (details.getRecipient().contains(",")) { + String recipients[] = details.getRecipient().split(","); + mimeMessageHelper.setTo(recipients); + } else { + mimeMessageHelper.setTo(details.getRecipient()); + } + mimeMessageHelper.setText(details.getMsgBody()); + mimeMessageHelper.setSubject(details.getSubject()); + createTempFile(details.getAttachment(), tempPath); + FileSystemResource fileSystemResource = new FileSystemResource(file); + mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file); + javaMailSender.send(mimeMessage); + log.info("Mail sent Successfully..."); + } catch (Exception e) { + log.error("Error while sending mail!!! " + e.getMessage()); + } finally { + if (file.exists() && file.delete()) { + log.info("file deleted : " + file.getPath()); + } + } + } + +// public void sendMailWithAttachment(String subject, String body, String recipient, XMessage xMessage, String attachmentFileName) { +// EmailDetails emailDetails = new EmailDetails(); +// emailDetails.setSubject(subject); +// emailDetails.setMsgBody(body); +// emailDetails.setRecipient(recipient); +// emailDetails.setAttachment(xMessage.toString()); +// emailDetails.setAttachmentFileName(attachmentFileName); +// log.info("EmailDetails :" + emailDetails); +// sendMailWithAttachment(emailDetails); +// } + + private void createTempFile(String fileData, String filePath) throws IOException { + Path path = Paths.get(filePath); + Files.writeString(path, fileData, StandardCharsets.UTF_8); + log.info("Email attachment temp file is created : " + filePath); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0e79a95..22990da 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -118,3 +118,13 @@ file.cdn.selected=${SELECTED_FILE_CDN:#{"azure"}} #for notification key enable/disable when notification triggered fcm.notificationKeyEnable=${NOTIFICATION_KEY_ENABLE:#{"true"}} + +#Email Config SMTP +spring.mail.host=${EMAIL_HOST:#{""}} +spring.mail.port=${EMAIL_PORT:#{""}} +spring.mail.username=${EMAIL_USERNAME:#{""}} +spring.mail.password=${EMAIL_PASSWORD:#{""}} +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.recipient=${RECIPIENT_EMAILS:#{""}} + From 8f06651343d032437d681cc533b9820583304e47 Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Tue, 4 Apr 2023 14:49:51 +0530 Subject: [PATCH 03/10] Email config added in properties file for utils --- .../consumers/OutboundKafkaController.java | 4 +- .../com/uci/outbound/entity/EmailDetails.java | 16 ---- .../uci/outbound/service/EmailService.java | 9 -- .../outbound/service/EmailServiceImpl.java | 92 ------------------- 4 files changed, 2 insertions(+), 119 deletions(-) delete mode 100644 src/main/java/com/uci/outbound/entity/EmailDetails.java delete mode 100644 src/main/java/com/uci/outbound/service/EmailService.java delete mode 100644 src/main/java/com/uci/outbound/service/EmailServiceImpl.java diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 143718d..9f024fc 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -5,9 +5,9 @@ import com.uci.dao.models.XMessageDAO; import com.uci.dao.repository.XMessageRepository; import com.uci.dao.utils.XMessageDAOUtils; -import com.uci.outbound.entity.EmailDetails; -import com.uci.outbound.service.EmailServiceImpl; import com.uci.utils.cache.service.RedisCacheService; +import com.uci.utils.model.EmailDetails; +import com.uci.utils.service.EmailServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import messagerosa.core.model.XMessage; diff --git a/src/main/java/com/uci/outbound/entity/EmailDetails.java b/src/main/java/com/uci/outbound/entity/EmailDetails.java deleted file mode 100644 index df3e401..0000000 --- a/src/main/java/com/uci/outbound/entity/EmailDetails.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.uci.outbound.entity; - -import lombok.*; - -@Data -@AllArgsConstructor -@NoArgsConstructor -@ToString -@Builder -public class EmailDetails { - private String recipient; - private String msgBody; - private String subject; - private String attachment; - private String attachmentFileName; -} \ No newline at end of file diff --git a/src/main/java/com/uci/outbound/service/EmailService.java b/src/main/java/com/uci/outbound/service/EmailService.java deleted file mode 100644 index b228a19..0000000 --- a/src/main/java/com/uci/outbound/service/EmailService.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.uci.outbound.service; - -import com.uci.outbound.entity.EmailDetails; - -public interface EmailService { - public void sendSimpleMail(EmailDetails details); - - public void sendMailWithAttachment(EmailDetails details); -} diff --git a/src/main/java/com/uci/outbound/service/EmailServiceImpl.java b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java deleted file mode 100644 index 4a8e566..0000000 --- a/src/main/java/com/uci/outbound/service/EmailServiceImpl.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.uci.outbound.service; - - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import javax.mail.internet.MimeMessage; - -import com.uci.outbound.entity.EmailDetails; -import lombok.extern.slf4j.Slf4j; -import messagerosa.core.model.XMessage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.FileSystemResource; -import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSender; -import org.springframework.mail.javamail.MimeMessageHelper; -import org.springframework.stereotype.Service; - -@Service -@Slf4j -public class EmailServiceImpl implements EmailService { - @Autowired - private JavaMailSender javaMailSender; - - @Value("${spring.mail.username}") - private String sender; - - public void sendSimpleMail(EmailDetails details) { - try { - SimpleMailMessage mailMessage = new SimpleMailMessage(); - mailMessage.setFrom(sender); - mailMessage.setTo(details.getRecipient()); - mailMessage.setText(details.getMsgBody()); - mailMessage.setSubject(details.getSubject()); - javaMailSender.send(mailMessage); - log.info("Mail Sent Successfully..."); - } catch (Exception e) { - log.error("Error while Sending Mail" + e.getMessage()); - } - } - - public void sendMailWithAttachment(EmailDetails details) { - MimeMessage mimeMessage = javaMailSender.createMimeMessage(); - MimeMessageHelper mimeMessageHelper; - String tempPath = "/tmp/" + details.getAttachmentFileName(); - File file = new File(tempPath); - try { - mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); - mimeMessageHelper.setFrom(sender); - if (details.getRecipient().contains(",")) { - String recipients[] = details.getRecipient().split(","); - mimeMessageHelper.setTo(recipients); - } else { - mimeMessageHelper.setTo(details.getRecipient()); - } - mimeMessageHelper.setText(details.getMsgBody()); - mimeMessageHelper.setSubject(details.getSubject()); - createTempFile(details.getAttachment(), tempPath); - FileSystemResource fileSystemResource = new FileSystemResource(file); - mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file); - javaMailSender.send(mimeMessage); - log.info("Mail sent Successfully..."); - } catch (Exception e) { - log.error("Error while sending mail!!! " + e.getMessage()); - } finally { - if (file.exists() && file.delete()) { - log.info("file deleted : " + file.getPath()); - } - } - } - -// public void sendMailWithAttachment(String subject, String body, String recipient, XMessage xMessage, String attachmentFileName) { -// EmailDetails emailDetails = new EmailDetails(); -// emailDetails.setSubject(subject); -// emailDetails.setMsgBody(body); -// emailDetails.setRecipient(recipient); -// emailDetails.setAttachment(xMessage.toString()); -// emailDetails.setAttachmentFileName(attachmentFileName); -// log.info("EmailDetails :" + emailDetails); -// sendMailWithAttachment(emailDetails); -// } - - private void createTempFile(String fileData, String filePath) throws IOException { - Path path = Paths.get(filePath); - Files.writeString(path, fileData, StandardCharsets.UTF_8); - log.info("Email attachment temp file is created : " + filePath); - } -} From 6d30e224085fb4b576e0e8c3c03329adad384928 Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Thu, 6 Apr 2023 12:01:41 +0530 Subject: [PATCH 04/10] Sending exception in Email --- pom.xml | 4 -- .../consumers/OutboundKafkaController.java | 38 +++++++++++++------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index b06cae4..3f105e1 100644 --- a/pom.xml +++ b/pom.xml @@ -150,10 +150,6 @@ 4.9.3 - - org.springframework.boot - spring-boot-starter-mail - diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 9f024fc..91e92ba 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import messagerosa.core.model.XMessage; import messagerosa.xml.XMessageParser; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationStartedEvent; @@ -23,6 +24,8 @@ import javax.xml.bind.JAXBException; import java.io.ByteArrayInputStream; +import java.util.HashMap; +import java.util.Map; import java.util.function.Consumer; @Component @@ -51,7 +54,6 @@ public class OutboundKafkaController { @EventListener(ApplicationStartedEvent.class) public void onMessage() { - reactiveKafkaReceiver .doOnNext(new Consumer>() { @Override @@ -62,16 +64,21 @@ public void accept(ReceiverRecord msg) { currentXmsg = XMessageParser.parse(new ByteArrayInputStream(msg.value().getBytes())); sendOutboundMessage(currentXmsg); } catch (Exception e) { - e.printStackTrace(); - } - + HashMap attachments = new HashMap<>(); + attachments.put("Exception", ExceptionUtils.getStackTrace(e)); + attachments.put("XMessage", currentXmsg.toString()); + sentEmail(currentXmsg, "Error in Outbound", "PFA", recipient, null, attachments); + log.error("An Error Occored : "+e.getMessage()); + } } }) .doOnError(new Consumer() { @Override public void accept(Throwable e) { - System.out.println(e.getMessage()); - log.error("KafkaFlux exception", e); + HashMap attachments = new HashMap<>(); + attachments.put("Exception", ExceptionUtils.getStackTrace(e)); + sentEmail(null, "Error in Outbound", "PFA", recipient, null, attachments); + log.error("KafkaFlux exception", e.getMessage()); } }) .subscribe(); @@ -90,7 +97,10 @@ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { .doOnError(new Consumer() { @Override public void accept(Throwable e) { - sentEmail(currentXmsg, "Error in Outbound", "PFA", recipient, "XMessage.txt"); + HashMap attachments = new HashMap<>(); + attachments.put("Exception", ExceptionUtils.getStackTrace(e)); + attachments.put("XMessage", currentXmsg.toString()); + sentEmail(currentXmsg, "Error in Outbound", "PFA", recipient, null, attachments); log.error("Exception in processOutBoundMessageF:"+e.getMessage()); } }).subscribe(new Consumer() { @@ -116,7 +126,10 @@ public void accept(XMessageDAO xMessageDAO) { } }); }catch(Exception e){ - sentEmail(xMessage, "Error in Outbound", "PFA", recipient, "XMessage.txt"); + HashMap attachments = new HashMap<>(); + attachments.put("Exception", ExceptionUtils.getStackTrace(e)); + attachments.put("XMessage", currentXmsg.toString()); + sentEmail(xMessage, "Error in Outbound", "PFA", recipient, null, attachments); log.error("Exception in convertXMessageToDAO:" + e.getMessage()); e.printStackTrace(); try{ @@ -139,16 +152,17 @@ private String redisKeyWithPrefix(String key) { return System.getenv("ENV")+"-"+key; } - private void sentEmail(XMessage xMessage, String subject, String body, String recipient, String attachmentFileName){ + private void sentEmail(XMessage xMessage, String subject, String body, String recipient, String attachmentFileName, HashMap attachments) { log.info("Email Sending...."); - EmailDetails emailDetails = new EmailDetails().builder() + EmailDetails emailDetails = new EmailDetails().builder() .subject(subject) .msgBody(body) .recipient(recipient) - .attachment(xMessage.toString()) + .attachment(xMessage == null ? "" : xMessage.toString()) .attachmentFileName(attachmentFileName) + .attachments(attachments) .build(); - log.info("EmailDetails :" + emailDetails); +// log.info("EmailDetails :" + emailDetails); emailService.sendMailWithAttachment(emailDetails); } } From 2ac68a92e6d81fc04db57038cb51a1adbcc77fd0 Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Thu, 6 Apr 2023 14:34:54 +0530 Subject: [PATCH 05/10] Bug fix commit --- .../consumers/OutboundKafkaController.java | 30 +++++++++---------- src/main/resources/logback-spring.xml | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 91e92ba..0ea653f 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -68,7 +68,7 @@ public void accept(ReceiverRecord msg) { attachments.put("Exception", ExceptionUtils.getStackTrace(e)); attachments.put("XMessage", currentXmsg.toString()); sentEmail(currentXmsg, "Error in Outbound", "PFA", recipient, null, attachments); - log.error("An Error Occored : "+e.getMessage()); + log.error("An Error Occored : " + e.getMessage()); } } }) @@ -86,6 +86,7 @@ public void accept(Throwable e) { /** * Send outbound message to user using the current xmsg + * * @param currentXmsg * @throws Exception */ @@ -101,13 +102,13 @@ public void accept(Throwable e) { attachments.put("Exception", ExceptionUtils.getStackTrace(e)); attachments.put("XMessage", currentXmsg.toString()); sentEmail(currentXmsg, "Error in Outbound", "PFA", recipient, null, attachments); - log.error("Exception in processOutBoundMessageF:"+e.getMessage()); + log.error("Exception in processOutBoundMessageF:" + e.getMessage()); } }).subscribe(new Consumer() { @Override public void accept(XMessage xMessage) { - if(xMessage.getApp() != null) { - try{ + if (xMessage.getApp() != null) { + try { XMessageDAO dao = XMessageDAOUtils.convertXMessageToDAO(xMessage); redisCacheService.setXMessageDaoCache(xMessage.getTo().getUserID(), dao); xMessageRepo @@ -116,7 +117,7 @@ public void accept(XMessage xMessage) { @Override public void accept(Throwable e) { redisCacheService.deleteXMessageDaoCache(xMessage.getTo().getUserID()); - log.error("Exception in xMsg Dao Save:"+e.getMessage()); + log.error("Exception in xMsg Dao Save:" + e.getMessage()); } }) .subscribe(new Consumer() { @@ -125,23 +126,22 @@ public void accept(XMessageDAO xMessageDAO) { log.info("XMessage Object saved is with sent user ID >> " + xMessageDAO.getUserId()); } }); - }catch(Exception e){ + } catch (Exception e) { HashMap attachments = new HashMap<>(); attachments.put("Exception", ExceptionUtils.getStackTrace(e)); attachments.put("XMessage", currentXmsg.toString()); sentEmail(xMessage, "Error in Outbound", "PFA", recipient, null, attachments); log.error("Exception in convertXMessageToDAO:" + e.getMessage()); - e.printStackTrace(); - try{ - log.error("The current XMessage was " + xMessage.toXML()); - }catch(JAXBException j) { - log.error("Unable to parse the current XMessage " + xMessage.toString()); - }catch(Exception ge) { - log.error("Unable to parse the current XMessage ge " + xMessage.toString()); + try { + log.error("The current XMessage was : " + xMessage.toString()); + } catch (Exception ge) { + ge.printStackTrace(); + log.error("Unable to parse the current XMessage : " + ge.getMessage() + " Xmessage : " + ge.getMessage()); } + e.printStackTrace(); } } else { - log.info("XMessage -> app is empty"); + log.info("XMessage -> app is empty"); } } @@ -149,7 +149,7 @@ public void accept(XMessageDAO xMessageDAO) { } private String redisKeyWithPrefix(String key) { - return System.getenv("ENV")+"-"+key; + return System.getenv("ENV") + "-" + key; } private void sentEmail(XMessage xMessage, String subject, String body, String recipient, String attachmentFileName, HashMap attachments) { diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index bc6a307..dc1c73c 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -7,7 +7,7 @@ class="ch.qos.logback.core.ConsoleAppender"> - %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable + %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}:%line): %msg%n%throwable From fc5b1d746f00dc9ac31d7ad21397a56c91f0791a Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Thu, 6 Apr 2023 14:56:47 +0530 Subject: [PATCH 06/10] Bug fix commit --- .../java/com/uci/outbound/consumers/OutboundKafkaController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 0ea653f..147d0bb 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -109,6 +109,7 @@ public void accept(Throwable e) { public void accept(XMessage xMessage) { if (xMessage.getApp() != null) { try { + log.info("Outbound convertXMessageToDAO : " + xMessage.toString()); XMessageDAO dao = XMessageDAOUtils.convertXMessageToDAO(xMessage); redisCacheService.setXMessageDaoCache(xMessage.getTo().getUserID(), dao); xMessageRepo From 2cc9bf817ba0011a8d3603c91eac9163b7d7790e Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Thu, 27 Apr 2023 21:57:09 +0530 Subject: [PATCH 07/10] Deleted empty test files. --- .../SamagraGupshupIncomingApplicationTests.java | 13 ------------- src/test/test.iml | 11 ----------- 2 files changed, 24 deletions(-) delete mode 100644 src/test/java/com/uci/inbound/incoming/SamagraGupshupIncomingApplicationTests.java delete mode 100644 src/test/test.iml diff --git a/src/test/java/com/uci/inbound/incoming/SamagraGupshupIncomingApplicationTests.java b/src/test/java/com/uci/inbound/incoming/SamagraGupshupIncomingApplicationTests.java deleted file mode 100644 index e6dd6f2..0000000 --- a/src/test/java/com/uci/inbound/incoming/SamagraGupshupIncomingApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.uci.inbound.incoming; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SamagraGupshupIncomingApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/src/test/test.iml b/src/test/test.iml deleted file mode 100644 index a0e49a3..0000000 --- a/src/test/test.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From b9ad68e3e1c527ac3425f0cd344df4cdc03f961a Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Wed, 3 May 2023 17:13:20 +0530 Subject: [PATCH 08/10] Creating new package naming for stagging and prod --- .github/workflows/build.yml | 4 ++-- .github/workflows/docker-build-push.yml | 2 +- pom.xml | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9b95847..34032cc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,10 +3,10 @@ name: Maven Build on: push: branches: - ["release-4.*.*", "release-5.*.*", "release-v2.*.*"] + ["release-4.*.*", "release-5.*.*", "release-v2.*.*", "master", "development"] pull_request: branches: - ["release-4.*.*", "release-5.*.*", "release-v2.*.*"] + ["release-4.*.*", "release-5.*.*", "release-v2.*.*", "master", "development"] jobs: build: diff --git a/.github/workflows/docker-build-push.yml b/.github/workflows/docker-build-push.yml index f3aedb8..4a8be8b 100644 --- a/.github/workflows/docker-build-push.yml +++ b/.github/workflows/docker-build-push.yml @@ -3,7 +3,7 @@ name: Docker Build on: push: tags: - - 'v*.*.*' + ["v*.*.*", "v*.*.*-*"] jobs: docker-build-push: diff --git a/pom.xml b/pom.xml index 3f105e1..07a19e8 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.uci outbound - 2.1.0 + 2.2.0-SNAPSHOT outbound outbound @@ -85,7 +85,7 @@ com.uci utils - 2.1.0 + 2.2.0-SNAPSHOT org.springframework.boot @@ -101,7 +101,7 @@ com.uci adapter - 2.1.0 + 2.2.0-SNAPSHOT @@ -122,7 +122,7 @@ com.uci dao - 2.1.0 + 2.2.0-SNAPSHOT compile From b22fea2b07be7a61b633345c430a20bcb7ec8f4d Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Mon, 8 May 2023 16:05:01 +0530 Subject: [PATCH 09/10] [Prod Deployment] - Change Package version and merge development branch in master --- pom.xml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 07a19e8..a10c2e1 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,9 @@ 11 + 2.2.1 + 2.2.1 + 2.2.1 @@ -85,7 +88,7 @@ com.uci utils - 2.2.0-SNAPSHOT + ${utils.version} org.springframework.boot @@ -101,7 +104,7 @@ com.uci adapter - 2.2.0-SNAPSHOT + ${adapter.version} @@ -122,7 +125,7 @@ com.uci dao - 2.2.0-SNAPSHOT + ${dao.version} compile From a05bcba41d4dc59a98cfc368b5be4e792288a407 Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Mon, 8 May 2023 16:20:31 +0530 Subject: [PATCH 10/10] [Hot Fix] - Change pom version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a10c2e1..ec9dc39 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.uci outbound - 2.2.0-SNAPSHOT + 2.2.1 outbound outbound