Set charset in web hook and replace magic number (#2986)
This commit is contained in:
parent
6c85693ca1
commit
9d7649d907
|
|
@ -21,7 +21,12 @@ package org.apache.skywalking.oap.server.core.alarm.provider;
|
|||
import com.google.gson.Gson;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
|
|
@ -58,7 +63,8 @@ public class WebhookCallback implements AlarmCallback {
|
|||
.setSocketTimeout(HTTP_SOCKET_TIMEOUT).build();
|
||||
}
|
||||
|
||||
@Override public void doAlarm(List<AlarmMessage> alarmMessage) {
|
||||
@Override
|
||||
public void doAlarm(List<AlarmMessage> alarmMessage) {
|
||||
if (remoteEndpoints.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -68,16 +74,16 @@ public class WebhookCallback implements AlarmCallback {
|
|||
remoteEndpoints.forEach(url -> {
|
||||
HttpPost post = new HttpPost(url);
|
||||
post.setConfig(requestConfig);
|
||||
post.setHeader("Accept", "application/json");
|
||||
post.setHeader("Content-type", "application/json");
|
||||
post.setHeader(HttpHeaders.ACCEPT, HttpHeaderValues.APPLICATION_JSON.toString());
|
||||
post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
|
||||
|
||||
StringEntity entity = null;
|
||||
StringEntity entity;
|
||||
try {
|
||||
entity = new StringEntity(gson.toJson(alarmMessage));
|
||||
entity = new StringEntity(gson.toJson(alarmMessage), StandardCharsets.UTF_8);
|
||||
post.setEntity(entity);
|
||||
CloseableHttpResponse httpResponse = httpClient.execute(post);
|
||||
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());
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue