Commit 08fedc38 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'mail_config_status' into 'main'

Added system status email service variable

See merge request grin-global/grin-global-server!375
parents 1c52b80c 54b76803
......@@ -15,6 +15,7 @@
*/
package org.gringlobal.api.v1.impl;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
import org.genesys.blocks.security.SecurityContextUtil;
import org.gringlobal.api.v1.ApiBaseController;
......@@ -44,6 +45,9 @@ public class SystemStatusController {
@Value("${captcha.enabled}")
private Boolean captchaEnabled;
@Value("${mail.host}")
private String mailHost;
@Autowired
private OAuthClientRepository oauthClientRepository;
......@@ -76,6 +80,7 @@ public class SystemStatusController {
status.captchaSiteKey = oAuthClient.getPublicRecaptchaKey();
}
}
status.smtpEnabled = StringUtils.isNotBlank(mailHost);
return status;
}
......@@ -86,6 +91,7 @@ public class SystemStatusController {
public long geographyCount;
public long inventoryCount;
public String captchaSiteKey;
public boolean smtpEnabled;
}
}
......@@ -17,6 +17,7 @@ package org.gringlobal.application.config;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -54,6 +55,10 @@ public class MailConfig {
@Bean
public JavaMailSenderImpl mailSender() {
if (StringUtils.isBlank(host)) {
System.err.println("SMTP is not configured");
return null;
}
final JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(host);
sender.setPort(port);
......
......@@ -15,9 +15,11 @@
*/
package org.gringlobal.service.impl;
import java.util.Arrays;
import java.io.ByteArrayOutputStream;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.service.EMailService;
import org.slf4j.Logger;
......@@ -36,7 +38,7 @@ public class EMailServiceImpl implements EMailService {
private static final Logger LOG = LoggerFactory.getLogger(EMailServiceImpl.class);
@Autowired
@Autowired(required = false)
private JavaMailSender mailSender;
@Autowired
......@@ -74,14 +76,12 @@ public class EMailServiceImpl implements EMailService {
}
public void sendSimpleEmail(final String subject, final String text, final String emailFrom, final String[] emailCc, final String... emailTo) {
printDebugInfo(subject, text, emailFrom, emailTo);
final MimeMessagePreparator preparator = mimeMessage -> {
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setFrom(emailFrom);
message.setTo(emailTo);
LOG.info("Email to: {}", ArrayUtils.toString(emailTo, "<NULL>"));
if (emailCc != null && emailCc.length > 0) {
message.setCc(emailCc);
}
......@@ -90,14 +90,31 @@ public class EMailServiceImpl implements EMailService {
message.setText(text, true);
};
if (LOG.isInfoEnabled()) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
var message = new MimeMessage((Session)null);
preparator.prepare(message);
message.writeTo(baos);
LOG.info("Outgoing message:\n{}\n<EOF>", baos);
} catch (Exception e) {
LOG.info(e.getMessage(), e);
}
}
doSend(preparator);
}
protected void doSend(final MimeMessagePreparator preparator) {
// execute sender in separate thread
if (mailSender == null) {
LOG.warn("SMTP not enabled, email was not sent.");
return;
}
JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;
if (async) {
// execute sender in separate thread
LOG.info("Sending email message asynchroniously");
executor.submit(() -> {
try {
......@@ -114,11 +131,4 @@ public class EMailServiceImpl implements EMailService {
}
}
protected void printDebugInfo(String subject, String text, String emailFrom, String[] emailTo) {
System.out.println(getDebugString(subject, text, emailFrom, emailTo));
}
protected String getDebugString(String subject, String text, String emailFrom, String[] emailTo) {
return String.format(debugMessage, emailFrom, Arrays.toString(emailTo), subject, text);
}
}
......@@ -116,8 +116,8 @@ repository.blacklist.filename=^(\\.|\\~)|^Thumbs.db$
repository.blacklist.foldername=(^\\.)|[~!\\/]
# Mail properties
mail.host=localhost
mail.port=25
mail.host=
mail.port=
mail.async=true
mail.debug=false
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment