-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: zhengqiwei <[email protected]> Co-authored-by: tomsun28 <[email protected]>
- Loading branch information
1 parent
b24afb7
commit 43dd5f8
Showing
3 changed files
with
42 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,15 +17,21 @@ | |
|
||
package org.dromara.hertzbeat.manager.component.alerter.impl; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.dromara.hertzbeat.common.entity.alerter.Alert; | ||
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver; | ||
import org.dromara.hertzbeat.common.entity.manager.NoticeTemplate; | ||
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException; | ||
import org.springframework.http.*; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Musk.Chen</a> | ||
|
@@ -39,8 +45,12 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a | |
try { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
// fix null pointer exception | ||
filterInvalidTags(alert); | ||
String webhookJson = renderContent(noticeTemplate, alert); | ||
webhookJson = webhookJson.replace(",\n }", "\n }"); | ||
|
||
HttpEntity<String> alertHttpEntity = new HttpEntity<>(webhookJson, headers); | ||
ResponseEntity<String> entity = restTemplate.postForEntity(receiver.getHookUrl(), alertHttpEntity, String.class); | ||
if (entity.getStatusCode().value() < HttpStatus.BAD_REQUEST.value()) { | ||
|
@@ -58,4 +68,25 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a | |
public byte type() { | ||
return 2; | ||
} | ||
|
||
private void filterInvalidTags(Alert alert) { | ||
if (alert.getTags() == null) { | ||
return; | ||
} | ||
|
||
Iterator<Map.Entry<String, String>> iterator = alert.getTags().entrySet().iterator(); | ||
while (iterator.hasNext()) { | ||
Map.Entry<String, String> entry = iterator.next(); | ||
if (StringUtils.isNoneBlank(entry.getKey(), entry.getValue())) { | ||
continue; | ||
} | ||
|
||
iterator.remove(); | ||
} | ||
|
||
// In order to beautify Freemarker template | ||
if (alert.getTags().entrySet().size() <= 0L) { | ||
alert.setTags(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
...g/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters