Skip to content

Commit

Permalink
Fix npt of webhook notify (#1474)
Browse files Browse the repository at this point in the history
Co-authored-by: zhengqiwei <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
3 people committed Jan 16, 2024
1 parent b24afb7 commit 43dd5f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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()) {
Expand All @@ -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);
}
}
}
6 changes: 4 additions & 2 deletions manager/src/main/resources/templates/2-WebhookTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"times": ${times},
"triggerTime": "${triggerTime}",
<#if (restoreTime??)>"restoreTime": "${restoreTime}",</#if>
<#if (tags??)>
"tags": {
<#list tags as key,value>
"${key}": "${value}",
</#list>
"${key}": "${value}",
</#list>
}
</#if>
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit 43dd5f8

Please sign in to comment.