Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix restTemplate change default content type application/json #932

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.util.List;
Expand All @@ -33,10 +34,10 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
.description(renderContent(alert))
.build()))
.build();

var url = String.format(alerterProperties.getDiscordNotifyUrl(), receiver.getDiscordChannelId());
var headers = new HttpHeaders();
headers.add("Authorization", "Bot " + receiver.getDiscordBotToken());
headers.setContentType(MediaType.APPLICATION_JSON);
var request = new HttpEntity<>(notifyBody, headers);
var entity = restTemplate.postForEntity(url, request, DiscordResponseDTO.class);
if (entity.getStatusCode() == HttpStatus.OK && entity.getBody() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
Expand Down Expand Up @@ -69,8 +68,11 @@ public void send(NoticeReceiver receiver, Alert alert) {
zhCn.setContent(contents);
flyBookWebHookDto.setContent(content);
String webHookUrl = alerterProperties.getFlyBookWebHookUrl() + receiver.getWechatId();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<FlyBookWebHookDto> flyEntity = new HttpEntity<>(flyBookWebHookDto, headers);
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl,
flyBookWebHookDto, CommonRobotNotifyResp.class);
flyEntity, CommonRobotNotifyResp.class);
if (entity.getStatusCode() == HttpStatus.OK) {
assert entity.getBody() != null;
if (entity.getBody().getCode() == null || entity.getBody().getCode() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class HuaweiyunSmnAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
final class HuaweiCloudSmnAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
private final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");

private final Map<String, SmnClient> smnClientMap = new ConcurrentHashMap<>();
Expand All @@ -57,7 +57,7 @@ public void send(NoticeReceiver receiver, Alert alert) {
.withMessage(renderContent(alert));
request.withBody(body);
var response = smnClient.publishMessage(request);
log.debug("huaweiyun smn alert response: {}", response);
log.debug("huaweiCloud smn alert response: {}", response);
} catch (Exception e) {
throw new AlertNoticeException("[WebHook Notify Error] " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

Expand All @@ -51,8 +54,10 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
var slackNotify = SlackNotifyDTO.builder()
.text(renderContent(alert))
.build();

var entity = restTemplate.postForEntity(receiver.getSlackWebHookUrl(), slackNotify, String.class);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<SlackNotifyDTO> slackNotifyEntity = new HttpEntity<>(slackNotify, headers);
var entity = restTemplate.postForEntity(receiver.getSlackWebHookUrl(), slackNotifyEntity, String.class);
if (entity.getStatusCode() == HttpStatus.OK && entity.getBody() != null) {
var body = entity.getBody();
if (Objects.equals(SUCCESS, body)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

Expand All @@ -55,7 +54,10 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
.text(renderContent(alert))
.disableWebPagePreview(true)
.build();
ResponseEntity<TelegramBotNotifyResponse> entity = restTemplate.postForEntity(url, notifyBody, TelegramBotNotifyResponse.class);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<TelegramBotNotifyDTO> telegramEntity = new HttpEntity<>(notifyBody, headers);
ResponseEntity<TelegramBotNotifyResponse> entity = restTemplate.postForEntity(url, telegramEntity, TelegramBotNotifyResponse.class);
if (entity.getStatusCode() == HttpStatus.OK && entity.getBody() != null) {
TelegramBotNotifyResponse body = entity.getBody();
if (body.ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppDTO;
import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppReq;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -65,7 +68,10 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
.agentId(agentId)
.text(textDTO)
.build();
ResponseEntity<WeChatAppReq> response = restTemplate.postForEntity(String.format(APP_MESSAGE_URL, accessToken), weChatAppDTO, WeChatAppReq.class);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<WeChatAppDTO> weChatAppEntity = new HttpEntity<>(weChatAppDTO, headers);
ResponseEntity<WeChatAppReq> response = restTemplate.postForEntity(String.format(APP_MESSAGE_URL, accessToken), weChatAppEntity, WeChatAppReq.class);
if (Objects.nonNull(response.getBody()) && !Objects.equals(response.getBody().getErrCode(), 0)) {
log.warn("Send Enterprise WeChat App Error: {} Failed: {}", receiver.getHookUrl(), response.getBody().getErrMsg());
throw new AlertNoticeException("Http StatusCode " + response.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Component;

/**
Expand All @@ -46,8 +45,11 @@ public void send(NoticeReceiver receiver, Alert alert) {
WeWorkWebHookDto.MarkdownDTO markdownDTO = new WeWorkWebHookDto.MarkdownDTO();
markdownDTO.setContent(renderContent(alert));
weWorkWebHookDTO.setMarkdown(markdownDTO);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<WeWorkWebHookDto> httpEntity = new HttpEntity<>(weWorkWebHookDTO, headers);
String webHookUrl = alerterProperties.getWeWorkWebHookUrl() + receiver.getWechatId();
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, CommonRobotNotifyResp.class);
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl, httpEntity, CommonRobotNotifyResp.class);
if (entity.getStatusCode() == HttpStatus.OK) {
assert entity.getBody() != null;
if (entity.getBody().getErrCode() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

Expand All @@ -41,7 +40,10 @@ final class WebHookAlertNotifyHandlerImpl implements AlertNotifyHandler {
@Override
public void send(NoticeReceiver receiver, Alert alert) {
try {
ResponseEntity<String> entity = restTemplate.postForEntity(receiver.getHookUrl(), alert, String.class);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Alert> alertHttpEntity = new HttpEntity<>(alert, headers);
ResponseEntity<String> entity = restTemplate.postForEntity(receiver.getHookUrl(), alertHttpEntity, String.class);
if (entity.getStatusCode().value() < HttpStatus.BAD_REQUEST.value()) {
log.debug("Send WebHook: {} Success", receiver.getHookUrl());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@

import java.io.IOException;

import static java.net.Proxy.Type.HTTP;

/**
* Rest Template interceptor adds request header information
* restTemplate拦截器添加请求头信息
*
* @author tom
*/
public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
Expand All @@ -48,4 +46,4 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttp
request.getHeaders().add(HttpHeaders.CONNECTION, "close");
return execution.execute(request, body);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
* Created by gcdd1993 on 2023/4/22
*/
@Slf4j
class HuaweiyunSmnAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest {
class HuaweiCloudSmnAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest {

@Resource
private HuaweiyunSmnAlertNotifyHandlerImpl huaweiyunSmnAlertNotifyHandler;
private HuaweiCloudSmnAlertNotifyHandlerImpl huaweiyunSmnAlertNotifyHandler;

@Test
void send() throws InterruptedException {
Expand Down Expand Up @@ -85,4 +85,4 @@ void send() throws InterruptedException {

huaweiyunSmnAlertNotifyHandler.send(receiver, alert);
}
}
}