Unverified Commit 7fec6c37 authored by Kerwin's avatar Kerwin Committed by GitHub

[Optimization][Style]Added dlink-alert module code style. (#891)

* Added dlink-alert module code style.

* update alert plugin module code style
parent 2d481f48
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
/** /**
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
import com.dlink.assertion.Asserts; import com.dlink.assertion.Asserts;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
import java.util.Map; import java.util.Map;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
/** /**
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
import lombok.Data; import lombok.Data;
...@@ -28,61 +27,79 @@ import lombok.Data; ...@@ -28,61 +27,79 @@ import lombok.Data;
* @author wenmo * @author wenmo
* @since 2022/3/7 18:30 * @since 2022/3/7 18:30
**/ **/
@Data @Data
public class AlertMsg { public class AlertMsg {
private String AlertType; // 告警类型 private String alertType;
private String AlertTime; // 告警时间 private String alertTime;
private String JobID; // 任务ID private String jobID;
private String JobName; // 任务名称 private String jobName;
private String JobType; // 任务类型 private String jobType;
private String JobStatus; // 任务状态 private String jobStatus;
private String JobStartTime; // 任务开始时间 private String jobStartTime;
private String JobEndTime; // 任务结束时间 private String jobEndTime;
private String JobDuration; // 任务耗时 private String jobDuration;
private String LinkUrl; // Flink webUI 链接
private String ExceptionUrl; // Flink job Root Exception 链接 /**
* Flink WebUI link url
*/
private String linkUrl;
/**
* Flink job Root Exception url
*/
private String exceptionUrl;
public AlertMsg() { public AlertMsg() {
} }
public AlertMsg(String alertType, String alertTime, String jobID, String jobName, String jobType, String jobStatus, String jobStartTime, String jobEndTime, String jobDuration, String linkUrl, String exceptionUrl) { public AlertMsg(String alertType,
this.AlertType = alertType; String alertTime,
this.AlertTime = alertTime; String jobID,
this.JobID = jobID; String jobName,
this.JobName = jobName; String jobType,
this.JobType = jobType; String jobStatus,
this.JobStatus = jobStatus; String jobStartTime,
this.JobStartTime = jobStartTime; String jobEndTime,
this.JobEndTime = jobEndTime; String jobDuration,
this.JobDuration = jobDuration; String linkUrl,
this.LinkUrl = linkUrl; String exceptionUrl) {
this.ExceptionUrl = exceptionUrl;
this.alertType = alertType;
this.alertTime = alertTime;
this.jobID = jobID;
this.jobName = jobName;
this.jobType = jobType;
this.jobStatus = jobStatus;
this.jobStartTime = jobStartTime;
this.jobEndTime = jobEndTime;
this.jobDuration = jobDuration;
this.linkUrl = linkUrl;
this.exceptionUrl = exceptionUrl;
} }
public String toString() { public String toString() {
return "[{ \"Alert Type\":\""+AlertType+"\"," return "[{ \"Alert Type\":\"" + alertType + "\","
+
"\"Alert Time\":\"" + alertTime + "\","
+ +
"\"Alert Time\":\""+AlertTime+"\"," "\"Job ID\":\"" + jobID + "\","
+ +
"\"Job ID\":\""+JobID+"\"," "\"Job Name\":\"" + jobName + "\","
+ +
"\"Job Name\":\""+ JobName +"\"," "\"Job Type\":\"" + jobType + "\","
+ +
"\"Job Type\":\""+ JobType +"\"," "\"Job Status\":\"" + jobStatus + "\","
+ +
"\"Job Status\":\""+ JobStatus +"\"," "\"Job StartTime\": \"" + jobStartTime + "\","
+ +
"\"Job StartTime\": \"" +JobStartTime +"\"," "\"Job EndTime\": \"" + jobEndTime + "\","
+ +
"\"Job EndTime\": \""+JobEndTime+"\"," "\"Job Duration\": \"" + jobDuration + "\","
+ +
"\"Job Duration\": \""+JobDuration+"\"," "\"Exception Log\" :\"" + exceptionUrl + "\""
// +
// "\"LinkUrl\": \""+ LinkUrl +"\","
+ +
"\"Exception Log\" :\""+ ExceptionUrl +"\"" +
"}]"; "}]";
} }
} }
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
import java.util.Map; import java.util.Map;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
/** /**
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
/** /**
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert; package com.dlink.alert;
/** /**
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.dingtalk; package com.dlink.alert.dingtalk;
import com.dlink.alert.AbstractAlert; import com.dlink.alert.AbstractAlert;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.dingtalk; package com.dlink.alert.dingtalk;
/** /**
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.dingtalk; package com.dlink.alert.dingtalk;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
...@@ -25,6 +24,7 @@ import com.dlink.alert.AlertSendResponse; ...@@ -25,6 +24,7 @@ import com.dlink.alert.AlertSendResponse;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
import com.dlink.assertion.Asserts; import com.dlink.assertion.Asserts;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils; import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
...@@ -40,15 +40,24 @@ import org.apache.http.impl.client.BasicCredentialsProvider; ...@@ -40,15 +40,24 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* DingTalkSender * DingTalkSender
...@@ -248,10 +257,10 @@ public class DingTalkSender { ...@@ -248,10 +257,10 @@ public class DingTalkSender {
String[] atUserArray = Asserts.isNotNullString(atUserIds) ? atUserIds.split(",") : new String[0]; String[] atUserArray = Asserts.isNotNullString(atUserIds) ? atUserIds.split(",") : new String[0];
boolean isAtAll = Objects.isNull(atAll) ? false : atAll; boolean isAtAll = Objects.isNull(atAll) ? false : atAll;
at.put("isAtAll", isAtAll); at.put("isAtAll", isAtAll);
if (atMobileArray.length > 0){ if (atMobileArray.length > 0) {
at.put("atMobiles", atMobileArray); at.put("atMobiles", atMobileArray);
} }
if (atMobileArray.length > 0){ if (atMobileArray.length > 0) {
at.put("atUserIds", atUserArray); at.put("atUserIds", atUserArray);
} }
items.put("at", at); items.put("at", at);
......
...@@ -17,18 +17,22 @@ ...@@ -17,18 +17,22 @@
* *
*/ */
package com.dlink.alert.dingtalk; package com.dlink.alert.dingtalk;
import com.dlink.alert.*; import com.dlink.alert.Alert;
import org.junit.Assert; import com.dlink.alert.AlertConfig;
import org.junit.Before; import com.dlink.alert.AlertMsg;
import org.junit.Test; import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/** /**
* DingTalkTest * DingTalkTest
* *
...@@ -52,12 +56,11 @@ public class DingTalkTest { ...@@ -52,12 +56,11 @@ public class DingTalkTest {
alertMsg.setJobStartTime("2018-08-06 10:31:34.0"); alertMsg.setJobStartTime("2018-08-06 10:31:34.0");
alertMsg.setJobEndTime("2018-08-06 10:31:49.0"); alertMsg.setJobEndTime("2018-08-06 10:31:49.0");
alertMsg.setJobDuration("23 Seconds"); alertMsg.setJobDuration("23 Seconds");
String linkUrl = "[跳转至该任务的FlinkWeb](http://cdh1:8081/#/job/"+uuid+"/overview)"; String linkUrl = "[跳转至该任务的FlinkWeb](http://cdh1:8081/#/job/" + uuid + "/overview)";
alertMsg.setLinkUrl(linkUrl); alertMsg.setLinkUrl(linkUrl);
String exceptionUrl = "[点击查看该任务的异常日志](http://cdh1:8081/#/job/"+uuid+"/exceptions)"; String exceptionUrl = "[点击查看该任务的异常日志](http://cdh1:8081/#/job/" + uuid + "/exceptions)";
alertMsg.setExceptionUrl(exceptionUrl); alertMsg.setExceptionUrl(exceptionUrl);
config.put(DingTalkConstants.KEYWORD, "Dinky-Fink 钉钉告警测试"); config.put(DingTalkConstants.KEYWORD, "Dinky-Fink 钉钉告警测试");
config.put(DingTalkConstants.WEB_HOOK, "url"); config.put(DingTalkConstants.WEB_HOOK, "url");
config.put(DingTalkConstants.MSG_TYPE, ShowType.MARKDOWN.getValue()); config.put(DingTalkConstants.MSG_TYPE, ShowType.MARKDOWN.getValue());
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.email; package com.dlink.alert.email;
import com.dlink.alert.AbstractAlert; import com.dlink.alert.AbstractAlert;
...@@ -37,7 +36,7 @@ public class EmailAlert extends AbstractAlert { ...@@ -37,7 +36,7 @@ public class EmailAlert extends AbstractAlert {
@Override @Override
public AlertResult send(String title, String content) { public AlertResult send(String title, String content) {
MailSender mailSender=new MailSender(getConfig().getParam()); MailSender mailSender = new MailSender(getConfig().getParam());
return mailSender.send(title,content); return mailSender.send(title, content);
} }
} }
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
* *
*/ */
package com.dlink.alert.email; package com.dlink.alert.email;
/** /**
......
...@@ -17,16 +17,18 @@ ...@@ -17,16 +17,18 @@
* *
*/ */
package com.dlink.alert.email; package com.dlink.alert.email;
import com.dlink.alert.AlertException; import com.dlink.alert.AlertException;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -35,6 +37,9 @@ import java.util.LinkedHashMap; ...@@ -35,6 +37,9 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* ExcelUtils excel工具类 * ExcelUtils excel工具类
* @author zhumingye * @author zhumingye
......
...@@ -17,32 +17,47 @@ ...@@ -17,32 +17,47 @@
* *
*/ */
package com.dlink.alert.email; package com.dlink.alert.email;
import static java.util.Objects.requireNonNull;
import com.dlink.alert.AlertException; import com.dlink.alert.AlertException;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
import com.dlink.alert.email.template.AlertTemplate; import com.dlink.alert.email.template.AlertTemplate;
import com.dlink.alert.email.template.DefaultHTMLTemplate; import com.dlink.alert.email.template.DefaultHTMLTemplate;
import com.sun.mail.smtp.SMTPProvider;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException; import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static java.util.Objects.requireNonNull; import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.mail.smtp.SMTPProvider;
/** /**
* MailSender 邮件发送器 * MailSender 邮件发送器
...@@ -179,7 +194,7 @@ public final class MailSender { ...@@ -179,7 +194,7 @@ public final class MailSender {
String partContent = (showType.equals(ShowType.ATTACHMENT.getValue()) String partContent = (showType.equals(ShowType.ATTACHMENT.getValue())
? "Please see the attachment " + title + EmailConstants.EXCEL_SUFFIX_XLSX ? "Please see the attachment " + title + EmailConstants.EXCEL_SUFFIX_XLSX
: htmlTable(title ,content, false)); : htmlTable(title, content, false));
attachment(title, content, partContent); attachment(title, content, partContent);
...@@ -201,8 +216,8 @@ public final class MailSender { ...@@ -201,8 +216,8 @@ public final class MailSender {
* @param showAll if show the whole content * @param showAll if show the whole content
* @return the html table form * @return the html table form
*/ */
private String htmlTable(String title,String content, boolean showAll) { private String htmlTable(String title, String content, boolean showAll) {
return alertTemplate.getMessageFromTemplate(title ,content, ShowType.TABLE, showAll); return alertTemplate.getMessageFromTemplate(title, content, ShowType.TABLE, showAll);
} }
/** /**
...@@ -211,7 +226,7 @@ public final class MailSender { ...@@ -211,7 +226,7 @@ public final class MailSender {
* @param content the content * @param content the content
* @return the html table form * @return the html table form
*/ */
private String htmlTable(String title,String content) { private String htmlTable(String title, String content) {
return htmlTable(title,content, true); return htmlTable(title,content, true);
} }
...@@ -221,8 +236,8 @@ public final class MailSender { ...@@ -221,8 +236,8 @@ public final class MailSender {
* @param content the content * @param content the content
* @return text in html form * @return text in html form
*/ */
private String htmlText(String title ,String content) { private String htmlText(String title, String content) {
return alertTemplate.getMessageFromTemplate(title,content, ShowType.TEXT); return alertTemplate.getMessageFromTemplate(title, content, ShowType.TEXT);
} }
/** /**
...@@ -274,22 +289,22 @@ public final class MailSender { ...@@ -274,22 +289,22 @@ public final class MailSender {
props.setProperty(EmailConstants.MAIL_SMTP_HOST, mailSmtpHost); props.setProperty(EmailConstants.MAIL_SMTP_HOST, mailSmtpHost);
props.setProperty(EmailConstants.MAIL_SMTP_PORT, mailSmtpPort); props.setProperty(EmailConstants.MAIL_SMTP_PORT, mailSmtpPort);
if (StringUtils.isNotEmpty(enableSmtpAuth)){ if (StringUtils.isNotEmpty(enableSmtpAuth)) {
props.setProperty(EmailConstants.MAIL_SMTP_AUTH, enableSmtpAuth); props.setProperty(EmailConstants.MAIL_SMTP_AUTH, enableSmtpAuth);
} }
if ( StringUtils.isNotEmpty(mailProtocol)){ if (StringUtils.isNotEmpty(mailProtocol)) {
props.setProperty(EmailConstants.MAIL_TRANSPORT_PROTOCOL, mailProtocol); props.setProperty(EmailConstants.MAIL_TRANSPORT_PROTOCOL, mailProtocol);
} }
if (StringUtils.isNotEmpty(mailUseSSL)){ if (StringUtils.isNotEmpty(mailUseSSL)) {
props.setProperty(EmailConstants.MAIL_SMTP_SSL_ENABLE, mailUseSSL); props.setProperty(EmailConstants.MAIL_SMTP_SSL_ENABLE, mailUseSSL);
} }
if (StringUtils.isNotEmpty(mailUseStartTLS)){ if (StringUtils.isNotEmpty(mailUseStartTLS)) {
props.setProperty(EmailConstants.MAIL_SMTP_STARTTLS_ENABLE, mailUseStartTLS); props.setProperty(EmailConstants.MAIL_SMTP_STARTTLS_ENABLE, mailUseStartTLS);
} }
if (StringUtils.isNotEmpty(sslTrust)){ if (StringUtils.isNotEmpty(sslTrust)) {
props.setProperty(EmailConstants.MAIL_SMTP_SSL_TRUST, sslTrust); props.setProperty(EmailConstants.MAIL_SMTP_SSL_TRUST, sslTrust);
} }
...@@ -360,9 +375,9 @@ public final class MailSender { ...@@ -360,9 +375,9 @@ public final class MailSender {
* to send information, you can use HTML tags in mail content because of the use of HtmlEmail * to send information, you can use HTML tags in mail content because of the use of HtmlEmail
*/ */
if (showType.equals(ShowType.TABLE.getValue())) { if (showType.equals(ShowType.TABLE.getValue())) {
email.setMsg(htmlTable(title ,content)); email.setMsg(htmlTable(title, content));
} else if (showType.equals(ShowType.TEXT.getValue())) { } else if (showType.equals(ShowType.TEXT.getValue())) {
email.setMsg(htmlText(title ,content)); email.setMsg(htmlText(title, content));
} }
// send // send
......
...@@ -17,10 +17,8 @@ ...@@ -17,10 +17,8 @@
* *
*/ */
package com.dlink.alert.email.template; package com.dlink.alert.email.template;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
/** /**
......
...@@ -17,19 +17,24 @@ ...@@ -17,19 +17,24 @@
* *
*/ */
package com.dlink.alert.email.template; package com.dlink.alert.email.template;
import static java.util.Objects.requireNonNull;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
import com.dlink.alert.email.EmailConstants; import com.dlink.alert.email.EmailConstants;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Objects.requireNonNull; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @Author: zhumingye * @Author: zhumingye
...@@ -41,7 +46,7 @@ public class DefaultHTMLTemplate implements AlertTemplate { ...@@ -41,7 +46,7 @@ public class DefaultHTMLTemplate implements AlertTemplate {
public static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplate.class); public static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplate.class);
@Override @Override
public String getMessageFromTemplate(String title , String content, ShowType showType, boolean showAll) { public String getMessageFromTemplate(String title, String content, ShowType showType, boolean showAll) {
switch (showType) { switch (showType) {
case TABLE: case TABLE:
...@@ -60,7 +65,7 @@ public class DefaultHTMLTemplate implements AlertTemplate { ...@@ -60,7 +65,7 @@ public class DefaultHTMLTemplate implements AlertTemplate {
* @param showAll weather to show all * @param showAll weather to show all
* @return alert message * @return alert message
*/ */
private String getTableTypeMessage(String title ,String content, boolean showAll) { private String getTableTypeMessage(String title,String content, boolean showAll) {
if (StringUtils.isNotEmpty(content)) { if (StringUtils.isNotEmpty(content)) {
List<LinkedHashMap> mapItemsList = JSONUtil.toList(content,LinkedHashMap.class); List<LinkedHashMap> mapItemsList = JSONUtil.toList(content,LinkedHashMap.class);
...@@ -107,7 +112,7 @@ public class DefaultHTMLTemplate implements AlertTemplate { ...@@ -107,7 +112,7 @@ public class DefaultHTMLTemplate implements AlertTemplate {
* @param content message content * @param content message content
* @return alert message * @return alert message
*/ */
private String getTextTypeMessage(String title ,String content) { private String getTextTypeMessage(String title, String content) {
StringBuilder stringBuilder = new StringBuilder(100); StringBuilder stringBuilder = new StringBuilder(100);
if (StringUtils.isNotEmpty(content)) { if (StringUtils.isNotEmpty(content)) {
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.email; package com.dlink.alert.email;
import com.dlink.alert.AlertMsg; import com.dlink.alert.AlertMsg;
...@@ -26,14 +25,21 @@ import com.dlink.alert.ShowType; ...@@ -26,14 +25,21 @@ import com.dlink.alert.ShowType;
import com.dlink.alert.email.template.AlertTemplate; import com.dlink.alert.email.template.AlertTemplate;
import com.dlink.alert.email.template.DefaultHTMLTemplate; import com.dlink.alert.email.template.DefaultHTMLTemplate;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.*;
public class EmailSenderTest { public class EmailSenderTest {
private static final Logger logger = LoggerFactory.getLogger(EmailSenderTest.class); private static final Logger logger = LoggerFactory.getLogger(EmailSenderTest.class);
static MailSender mailSender; static MailSender mailSender;
...@@ -43,7 +49,6 @@ public class EmailSenderTest { ...@@ -43,7 +49,6 @@ public class EmailSenderTest {
String title = "Dinky Email Alert"; String title = "Dinky Email Alert";
private AlertMsg alertMsg = new AlertMsg(); private AlertMsg alertMsg = new AlertMsg();
@Before @Before
public void initEmailConfig() { public void initEmailConfig() {
...@@ -58,9 +63,9 @@ public class EmailSenderTest { ...@@ -58,9 +63,9 @@ public class EmailSenderTest {
alertMsg.setJobStartTime("2018-08-06 10:31:34.0"); alertMsg.setJobStartTime("2018-08-06 10:31:34.0");
alertMsg.setJobEndTime("2018-08-06 10:31:49.0"); alertMsg.setJobEndTime("2018-08-06 10:31:49.0");
alertMsg.setJobDuration("23 Seconds"); alertMsg.setJobDuration("23 Seconds");
String linkUrl = "[跳转至该任务的FlinkWeb](http://cdh1:8081/#/job/"+uuid+"/overview)"; String linkUrl = "[跳转至该任务的FlinkWeb](http://cdh1:8081/#/job/" + uuid + "/overview)";
alertMsg.setLinkUrl(linkUrl); alertMsg.setLinkUrl(linkUrl);
String exceptionUrl = "[点击查看该任务的异常日志](http://cdh1:8081/#/job/"+uuid+"/exceptions)"; String exceptionUrl = "[点击查看该任务的异常日志](http://cdh1:8081/#/job/" + uuid + "/exceptions)";
alertMsg.setExceptionUrl(exceptionUrl); alertMsg.setExceptionUrl(exceptionUrl);
emailConfig.put(EmailConstants.NAME_MAIL_PROTOCOL, "smtp"); emailConfig.put(EmailConstants.NAME_MAIL_PROTOCOL, "smtp");
...@@ -86,7 +91,6 @@ public class EmailSenderTest { ...@@ -86,7 +91,6 @@ public class EmailSenderTest {
Assert.assertEquals(true, alertResult.getSuccess()); // 格式需要调整 Assert.assertEquals(true, alertResult.getSuccess()); // 格式需要调整
} }
@Test @Test
public void testSendTableMail() { public void testSendTableMail() {
emailConfig.put(EmailConstants.NAME_SHOW_TYPE, ShowType.TABLE.getValue()); emailConfig.put(EmailConstants.NAME_SHOW_TYPE, ShowType.TABLE.getValue());
...@@ -131,8 +135,6 @@ public class EmailSenderTest { ...@@ -131,8 +135,6 @@ public class EmailSenderTest {
stringBuilder.append(EmailConstants.TR_END); stringBuilder.append(EmailConstants.TR_END);
} }
System.out.println(stringBuilder.toString());
} }
} }
} }
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.feishu; package com.dlink.alert.feishu;
import com.dlink.alert.AbstractAlert; import com.dlink.alert.AbstractAlert;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.feishu; package com.dlink.alert.feishu;
/** /**
...@@ -41,7 +40,8 @@ public final class FeiShuConstants { ...@@ -41,7 +40,8 @@ public final class FeiShuConstants {
static final String AT_ALL = "isAtAll"; static final String AT_ALL = "isAtAll";
static final String AT_USERS = "users"; static final String AT_USERS = "users";
static final String FEI_SHU_TEXT_TEMPLATE = "{\"msg_type\":\"{msg_type}\",\"content\":{\"{msg_type}\":\"{msg} {users} \" }}"; static final String FEI_SHU_TEXT_TEMPLATE = "{\"msg_type\":\"{msg_type}\",\"content\":{\"{msg_type}\":\"{msg} {users} \" }}";
static final String FEI_SHU_POST_TEMPLATE ="{\"msg_type\":\"{msg_type}\",\"content\":{\"{msg_type}\":{\"zh_cn\":{\"title\":\"{keyword}\",\"content\":[[{\"tag\":\"text\",\"un_escape\": true,\"text\":\"{msg}\"},{users}]]}}}}"; static final String FEI_SHU_POST_TEMPLATE = "{\"msg_type\":\"{msg_type}\",\"content\":{\"{msg_type}\":{\"zh_cn\":{\"title\":\"{keyword}\","
+ "\"content\":[[{\"tag\":\"text\",\"un_escape\": true,\"text\":\"{msg}\"},{users}]]}}}}";
private FeiShuConstants() { private FeiShuConstants() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
......
...@@ -17,14 +17,12 @@ ...@@ -17,14 +17,12 @@
* *
*/ */
package com.dlink.alert.feishu; package com.dlink.alert.feishu;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.codec.binary.StringUtils; import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
...@@ -32,12 +30,19 @@ import org.apache.http.client.methods.CloseableHttpResponse; ...@@ -32,12 +30,19 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* @Author: zhumingye * @Author: zhumingye
...@@ -71,7 +76,7 @@ public final class FeiShuSender { ...@@ -71,7 +76,7 @@ public final class FeiShuSender {
FeiShuSender(Map<String, String> config) { FeiShuSender(Map<String, String> config) {
url = config.get(FeiShuConstants.WEB_HOOK); url = config.get(FeiShuConstants.WEB_HOOK);
msgType = config.get(FeiShuConstants.MSG_TYPE); msgType = config.get(FeiShuConstants.MSG_TYPE);
keyword= config.get(FeiShuConstants.KEY_WORD).replace("\r\n", ""); keyword = config.get(FeiShuConstants.KEY_WORD).replace("\r\n", "");
enableProxy = Boolean.valueOf(config.get(FeiShuConstants.FEI_SHU_PROXY_ENABLE)); enableProxy = Boolean.valueOf(config.get(FeiShuConstants.FEI_SHU_PROXY_ENABLE));
secret = config.get(FeiShuConstants.SECRET); secret = config.get(FeiShuConstants.SECRET);
if (Boolean.TRUE.equals(enableProxy)) { if (Boolean.TRUE.equals(enableProxy)) {
...@@ -87,14 +92,14 @@ public final class FeiShuSender { ...@@ -87,14 +92,14 @@ public final class FeiShuSender {
} }
private String toJsonSendMsg(String title, String content) { private String toJsonSendMsg(String title, String content) {
String jsonResult =""; String jsonResult = "";
byte[] byt = StringUtils.getBytesUtf8(formatContent(title,content)); byte[] byt = StringUtils.getBytesUtf8(formatContent(title,content));
String contentResult = StringUtils.newStringUtf8(byt); String contentResult = StringUtils.newStringUtf8(byt);
String userIdsToText = mkUserIds(org.apache.commons.lang3.StringUtils.isBlank(atUserIds)? "all": atUserIds); String userIdsToText = mkUserIds(org.apache.commons.lang3.StringUtils.isBlank(atUserIds) ? "all" : atUserIds);
if (StringUtils.equals(ShowType.TEXT.getValue(), msgType)) { if (StringUtils.equals(ShowType.TEXT.getValue(), msgType)) {
jsonResult = FeiShuConstants.FEI_SHU_TEXT_TEMPLATE.replace(MSG_TYPE_REGX, msgType) jsonResult = FeiShuConstants.FEI_SHU_TEXT_TEMPLATE.replace(MSG_TYPE_REGX, msgType)
.replace(MSG_RESULT_REGX, contentResult).replace(FEI_SHU_USER_REGX, userIdsToText).replaceAll("/n", "\\\\n"); .replace(MSG_RESULT_REGX, contentResult).replace(FEI_SHU_USER_REGX, userIdsToText).replaceAll("/n", "\\\\n");
}else { } else {
jsonResult = FeiShuConstants.FEI_SHU_POST_TEMPLATE.replace(MSG_TYPE_REGX, msgType) jsonResult = FeiShuConstants.FEI_SHU_POST_TEMPLATE.replace(MSG_TYPE_REGX, msgType)
.replace(FEI_SHU_MSG_TYPE_REGX, keyword).replace(MSG_RESULT_REGX, contentResult) .replace(FEI_SHU_MSG_TYPE_REGX, keyword).replace(MSG_RESULT_REGX, contentResult)
.replace(FEI_SHU_USER_REGX, userIdsToText).replaceAll("/n", "\\\\n"); .replace(FEI_SHU_USER_REGX, userIdsToText).replaceAll("/n", "\\\\n");
...@@ -102,8 +107,8 @@ public final class FeiShuSender { ...@@ -102,8 +107,8 @@ public final class FeiShuSender {
return jsonResult; return jsonResult;
} }
private String mkUserIds(String users){ private String mkUserIds(String users) {
String userIdsToText=""; String userIdsToText = "";
String[] userList = users.split(","); String[] userList = users.split(",");
if (msgType.equals(ShowType.TEXT.getValue())) { if (msgType.equals(ShowType.TEXT.getValue())) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -111,12 +116,12 @@ public final class FeiShuSender { ...@@ -111,12 +116,12 @@ public final class FeiShuSender {
sb.append("<at user_id=\\\"").append(user).append("\\\"></at>"); sb.append("<at user_id=\\\"").append(user).append("\\\"></at>");
} }
userIdsToText = sb.toString(); userIdsToText = sb.toString();
}else{ } else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String user : userList) { for (String user : userList) {
sb.append("{\"tag\":\"at\",\"user_id\":\"").append(user).append("\"},"); sb.append("{\"tag\":\"at\",\"user_id\":\"").append(user).append("\"},");
} }
sb.deleteCharAt(sb.length()-1); sb.deleteCharAt(sb.length() - 1);
userIdsToText = sb.toString(); userIdsToText = sb.toString();
} }
return userIdsToText; return userIdsToText;
...@@ -154,18 +159,17 @@ public final class FeiShuSender { ...@@ -154,18 +159,17 @@ public final class FeiShuSender {
logger.error("itemsList is null"); logger.error("itemsList is null");
throw new RuntimeException("itemsList is null"); throw new RuntimeException("itemsList is null");
} }
StringBuilder contents = new StringBuilder(100); StringBuilder contents = new StringBuilder(100);
contents.append(String.format("`%s` %s",title,FeiShuConstants.MARKDOWN_ENTER)); contents.append(String.format("`%s` %s",title,FeiShuConstants.MARKDOWN_ENTER));
for (LinkedHashMap mapItems : mapSendResultItemsList) { for (LinkedHashMap mapItems : mapSendResultItemsList) {
Set<Entry<String, Object>> entries = mapItems.entrySet(); Set<Entry<String, Object>> entries = mapItems.entrySet();
Iterator<Entry<String, Object>> iterator = entries.iterator(); Iterator<Entry<String, Object>> iterator = entries.iterator();
while (iterator.hasNext()) { { while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next(); Map.Entry<String, Object> entry = iterator.next();
String key = entry.getKey(); String key = entry.getKey();
String value = entry.getValue().toString(); String value = entry.getValue().toString();
contents.append(FeiShuConstants.MARKDOWN_QUOTE); contents.append(FeiShuConstants.MARKDOWN_QUOTE);
contents.append(key + ":" + value).append(FeiShuConstants.MARKDOWN_ENTER); contents.append(key + ":" + value).append(FeiShuConstants.MARKDOWN_ENTER);
}
} }
return contents.toString(); return contents.toString();
} }
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.feishu; package com.dlink.alert.feishu;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
......
...@@ -17,19 +17,19 @@ ...@@ -17,19 +17,19 @@
* *
*/ */
package com.dlink.alert.feishu; package com.dlink.alert.feishu;
import com.dlink.alert.AlertMsg; import com.dlink.alert.AlertMsg;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/** /**
* @Author: zhumingye * @Author: zhumingye
* @date: 2022/4/2 * @date: 2022/4/2
...@@ -54,12 +54,11 @@ public class FeiShuSenderTest { ...@@ -54,12 +54,11 @@ public class FeiShuSenderTest {
alertMsg.setJobStartTime("2018-08-06 10:31:34.0"); alertMsg.setJobStartTime("2018-08-06 10:31:34.0");
alertMsg.setJobEndTime("2018-08-06 10:31:49.0"); alertMsg.setJobEndTime("2018-08-06 10:31:49.0");
alertMsg.setJobDuration("23 Seconds"); alertMsg.setJobDuration("23 Seconds");
String linkUrl = "[跳转至该任务的FlinkWeb](http://cdh1:8081/#/job/"+uuid+"/overview)"; String linkUrl = "[跳转至该任务的FlinkWeb](http://cdh1:8081/#/job/" + uuid + "/overview)";
alertMsg.setLinkUrl(linkUrl); alertMsg.setLinkUrl(linkUrl);
String exceptionUrl = "[点击查看该任务的异常日志](http://cdh1:8081/#/job/"+uuid+"/exceptions)"; String exceptionUrl = "[点击查看该任务的异常日志](http://cdh1:8081/#/job/" + uuid + "/exceptions)";
alertMsg.setExceptionUrl(exceptionUrl); alertMsg.setExceptionUrl(exceptionUrl);
feiShuConfig.put(FeiShuConstants.WEB_HOOK, "https://open.feishu.cn/open-apis/bot/v2/hook/aea3cd7f-75b4-45cd-abea-2c0dc808f2a9"); feiShuConfig.put(FeiShuConstants.WEB_HOOK, "https://open.feishu.cn/open-apis/bot/v2/hook/aea3cd7f-75b4-45cd-abea-2c0dc808f2a9");
feiShuConfig.put(FeiShuConstants.KEY_WORD, "Dinky 飞书WebHook 告警测试"); feiShuConfig.put(FeiShuConstants.KEY_WORD, "Dinky 飞书WebHook 告警测试");
feiShuConfig.put(FeiShuConstants.MSG_TYPE,"text"); feiShuConfig.put(FeiShuConstants.MSG_TYPE,"text");
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.wechat; package com.dlink.alert.wechat;
import com.dlink.alert.AbstractAlert; import com.dlink.alert.AbstractAlert;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.wechat; package com.dlink.alert.wechat;
/** /**
......
...@@ -17,14 +17,16 @@ ...@@ -17,14 +17,16 @@
* *
*/ */
package com.dlink.alert.wechat; package com.dlink.alert.wechat;
import static java.util.Objects.requireNonNull;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.alert.AlertSendResponse; import com.dlink.alert.AlertSendResponse;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
import com.dlink.assertion.Asserts; import com.dlink.assertion.Asserts;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
...@@ -33,13 +35,19 @@ import org.apache.http.entity.StringEntity; ...@@ -33,13 +35,19 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Objects.requireNonNull; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* WeChatSender * WeChatSender
...@@ -65,53 +73,54 @@ public class WeChatSender { ...@@ -65,53 +73,54 @@ public class WeChatSender {
private final String sendType; private final String sendType;
private static String showType; private static String showType;
private final String webhookUrl; private final String webhookUrl;
private final String KeyWord ; private final String keyWord;
private final Boolean atAll; private final Boolean atAll;
WeChatSender(Map<String, String> config) { WeChatSender(Map<String, String> config) {
sendType = config.get(WeChatConstants.SEND_TYPE); sendType = config.get(WeChatConstants.SEND_TYPE);
weChatAgentId =sendType.equals(WeChatType.CHAT.getValue())? "" : config.get(WeChatConstants.AGENT_ID); weChatAgentId = sendType.equals(WeChatType.CHAT.getValue()) ? "" : config.get(WeChatConstants.AGENT_ID);
atAll = Boolean.valueOf(config.get(WeChatConstants.AT_ALL)); atAll = Boolean.valueOf(config.get(WeChatConstants.AT_ALL));
weChatUsers =sendType.equals(WeChatType.CHAT.getValue())?( atAll && config.get(WeChatConstants.USERS) == null ? "": config.get(WeChatConstants.USERS)): config.get(WeChatConstants.USERS); weChatUsers = sendType.equals(WeChatType.CHAT.getValue()) ? (atAll && config.get(WeChatConstants.USERS) == null ? "" : config.get(WeChatConstants.USERS)) : config.get(WeChatConstants.USERS);
String weChatCorpId = sendType.equals(WeChatType.CHAT.getValue())? "" :config.get(WeChatConstants.CORP_ID); String weChatCorpId = sendType.equals(WeChatType.CHAT.getValue()) ? "" : config.get(WeChatConstants.CORP_ID);
String weChatSecret = sendType.equals(WeChatType.CHAT.getValue())? "" :config.get(WeChatConstants.SECRET); String weChatSecret = sendType.equals(WeChatType.CHAT.getValue()) ? "" : config.get(WeChatConstants.SECRET);
String weChatTokenUrl =sendType.equals(WeChatType.CHAT.getValue())? "" : WeChatConstants.TOKEN_URL; String weChatTokenUrl = sendType.equals(WeChatType.CHAT.getValue()) ? "" : WeChatConstants.TOKEN_URL;
weChatUserSendMsg = WeChatConstants.USER_SEND_MSG; weChatUserSendMsg = WeChatConstants.USER_SEND_MSG;
showType = config.get(WeChatConstants.SHOW_TYPE); showType = config.get(WeChatConstants.SHOW_TYPE);
requireNonNull(showType, WeChatConstants.SHOW_TYPE + " must not null"); requireNonNull(showType, WeChatConstants.SHOW_TYPE + " must not null");
webhookUrl= config.get(WeChatConstants.WEBHOOK); webhookUrl = config.get(WeChatConstants.WEBHOOK);
KeyWord = config.get(WeChatConstants.KEYWORD); keyWord = config.get(WeChatConstants.KEYWORD);
if (sendType.equals(WeChatType.CHAT.getValue())) requireNonNull(webhookUrl, WeChatConstants.WEBHOOK + " must not null"); if (sendType.equals(WeChatType.CHAT.getValue())) {
requireNonNull(webhookUrl, WeChatConstants.WEBHOOK + " must not null");
}
weChatTokenUrlReplace = weChatTokenUrl weChatTokenUrlReplace = weChatTokenUrl
.replace(CORP_ID_REGEX, weChatCorpId) .replace(CORP_ID_REGEX, weChatCorpId)
.replace(SECRET_REGEX, weChatSecret); .replace(SECRET_REGEX, weChatSecret);
weChatToken = getToken(); weChatToken = getToken();
} }
public AlertResult send(String title, String content) { public AlertResult send(String title, String content) {
AlertResult alertResult = new AlertResult(); AlertResult alertResult = new AlertResult();
List<String> userList = new ArrayList<>(); List<String> userList = new ArrayList<>();
if (Asserts.isNotNullString(weChatUsers)) { if (Asserts.isNotNullString(weChatUsers)) {
userList = Arrays.asList(weChatUsers.split(",")); userList = Arrays.asList(weChatUsers.split(","));
} }
if(atAll){ if (atAll) {
userList.add("所有人"); userList.add("所有人");
} }
String data =""; String data = "";
if (sendType.equals(WeChatType.CHAT.getValue())) { if (sendType.equals(WeChatType.CHAT.getValue())) {
data = markdownByAlert(title, content ,userList);; data = markdownByAlert(title, content, userList);
}else{ } else {
data = markdownByAlert(title, content, userList); data = markdownByAlert(title, content, userList);
} }
String msg =""; String msg = "";
if (sendType.equals(WeChatType.APP.getValue())) { if (sendType.equals(WeChatType.APP.getValue())) {
msg= weChatUserSendMsg.replace(USER_REG_EXP, mkUserString(userList)) msg = weChatUserSendMsg.replace(USER_REG_EXP, mkUserString(userList))
.replace(AGENT_ID_REG_EXP, weChatAgentId).replace(MSG_REG_EXP, data) .replace(AGENT_ID_REG_EXP, weChatAgentId).replace(MSG_REG_EXP, data)
.replace(SHOW_TYPE_REGEX, showType); .replace(SHOW_TYPE_REGEX, showType);
}else{ } else {
msg= WeChatConstants.WEBHOOK_TEMPLATE.replace(SHOW_TYPE_REGEX, showType) msg = WeChatConstants.WEBHOOK_TEMPLATE.replace(SHOW_TYPE_REGEX, showType)
.replace(MSG_REG_EXP, data); .replace(MSG_REG_EXP, data);
} }
...@@ -149,12 +158,11 @@ public class WeChatSender { ...@@ -149,12 +158,11 @@ public class WeChatSender {
} finally { } finally {
response.close(); response.close();
} }
// logger.info("Enterprise WeChat send [{}], param:{}, resp:{}", url, data, resp); // logger.info("Enterprise WeChat send [{}], param:{}, resp:{}", url, data, resp);
return resp; return resp;
} }
} }
private static String mkUserList(Iterable<String> list) { private static String mkUserList(Iterable<String> list) {
StringBuilder sb = new StringBuilder("["); StringBuilder sb = new StringBuilder("[");
for (String name : list) { for (String name : list) {
...@@ -185,18 +193,18 @@ public class WeChatSender { ...@@ -185,18 +193,18 @@ public class WeChatSender {
/** /**
*@Author: zhumingye *@Author: zhumingye
*@date: 2022/3/26 *@date: 2022/3/26
*@Description: 将用户列表转换为<@用户名> </@用户名>的格式 *@Description: 将用户列表转换为 <@用户名> 的格式
* @param userList * @param userList
* @return java.lang.String * @return java.lang.String
*/ */
private static String mkMarkDownAtUsers(List<String> userList){ private static String mkMarkDownAtUsers(List<String> userList) {
StringBuilder builder = new StringBuilder("\n"); StringBuilder builder = new StringBuilder("\n");
if (Asserts.isNotNull(userList)) { if (Asserts.isNotNull(userList)) {
userList.forEach(value -> { userList.forEach(value -> {
if (value.equals("所有人") && showType.equals(ShowType.TEXT.getValue())) { if (value.equals("所有人") && showType.equals(ShowType.TEXT.getValue())) {
builder.append("@所有人 "); builder.append("@所有人 ");
}else{ } else {
builder.append("<@").append(value).append("> "); builder.append("<@").append(value).append("> ");
} }
}); });
...@@ -218,12 +226,10 @@ public class WeChatSender { ...@@ -218,12 +226,10 @@ public class WeChatSender {
return getMsgResult(title, content,userList,sendType); return getMsgResult(title, content,userList,sendType);
} }
private static String markdownText(String title, String content, List<String> userList, String sendType) { private static String markdownText(String title, String content, List<String> userList, String sendType) {
return getMsgResult(title, content,userList,sendType); return getMsgResult(title, content,userList,sendType);
} }
/** /**
*@Author: zhumingye *@Author: zhumingye
*@date: 2022/3/25 *@date: 2022/3/25
...@@ -263,8 +269,6 @@ public class WeChatSender { ...@@ -263,8 +269,6 @@ public class WeChatSender {
return contents.toString(); return contents.toString();
} }
private String getToken() { private String getToken() {
try { try {
return get(weChatTokenUrlReplace); return get(weChatTokenUrlReplace);
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
* *
*/ */
package com.dlink.alert.wechat; package com.dlink.alert.wechat;
/** /**
......
...@@ -17,20 +17,19 @@ ...@@ -17,20 +17,19 @@
* *
*/ */
package com.dlink.alert.wechat; package com.dlink.alert.wechat;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType; import com.dlink.alert.ShowType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/** /**
* WeChatSenderTest * WeChatSenderTest
*/ */
...@@ -79,7 +78,6 @@ public class WeChatSenderTest { ...@@ -79,7 +78,6 @@ public class WeChatSenderTest {
} }
@Test @Test
public void testSendAPPMarkDownMsg() { public void testSendAPPMarkDownMsg() {
WeChatSender weChatSender = new WeChatSender(weChatConfig); WeChatSender weChatSender = new WeChatSender(weChatConfig);
...@@ -107,7 +105,6 @@ public class WeChatSenderTest { ...@@ -107,7 +105,6 @@ public class WeChatSenderTest {
Assert.assertEquals(true, alertResult.getSuccess()); Assert.assertEquals(true, alertResult.getSuccess());
} }
@Test @Test
public void testChatTextMsg() throws IOException { public void testChatTextMsg() throws IOException {
weChatConfig.put(WeChatConstants.WEBHOOK, "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=822d17a1-d6e5-43c2-a566-4846fe13396c"); weChatConfig.put(WeChatConstants.WEBHOOK, "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=822d17a1-d6e5-43c2-a566-4846fe13396c");
......
Markdown is supported
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