diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java index 2c79e617ce1..130ed3d4716 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java @@ -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 Musk.Chen @@ -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 alertHttpEntity = new HttpEntity<>(webhookJson, headers); ResponseEntity 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> iterator = alert.getTags().entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry 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); + } + } } diff --git a/manager/src/main/resources/templates/2-WebhookTemplate.txt b/manager/src/main/resources/templates/2-WebhookTemplate.txt index 581d1be5e41..0e889878066 100644 --- a/manager/src/main/resources/templates/2-WebhookTemplate.txt +++ b/manager/src/main/resources/templates/2-WebhookTemplate.txt @@ -8,9 +8,11 @@ "times": ${times}, "triggerTime": "${triggerTime}", <#if (restoreTime??)>"restoreTime": "${restoreTime}", + <#if (tags??)> "tags": { <#list tags as key,value> - "${key}": "${value}", - + "${key}": "${value}", + } + } diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImplTest.java index e4ef7b87c2a..dc72a828517 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImplTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImplTest.java @@ -1,12 +1,15 @@ package org.dromara.hertzbeat.manager.component.alerter.impl; +import lombok.extern.slf4j.Slf4j; +import org.dromara.hertzbeat.manager.AbstractSpringIntegrationTest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** * Test case for {@link WebHookAlertNotifyHandlerImpl} */ -class WebHookAlertNotifyHandlerImplTest { +@Slf4j +class WebHookAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest { @BeforeEach void setUp() {