mirror of
https://gitee.com/dolphinscheduler/DolphinScheduler.git
synced 2024-11-29 18:58:05 +08:00
[Improvement] Fix alert code smell
This commit is contained in:
parent
e9d85914d7
commit
59f060e278
@ -48,6 +48,8 @@ import java.util.Objects;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
@ -189,7 +191,7 @@ public final class DingTalkSender {
|
||||
String resp;
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
resp = EntityUtils.toString(entity, "UTF-8");
|
||||
resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
EntityUtils.consume(entity);
|
||||
} finally {
|
||||
response.close();
|
||||
@ -317,15 +319,17 @@ public final class DingTalkSender {
|
||||
String sign = org.apache.commons.lang3.StringUtils.EMPTY;
|
||||
try {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
|
||||
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
|
||||
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
|
||||
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
|
||||
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), StandardCharsets.UTF_8.name());
|
||||
} catch (Exception e) {
|
||||
log.error("generate sign error, message:{}", e);
|
||||
}
|
||||
return url + "×tamp=" + timestamp + "&sign=" + sign;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
static final class DingTalkSendMsgResponse {
|
||||
|
||||
private Integer errcode;
|
||||
@ -334,22 +338,6 @@ public final class DingTalkSender {
|
||||
public DingTalkSendMsgResponse() {
|
||||
}
|
||||
|
||||
public Integer getErrcode() {
|
||||
return this.errcode;
|
||||
}
|
||||
|
||||
public void setErrcode(Integer errcode) {
|
||||
this.errcode = errcode;
|
||||
}
|
||||
|
||||
public String getErrmsg() {
|
||||
return this.errmsg;
|
||||
}
|
||||
|
||||
public void setErrmsg(String errmsg) {
|
||||
this.errmsg = errmsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) {
|
||||
|
@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.plugin.alert.dingtalk;
|
||||
|
||||
import org.apache.dolphinscheduler.alert.api.AlertResult;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -47,7 +48,7 @@ public class DingTalkSenderTest {
|
||||
@Test
|
||||
public void testSend() {
|
||||
DingTalkSender dingTalkSender = new DingTalkSender(dingTalkConfig);
|
||||
dingTalkSender.sendDingTalkMsg("keyWord+Welcome", "UTF-8");
|
||||
dingTalkSender.sendDingTalkMsg("keyWord+Welcome", StandardCharsets.UTF_8.name());
|
||||
dingTalkConfig.put(DingTalkParamsConstants.NAME_DING_TALK_PROXY_ENABLE, "true");
|
||||
dingTalkSender = new DingTalkSender(dingTalkConfig);
|
||||
AlertResult alertResult = dingTalkSender.sendDingTalkMsg("title", "content test");
|
||||
|
@ -56,8 +56,6 @@ public final class EmailConstants {
|
||||
|
||||
public static final String TABLE_BODY_HTML_TAIL = "</table></body></html>";
|
||||
|
||||
public static final String UTF_8 = "UTF-8";
|
||||
|
||||
public static final String EXCEL_SUFFIX_XLSX = ".xlsx";
|
||||
|
||||
public static final String SINGLE_SLASH = "/";
|
||||
|
@ -34,6 +34,7 @@ import org.apache.commons.mail.HtmlEmail;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -171,7 +172,7 @@ public final class MailSender {
|
||||
Session session = getSession();
|
||||
email.setMailSession(session);
|
||||
email.setFrom(mailSenderEmail);
|
||||
email.setCharset(EmailConstants.UTF_8);
|
||||
email.setCharset(StandardCharsets.UTF_8.name());
|
||||
if (CollectionUtils.isNotEmpty(receivers)) {
|
||||
// receivers mail
|
||||
for (String receiver : receivers) {
|
||||
@ -344,7 +345,8 @@ public final class MailSender {
|
||||
ExcelUtils.genExcelFile(content, randomFilename, xlsFilePath);
|
||||
|
||||
part2.attachFile(file);
|
||||
part2.setFileName(MimeUtility.encodeText(title + EmailConstants.EXCEL_SUFFIX_XLSX, EmailConstants.UTF_8, "B"));
|
||||
part2.setFileName(
|
||||
MimeUtility.encodeText(title + EmailConstants.EXCEL_SUFFIX_XLSX, StandardCharsets.UTF_8.name(), "B"));
|
||||
// add components to collection
|
||||
partList.addBodyPart(part1);
|
||||
partList.addBodyPart(part2);
|
||||
|
@ -66,8 +66,15 @@ public class ExcelUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGenExcelFileByCheckDir() {
|
||||
ExcelUtils.genExcelFile("[{\"a\": \"a\"},{\"a\": \"a\"}]", "t", "/tmp/xls");
|
||||
File file = new File("/tmp/xls" + EmailConstants.SINGLE_SLASH + "t" + EmailConstants.EXCEL_SUFFIX_XLSX);
|
||||
String path = "/tmp/xls";
|
||||
ExcelUtils.genExcelFile("[{\"a\": \"a\"},{\"a\": \"a\"}]", "t", path);
|
||||
File file =
|
||||
new File(
|
||||
path
|
||||
+ EmailConstants.SINGLE_SLASH
|
||||
+ "t"
|
||||
+ EmailConstants.EXCEL_SUFFIX_XLSX);
|
||||
file.delete();
|
||||
Assertions.assertFalse(file.exists());
|
||||
}
|
||||
}
|
||||
|
@ -18,10 +18,9 @@
|
||||
package org.apache.dolphinscheduler.plugin.alert.email;
|
||||
|
||||
import org.apache.dolphinscheduler.alert.api.AlertConstants;
|
||||
import org.apache.dolphinscheduler.alert.api.AlertResult;
|
||||
import org.apache.dolphinscheduler.alert.api.ShowType;
|
||||
import org.apache.dolphinscheduler.common.utils.JSONUtils;
|
||||
import org.apache.dolphinscheduler.plugin.alert.email.template.AlertTemplate;
|
||||
import org.apache.dolphinscheduler.plugin.alert.email.template.DefaultHTMLTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -42,7 +41,6 @@ public class MailUtilsTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MailUtilsTest.class);
|
||||
static MailSender mailSender;
|
||||
private static Map<String, String> emailConfig = new HashMap<>();
|
||||
private static AlertTemplate alertTemplate;
|
||||
|
||||
@BeforeAll
|
||||
public static void initEmailConfig() {
|
||||
@ -59,7 +57,6 @@ public class MailUtilsTest {
|
||||
emailConfig.put(MailParamsConstants.NAME_PLUGIN_DEFAULT_EMAIL_RECEIVERS, "347801120@qq.com");
|
||||
emailConfig.put(MailParamsConstants.NAME_PLUGIN_DEFAULT_EMAIL_RECEIVERCCS, "347801120@qq.com");
|
||||
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TEXT.getDescp());
|
||||
alertTemplate = new DefaultHTMLTemplate();
|
||||
mailSender = new MailSender(emailConfig);
|
||||
}
|
||||
|
||||
@ -77,9 +74,10 @@ public class MailUtilsTest {
|
||||
+ "\"Host: 192.168.xx.xx\","
|
||||
+ "\"Notify group :4\"]";
|
||||
|
||||
mailSender.sendMails(
|
||||
AlertResult alertResult = mailSender.sendMails(
|
||||
"Mysql Exception",
|
||||
content);
|
||||
Assertions.assertEquals("false", alertResult.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -108,7 +106,8 @@ public class MailUtilsTest {
|
||||
emailConfig.put(MailParamsConstants.NAME_MAIL_USER, "user");
|
||||
emailConfig.put(MailParamsConstants.NAME_MAIL_PASSWD, "passwd");
|
||||
mailSender = new MailSender(emailConfig);
|
||||
mailSender.sendMails(title, content);
|
||||
AlertResult alertResult = mailSender.sendMails(title, content);
|
||||
Assertions.assertEquals("false", alertResult.getStatus());
|
||||
}
|
||||
|
||||
public String list2String() {
|
||||
@ -134,7 +133,6 @@ public class MailUtilsTest {
|
||||
logger.info(mapjson);
|
||||
|
||||
return mapjson;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -143,7 +141,8 @@ public class MailUtilsTest {
|
||||
String content = list2String();
|
||||
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE.getDescp());
|
||||
mailSender = new MailSender(emailConfig);
|
||||
mailSender.sendMails(title, content);
|
||||
AlertResult alertResult = mailSender.sendMails(title, content);
|
||||
Assertions.assertEquals("false", alertResult.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -151,7 +150,8 @@ public class MailUtilsTest {
|
||||
String content = list2String();
|
||||
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.ATTACHMENT.getDescp());
|
||||
mailSender = new MailSender(emailConfig);
|
||||
mailSender.sendMails("gaojing", content);
|
||||
AlertResult alertResult = mailSender.sendMails("gaojing", content);
|
||||
Assertions.assertEquals("false", alertResult.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -159,7 +159,7 @@ public class MailUtilsTest {
|
||||
String content = list2String();
|
||||
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE_ATTACHMENT.getDescp());
|
||||
mailSender = new MailSender(emailConfig);
|
||||
mailSender.sendMails("gaojing", content);
|
||||
AlertResult alertResult = mailSender.sendMails("gaojing", content);
|
||||
Assertions.assertEquals("false", alertResult.getStatus());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -161,7 +162,7 @@ public final class FeiShuSender {
|
||||
String resp;
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
resp = EntityUtils.toString(entity, "utf-8");
|
||||
resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
EntityUtils.consume(entity);
|
||||
} finally {
|
||||
response.close();
|
||||
|
@ -39,6 +39,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -58,7 +59,6 @@ public final class HttpSender {
|
||||
* request type get
|
||||
*/
|
||||
private static final String REQUEST_TYPE_GET = "GET";
|
||||
private static final String DEFAULT_CHARSET = "utf-8";
|
||||
private final String headerParams;
|
||||
private final String bodyParams;
|
||||
private final String contentField;
|
||||
@ -124,7 +124,7 @@ public final class HttpSender {
|
||||
|
||||
CloseableHttpResponse response = httpClient.execute(httpRequest);
|
||||
HttpEntity entity = response.getEntity();
|
||||
return EntityUtils.toString(entity, DEFAULT_CHARSET);
|
||||
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void createHttpRequest(String msg) throws MalformedURLException, URISyntaxException {
|
||||
@ -157,7 +157,8 @@ public final class HttpSender {
|
||||
type = URL_SPLICE_CHAR;
|
||||
}
|
||||
try {
|
||||
url = String.format("%s%s%s=%s", url, type, contentField, URLEncoder.encode(msg, DEFAULT_CHARSET));
|
||||
url = String.format("%s%s%s=%s", url, type, contentField,
|
||||
URLEncoder.encode(msg, StandardCharsets.UTF_8.name()));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -190,7 +191,7 @@ public final class HttpSender {
|
||||
}
|
||||
// set msg content field
|
||||
objectNode.put(contentField, msg);
|
||||
StringEntity entity = new StringEntity(JSONUtils.toJsonString(objectNode), DEFAULT_CHARSET);
|
||||
StringEntity entity = new StringEntity(JSONUtils.toJsonString(objectNode), StandardCharsets.UTF_8);
|
||||
((HttpPost) httpRequest).setEntity(entity);
|
||||
} catch (Exception e) {
|
||||
log.error("send http alert msg exception : {}", e.getMessage());
|
||||
|
@ -34,6 +34,7 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
@ -90,7 +91,7 @@ public class PrometheusAlertSender {
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
resp = EntityUtils.toString(entity, "utf-8");
|
||||
resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
EntityUtils.consume(entity);
|
||||
log.error(
|
||||
"Prometheus alert manager send alert failed, http status code: {}, title: {} ,content: {}, resp: {}",
|
||||
|
@ -33,6 +33,7 @@ public class ProcessUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testExecuteScript() {
|
||||
ProcessUtils.executeScript(cmd);
|
||||
int code = ProcessUtils.executeScript(cmd);
|
||||
assert code != -1;
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -81,11 +82,11 @@ public final class SlackSender {
|
||||
}
|
||||
|
||||
HttpPost httpPost = new HttpPost(webHookUrl);
|
||||
httpPost.setEntity(new StringEntity(JSONUtils.toJsonString(paramMap), "UTF-8"));
|
||||
httpPost.setEntity(new StringEntity(JSONUtils.toJsonString(paramMap), StandardCharsets.UTF_8));
|
||||
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
return EntityUtils.toString(entity, "UTF-8");
|
||||
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
log.error("Send message to slack error.", e);
|
||||
return "System Exception";
|
||||
|
@ -153,7 +153,7 @@ public final class TelegramSender {
|
||||
String resp;
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
resp = EntityUtils.toString(entity, "UTF-8");
|
||||
resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
EntityUtils.consume(entity);
|
||||
} finally {
|
||||
response.close();
|
||||
|
@ -23,8 +23,6 @@ public final class WeChatAlertConstants {
|
||||
|
||||
static final String MARKDOWN_ENTER = "\n";
|
||||
|
||||
static final String CHARSET = "UTF-8";
|
||||
|
||||
static final String WE_CHAT_PUSH_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}";
|
||||
|
||||
static final String WE_CHAT_APP_CHAT_PUSH_URL = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token" +
|
||||
|
@ -38,6 +38,7 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@ -45,6 +46,8 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@ -85,12 +88,12 @@ public final class WeChatSender {
|
||||
CloseableHttpClient httpClient =
|
||||
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build()) {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setEntity(new StringEntity(data, WeChatAlertConstants.CHARSET));
|
||||
httpPost.setEntity(new StringEntity(data, StandardCharsets.UTF_8));
|
||||
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||||
String resp;
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
resp = EntityUtils.toString(entity, WeChatAlertConstants.CHARSET);
|
||||
resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
EntityUtils.consume(entity);
|
||||
} finally {
|
||||
response.close();
|
||||
@ -142,7 +145,7 @@ public final class WeChatSender {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
resp = EntityUtils.toString(entity, WeChatAlertConstants.CHARSET);
|
||||
resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
EntityUtils.consume(entity);
|
||||
}
|
||||
|
||||
@ -259,6 +262,8 @@ public final class WeChatSender {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
static final class WeChatSendMsgResponse {
|
||||
|
||||
private Integer errcode;
|
||||
@ -267,22 +272,6 @@ public final class WeChatSender {
|
||||
public WeChatSendMsgResponse() {
|
||||
}
|
||||
|
||||
public Integer getErrcode() {
|
||||
return this.errcode;
|
||||
}
|
||||
|
||||
public void setErrcode(Integer errcode) {
|
||||
this.errcode = errcode;
|
||||
}
|
||||
|
||||
public String getErrmsg() {
|
||||
return this.errmsg;
|
||||
}
|
||||
|
||||
public void setErrmsg(String errmsg) {
|
||||
this.errmsg = errmsg;
|
||||
}
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
|
@ -242,11 +242,6 @@ public final class Constants {
|
||||
*/
|
||||
public static final String HTTP_X_REAL_IP = "X-Real-IP";
|
||||
|
||||
/**
|
||||
* UTF-8
|
||||
*/
|
||||
public static final String UTF_8 = "UTF-8";
|
||||
|
||||
/**
|
||||
* user name regex
|
||||
*/
|
||||
|
@ -22,7 +22,6 @@ import static org.apache.dolphinscheduler.common.constants.Constants.FOLDER_SEPA
|
||||
import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_S_S;
|
||||
import static org.apache.dolphinscheduler.common.constants.Constants.RESOURCE_VIEW_SUFFIXES;
|
||||
import static org.apache.dolphinscheduler.common.constants.Constants.RESOURCE_VIEW_SUFFIXES_DEFAULT_VALUE;
|
||||
import static org.apache.dolphinscheduler.common.constants.Constants.UTF_8;
|
||||
import static org.apache.dolphinscheduler.common.constants.DateConstants.YYYYMMDDHHMMSS;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@ -207,7 +206,7 @@ public class FileUtils {
|
||||
while ((length = inputStream.read(buffer)) != -1) {
|
||||
output.write(buffer, 0, length);
|
||||
}
|
||||
return output.toString(UTF_8);
|
||||
return output.toString(StandardCharsets.UTF_8.name());
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
|
@ -36,6 +36,7 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -143,7 +144,7 @@ public class HttpUtils {
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
return entity != null ? EntityUtils.toString(entity, Constants.UTF_8) : null;
|
||||
return entity != null ? EntityUtils.toString(entity, StandardCharsets.UTF_8) : null;
|
||||
} catch (IOException e) {
|
||||
log.error("Error executing HTTP GET request", e);
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user