diff --git a/pom.xml b/pom.xml index db2e440..3f105e1 100644 --- a/pom.xml +++ b/pom.xml @@ -150,7 +150,7 @@ 4.9.3 - + diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 28cbc8b..147d0bb 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -4,26 +4,28 @@ 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.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; -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.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; 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.HashMap; +import java.util.Map; import java.util.function.Consumer; @Component @@ -38,35 +40,45 @@ 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(); + 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(); @@ -74,6 +86,7 @@ public void accept(Throwable e) { /** * Send outbound message to user using the current xmsg + * * @param currentXmsg * @throws Exception */ @@ -85,13 +98,18 @@ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { .doOnError(new Consumer() { @Override public void accept(Throwable e) { - log.error("Exception in processOutBoundMessageF:"+e.getMessage()); + 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() { @Override public void accept(XMessage xMessage) { - if(xMessage.getApp() != null) { - try{ + if (xMessage.getApp() != null) { + try { + log.info("Outbound convertXMessageToDAO : " + xMessage.toString()); XMessageDAO dao = XMessageDAOUtils.convertXMessageToDAO(xMessage); redisCacheService.setXMessageDaoCache(xMessage.getTo().getUserID(), dao); xMessageRepo @@ -100,7 +118,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() { @@ -109,25 +127,43 @@ 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()); - 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"); } } }); } - + 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) { + log.info("Email Sending...."); + EmailDetails emailDetails = new EmailDetails().builder() + .subject(subject) + .msgBody(body) + .recipient(recipient) + .attachment(xMessage == null ? "" : xMessage.toString()) + .attachmentFileName(attachmentFileName) + .attachments(attachments) + .build(); +// log.info("EmailDetails :" + emailDetails); + emailService.sendMailWithAttachment(emailDetails); } } 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:#{""}} + 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