diff --git a/dolphinscheduler-alert/pom.xml b/dolphinscheduler-alert/pom.xml index 188bfa3d9e..d63e3808b4 100644 --- a/dolphinscheduler-alert/pom.xml +++ b/dolphinscheduler-alert/pom.xml @@ -66,11 +66,6 @@ commons-email - - com.alibaba - fastjson - - com.fasterxml.jackson.core jackson-core diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java index 79123a1e80..7331df3913 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java @@ -16,13 +16,15 @@ */ package org.apache.dolphinscheduler.alert.template.impl; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; import org.apache.dolphinscheduler.alert.template.AlertTemplate; import org.apache.dolphinscheduler.alert.utils.Constants; -import org.apache.dolphinscheduler.alert.utils.JSONUtils; import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.apache.dolphinscheduler.common.utils.*; import java.util.*; @@ -107,18 +109,11 @@ public class DefaultHTMLTemplate implements AlertTemplate { private String getTextTypeMessage(String content,boolean showAll){ if (StringUtils.isNotEmpty(content)){ - List list; - try { - list = JSONUtils.toList(content,String.class); - }catch (Exception e){ - logger.error("json format exception",e); - return null; - } - + ArrayNode list = JSONUtils.parseArray(content); StringBuilder contents = new StringBuilder(100); - for (String str : list){ + for (JsonNode jsonNode : list){ contents.append(Constants.TR); - contents.append(Constants.TD).append(str).append(Constants.TD_END); + contents.append(Constants.TD).append(jsonNode.toString()).append(Constants.TD_END); contents.append(Constants.TR_END); } diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java index f2c9cd812c..455d5de834 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java @@ -17,7 +17,7 @@ package org.apache.dolphinscheduler.alert.utils; -import com.alibaba.fastjson.JSON; +import org.apache.dolphinscheduler.common.utils.*; import org.apache.commons.codec.binary.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; @@ -129,7 +129,7 @@ public class DingTalkUtils { textContent.put("content", txt); items.put("text", textContent); - return JSON.toJSONString(items); + return JSONUtils.toJsonString(items); } diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java index d199d154aa..38dc38b4a8 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java @@ -18,9 +18,8 @@ package org.apache.dolphinscheduler.alert.utils; import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.utils.StringUtils; -import com.alibaba.fastjson.JSON; +import org.apache.dolphinscheduler.common.utils.*; -import com.google.common.reflect.TypeToken; import org.apache.dolphinscheduler.plugin.model.AlertData; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; @@ -64,14 +63,15 @@ public class EnterpriseWeChatUtils { /** * get Enterprise WeChat is enable + * * @return isEnable */ - public static boolean isEnable(){ + public static boolean isEnable() { Boolean isEnable = null; try { isEnable = PropertyUtils.getBoolean(Constants.ENTERPRISE_WECHAT_ENABLE); } catch (Exception e) { - logger.error(e.getMessage(),e); + logger.error(e.getMessage(), e); } if (isEnable == null) { return false; @@ -81,6 +81,7 @@ public class EnterpriseWeChatUtils { /** * get Enterprise WeChat token info + * * @return token string info * @throws IOException the IOException */ @@ -99,10 +100,12 @@ public class EnterpriseWeChatUtils { response.close(); } - Map map = JSON.parseObject(resp, - new TypeToken>() { - }.getType()); - return map.get("access_token").toString(); + Map map = JSONUtils.toMap(resp); + if (map != null) { + return map.get("access_token"); + } else { + return null; + } } finally { httpClient.close(); } @@ -110,9 +113,10 @@ public class EnterpriseWeChatUtils { /** * make team single Enterprise WeChat message + * * @param toParty the toParty * @param agentId the agentId - * @param msg the msg + * @param msg the msg * @return Enterprise WeChat send message */ public static String makeTeamSendMsg(String toParty, String agentId, String msg) { @@ -123,9 +127,10 @@ public class EnterpriseWeChatUtils { /** * make team multi Enterprise WeChat message + * * @param toParty the toParty * @param agentId the agentId - * @param msg the msg + * @param msg the msg * @return Enterprise WeChat send message */ public static String makeTeamSendMsg(Collection toParty, String agentId, String msg) { @@ -137,9 +142,10 @@ public class EnterpriseWeChatUtils { /** * make team single user message - * @param toUser the toUser + * + * @param toUser the toUser * @param agentId the agentId - * @param msg the msg + * @param msg the msg * @return Enterprise WeChat send message */ public static String makeUserSendMsg(String toUser, String agentId, String msg) { @@ -150,9 +156,10 @@ public class EnterpriseWeChatUtils { /** * make team multi user message - * @param toUser the toUser + * + * @param toUser the toUser * @param agentId the agentId - * @param msg the msg + * @param msg the msg * @return Enterprise WeChat send message */ public static String makeUserSendMsg(Collection toUser, String agentId, String msg) { @@ -164,9 +171,10 @@ public class EnterpriseWeChatUtils { /** * send Enterprise WeChat + * * @param charset the charset - * @param data the data - * @param token the token + * @param data the data + * @param token the token * @return Enterprise WeChat resp, demo: {"errcode":0,"errmsg":"ok","invaliduser":""} * @throws IOException the IOException */ @@ -196,21 +204,22 @@ public class EnterpriseWeChatUtils { /** * convert table to markdown style - * @param title the title + * + * @param title the title * @param content the content * @return markdown table content */ - public static String markdownTable(String title,String content){ + public static String markdownTable(String title, String content) { List mapItemsList = JSONUtils.toList(content, LinkedHashMap.class); StringBuilder contents = new StringBuilder(200); if (null != mapItemsList) { - for (LinkedHashMap mapItems : mapItemsList){ + for (LinkedHashMap mapItems : mapItemsList) { Set> entries = mapItems.entrySet(); Iterator> iterator = entries.iterator(); - StringBuilder t = new StringBuilder(String.format("`%s`%s",title,Constants.MARKDOWN_ENTER)); + StringBuilder t = new StringBuilder(String.format("`%s`%s", title, Constants.MARKDOWN_ENTER)); - while (iterator.hasNext()){ + while (iterator.hasNext()) { Map.Entry entry = iterator.next(); t.append(Constants.MARKDOWN_QUOTE); @@ -225,23 +234,24 @@ public class EnterpriseWeChatUtils { /** * convert text to markdown style - * @param title the title + * + * @param title the title * @param content the content * @return markdown text */ - public static String markdownText(String title,String content){ - if (StringUtils.isNotEmpty(content)){ + public static String markdownText(String title, String content) { + if (StringUtils.isNotEmpty(content)) { List list; try { - list = JSONUtils.toList(content,String.class); - }catch (Exception e){ - logger.error("json format exception",e); + list = JSONUtils.toList(content, String.class); + } catch (Exception e) { + logger.error("json format exception", e); return null; } StringBuilder contents = new StringBuilder(100); - contents.append(String.format("`%s`%n",title)); - for (String str : list){ + contents.append(String.format("`%s`%n", title)); + for (String str : list) { contents.append(Constants.MARKDOWN_QUOTE); contents.append(str); contents.append(Constants.MARKDOWN_ENTER); @@ -255,14 +265,15 @@ public class EnterpriseWeChatUtils { /** * Determine the mardown style based on the show type of the alert + * * @return the markdown alert table/text */ - public static String markdownByAlert(AlertData alert){ + public static String markdownByAlert(AlertData alert) { String result = ""; if (alert.getShowType().equals(ShowType.TABLE.getDescp())) { - result = markdownTable(alert.getTitle(),alert.getContent()); - }else if(alert.getShowType().equals(ShowType.TEXT.getDescp())){ - result = markdownText(alert.getTitle(),alert.getContent()); + result = markdownTable(alert.getTitle(), alert.getContent()); + } else if (alert.getShowType().equals(ShowType.TEXT.getDescp())) { + result = markdownText(alert.getTitle(), alert.getContent()); } return result; diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java index 366e2828c5..08256860e2 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java @@ -30,6 +30,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; +import org.apache.dolphinscheduler.common.utils.*; /** * excel utils diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/JSONUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/JSONUtils.java deleted file mode 100644 index 5f8371de2d..0000000000 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/JSONUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.dolphinscheduler.alert.utils; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import org.apache.dolphinscheduler.common.utils.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collections; -import java.util.List; - -/** - * json utils - */ -public class JSONUtils { - - private static final Logger logger = LoggerFactory.getLogger(JSONUtils.class); - - /** - * object to json string - * @param object the object to be converted to json - * @return json string - */ - public static String toJsonString(Object object) { - try{ - return JSON.toJSONString(object,false); - } catch (Exception e) { - throw new RuntimeException("Json deserialization exception.", e); - } - } - - /** - * json to list - * - * @param json the json - * @param clazz c - * @param the generic clazz - * @return the result list or empty list - */ - public static List toList(String json, Class clazz) { - if (StringUtils.isEmpty(json)) { - return Collections.emptyList(); - } - try { - return JSON.parseArray(json, clazz); - } catch (Exception e) { - logger.error("JSONArray.parseArray exception!",e); - } - - return Collections.emptyList(); - } - -} diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java index 58609c07cb..76d01c134d 100644 --- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java @@ -16,7 +16,7 @@ */ package org.apache.dolphinscheduler.alert.template.impl; -import org.apache.dolphinscheduler.alert.utils.JSONUtils; +import org.apache.dolphinscheduler.common.utils.*; import org.apache.dolphinscheduler.common.enums.ShowType; import org.junit.Test; import org.slf4j.Logger; @@ -116,7 +116,7 @@ public class DefaultHTMLTemplateTest{ " \n" + " \n" + " \n" + - "{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"190\",\"port\":\"3306\",\"no index of number\":\"80\"}{\"mysql service name\":\"mysql210\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"90\",\"port\":\"3306\",\"no index of number\":\"10\"} \n" + + "{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"port\":\"3306\",\"no index of number\":\"80\",\"database client connections\":\"190\"}{\"mysql service name\":\"mysql210\",\"mysql address\":\"192.168.xx.xx\",\"port\":\"3306\",\"no index of number\":\"10\",\"database client connections\":\"90\"} \n" + " \n" + "