Commit c7751365 authored by wenmo's avatar wenmo

alert 代码美化

parent eabb360b
package com.dlink.alert; package com.dlink.alert;
import com.dlink.assertion.Asserts; import com.dlink.assertion.Asserts;
import com.dlink.exception.MetaDataException;
import sun.misc.Service; import sun.misc.Service;
import java.util.Iterator; import java.util.Iterator;
import java.util.Optional; import java.util.Optional;
import java.util.ServiceLoader;
/** /**
* Alert * Alert
...@@ -44,8 +42,8 @@ public interface Alert { ...@@ -44,8 +42,8 @@ public interface Alert {
Alert setConfig(AlertConfig config); Alert setConfig(AlertConfig config);
default boolean canHandle(String type){ default boolean canHandle(String type) {
return Asserts.isEqualsIgnoreCase(getType(),type); return Asserts.isEqualsIgnoreCase(getType(), type);
} }
String getType(); String getType();
......
...@@ -23,7 +23,7 @@ public class AlertResult { ...@@ -23,7 +23,7 @@ public class AlertResult {
} }
public Integer getSuccessCode() { public Integer getSuccessCode() {
return success?1:0; return success ? 1 : 0;
} }
public void setSuccess(boolean success) { public void setSuccess(boolean success) {
......
...@@ -86,7 +86,7 @@ public class DingTalkSender { ...@@ -86,7 +86,7 @@ public class DingTalkSender {
private String sendMsg(String title, String content) throws IOException { private String sendMsg(String title, String content) throws IOException {
String msg = generateMsgJson(title, content); String msg = generateMsgJson(title, content);
String httpUrl = url; String httpUrl = url;
if(Asserts.isNotNullString(secret)){ if (Asserts.isNotNullString(secret)) {
httpUrl = generateSignedUrl(); httpUrl = generateSignedUrl();
} }
HttpPost httpPost = new HttpPost(httpUrl); HttpPost httpPost = new HttpPost(httpUrl);
...@@ -175,6 +175,7 @@ public class DingTalkSender { ...@@ -175,6 +175,7 @@ public class DingTalkSender {
/** /**
* 公共生成 markdown 和 text 消息 * 公共生成 markdown 和 text 消息
*
* @param title 标题 * @param title 标题
* @param content 内容 * @param content 内容
* @param builder 拼接字符串 * @param builder 拼接字符串
...@@ -213,7 +214,7 @@ public class DingTalkSender { ...@@ -213,7 +214,7 @@ public class DingTalkSender {
Mac mac = Mac.getInstance("HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8"); sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
} catch (Exception e) { } catch (Exception e) {
logger.error("generate sign error, message:{}", e); logger.error("generate sign error, message:{}", e);
} }
......
...@@ -39,6 +39,7 @@ public class DingTalkTest { ...@@ -39,6 +39,7 @@ public class DingTalkTest {
"\"Host\": \"192.168.xx.xx\"," "\"Host\": \"192.168.xx.xx\","
+ +
"\"Notify group\" :\"4\"}]"; "\"Notify group\" :\"4\"}]";
@Before @Before
public void initDingTalkConfig() { public void initDingTalkConfig() {
...@@ -53,7 +54,7 @@ public class DingTalkTest { ...@@ -53,7 +54,7 @@ public class DingTalkTest {
} }
@Test @Test
public void sendMarkDownMsgTest(){ public void sendMarkDownMsgTest() {
AlertConfig config = AlertConfig.build("MarkDownTest", "DingTalk", DingTalkTest.config); AlertConfig config = AlertConfig.build("MarkDownTest", "DingTalk", DingTalkTest.config);
Alert alert = Alert.build(config); Alert alert = Alert.build(config);
AlertResult result = alert.send("Dlinky钉钉告警测试", contentTest); AlertResult result = alert.send("Dlinky钉钉告警测试", contentTest);
...@@ -61,7 +62,7 @@ public class DingTalkTest { ...@@ -61,7 +62,7 @@ public class DingTalkTest {
} }
@Test @Test
public void sendTextMsgTest(){ public void sendTextMsgTest() {
config.put(DingTalkConstants.MSG_TYPE, ShowType.TEXT.getValue()); config.put(DingTalkConstants.MSG_TYPE, ShowType.TEXT.getValue());
AlertConfig config = AlertConfig.build("TextMsgTest", "DingTalk", DingTalkTest.config); AlertConfig config = AlertConfig.build("TextMsgTest", "DingTalk", DingTalkTest.config);
Alert alert = Alert.build(config); Alert alert = Alert.build(config);
......
...@@ -72,7 +72,7 @@ public class WeChatSender { ...@@ -72,7 +72,7 @@ public class WeChatSender {
String data = markdownByAlert(title, content); String data = markdownByAlert(title, content);
String msg = weChatUserSendMsg.replace(USER_REG_EXP, mkString(userList)) String msg = weChatUserSendMsg.replace(USER_REG_EXP, mkString(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);
if (Asserts.isNullString(weChatToken)) { if (Asserts.isNullString(weChatToken)) {
alertResult.setMessage("send we chat alert fail,get weChat token error"); alertResult.setMessage("send we chat alert fail,get weChat token error");
alertResult.setSuccess(false); alertResult.setSuccess(false);
......
...@@ -84,7 +84,7 @@ public class WeChatSenderTest { ...@@ -84,7 +84,7 @@ public class WeChatSenderTest {
@Test @Test
public void testSendWeChatTextMsg() { public void testSendWeChatTextMsg() {
weChatConfig.put(WeChatConstants.SHOW_TYPE,ShowType.TEXT.getValue()); weChatConfig.put(WeChatConstants.SHOW_TYPE, ShowType.TEXT.getValue());
WeChatSender weChatSender = new WeChatSender(weChatConfig); WeChatSender weChatSender = new WeChatSender(weChatConfig);
AlertResult alertResult = weChatSender.send("TEXT-TEST", contentTest); AlertResult alertResult = weChatSender.send("TEXT-TEST", contentTest);
Assert.assertEquals(true, alertResult.getSuccess()); Assert.assertEquals(true, alertResult.getSuccess());
......
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