Enable empty username and password in ElasticsearchHibernatePropertiesBuilder
@@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.search.lastn;
* #L%
*/
+import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
@@ -31,6 +32,8 @@ import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
+import javax.annotation.Nullable;
+
public class ElasticsearchRestClientFactory {
@@ -52,16 +55,17 @@ public class ElasticsearchRestClientFactory {
}
}
- 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));
+ static public RestHighLevelClient createElasticsearchHighLevelRestClient(
+ String theHostname, int thePort, @Nullable String theUsername, @Nullable String thePassword) {
+
RestClientBuilder clientBuilder = RestClient.builder(
- new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)))
- .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
+ new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)));
+ if (StringUtils.isNotBlank(theUsername) && StringUtils.isNotBlank(thePassword)) {
+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(theUsername, thePassword));
+ clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider));
-
+ }
Header[] defaultHeaders = new Header[]{new BasicHeader("Content-Type", "application/json")};
clientBuilder.setDefaultHeaders(defaultHeaders);
