2018-07-11 15:47:36 +02:00
|
|
|
package eu.eudat.configurations;
|
|
|
|
|
|
|
|
import org.apache.http.HttpHost;
|
2020-03-09 15:47:32 +01:00
|
|
|
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;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 7/5/2018.
|
|
|
|
*/
|
|
|
|
@Configuration
|
|
|
|
public class ElasticSearchConfiguration {
|
|
|
|
|
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public ElasticSearchConfiguration(Environment environment) {
|
|
|
|
this.environment = environment;
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:47:32 +01:00
|
|
|
@Bean(destroyMethod = "close")
|
2018-07-11 15:47:36 +02:00
|
|
|
public RestHighLevelClient client() throws Exception {
|
2020-03-09 15:47:32 +01:00
|
|
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
|
credentialsProvider.setCredentials(AuthScope.ANY,
|
|
|
|
new UsernamePasswordCredentials(this.environment.getProperty("elasticsearch.username"), this.environment.getProperty("elasticsearch.password")));
|
|
|
|
|
2018-07-11 15:47:36 +02:00
|
|
|
RestHighLevelClient client = new RestHighLevelClient(
|
|
|
|
RestClient.builder(
|
|
|
|
new HttpHost(this.environment.getProperty("elasticsearch.host"),
|
2020-03-09 15:47:32 +01:00
|
|
|
Integer.parseInt(this.environment.getProperty("elasticsearch.port")), "http"))
|
|
|
|
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
|
|
|
|
.setDefaultCredentialsProvider(credentialsProvider)));
|
2018-07-11 15:47:36 +02:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|