Skip to content
Snippets Groups Projects
ElasticsearchClientProvider.java 3.32 KiB
/*-
 * %%----------------------------------------------------------------------------------------------
 * Solidify Framework - Solidify Index - ElasticsearchClientProvider.java
 * SPDX-License-Identifier: GPL-2.0-or-later
 * %----------------------------------------------------------------------------------------------%
 * Copyright (C) 2017 - 2022 University of Geneva
 * %----------------------------------------------------------------------------------------------%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * ----------------------------------------------------------------------------------------------%%
 */

package ch.unige.solidify.index;

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.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestHighLevelClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import ch.unige.solidify.IndexProperties;
import ch.unige.solidify.controller.IndexController;
import ch.unige.solidify.util.StringTool;

@Profile("index-elasticsearch")
@Service
@ConditionalOnBean(IndexController.class)
public class ElasticsearchClientProvider {

  private final RestHighLevelClient elasticsearchClient;

  public ElasticsearchClientProvider(IndexProperties indexProperties) {
    final String userName = indexProperties.getConfig().getUsername();
    final String userPassword = indexProperties.getConfig().getPassword();
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    if(!StringTool.isNullOrEmpty(userName)) {
      credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, userPassword));
    }

    String scheme = "http";
    if (indexProperties.getConfig().isSslEnabled()) {
      scheme = "https";
    }

    HttpHost host = new HttpHost(indexProperties.getConfig().getHost(), indexProperties.getConfig().getPort(), scheme);
    RestClientBuilder restClientBuilder = RestClient.builder(host);
    restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

    this.elasticsearchClient = new RestHighLevelClientBuilder(restClientBuilder.build())
            .setApiCompatibilityMode(true)
            .build();
  }

  public RestHighLevelClient getElasticsearchClient() {
    return this.elasticsearchClient;
  }

}