55import org .apache .http .entity .ContentType ;
66import org .apache .http .message .BasicHeader ;
77import org .apache .http .nio .entity .NStringEntity ;
8+ import org .elasticsearch .client .Request ;
9+ import org .elasticsearch .client .RequestOptions ;
810import org .elasticsearch .client .Response ;
911import org .elasticsearch .client .RestClient ;
1012import org .slf4j .Logger ;
@@ -22,15 +24,13 @@ public class ElasticSearchMetricSender {
2224 private List <String > metricList ;
2325 private String authUser ;
2426 private String authPwd ;
25- private BasicHeader authHeader ;
2627
2728 public ElasticSearchMetricSender (RestClient cli , String index , String user , String pwd ) {
2829 this .client = cli ;
2930 this .esIndex = index ;
3031 this .metricList = new LinkedList <String >();
3132 this .authUser = user .trim ();
3233 this .authPwd = pwd .trim ();
33- this .authHeader = null ;
3434 }
3535
3636 /**
@@ -72,26 +72,28 @@ public void createIndex() {
7272 * All is being sent through the low-level ElasticSearch REST Client.
7373 */
7474 public void sendRequest () {
75+ Request request = new Request ("POST" , "/" + this .esIndex + "/SampleResult/_bulk" );
76+ StringBuilder bulkRequestBody = new StringBuilder ();
7577 String actionMetaData = String .format ("{ \" index\" : { \" _index\" : \" %s\" , \" _type\" : \" %s\" } }%n" , this .esIndex , "SampleResult" );
7678
77- StringBuilder bulkRequestBody = new StringBuilder ();
7879 for (String metric : this .metricList ) {
7980 bulkRequestBody .append (actionMetaData );
8081 bulkRequestBody .append (metric );
8182 bulkRequestBody .append ("\n " );
8283 }
8384
8485 HttpEntity entity = new NStringEntity (bulkRequestBody .toString (), ContentType .APPLICATION_JSON );
86+ request .setEntity (entity );
87+
8588 try {
8689 if (!this .authUser .equals ("" ) && !this .authPwd .equals ("" )) {
8790 String encodedCredentials = Base64 .getEncoder ().encodeToString ((this .authUser + ":" + this .authPwd ).getBytes ());
88-
89- this .authHeader = new BasicHeader ("Authorization" , "Basic " + encodedCredentials );
91+ RequestOptions .Builder options = request .getOptions ().toBuilder ();
92+ options .addHeader ("Authorization" , "Basic " + encodedCredentials );
93+ request .setOptions (options );
9094 }
9195
92- Response response = (this .authHeader != null )
93- ? this .client .performRequest ("POST" , "/" + this .esIndex +"/SampleResult/_bulk" , Collections .emptyMap (), entity , this .authHeader )
94- : this .client .performRequest ("POST" , "/" + this .esIndex +"/SampleResult/_bulk" , Collections .emptyMap (), entity );
96+ Response response = this .client .performRequest (request );
9597
9698 if (response .getStatusLine ().getStatusCode () != HttpStatus .SC_OK ) {
9799 if (logger .isErrorEnabled ()) {
0 commit comments