From abd57987552b77cc783cad6894ceeda898cccb6c Mon Sep 17 00:00:00 2001 From: jackfanwan <61672564+jackfanwan@users.noreply.github.com> Date: Fri, 12 Aug 2022 09:57:27 +0800 Subject: [PATCH] [fix-10961][alert server]Change the content of alert to an array (#11033) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change the content of alert to an array * Increase unit test coverage * Modify the code format * Modify the code format * Modify judgment statement * Deleting invalid References * Modify the previous code implementation and improve the test data * Modify the code format * Move the location of the Content conversion * Remove useless code * Change the code format * Modify unit tests * Modify unit tests * Modify unit tests * Changing the location of the code * Modify the code Co-authored-by: fanwanlong Co-authored-by: 范万龙 --- .../email/template/DefaultHTMLTemplate.java | 16 ++++++++++++++++ .../email/template/DefaultHTMLTemplateTest.java | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java index 433cfda3f7..4167e77235 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java @@ -24,6 +24,7 @@ import org.apache.dolphinscheduler.plugin.alert.email.EmailConstants; import org.apache.dolphinscheduler.spi.utils.JSONUtils; import org.apache.dolphinscheduler.spi.utils.StringUtils; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -35,6 +36,11 @@ import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import org.springframework.boot.configurationprocessor.json.JSONArray; +import org.springframework.boot.configurationprocessor.json.JSONException; +import org.springframework.boot.configurationprocessor.json.JSONTokener; public class DefaultHTMLTemplate implements AlertTemplate { @@ -113,6 +119,16 @@ public class DefaultHTMLTemplate implements AlertTemplate { private String getTextTypeMessage(String content) { if (StringUtils.isNotEmpty(content)) { + // Converts an object type to an array type to prevent subsequent conversions from reporting errors + try { + Object contentObject = new JSONTokener(content).nextValue(); + if (!(contentObject instanceof JSONArray)) { + ObjectNode jsonNodes = JSONUtils.parseObject(content); + content = JSONUtils.toJsonString(Collections.singletonList(jsonNodes)); + } + } catch (JSONException e) { + logger.error("alert content is null"); + } ArrayNode list = JSONUtils.parseArray(content); StringBuilder contents = new StringBuilder(100); for (JsonNode jsonNode : list) { diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java index bc357cd34a..37fe97857c 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java @@ -46,6 +46,14 @@ public class DefaultHTMLTemplateTest { String textTypeMessage = template.getMessageFromTemplate(list2String(), ShowType.TEXT, true); assertEquals(textTypeMessage, generateMockTextTypeResultByHand()); + + String mapjson = "{\"taskInstanceId\":94,\"taskName\":\"000\",\"taskType\":\"DATA_QUALITY\"," + + "\"processDefinitionId\":0,\"processInstanceId\":58,\"state\":\"RUNNING_EXECUTION\"," + + "\"startTime\":\"2022-07-17 16:00:32\",\"host\":\"192.168.18.182:1234\"," + + "\"logPath\":\"/Users/mac/学习/dolphinscheduler/dolphinscheduler/logs/20220717/6222644042400_1-58-94.log\"}"; + textTypeMessage = template.getMessageFromTemplate(mapjson, ShowType.TEXT, true); + String result = textTypeMessage; + assertEquals(textTypeMessage, result); } private String list2String() {