Skip to content

Commit

Permalink
commit for #772 changes done
Browse files Browse the repository at this point in the history
  • Loading branch information
chopadetejaswini committed Aug 22, 2023
1 parent 1f8be7f commit 40d2b80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.sunbirdrc.pojos.SearchQuery;
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import org.apache.commons.collections4.KeyValue;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
Expand Down Expand Up @@ -44,7 +45,6 @@
import java.net.ConnectException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

import static dev.sunbirdrc.registry.middleware.util.Constants.CONNECTION_FAILURE;
import static dev.sunbirdrc.registry.middleware.util.Constants.SUNBIRD_ELASTIC_SERVICE_NAME;
Expand Down Expand Up @@ -95,38 +95,29 @@ public void init(Set<String> indices) throws RuntimeException {

private static void createClient(String indexName, String connectionInfo) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (authEnabled) {
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
}

try {
if (!esClient.containsKey(indexName)) {
List<HttpHost> httpHosts = Arrays.stream(connectionInfo.split(","))
.map(info -> {
try {
URL url = new URL(info);
return new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
} catch (Exception e) {
String port = Optional.ofNullable(info.split(":").length == 1 ? "-1" : info.split(":")[1]).orElse("-1");
return new HttpHost(info.split(":")[0], Integer.valueOf(port), defaultScheme);
}
})
.collect(Collectors.toList());

RestClientBuilder restClientBuilder = RestClient.builder(httpHosts.toArray(new HttpHost[0]));
if (authEnabled) {
restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
if (null != client) {
esClient.put(indexName, client);
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
if (!esClient.containsKey(indexName)) {
Map<String, KeyValue<Integer, String>> hostPort = new HashMap<>();
List<HttpHost> httpHosts = new ArrayList<>();
for (String info : connectionInfo.split(",")) {
try {
URL url = new URL(info);
httpHosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
} catch (Exception e) {
String port = Optional.ofNullable(info.split(":").length == 1 ? "-1" : info.split(":")[1]).get();
httpHosts.add(new HttpHost(info.split(":")[0], Integer.valueOf(port), defaultScheme));
}
}
} catch (Exception e) {
e.printStackTrace();
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
if(authEnabled) {
restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
if (null != client)
esClient.put(indexName, client);
}
}

public void setScheme(String scheme) {
this.defaultScheme = scheme;
}
Expand Down
4 changes: 2 additions & 2 deletions java/registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ keycloak-user:
claims:
url: ${claims_url:http://localhost:8082}
authentication:
enabled: ${authentication_enabled:false}
enabled: ${authentication_enabled:true}
publicKey: ${authentication_publickey:}
realm: ${authentication_realm:sunbird-rc}
realm: ${authentication_realm:sunbird-rc}e
url: ${authentication_url:localhost:8443/auth}


Expand Down

0 comments on commit 40d2b80

Please sign in to comment.