提交 9d7649d9 编写于 作者: K kezhenxu94 提交者: wu-sheng

Set charset in web hook and replace magic number (#2986)

上级 6c85693c
...@@ -21,7 +21,12 @@ package org.apache.skywalking.oap.server.core.alarm.provider; ...@@ -21,7 +21,12 @@ package org.apache.skywalking.oap.server.core.alarm.provider;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import io.netty.handler.codec.http.HttpHeaderValues;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
...@@ -58,7 +63,8 @@ public class WebhookCallback implements AlarmCallback { ...@@ -58,7 +63,8 @@ public class WebhookCallback implements AlarmCallback {
.setSocketTimeout(HTTP_SOCKET_TIMEOUT).build(); .setSocketTimeout(HTTP_SOCKET_TIMEOUT).build();
} }
@Override public void doAlarm(List<AlarmMessage> alarmMessage) { @Override
public void doAlarm(List<AlarmMessage> alarmMessage) {
if (remoteEndpoints.size() == 0) { if (remoteEndpoints.size() == 0) {
return; return;
} }
...@@ -68,16 +74,16 @@ public class WebhookCallback implements AlarmCallback { ...@@ -68,16 +74,16 @@ public class WebhookCallback implements AlarmCallback {
remoteEndpoints.forEach(url -> { remoteEndpoints.forEach(url -> {
HttpPost post = new HttpPost(url); HttpPost post = new HttpPost(url);
post.setConfig(requestConfig); post.setConfig(requestConfig);
post.setHeader("Accept", "application/json"); post.setHeader(HttpHeaders.ACCEPT, HttpHeaderValues.APPLICATION_JSON.toString());
post.setHeader("Content-type", "application/json"); post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
StringEntity entity = null; StringEntity entity;
try { try {
entity = new StringEntity(gson.toJson(alarmMessage)); entity = new StringEntity(gson.toJson(alarmMessage), StandardCharsets.UTF_8);
post.setEntity(entity); post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post); CloseableHttpResponse httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine(); StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != 200) { if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
logger.error("send alarm to " + url + " failure. Response code: " + statusLine.getStatusCode()); logger.error("send alarm to " + url + " failure. Response code: " + statusLine.getStatusCode());
} }
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册