com.infusers.core.email.RemoteEmailUtility Maven / Gradle / Ivy
package com.infusers.core.email;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.infusers.core.constants.Constants;
import com.infusers.core.email.eventdriven.EmailEvent;
//import com.infusers.core.email.kafka.KafkaProducer;
import com.infusers.core.user.dto.UserDetailsDto;
import com.infusers.core.util.InfusersUtility;
@Service
public class RemoteEmailUtility {
final Logger log = LogManager.getLogger(RemoteEmailUtility.class);
private static final String CLASS_NAME = "RemoteEmailUtility";
@Value("${infusers.auth.verify.url}")
private String authVerifyEndPoint;
@Value("${infusers.email.endpoint}")
private transient String emailEndPoint;
@Value("${infusers.email.url}")
private transient String host;
@Value("${myapp.rom.request.mapping}")
private transient String requestMapping;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired
private InfusersUtility infusersUtility;
// private final KafkaProducer kafkaProducer;
//
// @Autowired
// public RemoteEmailUtility(KafkaProducer kafkaProducer) {
// this.kafkaProducer = kafkaProducer;
// }
public boolean sendEmailNewAccountCreated(UserDetailsDto user) {
if(user==null) {
log.error(CLASS_NAME+".sendEmailNewAccountCreated()--> user is NULL." + user);
return false;
}
String subject = "[FYA] Warm welcome to Infusers!! Please verify your registration.";
String content =
"You have created a new Account!
"
+ "Please click the link below to verify your registration:
"
+ "VERIFY
";
String verifyURL = authVerifyEndPoint + "?code=" + user.getVerificationCode();
content = content.replace("[[URL]]", verifyURL);
return sendEmail(user.getUsername(), subject, content);
}
public boolean sendEmailUserLoggedIn(String userName) {
String subject = "[FYI] Dear Infuser, Welcome Back :)";
String message = "Thank you for your continued support in using Infuser's services, Have a great time ahead!!";
return sendEmail(userName, subject, message);
}
@Async
public boolean sendEmail(String userName, String subject, String message) {
String methodName = "sendEmail";
log.debug(CLASS_NAME+"."+methodName+" userName : " + userName+" :: subject = "+subject);
EmailMessage eMessage = new EmailMessage(userName, subject, message, host + requestMapping + emailEndPoint);
eventPublisher.publishEvent(new EmailEvent(this, eMessage));
// kafkaProducer.sendMessage(eMessage);
return true;
// return sendEmail(log, eMessage);
}
public static boolean sendEmailUtility(InfusersUtility infUtil, Logger logger, EmailMessage eMessage) {
String userName = eMessage.getUserName();
String subject = eMessage.getSubject();
String message = eMessage.getMessage();
String urlString = eMessage.getURLString();
String methodName = "sendEmailUtility";
logger.debug(CLASS_NAME+"."+methodName+" :: eMessage = "+eMessage+" :: urlString = "+urlString+" :: thread name = "+Thread.currentThread().getName());
String endpoint = "";
try {
String bearerToken = infUtil.getToken(userName);
URI uri = new URIBuilder(urlString)
.addParameter("subject", subject)
.addParameter("message", message)
.build();
endpoint = uri.toString();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(endpoint);
request.addHeader("content-type", "application/json");
request.setHeader(Constants.STATIC_TEXT_AUTHORIZATION, Constants.STATIC_TEXT_BEARER+" " + bearerToken);
HttpResponse response = httpClient.execute(request);
boolean returnVal = response.getStatusLine().getStatusCode() == 200;
if(returnVal) {
logger.debug(CLASS_NAME+"."+methodName+" eMessage : " + eMessage+" :: response code = "+response.getStatusLine().getStatusCode());
}
else {
logger.error(CLASS_NAME+"."+methodName+" eMessage : " + eMessage+" :: response code = "+response.getStatusLine().getStatusCode()+" :: endpoint = "+endpoint);
}
logger.debug(CLASS_NAME+"."+methodName+" Email successfully sent: eMessage : " + eMessage);
return returnVal;
}
catch (Exception e) {
logger.error(CLASS_NAME+"."+methodName+" :: " + e.getMessage()+" :: "+eMessage);
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy