Fix conflicts in rulenode-core-config.js
This commit is contained in:
commit
973fb8ec54
@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
import org.springframework.core.io.InputStreamSource;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -45,8 +47,8 @@ import org.thingsboard.server.queue.usagestats.TbApiUsageClient;
|
||||
import org.thingsboard.server.service.apiusage.TbApiUsageStateService;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -234,22 +236,47 @@ public class DefaultMailService implements MailService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body) throws MessagingException {
|
||||
public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images) throws ThingsboardException {
|
||||
sendMail(tenantId, customerId, from, to, cc, bcc, subject, body, isHtml, images, this.mailSender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException {
|
||||
sendMail(tenantId, customerId, from, to, cc, bcc, subject, body, isHtml, images, javaMailSender);
|
||||
}
|
||||
|
||||
private void sendMail(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException {
|
||||
if (apiUsageStateService.getApiUsageState(tenantId).isEmailSendEnabled()) {
|
||||
MimeMessage mailMsg = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8");
|
||||
helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from);
|
||||
helper.setTo(to.split("\\s*,\\s*"));
|
||||
if (!StringUtils.isBlank(cc)) {
|
||||
helper.setCc(cc.split("\\s*,\\s*"));
|
||||
try {
|
||||
MimeMessage mailMsg = javaMailSender.createMimeMessage();
|
||||
boolean multipart = (images != null && !images.isEmpty());
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mailMsg, multipart, "UTF-8");
|
||||
helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from);
|
||||
helper.setTo(to.split("\\s*,\\s*"));
|
||||
if (!StringUtils.isBlank(cc)) {
|
||||
helper.setCc(cc.split("\\s*,\\s*"));
|
||||
}
|
||||
if (!StringUtils.isBlank(bcc)) {
|
||||
helper.setBcc(bcc.split("\\s*,\\s*"));
|
||||
}
|
||||
helper.setSubject(subject);
|
||||
helper.setText(body, isHtml);
|
||||
|
||||
if (multipart) {
|
||||
for (String imgId : images.keySet()) {
|
||||
String imgValue = images.get(imgId);
|
||||
String value = imgValue.replaceFirst("^data:image/[^;]*;base64,?", "");
|
||||
byte[] bytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(value);
|
||||
String contentType = helper.getFileTypeMap().getContentType(imgId);
|
||||
InputStreamSource iss = () -> new ByteArrayInputStream(bytes);
|
||||
helper.addInline(imgId, iss, contentType);
|
||||
}
|
||||
}
|
||||
javaMailSender.send(helper.getMimeMessage());
|
||||
apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.EMAIL_EXEC_COUNT, 1);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
if (!StringUtils.isBlank(bcc)) {
|
||||
helper.setBcc(bcc.split("\\s*,\\s*"));
|
||||
}
|
||||
helper.setSubject(subject);
|
||||
helper.setText(body);
|
||||
mailSender.send(helper.getMimeMessage());
|
||||
apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.EMAIL_EXEC_COUNT, 1);
|
||||
} else {
|
||||
throw new RuntimeException("Email sending is disabled due to API limits!");
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
package org.thingsboard.rule.engine.api;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.thingsboard.server.common.data.ApiFeature;
|
||||
import org.thingsboard.server.common.data.ApiUsageStateMailMessage;
|
||||
import org.thingsboard.server.common.data.ApiUsageStateValue;
|
||||
@ -23,7 +24,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import java.util.Map;
|
||||
|
||||
public interface MailService {
|
||||
|
||||
@ -38,12 +39,14 @@ public interface MailService {
|
||||
void sendAccountActivatedEmail(String loginLink, String email) throws ThingsboardException;
|
||||
|
||||
void sendResetPasswordEmail(String passwordResetLink, String email) throws ThingsboardException;
|
||||
|
||||
void sendPasswordWasResetEmail(String loginLink, String email) throws ThingsboardException;
|
||||
|
||||
void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body) throws MessagingException;
|
||||
void sendPasswordWasResetEmail(String loginLink, String email) throws ThingsboardException;
|
||||
|
||||
void sendAccountLockoutEmail( String lockoutEmail, String email, Integer maxFailedLoginAttempts) throws ThingsboardException;
|
||||
|
||||
void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images) throws ThingsboardException;
|
||||
|
||||
void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException;
|
||||
|
||||
void sendApiFeatureStateEmail(ApiFeature apiFeature, ApiUsageStateValue stateValue, String email, ApiUsageStateMailMessage msg) throws ThingsboardException;
|
||||
}
|
||||
|
||||
@ -18,6 +18,8 @@ package org.thingsboard.rule.engine.mail;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
class EmailPojo {
|
||||
@ -28,5 +30,7 @@ class EmailPojo {
|
||||
private final String bcc;
|
||||
private final String subject;
|
||||
private final String body;
|
||||
private final Map<String, String> images;
|
||||
private final boolean html;
|
||||
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
package org.thingsboard.rule.engine.mail;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
@ -27,9 +28,10 @@ import org.thingsboard.rule.engine.api.TbNodeException;
|
||||
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
||||
import org.thingsboard.server.common.data.plugin.ComponentType;
|
||||
import org.thingsboard.server.common.msg.TbMsg;
|
||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS;
|
||||
import static org.thingsboard.rule.engine.mail.TbSendEmailNode.SEND_EMAIL_TYPE;
|
||||
@ -49,13 +51,16 @@ import static org.thingsboard.rule.engine.mail.TbSendEmailNode.SEND_EMAIL_TYPE;
|
||||
public class TbMsgToEmailNode implements TbNode {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String IMAGES = "images";
|
||||
|
||||
private TbMsgToEmailNodeConfiguration config;
|
||||
private boolean isDynamicHtmlTemplate;
|
||||
|
||||
@Override
|
||||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
||||
this.config = TbNodeUtils.convert(configuration, TbMsgToEmailNodeConfiguration.class);
|
||||
}
|
||||
this.isDynamicHtmlTemplate = this.config.getMailBodyType().equals("dynamic");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMsg(TbContext ctx, TbMsg msg) {
|
||||
@ -80,8 +85,18 @@ public class TbMsgToEmailNode implements TbNode {
|
||||
builder.to(fromTemplate(this.config.getToTemplate(), msg));
|
||||
builder.cc(fromTemplate(this.config.getCcTemplate(), msg));
|
||||
builder.bcc(fromTemplate(this.config.getBccTemplate(), msg));
|
||||
if(isDynamicHtmlTemplate) {
|
||||
builder.html(Boolean.parseBoolean(fromTemplate(this.config.getIsHtmlTemplate(), msg)));
|
||||
} else {
|
||||
builder.html(Boolean.parseBoolean(this.config.getMailBodyType()));
|
||||
}
|
||||
builder.subject(fromTemplate(this.config.getSubjectTemplate(), msg));
|
||||
builder.body(fromTemplate(this.config.getBodyTemplate(), msg));
|
||||
String imagesStr = msg.getMetaData().getValue(IMAGES);
|
||||
if (!StringUtils.isEmpty(imagesStr)) {
|
||||
Map<String, String> imgMap = MAPPER.readValue(imagesStr, new TypeReference<HashMap<String, String>>() {});
|
||||
builder.images(imgMap);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
@ -27,6 +27,8 @@ public class TbMsgToEmailNodeConfiguration implements NodeConfiguration {
|
||||
private String bccTemplate;
|
||||
private String subjectTemplate;
|
||||
private String bodyTemplate;
|
||||
private String isHtmlTemplate;
|
||||
private String mailBodyType;
|
||||
|
||||
@Override
|
||||
public TbMsgToEmailNodeConfiguration defaultConfiguration() {
|
||||
@ -35,6 +37,7 @@ public class TbMsgToEmailNodeConfiguration implements NodeConfiguration {
|
||||
configuration.toTemplate = "${userEmail}";
|
||||
configuration.subjectTemplate = "Device ${deviceType} temperature high";
|
||||
configuration.bodyTemplate = "Device ${deviceName} has high temperature ${temp}";
|
||||
configuration.mailBodyType = "false";
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,7 +19,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.thingsboard.rule.engine.api.RuleNode;
|
||||
import org.thingsboard.rule.engine.api.TbContext;
|
||||
import org.thingsboard.rule.engine.api.TbNode;
|
||||
@ -29,7 +28,6 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
||||
import org.thingsboard.server.common.data.plugin.ComponentType;
|
||||
import org.thingsboard.server.common.msg.TbMsg;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
@ -88,21 +86,10 @@ public class TbSendEmailNode implements TbNode {
|
||||
private void sendEmail(TbContext ctx, TbMsg msg, EmailPojo email) throws Exception {
|
||||
if (this.config.isUseSystemSmtpSettings()) {
|
||||
ctx.getMailService().send(ctx.getTenantId(), msg.getCustomerId(), email.getFrom(), email.getTo(), email.getCc(),
|
||||
email.getBcc(), email.getSubject(), email.getBody());
|
||||
email.getBcc(), email.getSubject(), email.getBody(), email.isHtml(), email.getImages());
|
||||
} else {
|
||||
MimeMessage mailMsg = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8");
|
||||
helper.setFrom(email.getFrom());
|
||||
helper.setTo(email.getTo().split("\\s*,\\s*"));
|
||||
if (!StringUtils.isBlank(email.getCc())) {
|
||||
helper.setCc(email.getCc().split("\\s*,\\s*"));
|
||||
}
|
||||
if (!StringUtils.isBlank(email.getBcc())) {
|
||||
helper.setBcc(email.getBcc().split("\\s*,\\s*"));
|
||||
}
|
||||
helper.setSubject(email.getSubject());
|
||||
helper.setText(email.getBody());
|
||||
mailSender.send(helper.getMimeMessage());
|
||||
ctx.getMailService().send(ctx.getTenantId(), msg.getCustomerId(), email.getFrom(), email.getTo(), email.getCc(),
|
||||
email.getBcc(), email.getSubject(), email.getBody(), email.isHtml(), email.getImages(), this.mailSender);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -97,6 +97,7 @@ public class TbMsgToEmailNodeTest {
|
||||
config.setToTemplate("${userEmail}");
|
||||
config.setSubjectTemplate("Hi ${username} there");
|
||||
config.setBodyTemplate("${name} is to high. Current ${passed} and ${count}");
|
||||
config.setMailBodyType("false");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user