23
23
import org .apache .http .client .methods .CloseableHttpResponse ;
24
24
import org .apache .http .client .methods .HttpPost ;
25
25
import org .apache .http .client .utils .URIBuilder ;
26
+ import org .apache .http .entity .ContentType ;
26
27
import org .apache .http .entity .StringEntity ;
27
28
import org .apache .http .impl .client .CloseableHttpClient ;
28
29
import org .apache .http .impl .client .DefaultHttpRequestRetryHandler ;
34
35
import org .elasticsearch .common .Strings ;
35
36
import org .elasticsearch .common .unit .TimeValue ;
36
37
import org .elasticsearch .rest .RestStatus ;
37
-
38
38
import java .io .IOException ;
39
39
import java .net .URI ;
40
40
import java .net .URISyntaxException ;
41
+ import java .nio .charset .StandardCharsets ;
41
42
import java .util .Map ;
42
43
43
44
@@ -52,7 +53,8 @@ public class DestinationHttpClient {
52
53
private static final int MAX_CONNECTIONS_PER_ROUTE = 20 ;
53
54
private static final int TIMEOUT_MILLISECONDS = (int ) TimeValue .timeValueSeconds (5 ).millis ();
54
55
private static final int SOCKET_TIMEOUT_MILLISECONDS = (int )TimeValue .timeValueSeconds (50 ).millis ();
55
-
56
+ private static final String DEFAULT_CONTENT_TYPE_KEY = "Content-Type" ;
57
+ private static final String DEFAULT_CONTENT_TYPE_VALUE = "application/json" ;
56
58
private static CloseableHttpClient HTTP_CLIENT = createHttpClient ();
57
59
58
60
private static CloseableHttpClient createHttpClient () {
@@ -90,6 +92,7 @@ public String execute(BaseMessage message) throws Exception {
90
92
private CloseableHttpResponse getHttpResponse (BaseMessage message ) throws Exception {
91
93
URI uri = null ;
92
94
HttpPost httpPostRequest = new HttpPost ();
95
+ String entityContentType = DEFAULT_CONTENT_TYPE_VALUE ;
93
96
if (message instanceof CustomWebhookMessage ) {
94
97
CustomWebhookMessage customWebhookMessage = (CustomWebhookMessage ) message ;
95
98
uri = buildUri (customWebhookMessage .getUrl (), customWebhookMessage .getScheme (), customWebhookMessage .getHost (),
@@ -98,20 +101,24 @@ private CloseableHttpResponse getHttpResponse(BaseMessage message) throws Except
98
101
// set headers
99
102
Map <String , String > headerParams = customWebhookMessage .getHeaderParams ();
100
103
if (headerParams == null || headerParams .isEmpty ()) {
101
- // set default header
102
- httpPostRequest .setHeader ("Content-Type" , "application/json" );
104
+ // set default header and content Type
105
+ httpPostRequest .setHeader (DEFAULT_CONTENT_TYPE_KEY , DEFAULT_CONTENT_TYPE_VALUE );
103
106
} else {
104
- for (Map .Entry <String , String > e : customWebhookMessage .getHeaderParams ().entrySet ())
107
+ for (Map .Entry <String , String > e : customWebhookMessage .getHeaderParams ().entrySet ()){
108
+ if (e .getKey ().equals (DEFAULT_CONTENT_TYPE_KEY )) {
109
+ entityContentType = e .getValue ();
110
+ }
105
111
httpPostRequest .setHeader (e .getKey (), e .getValue ());
112
+ }
106
113
}
107
114
} else {
108
115
uri = buildUri (message .getUrl ().trim (), null , null , -1 , null , null );
109
116
}
110
117
111
118
httpPostRequest .setURI (uri );
112
- StringEntity entity = new StringEntity (extractBody (message ));
119
+ StringEntity entity = new StringEntity (extractBody (message ),
120
+ ContentType .create (entityContentType , StandardCharsets .UTF_8 ));
113
121
httpPostRequest .setEntity (entity );
114
-
115
122
return HTTP_CLIENT .execute (httpPostRequest );
116
123
}
117
124
0 commit comments