package ca.uhn.fhir.jpa.search.lastn;

/*-
 * #%L
 * HAPI FHIR JPA Server
 * %%
 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

public class ElasticsearchRestClientFactory {


	private static String determineScheme(String theHostname) {
		int schemeIdx = theHostname.indexOf("://");
		if (schemeIdx > 0) {
			return theHostname.substring(0, schemeIdx);
		} else {
			return "http";
		}
	}

	private static String stripHostOfScheme(String theHostname) {
		int schemeIdx = theHostname.indexOf("://");
		if (schemeIdx > 0) {
			return theHostname.substring(schemeIdx + 3);
		} else {
			return theHostname;
		}
	}

    static public RestHighLevelClient createElasticsearchHighLevelRestClient(String theHostname, int thePort, String theUsername, String thePassword) {
        final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(theUsername, thePassword));
        RestClientBuilder clientBuilder = RestClient.builder(
                new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider));

        Header[] defaultHeaders = new Header[]{new BasicHeader("Content-Type", "application/json")};
        clientBuilder.setDefaultHeaders(defaultHeaders);

        return new RestHighLevelClient(clientBuilder);

    }
}

@@ -692,16 +692,61 @@
 L%25%0A */%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -1202,16 +1202,51 @@
 lient;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 public c
@@ -1721,20 +1721,17 @@
 %09%09%7D%0A%09%7D%0A%0A
-    
+%09
 static p
@@ -1795,16 +1795,19 @@
 tClient(
+%0A%09%09
 String t
@@ -1830,16 +1830,26 @@
 thePort,
+ @Nullable
  String 
@@ -1860,16 +1860,26 @@
 sername,
+ @Nullable
  String 
@@ -1893,24 +1893,249 @@
 word) %7B%0A
-        
+%0A%09%09RestClientBuilder clientBuilder = RestClient.builder(%0A%09%09%09new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)));%0A%09%09if (StringUtils.isNotBlank(theUsername) && StringUtils.isNotBlank(thePassword)) %7B%0A%09%09%09
 final Cr
@@ -2173,32 +2173,16 @@
 ovider =
-%0A               
  new Bas
@@ -2206,24 +2206,19 @@
 ider();%0A
-        
+%09%09%09
 credenti
@@ -2258,32 +2258,16 @@
 ope.ANY,
-%0A               
  new Use
@@ -2323,187 +2323,24 @@
 ));%0A
-        RestClientBuilder clientBuilder = RestClient.builder(%0A                new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)))%0A                
+%09%09%09clientBuilder
 .set
@@ -2407,32 +2407,12 @@
 der%0A
-                        
+%09%09%09%09
 .set
@@ -2461,25 +2461,22 @@
 ider));%0A
-%0A        
+%09%09%7D%0A%09%09
 Header%5B%5D
@@ -2560,24 +2560,18 @@
 son%22)%7D;%0A
-        
+%09%09
 clientBu
@@ -2616,16 +2616,10 @@
 );%0A%0A
-        
+%09%09
 retu
@@ -2662,16 +2662,13 @@
 lder);%0A%0A
-    
+%09
 %7D%0A%7D%0A

Enable empty username and password in ElasticsearchHibernatePropertiesBuilder