From 43dd5f8e74eecdaa796008abc851c41b8e3bd2f9 Mon Sep 17 00:00:00 2001 From: Calvin <131688897+Calvin979@users.noreply.github.com> Date: Sat, 6 Jan 2024 14:31:48 +0800 Subject: [PATCH] Fix npt of webhook notify (#1474) Co-authored-by: zhengqiwei Co-authored-by: tomsun28 --- .../impl/WebHookAlertNotifyHandlerImpl.java | 37 +++++++++++++++++-- .../resources/templates/2-WebhookTemplate.txt | 6 ++- .../WebHookAlertNotifyHandlerImplTest.java | 5 ++- 3 files changed, 42 insertions(+), 6 deletions(-) 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() {