001package ca.uhn.fhir.jpa.search.lastn;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.context.ConfigurationException;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.http.Header;
027import org.apache.http.HttpHost;
028import org.apache.http.auth.AuthScope;
029import org.apache.http.auth.UsernamePasswordCredentials;
030import org.apache.http.client.CredentialsProvider;
031import org.apache.http.impl.client.BasicCredentialsProvider;
032import org.apache.http.message.BasicHeader;
033import org.elasticsearch.client.Node;
034import org.elasticsearch.client.RestClient;
035import org.elasticsearch.client.RestClientBuilder;
036import org.elasticsearch.client.RestHighLevelClient;
037
038import javax.annotation.Nullable;
039import java.util.Arrays;
040import java.util.List;
041import java.util.stream.Collectors;
042
043public class ElasticsearchRestClientFactory {
044
045
046        static public RestHighLevelClient createElasticsearchHighLevelRestClient(
047                String protocol, String hosts, @Nullable String theUsername, @Nullable String thePassword) {
048
049                if (hosts.contains("://")) {
050                        throw new ConfigurationException(Msg.code(1173) + "Elasticsearch URLs cannot include a protocol, that is a separate property. Remove http:// or https:// from this URL.");
051                }
052                String[] hostArray = hosts.split(",");
053                List<Node> clientNodes = Arrays.stream(hostArray)
054                        .map(String::trim)
055                        .filter(s -> s.contains(":"))
056                        .map(h -> {
057                                int colonIndex = h.indexOf(":");
058                                String host = h.substring(0, colonIndex);
059                                int port = Integer.parseInt(h.substring(colonIndex + 1));
060                                return new Node(new HttpHost(host, port, protocol));
061                        })
062                        .collect(Collectors.toList());
063                if (hostArray.length != clientNodes.size()) {
064                        throw new ConfigurationException(Msg.code(1174) + "Elasticsearch URLs have to contain ':' as a host:port separator. Example: localhost:9200,localhost:9201,localhost:9202");
065                }
066
067                RestClientBuilder clientBuilder = RestClient.builder(clientNodes.toArray(new Node[0]));
068                if (StringUtils.isNotBlank(theUsername) && StringUtils.isNotBlank(thePassword)) {
069                        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
070                        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(theUsername, thePassword));
071                        clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
072                                .setDefaultCredentialsProvider(credentialsProvider));
073                }
074
075                Header[] defaultHeaders = new Header[]{new BasicHeader("Content-Type", "application/json")};
076                clientBuilder.setDefaultHeaders(defaultHeaders);
077
078                return new RestHighLevelClient(clientBuilder);
079
080        }
081}