From 8c481f20e0566293d480f0c700bbdd2b8fd125bd Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Wed, 9 May 2018 16:35:06 +0300 Subject: [PATCH 1/5] Improve email Rule Nodes configuration. --- rule-engine/rule-engine-components/pom.xml | 10 --- .../rule/engine/mail/RuleVelocityUtils.java | 68 ------------------- .../rule/engine/mail/TbMsgToEmailNode.java | 44 +++--------- .../mail/TbMsgToEmailNodeConfiguration.java | 7 +- .../rule/engine/mail/TbSendEmailNode.java | 68 +++++++++++++++++-- .../mail/TbSendEmailNodeConfiguration.java | 16 ++++- .../static/rulenode/rulenode-core-config.js | 6 +- 7 files changed, 95 insertions(+), 124 deletions(-) delete mode 100644 rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/RuleVelocityUtils.java diff --git a/rule-engine/rule-engine-components/pom.xml b/rule-engine/rule-engine-components/pom.xml index a38df289d3..3352f5acec 100644 --- a/rule-engine/rule-engine-components/pom.xml +++ b/rule-engine/rule-engine-components/pom.xml @@ -72,16 +72,6 @@ com.google.guava guava - - org.apache.velocity - velocity - provided - - - org.apache.velocity - velocity-tools - provided - org.springframework spring-web diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/RuleVelocityUtils.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/RuleVelocityUtils.java deleted file mode 100644 index 7413cadd48..0000000000 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/RuleVelocityUtils.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright © 2016-2018 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.thingsboard.rule.engine.mail; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.runtime.RuntimeServices; -import org.apache.velocity.runtime.RuntimeSingleton; -import org.apache.velocity.runtime.parser.ParseException; -import org.apache.velocity.runtime.parser.node.SimpleNode; -import org.thingsboard.server.common.msg.TbMsg; - -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.Map; - -import static org.thingsboard.server.common.msg.TbMsgDataType.JSON; - -public class RuleVelocityUtils { - - public static VelocityContext createContext(TbMsg msg) throws IOException { - VelocityContext context = new VelocityContext(); - context.put("originator", msg.getOriginator()); - context.put("type", msg.getType()); - context.put("metadata", msg.getMetaData().values()); - if (msg.getDataType() == JSON) { - Map map = new ObjectMapper().readValue(msg.getData(), Map.class); - context.put("msg", map); - } else { - context.put("msg", msg.getData()); - } - return context; - } - - public static String merge(Template template, VelocityContext context) { - StringWriter writer = new StringWriter(); - template.merge(context, writer); - return writer.toString(); - } - - public static Template create(String source, String templateName) throws ParseException { - RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); - StringReader reader = new StringReader(source); - SimpleNode node = runtimeServices.parse(reader, templateName); - Template template = new Template(); - template.setRuntimeServices(runtimeServices); - template.setData(node); - template.initDocument(); - return template; - } - - -} diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/TbMsgToEmailNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/TbMsgToEmailNode.java index 69c0901057..8366bd50b5 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/TbMsgToEmailNode.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mail/TbMsgToEmailNode.java @@ -18,17 +18,14 @@ package org.thingsboard.rule.engine.mail; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.runtime.parser.ParseException; import org.springframework.util.StringUtils; import org.thingsboard.rule.engine.TbNodeUtils; import org.thingsboard.rule.engine.api.*; 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.Optional; import static org.thingsboard.rule.engine.mail.TbSendEmailNode.SEND_EMAIL_TYPE; @@ -48,27 +45,9 @@ public class TbMsgToEmailNode implements TbNode { private TbMsgToEmailNodeConfiguration config; - private Optional